ChangeLog removed.
[bubblegum.git] / src / file.c
blobfbea1ad4e0d69a5f3f46ed858a695ac41883d870
1 /*
2 Copyright (C) 2002 Ben Kibbey <bjk@arbornet.org>
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
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
29 #ifdef HAVE_STRINGS_H
30 #include <strings.h>
31 #endif
33 #ifdef HAVE_ERR_H
34 #include <err.h>
35 #endif
37 #include "common.h"
39 #ifdef WANT_DMALLOC
40 #include <dmalloc.h>
41 #endif
43 FILES *filecmp(const char *filename, FILES * list)
45 FILES *cur, *next;
47 for (cur = list; cur; cur = next) {
48 next = cur->next;
50 if (!cur->filename)
51 break;
53 if (strcmp(filename, cur->filename) == 0)
54 return cur;
57 return NULL;
60 int duplicate(const char *filename, FILES * list)
62 FILES *cur, *next;
64 for (cur = list; cur; cur = next) {
65 next = cur->next;
67 if (!cur->filename)
68 break;
70 if (strcmp(filename, cur->filename) == 0)
71 return 1;
74 return 0;
77 FILES *loadfile(const char *filename, FILES * oldlist)
79 FILE *fp;
80 char *cp, buf[LINE_MAX];
81 int err, oldtotal = total;
82 FILES *fptr, *fhead;
84 total = 0;
86 if (oldlist && (err = is_file(filenamelist)) != 0) {
87 if (err < 0)
88 logit("%s: %s", filenamelist, strerror(errno));
89 else
90 logit("%s: Not a regular file", filenamelist);
92 return NULL;
95 if (!(fp = fopen(filename, "r"))) {
96 if (oldlist)
97 logit("%s: %s", filenamelist, strerror(errno));
98 else
99 warn("%s", filenamelist);
101 return NULL;
104 fhead = Calloc(1, sizeof(FILES));
105 fptr = fhead;
107 while ((cp = fgets(buf, sizeof(buf), fp)) != NULL) {
108 struct stat st;
109 FILES *cur;
110 char tmp[FILENAME_MAX];
112 cp[strlen(cp) - 1] = 0;
113 cp = fullpath(wd, cp, tmp, sizeof(tmp));
115 /* make sure there are no duplicate files in the list*/
116 if (duplicate(cp, fhead)) {
117 fptr->next = NULL;
119 if (oldlist)
120 logit("%s: Duplicate file", cp);
121 else
122 warnx("%s: Duplicate file", cp);
124 continue;
127 /* keep old stat() and bit data if filenames are the same*/
128 if (oldlist) {
129 if ((cur = filecmp(cp, oldlist)) != NULL) {
130 bcopy(cur, fptr, sizeof(FILES));
131 fptr->filename = strdup(cur->filename);
132 fptr->next = Calloc(1, sizeof(FILES));
133 fptr = fptr->next;
135 total++;
136 continue;
140 if (STAT(cp, &st) == -1) {
141 if (oldlist)
142 logit("%s: %s", cp, strerror(errno));
143 else
144 warn("%s", cp);
146 continue;
149 if (S_ISDIR(st.st_mode) && cp[strlen(cp) - 1] == '/') {
150 fptr = recurse_directory(fptr, cp, (oldlist) ? 1 : 0);
151 stat(cp, &st);
154 fptr = add_file(fptr, cp, st, (oldlist) ? 1 : 0);
155 fptr = fptr->next;
158 fptr->next = NULL;
159 fclose(fp);
161 if (!total && oldlist) {
162 total = oldtotal;
163 logit("%s: No files", filenamelist);
164 free(fhead);
165 return NULL;
168 return fhead;