2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * This file is part of Jam - see jam.c for Copyright information.
8 * fileunix.c - manipulate file names and scan directories on UNIX/AmigaOS
12 * file_dirscan() - scan a directory for files
13 * file_time() - get timestamp of file, if not done by file_dirscan()
14 * file_archscan() - scan an archive for files
16 * File_dirscan() and file_archscan() call back a caller provided function
17 * for each file found. A flag to this callback function lets file_dirscan()
18 * and file_archscan() indicate that a timestamp is being provided with the
19 * file. If file_dirscan() or file_archscan() do not provide the file's
20 * timestamp, interested parties may later call file_time().
22 * 04/08/94 (seiwald) - Coherent/386 support added.
23 * 12/19/94 (mikem) - solaris string table insanity support
24 * 02/14/95 (seiwald) - parse and build /xxx properly
25 * 05/03/96 (seiwald) - split into pathunix.c
26 * 11/21/96 (peterk) - BEOS does not have Unix-style archives
27 * 01/08/01 (seiwald) - closure param for file_dirscan/file_archscan
28 * 04/03/01 (seiwald) - AIX uses SARMAG
29 * 07/16/02 (seiwald) - Support BSD style long filename in archives.
30 * 11/04/02 (seiwald) - const-ing for string literals
31 * 12/27/02 (seiwald) - support for AIX big archives
32 * 12/30/02 (seiwald) - terminate ar_hdr for solaris sscanf()
33 * 12/30/02 (seiwald) - skip solaris' empty archive member names (/, //xxx)
44 # if defined(OS_SEQUENT) || defined(OS_DGUX) || defined(OS_SCO) || defined(OS_ISC)
48 # if defined(OS_MACOSX)
49 /* need unistd for rhapsody's proper lseek */
52 # define STRUCT_DIRENT struct direct
55 # define STRUCT_DIRENT struct dirent
62 * file_dirscan() - scan a directory for files
64 void file_dirscan (const char *dir
, scanback func
, void *closure
) {
67 STRUCT_DIRENT
*dirent
;
68 char filename
[MAXJPATH
];
70 /* First enter directory itself */
71 memset((char *)&f
, '\0', sizeof(f
));
73 f
.f_dir
.len
= strlen(dir
);
75 /* Special case / : enter it */
76 if (f
.f_dir
.len
== 1 && f
.f_dir
.ptr
[0] == '/')
77 (*func
)(closure
, dir
, 0 /* not stat()'ed */, (time_t)0);
78 /* Now enter contents of directory */
79 if (!(d
= opendir(dir
))) return;
80 if (DEBUG_BINDSCAN
) printf("scan directory %s\n", dir
);
81 while ((dirent
= readdir(d
))) {
83 /* Broken structure definition on sinix. */
84 f
.f_base
.ptr
= dirent
->d_name
- 2;
86 f
.f_base
.ptr
= dirent
->d_name
;
88 f
.f_base
.len
= strlen(f
.f_base
.ptr
);
89 path_build(&f
, filename
, 0);
90 (*func
)(closure
, filename
, 0 /* not stat()'ed */, (time_t)0);
97 * file_time() - get timestamp of file, if not done by file_dirscan()
99 int file_time (const char *filename
, time_t *time
) {
102 if (stat(filename
, &statbuf
) < 0) return -1;
103 *time
= statbuf
.st_mtime
;
109 * file_archscan() - scan an archive for files
112 #define SARHDR sizeof(struct ar_hdr)
114 void file_archscan (const char *archive
, scanback func
, void *closure
) {
116 struct ar_hdr ar_hdr
;
119 char *string_table
= 0;
122 if ((fd
= open(archive
, O_RDONLY
, 0)) < 0) return;
123 if (read(fd
, buf
, SARMAG
) != SARMAG
|| strncmp(ARMAG
, buf
, SARMAG
)) {
128 if (DEBUG_BINDSCAN
) printf("scan archive %s\n", archive
);
129 while (read(fd
, &ar_hdr
, SARHDR
) == SARHDR
&& !memcmp(ar_hdr
.ar_fmag
, ARFMAG
, SARFMAG
)) {
133 char *dst
= lar_name
;
135 /* solaris sscanf() does strlen first, so terminate somewhere */
136 ar_hdr
.ar_fmag
[0] = 0;
137 /* Get date & size */
138 sscanf(ar_hdr
.ar_date
, "%ld", &lar_date
);
139 sscanf(ar_hdr
.ar_size
, "%ld", &lar_size
);
140 /* Handle solaris string table.
141 ** The entry under the name // is the table,
142 ** and entries with the name /nnnn refer to the table.
144 if (ar_hdr
.ar_name
[0] != '/') {
145 /* traditional archive entry names:
146 ** ends at the first space, /, or null.
148 char *src
= ar_hdr
.ar_name
;
149 const char *e
= src
+sizeof(ar_hdr
.ar_name
);
151 while (src
< e
&& *src
&& *src
!= ' ' && *src
!= '/') *dst
++ = *src
++;
152 } else if (ar_hdr
.ar_name
[1] == '/') {
153 /* this is the "string table" entry of the symbol table,
154 ** which holds strings of filenames that are longer than
155 ** 15 characters (ie. don't fit into a ar_name)
157 string_table
= (char *)malloc(lar_size
);
158 lseek(fd
, offset
+SARHDR
, 0);
159 if (read(fd
, string_table
, lar_size
) != lar_size
) printf("error reading string table\n");
160 } else if (string_table
&& ar_hdr
.ar_name
[1] != ' ') {
161 /* Long filenames are recognized by "/nnnn" where nnnn is
162 ** the offset of the string in the string table represented
163 ** in ASCII decimals.
165 char *src
= string_table
+atoi(ar_hdr
.ar_name
+1);
167 while (*src
!= '/') *dst
++ = *src
++;
169 /* Terminate lar_name */
171 /* Modern (BSD4.4) long names: if the name is "#1/nnnn",
172 ** then the actual name is the nnnn bytes after the header.
174 if (!strcmp(lar_name
, "#1")) {
175 int len
= atoi(ar_hdr
.ar_name
+3);
176 if (read(fd
, lar_name
, len
) != len
) printf("error reading archive entry\n");
179 /* Build name and pass it on. */
181 if (DEBUG_BINDSCAN
) printf("archive name %s found\n", lar_name
);
182 sprintf(buf
, "%s(%s)", archive
, lar_name
);
183 (*func
)(closure
, buf
, 1 /* time valid */, (time_t)lar_date
);
185 /* Position at next member */
186 offset
+= SARHDR
+((lar_size
+ 1) & ~1);
187 lseek(fd
, offset
, 0);
189 if (string_table
) free(string_table
);
195 # endif /* USE_FILEUNIX */