2 * Copyright (C) 2000, Matias Atria
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 char *dgets(Dstring *dstr, FILE *in)
32 while(fgets(buffer, 256, in) != NULL) {
33 int len = strlen(buffer);
35 if(buffer[len-1] == '\n') {
36 dstring_append(dstr, buffer, len - 1);
39 dstring_append(dstr, buffer, len);
42 dstr->data[dstr->length] = 0;
46 /* some simple helper functions to manipulate file names */
48 const char *file_basename(const char *filename)
50 const char *ptr = strrchr(filename, '/');
52 return (ptr ? ptr + 1 : filename);
55 const char *file_extension(const char *filename)
57 const char *ptr = strchr(file_basename(filename), '.');
59 return (ptr ? ptr + 1 : NULL);
62 int file_readable(const char *filename)
64 int status = (access(filename, R_OK) == 0);
66 DEBUG((DBG_FILES, "file_redable(%s) -> %s\n",
67 filename, status ? "Yes" : "No"));
71 int file_exists(const char *filename)
73 int status = (access(filename, F_OK) == 0);
75 DEBUG((DBG_FILES, "file_exists(%s) -> %s\n",
76 filename, status ? "Yes" : "No"));
80 static char *xstrchre(const char *string, int c)
84 for(ptr = string; *ptr && *ptr != c; ptr++);
88 char *find_in_path(const char *filename, const char *path)
94 /* if the file is readable as given, return it */
95 if(file_readable(filename))
96 return xstrdup(filename);
98 /* worst case scenario */
99 try = xmalloc(strlen(path) + strlen(filename) + 2);
102 for(p = path; *p; p = q) {
103 q = xstrchre(p, ':');
105 xstrncpy(try, p, len);
107 strcpy(try + len + 1, filename);
108 if(file_readable(try))
121 char *read_into_core(const char *file, size_t *size)
127 in = fopen(file, "r");
130 if(fstat(fileno(in), &st) < 0) {
131 /* I don't think this is possible, but who knows */
135 if(st.st_size == 0) {
136 warning("%s: file is empty\n", file);
140 ptr = xmalloc(st.st_size + 1);
141 if(fread(ptr, st.st_size, 1, in) != 1) {
148 if(size) *size = st.st_size;