option.c: fixed warnings
[k8jam.git] / src / filent.c
blob074ec2a0dea2c9cdad7ffd0c4cb7b2fdee0f8d82
1 /*
2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
3 * This file is part of Jam - see jam.c for Copyright information.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
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, see <http://www.gnu.org/licenses/>.
18 * filent.c - scan directories and archives on NT
20 * External routines:
22 * file_dirscan() - scan a directory for files
23 * file_time() - get timestamp of file, if not done by file_dirscan()
24 * file_archscan() - scan an archive for files
26 * File_dirscan() and file_archscan() call back a caller provided function
27 * for each file found. A flag to this callback function lets file_dirscan()
28 * and file_archscan() indicate that a timestamp is being provided with the
29 * file. If file_dirscan() or file_archscan() do not provide the file's
30 * timestamp, interested parties may later call file_time().
32 #include "jam.h"
33 #include "filesys.h"
34 #include "pathsys.h"
36 #ifdef OS_NT
38 #include <io.h>
39 #include <stdint.h>
40 #include <sys/stat.h>
43 #define FINDTYPE intptr_t
47 * file_dirscan() - scan a directory for files
49 // <0: error; 0: regular; 1: directory
50 int file_type (const char *diskname) {
51 FINDTYPE handle;
52 struct _finddata_t finfo[1];
53 int res;
54 handle = _findfirst(diskname, finfo);
55 if (handle == (FINDTYPE)(-1)) return -1;
56 res = (finfo->attrib&(_A_SUBDIR) ? 1 : 0);
57 _findclose(handle);
58 return res;
62 void file_dirscan (const char *dir, scanback func, void *closure) {
63 PATHNAME f;
64 static char filespec[MAXJPATH];
65 static char filename[MAXJPATH];
66 FINDTYPE handle;
67 int ret;
68 struct _finddata_t finfo[1];
69 /* first enter directory itself */
70 memset((char *)&f, '\0', sizeof(f));
71 f.f_dir.ptr = dir;
72 f.f_dir.len = strlen(dir);
73 dir = (*dir ? dir : ".");
74 /* special case \ or d:\ : enter it */
75 if (f.f_dir.len == 1 && f.f_dir.ptr[0] == '\\') (*func)(closure, dir, 0 /* not stat()'ed */, (time_t)0);
76 else if (f.f_dir.len == 3 && f.f_dir.ptr[1] == ':') (*func)(closure, dir, 0 /* not stat()'ed */, (time_t)0);
77 /* now enter contents of directory */
78 snprintf(filespec, sizeof(filespec), "%s/*", dir);
79 if (DEBUG_BINDSCAN) printf("scan directory %s\n", dir);
80 handle = _findfirst(filespec, finfo);
81 if ((ret = (handle == (FINDTYPE)(-1)))) return;
82 while (!ret) {
83 f.f_base.ptr = finfo->name;
84 f.f_base.len = strlen(finfo->name);
85 path_build(filename, &f);
86 (*func)(closure, filename, 1 /* stat()'ed */, finfo->time_write);
87 ret = _findnext(handle, finfo);
89 _findclose(handle);
94 * file_time() - get timestamp of file, if not done by file_dirscan()
96 int file_time (const char *filename, time_t *time) {
97 /* on NT this is called only for C:/ */
98 struct stat statbuf;
99 if (stat(filename, &statbuf) < 0) return -1;
100 *time = statbuf.st_mtime;
101 return 0;
105 static long scan_long (const char *str, size_t ssize) {
106 long res = 0;
107 while (ssize > 0 && str[0] && (str[0] < '0' || str[0] > '9')) { ++str; --ssize; }
108 if (ssize > 0 && str[0]) {
109 while (ssize > 0 && str[0] >= '0' && str[0] <= '9') {
110 res = res*10+str[0]-'0';
111 ++str;
112 --ssize;
115 return res;
120 * file_archscan() - scan an archive for files
122 /* straight from SunOS */
123 #define ARMAG "!<arch>\n"
124 #define SARMAG 8
125 #define ARFMAG "`\n"
127 struct ar_hdr {
128 char ar_name[16];
129 char ar_date[12];
130 char ar_uid[6];
131 char ar_gid[6];
132 char ar_mode[8];
133 char ar_size[10];
134 char ar_fmag[2];
137 #define SARFMAG 2
138 #define SARHDR sizeof(struct ar_hdr)
140 void file_archscan (const char *archive, scanback func, void *closure) {
141 struct ar_hdr ar_hdr;
142 char *string_table = 0;
143 long string_table_len = 0;
144 static char buf[MAXJPATH];
145 long offset;
146 int fd;
147 if ((fd = open(archive, O_RDONLY|O_BINARY, 0)) < 0) return;
148 if (read(fd, buf, SARMAG) != SARMAG || strncmp(ARMAG, buf, SARMAG)) {
149 close(fd);
150 return;
152 offset = SARMAG;
153 if (DEBUG_BINDSCAN) printf("scan archive %s\n", archive);
154 while (read(fd, &ar_hdr, SARHDR) == SARHDR && !memcmp(ar_hdr.ar_fmag, ARFMAG, SARFMAG)) {
155 long lar_date;
156 long lar_size;
157 char *name = 0;
158 char *endname;
159 /*char *c;*/
161 sscanf(ar_hdr.ar_date, "%ld", &lar_date);
162 sscanf(ar_hdr.ar_size, "%ld", &lar_size);
164 lar_date = scan_long(ar_hdr.ar_date, sizeof(ar_hdr.ar_date));
165 lar_size = scan_long(ar_hdr.ar_size, sizeof(ar_hdr.ar_size));
166 lar_size = (lar_size+1)&~1;
167 if (ar_hdr.ar_name[0] == '/' && ar_hdr.ar_name[1] == '/') {
168 /* this is the "string table" entry of the symbol table,
169 * which holds strings of filenames that are longer than
170 * 15 characters (ie. don't fit into a ar_name */
171 string_table = malloc(lar_size);
172 if (read(fd, string_table, lar_size) != lar_size) printf("error reading string table\n");
173 string_table_len = lar_size;
174 goto next;
175 } else if (ar_hdr.ar_name[0] == '/' && ar_hdr.ar_name[1] != ' ') {
176 /* Long filenames are recognized by "/nnnn" where nnnn is
177 * the offset of the string in the string table represented
178 * in ASCII decimals.
180 * however, the name end with 0 or '/', depending on
181 * the librarian used to generate them (0 for Mingw, '/' for Visual C++) */
182 long off = atoi(ar_hdr.ar_name+1);
183 if (off < 0 || off > string_table_len) goto next;
184 name = string_table+off;
185 for (; off < string_table_len; ++off) {
186 int c = string_table[off];
187 if (c == 0 || c == '/') break;
189 endname = string_table+off;
190 } else {
191 /* normal name */
192 long off;
194 name = ar_hdr.ar_name;
195 for (off = 0; off < sizeof(ar_hdr.ar_name); ++off) {
196 if (name[off] == '/' || name[off] == 0) break; /* not strictly required, but safe */
198 endname = name+off;
200 /* strip trailing space, slashes, and backslashes */
201 while (endname > name) {
202 int c = endname[-1];
203 if (c != ' ' && c != '\\' && c != '/') break;
204 --endname;
206 /* strip leading directory names, since they're present in
207 * files generated by the Microsoft Librarian */
209 char *p = name;
210 for (; p < endname; ++p) if (*p == '\\') name = p+1;
212 /* don't count empty entries */
213 if (name >= endname) goto next;
214 /* create name */
215 snprintf(buf, sizeof(buf), "%s(%.*s)", archive, endname-name, name);
216 (*func)(closure, buf, 1 /* time valid */, (time_t)lar_date);
217 next:
218 offset += SARHDR+lar_size;
219 lseek(fd, offset, 0);
221 close(fd);
225 #endif /* NT */