Blob


1 #include "mk.h"
2 #define ARMAG "!<arch>\n"
3 #define SARMAG 8
5 #define ARFMAG "`\n"
6 #define SARNAME 16
8 struct ar_hdr
9 {
10 char name[SARNAME];
11 char date[12];
12 char uid[6];
13 char gid[6];
14 char mode[8];
15 char size[10];
16 char fmag[2];
17 };
18 #define SAR_HDR (SARNAME+44)
20 static int dolong;
22 static void atimes(char *);
23 static char *split(char*, char**);
25 long
26 atimeof(int force, char *name)
27 {
28 Symtab *sym;
29 long t;
30 char *archive, *member, buf[512];
32 archive = split(name, &member);
33 if(archive == 0)
34 Exit();
36 t = mtime(archive);
37 sym = symlook(archive, S_AGG, 0);
38 if(sym){
39 if(force || (t > (long)sym->value)){
40 atimes(archive);
41 sym->value = (void *)t;
42 }
43 }
44 else{
45 atimes(archive);
46 /* mark the aggegate as having been done */
47 symlook(strdup(archive), S_AGG, "")->value = (void *)t;
48 }
49 /* truncate long member name to sizeof of name field in archive header */
50 if(dolong)
51 snprint(buf, sizeof(buf), "%s(%s)", archive, member);
52 else
53 snprint(buf, sizeof(buf), "%s(%.*s)", archive, SARNAME, member);
54 sym = symlook(buf, S_TIME, 0);
55 if (sym)
56 return (long)sym->value; /* uggh */
57 return 0;
58 }
60 void
61 atouch(char *name)
62 {
63 char *archive, *member;
64 int fd, i;
65 struct ar_hdr h;
66 long t;
68 archive = split(name, &member);
69 if(archive == 0)
70 Exit();
72 fd = open(archive, ORDWR);
73 if(fd < 0){
74 fd = create(archive, OWRITE, 0666);
75 if(fd < 0){
76 fprint(2, "create %s: %r\n", archive);
77 Exit();
78 }
79 write(fd, ARMAG, SARMAG);
80 }
81 if(symlook(name, S_TIME, 0)){
82 /* hoon off and change it in situ */
83 LSEEK(fd, SARMAG, 0);
84 while(read(fd, (char *)&h, sizeof(h)) == sizeof(h)){
85 for(i = SARNAME-1; i > 0 && h.name[i] == ' '; i--)
86 ;
87 h.name[i+1]=0;
88 if(strcmp(member, h.name) == 0){
89 t = SARNAME-sizeof(h); /* ughgghh */
90 LSEEK(fd, t, 1);
91 fprint(fd, "%-12ld", time(0));
92 break;
93 }
94 t = atol(h.size);
95 if(t&01) t++;
96 LSEEK(fd, t, 1);
97 }
98 }
99 close(fd);
102 static void
103 atimes(char *ar)
105 struct ar_hdr h;
106 long t;
107 int fd, i;
108 char buf[BIGBLOCK];
109 char name[sizeof(h.name)+1];
111 fd = open(ar, OREAD);
112 if(fd < 0)
113 return;
115 if(read(fd, buf, SARMAG) != SARMAG){
116 close(fd);
117 return;
119 while(read(fd, (char *)&h, sizeof(h)) == sizeof(h)){
120 t = atol(h.date);
121 if(t == 0) /* as it sometimes happens; thanks ken */
122 t = 1;
123 strncpy(name, h.name, sizeof(h.name));
124 for(i = sizeof(h.name)-1; i > 0 && name[i] == ' '; i--)
126 if(name[i] == '/') /* system V bug */
127 i--;
128 name[i+1]=0;
129 sprint(buf, "%s(%s)", ar, h.size);
130 symlook(strdup(buf), S_TIME, (void *)t)->value = (void *)t;
131 t = atol(h.size);
132 if(t&01) t++;
133 LSEEK(fd, t, 1);
135 close(fd);
138 static int
139 type(char *file)
141 int fd;
142 char buf[SARMAG];
144 fd = open(file, OREAD);
145 if(fd < 0){
146 if(symlook(file, S_BITCH, 0) == 0){
147 Bprint(&bout, "%s doesn't exist: assuming it will be an archive\n", file);
148 symlook(file, S_BITCH, (void *)file);
150 return 1;
152 if(read(fd, buf, SARMAG) != SARMAG){
153 close(fd);
154 return 0;
156 close(fd);
157 return !strncmp(ARMAG, buf, SARMAG);
160 static char*
161 split(char *name, char **member)
163 char *p, *q;
165 p = strdup(name);
166 q = utfrune(p, '(');
167 if(q){
168 *q++ = 0;
169 if(member)
170 *member = q;
171 q = utfrune(q, ')');
172 if (q)
173 *q = 0;
174 if(type(p))
175 return p;
176 free(p);
177 fprint(2, "mk: '%s' is not an archive\n", name);
179 return 0;