2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * This file is part of Jam - see jam.c for Copyright information.
7 * filent.c - scan directories and archives on NT
11 * file_dirscan() - scan a directory for files
12 * file_time() - get timestamp of file, if not done by file_dirscan()
13 * file_archscan() - scan an archive for files
15 * File_dirscan() and file_archscan() call back a caller provided function
16 * for each file found. A flag to this callback function lets file_dirscan()
17 * and file_archscan() indicate that a timestamp is being provided with the
18 * file. If file_dirscan() or file_archscan() do not provide the file's
19 * timestamp, interested parties may later call file_time().
21 * 07/10/95 (taylor) Findfirst() returns the first file on NT.
22 * 05/03/96 (seiwald) split apart into pathnt.c
23 * 01/20/00 (seiwald) - Upgraded from K&R to ANSI C
24 * 10/03/00 (anton) - Porting for Borland C++ 5.5
25 * 01/08/01 (seiwald) - closure param for file_dirscan/file_archscan
26 * 11/04/02 (seiwald) - const-ing for string literals
27 * 01/23/03 (seiwald) - long long handles for NT IA64
40 * file_dirscan() - scan a directory for files
43 # define FINDTYPE long long
45 # define FINDTYPE long
49 void file_dirscan (const char *dir
, scanback func
, void *closure
) {
51 char filespec
[MAXJPATH
];
52 char filename
[MAXJPATH
];
55 struct _finddata_t finfo
[1];
57 /* first enter directory itself */
58 memset((char *)&f
, '\0', sizeof(f
));
60 f
.f_dir
.len
= strlen(dir
);
61 dir
= *dir
? dir
: ".";
62 /* special case \ or d:\ : enter it */
63 if (f
.f_dir
.len
== 1 && f
.f_dir
.ptr
[0] == '\\') (*func
)(closure
, dir
, 0 /* not stat()'ed */, (time_t)0);
64 else if (f
.f_dir
.len
== 3 && f
.f_dir
.ptr
[1] == ':') (*func
)(closure
, dir
, 0 /* not stat()'ed */, (time_t)0);
65 /* now enter contents of directory */
66 sprintf(filespec
, "%s/*", dir
);
67 if (DEBUG_BINDSCAN
) printf("scan directory %s\n", dir
);
68 handle
= _findfirst(filespec
, finfo
);
69 if ((ret
= (handle
== (FINDTYPE
)(-1)))) return;
71 f
.f_base
.ptr
= finfo
->name
;
72 f
.f_base
.len
= strlen(finfo
->name
);
73 path_build(&f
, filename
, 0);
74 (*func
)(closure
, filename
, 1 /* stat()'ed */, finfo
->time_write
);
75 ret
= _findnext(handle
, finfo
);
82 * file_time() - get timestamp of file, if not done by file_dirscan()
84 int file_time (const char *filename
, time_t *time
) {
85 /* on NT this is called only for C:/ */
88 if (stat(filename
, &statbuf
) < 0) return -1;
89 *time
= statbuf
.st_mtime
;
95 * file_archscan() - scan an archive for files
97 /* straight from SunOS */
98 #define ARMAG "!<arch>\n"
113 #define SARHDR sizeof(struct ar_hdr)
115 void file_archscan (const char *archive
, scanback func
, void *closure
) {
116 struct ar_hdr ar_hdr
;
117 char *string_table
= 0;
118 long string_table_len
= 0;
123 if ((fd
= open(archive
, O_RDONLY
| O_BINARY
, 0)) < 0) return;
124 if (read(fd
, buf
, SARMAG
) != SARMAG
|| strncmp(ARMAG
, buf
, SARMAG
)) {
129 if (DEBUG_BINDSCAN
) printf("scan archive %s\n", archive
);
131 while (read(fd
, &ar_hdr
, SARHDR
) == SARHDR
&& !memcmp(ar_hdr
.ar_fmag
, ARFMAG
, SARFMAG
)) {
137 sscanf(ar_hdr
.ar_date
, "%ld", &lar_date
);
138 sscanf(ar_hdr
.ar_size
, "%ld", &lar_size
);
139 lar_size
= (lar_size
+1) & ~1;
140 if (ar_hdr
.ar_name
[0] == '/' && ar_hdr
.ar_name
[1] == '/') {
141 /* this is the "string table" entry of the symbol table,
142 * which holds strings of filenames that are longer than
143 * 15 characters (ie. don't fit into a ar_name */
144 string_table
= malloc(lar_size
);
145 if (read(fd
, string_table
, lar_size
) != lar_size
) printf("error reading string table\n");
146 string_table_len
= lar_size
;
148 } else if (ar_hdr
.ar_name
[0] == '/' && ar_hdr
.ar_name
[1] != ' ') {
149 /* Long filenames are recognized by "/nnnn" where nnnn is
150 * the offset of the string in the string table represented
153 * however, the name end with 0 or '/', depending on
154 * the librarian used to generate them (0 for Mingw, '/' for Visual C++) */
155 long off
= atoi(ar_hdr
.ar_name
+1);
156 if (off
< 0 || off
> string_table_len
) goto next
;
157 name
= string_table
+off
;
158 for (; off
< string_table_len
; ++off
) {
159 int c
= string_table
[off
];
160 if (c
== 0 || c
== '/') break;
162 endname
= string_table
+off
;
167 name
= ar_hdr
.ar_name
;
168 for (off
= 0; off
< sizeof(ar_hdr
.ar_name
); ++off
) {
169 if (name
[off
] == '/' || name
[off
] == 0) break; /* not strictly required, but safe */
173 /* strip trailing space, slashes, and backslashes */
174 while (endname
> name
) {
176 if (c
!= ' ' && c
!= '\\' && c
!= '/') break;
179 /* strip leading directory names, since they're present in
180 * files generated by the Microsoft Librarian */
183 for (; p
< endname
; ++p
) if (*p
== '\\') name
= p
+1;
185 /* don't count empty entries */
186 if (name
>= endname
) goto next
;
188 sprintf(buf
, "%s(%.*s)", archive
, endname
-name
, name
);
189 (*func
)(closure
, buf
, 1 /* time valid */, (time_t)lar_date
);
191 offset
+= SARHDR
+lar_size
;
192 lseek(fd
, offset
, 0);