cosmetix
[k8jam.git] / src / fileunix.c
blob0bf123f84c2530d76849c63158f712c27896ddf4
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 */
7 /*
8 * fileunix.c - manipulate file names and scan directories on UNIX/AmigaOS
10 * External routines:
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)
35 #include <unistd.h>
37 #include "jam.h"
38 #include "filesys.h"
39 #include "pathsys.h"
41 #ifdef USE_FILEUNIX
43 #if defined(OS_SEQUENT) || defined(OS_DGUX) || defined(OS_SCO) || defined(OS_ISC)
44 # define PORTAR 1
45 #endif
47 #if defined(OS_MACOSX)
48 /* need unistd for rhapsody's proper lseek */
49 # include <sys/dir.h>
50 # include <unistd.h>
51 # define STRUCT_DIRENT struct direct
52 #else
53 # include <dirent.h>
54 # define STRUCT_DIRENT struct dirent
55 #endif
57 #include <ar.h>
61 * file_dirscan() - scan a directory for files
63 void file_dirscan (const char *dir, scanback func, void *closure) {
64 PATHNAME f;
65 DIR *d;
66 STRUCT_DIRENT *dirent;
67 char filename[MAXJPATH];
69 /* first enter directory itself */
70 memset((char *)&f, '\0', sizeof(f));
71 f.f_dir.ptr = dir;
72 f.f_dir.len = strlen(dir);
73 dir = *dir?dir:".";
74 /* special case / : 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 /* Now enter contents of directory */
77 if (!(d = opendir(dir))) return;
78 if (DEBUG_BINDSCAN) printf("scan directory %s\n", dir);
79 while ((dirent = readdir(d))) {
80 #ifdef old_sinix
81 /* broken structure definition on sinix. */
82 f.f_base.ptr = dirent->d_name-2;
83 #else
84 f.f_base.ptr = dirent->d_name;
85 #endif
86 f.f_base.len = strlen(f.f_base.ptr);
87 path_build(&f, filename, 0);
88 (*func)(closure, filename, 0 /* not stat()'ed */, (time_t)0);
90 closedir(d);
95 * file_time() - get timestamp of file, if not done by file_dirscan()
97 int file_time (const char *filename, time_t *time) {
98 struct stat statbuf;
100 if (stat(filename, &statbuf) < 0) return -1;
101 *time = statbuf.st_mtime;
102 return 0;
107 * file_archscan() - scan an archive for files
109 #define SARFMAG 2
110 #define SARHDR sizeof(struct ar_hdr)
112 void file_archscan (const char *archive, scanback func, void *closure) {
113 #ifndef NO_AR
114 struct ar_hdr ar_hdr;
115 char buf[MAXJPATH];
116 long offset;
117 char *string_table = 0;
118 int fd;
120 if ((fd = open(archive, O_RDONLY, 0)) < 0) return;
121 if (read(fd, buf, SARMAG) != SARMAG || strncmp(ARMAG, buf, SARMAG)) {
122 close(fd);
123 return;
125 offset = SARMAG;
126 if (DEBUG_BINDSCAN) printf("scan archive %s\n", archive);
127 while (read(fd, &ar_hdr, SARHDR) == SARHDR && !memcmp(ar_hdr.ar_fmag, ARFMAG, SARFMAG)) {
128 long lar_date;
129 long lar_size;
130 char lar_name[256];
131 char *dst = lar_name;
132 /* solaris sscanf() does strlen first, so terminate somewhere */
133 ar_hdr.ar_fmag[0] = 0;
134 /* get date & size */
135 sscanf(ar_hdr.ar_date, "%ld", &lar_date);
136 sscanf(ar_hdr.ar_size, "%ld", &lar_size);
137 /* handle solaris string table
138 * the entry under the name // is the table, and entries with the name /nnnn refer to the table */
139 if (ar_hdr.ar_name[0] != '/') {
140 /* traditional archive entry names: ends at the first space, /, or null */
141 char *src = ar_hdr.ar_name;
142 const char *e = src+sizeof(ar_hdr.ar_name);
144 while (src < e && *src && *src != ' ' && *src != '/') *dst++ = *src++;
145 } else if (ar_hdr.ar_name[1] == '/') {
146 /* this is the "string table" entry of the symbol table,
147 * which holds strings of filenames that are longer than
148 * 15 characters (ie. don't fit into a ar_name) */
149 string_table = (char *)malloc(lar_size);
150 lseek(fd, offset+SARHDR, 0);
151 if (read(fd, string_table, lar_size) != lar_size) printf("error reading string table\n");
152 } else if (string_table && ar_hdr.ar_name[1] != ' ') {
153 /* long filenames are recognized by "/nnnn" where nnnn is
154 * the offset of the string in the string table represented
155 * in ASCII decimals */
156 char *src = string_table+atoi(ar_hdr.ar_name+1);
157 while (*src != '/') *dst++ = *src++;
159 /* terminate lar_name */
160 *dst = 0;
161 /* modern (BSD4.4) long names: if the name is "#1/nnnn", then the actual name is the nnnn bytes after the header */
162 if (!strcmp(lar_name, "#1")) {
163 int len = atoi(ar_hdr.ar_name+3);
164 if (read(fd, lar_name, len) != len) printf("error reading archive entry\n");
165 lar_name[len] = 0;
167 /* build name and pass it on */
168 if (lar_name[0]) {
169 if (DEBUG_BINDSCAN) printf("archive name %s found\n", lar_name);
170 sprintf(buf, "%s(%s)", archive, lar_name);
171 (*func)(closure, buf, 1 /* time valid */, (time_t)lar_date);
173 /* Position at next member */
174 offset += SARHDR+((lar_size+1)&(~1));
175 lseek(fd, offset, 0);
177 if (string_table) free(string_table);
178 close(fd);
179 #endif /* NO_AR */
183 #endif /* USE_FILEUNIX */