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
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().
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
) {
52 struct _finddata_t finfo
[1];
54 handle
= _findfirst(diskname
, finfo
);
55 if (handle
== (FINDTYPE
)(-1)) return -1;
56 res
= (finfo
->attrib
&(_A_SUBDIR
) ? 1 : 0);
62 void file_dirscan (const char *dir
, scanback func
, void *closure
) {
64 static char filespec
[MAXJPATH
];
65 static char filename
[MAXJPATH
];
68 struct _finddata_t finfo
[1];
69 /* first enter directory itself */
70 memset((char *)&f
, '\0', sizeof(f
));
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;
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
);
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:/ */
99 if (stat(filename
, &statbuf
) < 0) return -1;
100 *time
= statbuf
.st_mtime
;
105 static long scan_long (const char *str
, size_t ssize
) {
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';
120 * file_archscan() - scan an archive for files
122 /* straight from SunOS */
123 #define ARMAG "!<arch>\n"
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
];
147 if ((fd
= open(archive
, O_RDONLY
|O_BINARY
, 0)) < 0) return;
148 if (read(fd
, buf
, SARMAG
) != SARMAG
|| strncmp(ARMAG
, buf
, 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
)) {
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
;
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
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
;
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 */
200 /* strip trailing space, slashes, and backslashes */
201 while (endname
> name
) {
203 if (c
!= ' ' && c
!= '\\' && c
!= '/') break;
206 /* strip leading directory names, since they're present in
207 * files generated by the Microsoft Librarian */
210 for (; p
< endname
; ++p
) if (*p
== '\\') name
= p
+1;
212 /* don't count empty entries */
213 if (name
>= endname
) goto next
;
215 snprintf(buf
, sizeof(buf
), "%s(%.*s)", archive
, endname
-name
, name
);
216 (*func
)(closure
, buf
, 1 /* time valid */, (time_t)lar_date
);
218 offset
+= SARHDR
+lar_size
;
219 lseek(fd
, offset
, 0);