comparison files.c @ 13:120629a5d6bf

seenfile -> placefile (clearer)
author David A. Holland
date Sun, 19 Dec 2010 19:49:43 -0500
parents 800f3a560a3b
children f6177d3ed5c2
comparison
equal deleted inserted replaced
12:6c15ca895585 13:120629a5d6bf
1 #include <stdbool.h> 1 #include <stdbool.h>
2 #include <stdio.h> 2 #include <stdio.h>
3 #include <stdlib.h> 3 #include <stdlib.h>
4 #include <string.h>
4 #include <unistd.h> 5 #include <unistd.h>
5 #include <fcntl.h> 6 #include <fcntl.h>
6 #include <err.h> 7 #include <err.h>
7 8
8 #include "array.h" 9 #include "array.h"
82 83
83 //////////////////////////////////////////////////////////// 84 ////////////////////////////////////////////////////////////
84 // parsing 85 // parsing
85 86
86 void 87 void
87 file_read(struct seenfile *sf, int fd); 88 file_read(const struct placefile *pf, int fd);
88 89
89 //////////////////////////////////////////////////////////// 90 ////////////////////////////////////////////////////////////
90 // path search 91 // path search
92
93 static
94 char *
95 mkfilename(const char *dir, const char *file)
96 {
97 size_t dlen, flen, rlen;
98 char *ret;
99 bool needslash = false;
100
101 dlen = strlen(dir);
102 flen = strlen(file);
103 if (dlen > 0 && dir[dlen-1] != '/') {
104 needslash = true;
105 }
106
107 rlen = dlen + (needslash ? 1 : 0) + flen;
108 ret = domalloc(rlen + 1);
109 strcpy(ret, dir);
110 if (needslash) {
111 strcat(ret, "/");
112 }
113 strcat(ret, file);
114 return ret;
115 }
91 116
92 static 117 static
93 int 118 int
94 file_tryopen(const char *file) 119 file_tryopen(const char *file)
95 { 120 {
107 void 132 void
108 file_search(struct place *place, struct incdirarray *path, const char *name) 133 file_search(struct place *place, struct incdirarray *path, const char *name)
109 { 134 {
110 unsigned i, num; 135 unsigned i, num;
111 struct incdir *id; 136 struct incdir *id;
112 struct seenfile *sf; 137 const struct placefile *pf;
113 char *file; 138 char *file;
114 int fd; 139 int fd;
115 140
116 assert(place != NULL); 141 assert(place != NULL);
117 142
118 num = incdirarray_num(path); 143 num = incdirarray_num(path);
119 for (i=0; i<num; i++) { 144 for (i=0; i<num; i++) {
120 id = incdirarray_get(path, i); 145 id = incdirarray_get(path, i);
121 file = dostrdup3(id->name, "/", name); 146 file = mkfilename(id->name, name);
122 fd = file_tryopen(file); 147 fd = file_tryopen(file);
123 if (fd >= 0) { 148 if (fd >= 0) {
124 sf = place_seen_file(place, file, id->issystem); 149 pf = place_addfile(place, file, id->issystem);
125 file_read(sf, fd); 150 free(file);
151 file_read(pf, fd);
126 close(fd); 152 close(fd);
127 return; 153 return;
128 } 154 }
129 free(file); 155 free(file);
130 } 156 }
145 } 171 }
146 172
147 void 173 void
148 file_readabsolute(struct place *place, const char *name) 174 file_readabsolute(struct place *place, const char *name)
149 { 175 {
150 struct seenfile *sf; 176 const struct placefile *pf;
151 int fd; 177 int fd;
152 178
153 assert(place != NULL); 179 assert(place != NULL);
154 180
155 fd = file_tryopen(name); 181 fd = file_tryopen(name);
156 if (fd < 0) { 182 if (fd < 0) {
157 warn("%s", name); 183 warn("%s", name);
158 die(); 184 die();
159 } 185 }
160 sf = place_seen_file(place, dostrdup(name), false); 186 pf = place_addfile(place, name, false);
161 file_read(sf, fd); 187 file_read(pf, fd);
162 close(fd); 188 close(fd);
163 } 189 }