15 #define _IOSM_INTERNAL
33 static int strntoul(
const char *str,
char **endptr,
41 strncpy(buf, str, num);
44 ret = strtoul(buf, &end, base);
47 *endptr = ((
char *)str) + (end - buf);
49 *endptr = ((
char *)str) + strlen(buf);
56 #define _IOSMRC(_rc) \
57 if ((_rc) <= 0) return ((_rc) ? (int) -rc : IOSMERR_HDR_TRAILER)
59 static ssize_t
arRead(
void * _iosm,
void * buf,
size_t count)
69 fprintf(stderr,
"\t arRead(%p, %p[%u])\n", iosm, buf, (
unsigned)count);
76 if (!rc && iosm->rdnb != iosm->wrlen) {
77 if (iosm->rdnb == 0)
return -IOSMERR_HDR_TRAILER;
78 rc = IOSMERR_READ_FAILED;
83 rc = (count > iosm->rdnb ? iosm->rdnb : count);
84 if (buf != iosm->wrbuf)
85 memcpy(t + nb, iosm->wrbuf, rc);
100 fprintf(stderr,
" arHeaderRead(%p, %p)\n", iosm, st);
109 return IOSMERR_BAD_MAGIC;
115 if (rc)
return (
int) rc;
117 rc =
arRead(iosm, hdr,
sizeof(*hdr));
120 fprintf(stderr,
"==> %p[%u] \"%.*s\"\n", hdr, (
unsigned)rc, (
int)
sizeof(*hdr)-2, (
char *)hdr);
124 return IOSMERR_BAD_MAGIC;
129 if (hdr->
name[0] ==
'/') {
131 if (hdr->
name[1] ==
'/' && hdr->
name[2] ==
' ') {
135 rc =
arRead(iosm, iosm->wrbuf, st->st_size);
138 iosm->wrbuf[rc] =
'\0';
139 iosm->lmtab = t =
xstrdup(iosm->wrbuf);
143 for (i = 1; i < iosm->lmtablen; i++) {
145 if (t[0] !=
'\n')
continue;
148 if (t[-1] ==
'/') t[-1] =
'\0';
153 if (hdr->
name[1] ==
' ') {
154 rc =
arRead(iosm, iosm->wrbuf, st->st_size);
162 if (*te ==
' ' && iosm->lmtab != NULL && i < (
int)iosm->lmtablen)
163 iosm->path =
xstrdup(iosm->lmtab + i);
166 if (hdr->
name[0] !=
' ') {
167 size_t nb =
sizeof(hdr->
name);
168 char t[
sizeof(hdr->
name)+1];
169 memcpy(t, hdr->
name, nb);
171 while (nb > 0 && t[nb-1] ==
' ')
174 if (nb > 0 && t[nb - 1] ==
'/')
180 st->st_ctime = st->st_atime = st->st_mtime;
191 fprintf(stderr,
"\t %06o%3d (%4d,%4d)%12lu %s\n",
192 (
unsigned)st->st_mode, (
int)st->st_nlink,
193 (
int)st->st_uid, (
int)st->st_gid, (
unsigned long)st->st_size,
194 (iosm->path ? iosm->path :
""));
199 static ssize_t
arWrite(
void * _iosm,
const void *buf,
size_t count)
204 const char * s = buf;
209 fprintf(stderr,
"\tarWrite(%p, %p[%u])\n", iosm, buf, (
unsigned)count);
215 if (s != iosm->rdbuf)
216 memmove(iosm->rdbuf, s + nb, iosm->rdnb);
219 if (!rc && iosm->rdnb != iosm->wrnb)
220 rc = IOSMERR_WRITE_FAILED;
227 #if defined(JBJ_WRITEPAD)
243 fprintf(stderr,
" arHeaderWrite(%p, %p)\n", iosm, st);
251 if (iosm->lmtab != NULL) {
252 memset(hdr, (
int)
' ',
sizeof(*hdr));
255 sprintf(hdr->
filesize,
"%-10d", (
unsigned) (iosm->lmtablen & 037777777777));
258 rc =
arWrite(iosm, hdr,
sizeof(*hdr));
260 rc =
arWrite(iosm, iosm->lmtab, iosm->lmtablen);
262 #if !defined(JBJ_WRITEPAD)
264 if (rc)
return (
int)rc;
269 memset(hdr, (
int)
' ',
sizeof(*hdr));
271 nb = strlen(iosm->path);
272 if (nb >=
sizeof(hdr->
name)) {
275 assert(iosm->lmtab != NULL);
276 t = iosm->lmtab + iosm->lmtaboff;
277 te = strchr(t,
'\n');
279 nb =
snprintf(hdr->
name,
sizeof(hdr->
name)-1,
"/%u", (
unsigned)iosm->lmtaboff);
282 iosm->lmtaboff += (te - t) + 1;
284 strncpy(hdr->
name, iosm->path, nb);
288 sprintf(hdr->
mtime,
"%-12u", (
unsigned) (st->st_mtime & 037777777777));
289 sprintf(hdr->
uid,
"%-6u", (
unsigned int)(st->st_uid & 07777777));
290 sprintf(hdr->
gid,
"%-6u", (
unsigned int)(st->st_gid & 07777777));
292 sprintf(hdr->
mode,
"%-8o", (
unsigned int)(st->st_mode & 07777777));
293 sprintf(hdr->
filesize,
"%-10u", (
unsigned) (st->st_size & 037777777777));
297 rc = (int)
sizeof(*hdr);
299 fprintf(stderr,
"==> %p[%u] \"%.*s\"\n", hdr, (
unsigned)rc, (
int)
sizeof(*hdr), (
char *)hdr);
301 rc =
arWrite(iosm, hdr,
sizeof(*hdr));
314 fprintf(stderr,
" arTrailerWrite(%p)\n", iosm);
316 #if defined(JBJ_WRITEPAD)