1
/* *** This file was borrowed from jam 2.5. The copyright statement from
2
* *** jam.c appears below.
6
* +\ Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
9
* This file is part of jam.
11
* License is hereby granted to use this software and distribute it
12
* freely, as long as this copyright notice is retained and modifications
15
* ALL WARRANTIES ARE HEREBY DISCLAIMED.
19
* Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
21
* This file is part of Jam - see jam.c for Copyright information.
25
* filent.c - scan directories and archives on NT
29
* file_dirscan() - scan a directory for files
30
* file_time() - get timestamp of file, if not done by file_dirscan()
31
* file_archscan() - scan an archive for files
33
* File_dirscan() and file_archscan() call back a caller provided function
34
* for each file found. A flag to this callback function lets file_dirscan()
35
* and file_archscan() indicate that a timestamp is being provided with the
36
* file. If file_dirscan() or file_archscan() do not provide the file's
37
* timestamp, interested parties may later call file_time().
39
* 07/10/95 (taylor) Findfirst() returns the first file on NT.
40
* 05/03/96 (seiwald) split apart into pathnt.c
41
* 01/20/00 (seiwald) - Upgraded from K&R to ANSI C
42
* 10/03/00 (anton) - Porting for Borland C++ 5.5
43
* 01/08/01 (seiwald) - closure param for file_dirscan/file_archscan
44
* 11/04/02 (seiwald) - const-ing for string literals
45
* 01/23/03 (seiwald) - long long handles for NT IA64
55
# if __BORLANDC__ < 0x550
59
# undef PATHNAME /* cpp namespace collision */
60
# define _finddata_t ffblk
64
# include <sys/stat.h>
67
* file_dirscan() - scan a directory for files
71
# define FINDTYPE long long
73
# define FINDTYPE long
83
char filespec[ MAXJPATH ];
84
char filename[ MAXJPATH ];
87
struct _finddata_t finfo[1];
89
/* First enter directory itself */
91
memset( (char *)&f, '\0', sizeof( f ) );
94
f.f_dir.len = strlen(dir);
96
dir = *dir ? dir : ".";
98
/* Special case \ or d:\ : enter it */
100
if( f.f_dir.len == 1 && f.f_dir.ptr[0] == '\\' )
101
(*func)( closure, dir, 0 /* not stat()'ed */, (time_t)0 );
102
else if( f.f_dir.len == 3 && f.f_dir.ptr[1] == ':' )
103
(*func)( closure, dir, 0 /* not stat()'ed */, (time_t)0 );
105
/* Now enter contents of directory */
107
sprintf( filespec, "%s/*", dir );
110
printf( "scan directory %s\n", dir );
112
# if defined(__BORLANDC__) && __BORLANDC__ < 0x550
113
if ( ret = findfirst( filespec, finfo, FA_NORMAL | FA_DIREC ) )
118
time_t time_write = finfo->ff_fdate;
120
time_write = (time_write << 16) | finfo->ff_ftime;
121
f.f_base.ptr = finfo->ff_name;
122
f.f_base.len = strlen( finfo->ff_name );
124
path_build( &f, filename );
126
(*func)( closure, filename, 1 /* stat()'ed */, time_write );
128
ret = findnext( finfo );
131
handle = _findfirst( filespec, finfo );
133
if( ( ret = ( handle == (FINDTYPE)(-1) ) ) ) /* TNB */
138
f.f_base.ptr = finfo->name;
139
f.f_base.len = strlen( finfo->name );
141
path_build( &f, filename, 0 );
143
(*func)( closure, filename, 1 /* stat()'ed */, finfo->time_write );
145
ret = _findnext( handle, finfo );
148
_findclose( handle );
154
* file_time() - get timestamp of file, if not done by file_dirscan()
159
const char *filename,
162
/* On NT this is called only for C:/ */
166
if( stat( filename, &statbuf ) < 0 )
169
*time = statbuf.st_mtime;
175
* file_archscan() - scan an archive for files
178
/* Straight from SunOS */
180
#define ARMAG "!<arch>\n"
196
# define SARHDR sizeof( struct ar_hdr )
204
struct ar_hdr ar_hdr;
205
char *string_table = 0;
206
char buf[ MAXJPATH ];
210
if( ( fd = open( archive, O_RDONLY | O_BINARY, 0 ) ) < 0 )
213
if( read( fd, buf, SARMAG ) != SARMAG ||
214
strncmp( ARMAG, buf, SARMAG ) )
223
printf( "scan archive %s\n", archive );
225
while( read( fd, &ar_hdr, SARHDR ) == SARHDR &&
226
!memcmp( ar_hdr.ar_fmag, ARFMAG, SARFMAG ) )
234
sscanf( ar_hdr.ar_date, "%ld", &lar_date );
235
sscanf( ar_hdr.ar_size, "%ld", &lar_size );
237
lar_size = ( lar_size + 1 ) & ~1;
239
if (ar_hdr.ar_name[0] == '/' && ar_hdr.ar_name[1] == '/' )
241
/* this is the "string table" entry of the symbol table,
242
** which holds strings of filenames that are longer than
243
** 15 characters (ie. don't fit into a ar_name
246
string_table = malloc(lar_size);
247
if (read(fd, string_table, lar_size) != lar_size)
248
printf("error reading string table\n");
249
offset += SARHDR + lar_size;
252
else if (ar_hdr.ar_name[0] == '/' && ar_hdr.ar_name[1] != ' ')
254
/* Long filenames are recognized by "/nnnn" where nnnn is
255
** the offset of the string in the string table represented
256
** in ASCII decimals.
259
name = string_table + atoi( ar_hdr.ar_name + 1 );
260
endname = name + strlen( name );
265
name = ar_hdr.ar_name;
266
endname = name + sizeof( ar_hdr.ar_name );
269
/* strip trailing space, slashes, and backslashes */
271
while( endname-- > name )
272
if( *endname != ' ' && *endname != '\\' && *endname != '/' )
276
/* strip leading directory names, an NT specialty */
278
if( ( c = strrchr( name, '/' ) ) ) /* TNB */
280
if( ( c = strrchr( name, '\\' ) ) ) /* TNB */
283
sprintf( buf, "%s(%.*s)", archive, endname - name, name );
284
(*func)( closure, buf, 1 /* time valid */, (time_t)lar_date );
286
offset += SARHDR + lar_size;
287
lseek( fd, offset, 0 );