built-in jambase is packed now
[k8jam.git] / src / filent.c
blob6df0087653de376c1d1384222248fbefa5e7e257
1 /*
2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
6 /*
7 * filent.c - scan directories and archives on NT
9 * External routines:
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
29 #include "jam.h"
30 #include "filesys.h"
31 #include "pathsys.h"
33 #ifdef OS_NT
35 #include <io.h>
36 #include <sys/stat.h>
40 * file_dirscan() - scan a directory for files
42 #ifdef _M_IA64
43 # define FINDTYPE long long
44 #else
45 # define FINDTYPE long
46 #endif
49 // <0: error; 0: regular; 1: directory
50 int getFileType (const char *diskname) {
51 FINDTYPE handle;
52 struct _finddata_t finfo[1];
53 int res;
55 handle = _findfirst(diskname, finfo);
56 if (handle == (FINDTYPE)(-1)) return -1;
57 res = (finfo->attrib&(_A_SUBDIR)) ? 1 : 0;
58 _findclose(handle);
59 return res;
63 void file_dirscan (const char *dir, scanback func, void *closure) {
64 PATHNAME f;
65 char filespec[MAXJPATH];
66 char filename[MAXJPATH];
67 FINDTYPE handle;
68 int ret;
69 struct _finddata_t finfo[1];
71 /* first enter directory itself */
72 memset((char *)&f, '\0', sizeof(f));
73 f.f_dir.ptr = dir;
74 f.f_dir.len = strlen(dir);
75 dir = *dir ? dir : ".";
76 /* special case \ or d:\ : enter it */
77 if (f.f_dir.len == 1 && f.f_dir.ptr[0] == '\\') (*func)(closure, dir, 0 /* not stat()'ed */, (time_t)0);
78 else if (f.f_dir.len == 3 && f.f_dir.ptr[1] == ':') (*func)(closure, dir, 0 /* not stat()'ed */, (time_t)0);
79 /* now enter contents of directory */
80 sprintf(filespec, "%s/*", dir);
81 if (DEBUG_BINDSCAN) printf("scan directory %s\n", dir);
82 handle = _findfirst(filespec, finfo);
83 if ((ret = (handle == (FINDTYPE)(-1)))) return;
84 while (!ret) {
85 f.f_base.ptr = finfo->name;
86 f.f_base.len = strlen(finfo->name);
87 path_build(&f, filename, 0);
88 (*func)(closure, filename, 1 /* stat()'ed */, finfo->time_write);
89 ret = _findnext(handle, finfo);
91 _findclose(handle);
96 * file_time() - get timestamp of file, if not done by file_dirscan()
98 int file_time (const char *filename, time_t *time) {
99 /* on NT this is called only for C:/ */
100 struct stat statbuf;
102 if (stat(filename, &statbuf) < 0) return -1;
103 *time = statbuf.st_mtime;
104 return 0;
109 * file_archscan() - scan an archive for files
111 /* straight from SunOS */
112 #define ARMAG "!<arch>\n"
113 #define SARMAG 8
114 #define ARFMAG "`\n"
116 struct ar_hdr {
117 char ar_name[16];
118 char ar_date[12];
119 char ar_uid[6];
120 char ar_gid[6];
121 char ar_mode[8];
122 char ar_size[10];
123 char ar_fmag[2];
126 #define SARFMAG 2
127 #define SARHDR sizeof(struct ar_hdr)
129 void file_archscan (const char *archive, scanback func, void *closure) {
130 struct ar_hdr ar_hdr;
131 char *string_table = 0;
132 long string_table_len = 0;
133 char buf[MAXJPATH];
134 long offset;
135 int fd;
137 if ((fd = open(archive, O_RDONLY | O_BINARY, 0)) < 0) return;
138 if (read(fd, buf, SARMAG) != SARMAG || strncmp(ARMAG, buf, SARMAG)) {
139 close(fd);
140 return;
142 offset = SARMAG;
143 if (DEBUG_BINDSCAN) printf("scan archive %s\n", archive);
145 while (read(fd, &ar_hdr, SARHDR) == SARHDR && !memcmp(ar_hdr.ar_fmag, ARFMAG, SARFMAG)) {
146 long lar_date;
147 long lar_size;
148 char *name = 0;
149 char *endname;
150 /*char *c;*/
151 sscanf(ar_hdr.ar_date, "%ld", &lar_date);
152 sscanf(ar_hdr.ar_size, "%ld", &lar_size);
153 lar_size = (lar_size+1) & ~1;
154 if (ar_hdr.ar_name[0] == '/' && ar_hdr.ar_name[1] == '/') {
155 /* this is the "string table" entry of the symbol table,
156 * which holds strings of filenames that are longer than
157 * 15 characters (ie. don't fit into a ar_name */
158 string_table = malloc(lar_size);
159 if (read(fd, string_table, lar_size) != lar_size) printf("error reading string table\n");
160 string_table_len = lar_size;
161 goto next;
162 } else if (ar_hdr.ar_name[0] == '/' && ar_hdr.ar_name[1] != ' ') {
163 /* Long filenames are recognized by "/nnnn" where nnnn is
164 * the offset of the string in the string table represented
165 * in ASCII decimals.
167 * however, the name end with 0 or '/', depending on
168 * the librarian used to generate them (0 for Mingw, '/' for Visual C++) */
169 long off = atoi(ar_hdr.ar_name+1);
170 if (off < 0 || off > string_table_len) goto next;
171 name = string_table+off;
172 for (; off < string_table_len; ++off) {
173 int c = string_table[off];
174 if (c == 0 || c == '/') break;
176 endname = string_table+off;
177 } else {
178 /* normal name */
179 long off;
181 name = ar_hdr.ar_name;
182 for (off = 0; off < sizeof(ar_hdr.ar_name); ++off) {
183 if (name[off] == '/' || name[off] == 0) break; /* not strictly required, but safe */
185 endname = name+off;
187 /* strip trailing space, slashes, and backslashes */
188 while (endname > name) {
189 int c = endname[-1];
190 if (c != ' ' && c != '\\' && c != '/') break;
191 --endname;
193 /* strip leading directory names, since they're present in
194 * files generated by the Microsoft Librarian */
196 char *p = name;
197 for (; p < endname; ++p) if (*p == '\\') name = p+1;
199 /* don't count empty entries */
200 if (name >= endname) goto next;
201 /* create name */
202 sprintf(buf, "%s(%.*s)", archive, endname-name, name);
203 (*func)(closure, buf, 1 /* time valid */, (time_t)lar_date);
204 next:
205 offset += SARHDR+lar_size;
206 lseek(fd, offset, 0);
208 close(fd);
212 #endif /* NT */