3 #define ARMAG "<bigaf>\n"
5 #define ARMAG "!<arch>\n"
7 #define SARMAG (sizeof(ARMAG) - sizeof(""))
22 #define SAR_HDR (SARNAME+44)
24 static int dolong = 1;
26 static void atimes(char *);
27 static char *split(char*, char**);
30 readn(int f, void *av, long n)
38 m = read(f, a+t, n-t);
49 atimeof(int force, char *name)
53 char *archive, *member, buf[512];
55 archive = split(name, &member);
60 sym = symlook(archive, S_AGG, 0);
62 if(force || (t > sym->u.value)){
69 /* mark the aggegate as having been done */
70 symlook(strdup(archive), S_AGG, "")->u.value = t;
72 /* truncate long member name to sizeof of name field in archive header */
74 snprint(buf, sizeof(buf), "%s(%s)", archive, member);
76 snprint(buf, sizeof(buf), "%s(%.*s)", archive, SARNAME, member);
77 sym = symlook(buf, S_TIME, 0);
86 char *archive, *member;
91 archive = split(name, &member);
95 fd = open(archive, ORDWR);
97 fd = create(archive, OWRITE, 0666);
99 fprint(2, "create %s: %r\n", archive);
102 write(fd, ARMAG, SARMAG);
104 if(symlook(name, S_TIME, 0)){
105 /* hoon off and change it in situ */
106 LSEEK(fd, SARMAG, 0);
107 while(read(fd, (char *)&h, sizeof(h)) == sizeof(h)){
108 for(i = SARNAME-1; i > 0 && h.name[i] == ' '; i--)
111 if(strcmp(member, h.name) == 0){
112 t = SARNAME-sizeof(h); /* ughgghh */
114 fprint(fd, "%-12ld", time(0));
126 allspaces(char *a, int n)
143 char buf[2048], *p, *strings;
148 fd = open(ar, OREAD);
152 if(read(fd, buf, SARMAG) != SARMAG){
156 while(readn(fd, (char *)&h, sizeof(h)) == sizeof(h)){
158 if(t == 0) /* as it sometimes happens; thanks ken */
161 if(memcmp(h.name, "#1/", 3) == 0){ /* BSD */
162 namelen = atoi(h.name+3);
163 if(namelen >= sizeof name){
167 if(readn(fd, name, namelen) != namelen)
170 }else if(memcmp(h.name, "// ", 3) == 0){ /* GNU */
171 /* date, uid, gid, mode all ' ' */
172 if(!allspaces(&h.name[3], sizeof(h.name) - 3) ||
173 !allspaces(h.date, sizeof(h.date)) ||
174 !allspaces(h.uid, sizeof(h.uid)) ||
175 !allspaces(h.gid, sizeof(h.gid)) ||
176 !allspaces(h.mode, sizeof(h.mode)))
182 strings = malloc(t+1);
184 if(readn(fd, strings, t) != t){
193 }else if(strings && h.name[0]=='/' && isdigit((uchar)h.name[1])){
194 i = strtol(h.name+1, &p, 10);
195 if(*p != ' ' || i >= strlen(strings))
198 for(; *p && *p != '/'; p++)
200 namelen = p-(strings+i);
201 if(namelen >= sizeof name){
205 memmove(name, strings+i, namelen);
209 memmove(name, h.name, sizeof(h.name));
210 for(i = sizeof(h.name)-1; i > 0 && name[i] == ' '; i--)
212 if(name[i] == '/') /* system V bug */
216 snprint(buf, sizeof buf, "%s(%s)", ar, name);
217 sym = symlook(strdup(buf), S_TIME, (void *)t);
235 fd = open(file, OREAD);
237 if(symlook(file, S_BITCH, 0) == 0){
238 if(strlen(file) < 2 || strcmp(file+strlen(file)-2, ".a") != 0)
239 Bprint(&bout, "%s doesn't exist: assuming it will be an archive\n", file);
240 symlook(file, S_BITCH, (void *)file);
244 if(read(fd, buf, SARMAG) != SARMAG){
249 return !strncmp(ARMAG, buf, SARMAG);
253 split(char *name, char **member)
269 fprint(2, "mk: '%s' is not an archive\n", name);