15 #define _IOSM_INTERNAL
40 buf = (
char *)
alloca(num + 1);
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)
64 char * t = (
char *) buf;
69 fprintf(stderr,
"\t arRead(%p, %p[%u])\n", iosm, buf, (
unsigned)count);
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);
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);
129 if (hdr->
name[0] ==
'/') {
131 if (hdr->
name[1] ==
'/' && hdr->
name[2] ==
' ') {
138 iosm->
wrbuf[rc] =
'\0';
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] ==
' ') {
162 if (*te ==
' ' && iosm->
lmtab != NULL && i < (
int)iosm->
lmtablen)
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,
199 static ssize_t
arWrite(
void * _iosm,
const void *buf,
size_t count)
204 const char * s = (
const char *) 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)
227 #if defined(JBJ_WRITEPAD)
239 const char * path = (iosm && iosm->
path ? iosm->
path :
"");
244 fprintf(stderr,
" arHeaderWrite(%p, %p)\n", iosm, st);
252 if (iosm->
lmtab != NULL) {
253 memset(hdr, (
int)
' ',
sizeof(*hdr));
256 sprintf(hdr->
filesize,
"%-10d", (
unsigned) (iosm->
lmtablen & 037777777777));
259 rc =
arWrite(iosm, hdr,
sizeof(*hdr));
263 #if !defined(JBJ_WRITEPAD)
265 if (rc)
return (
int)rc;
270 memset(hdr, (
int)
' ',
sizeof(*hdr));
273 if (nb >=
sizeof(hdr->
name)) {
276 assert(iosm->
lmtab != NULL);
278 te = strchr(t,
'\n');
285 strncpy(hdr->
name, path, nb);
289 sprintf(hdr->
mtime,
"%-12u", (
unsigned) (st->st_mtime & 037777777777));
290 sprintf(hdr->
uid,
"%-6u", (
unsigned int)(st->st_uid & 07777777));
291 sprintf(hdr->
gid,
"%-6u", (
unsigned int)(st->st_gid & 07777777));
293 sprintf(hdr->
mode,
"%-8o", (
unsigned int)(st->st_mode & 07777777));
294 sprintf(hdr->
filesize,
"%-10u", (
unsigned) (st->st_size & 037777777777));
298 rc = (int)
sizeof(*hdr);
300 fprintf(stderr,
"==> %p[%u] \"%.*s\"\n", hdr, (
unsigned)rc, (
int)
sizeof(*hdr), (
char *)hdr);
302 rc =
arWrite(iosm, hdr,
sizeof(*hdr));
315 fprintf(stderr,
" arTrailerWrite(%p)\n", iosm);
317 #if defined(JBJ_WRITEPAD)
int arHeaderRead(void *_iosm, struct stat *st)
Read ar(1) header.
char * xstrdup(const char *str)
static ssize_t arRead(void *_iosm, void *buf, size_t count)
Structures used for ar(1) archives.
int(* _iosmNext)(IOSM_t iosm, iosmFileStage nstage)
Vector to iosmNext.
struct iosm_s * IOSM_t
File state machine data.
static unsigned long long fdGetCpioPos(FD_t fd)
struct arHeader_s * arHeader
static int strntoul(const char *str, char **endptr, int base, size_t num)
Convert string to unsigned integer (with buffer size check).
static int xisdigit(int c)
static int snprintf(char *buf, int nb, const char *fmt,...)
int arHeaderWrite(void *_iosm, struct stat *st)
Write ar(1) header.
int arTrailerWrite(void *_iosm)
Write ar(1) trailer.
static ssize_t arWrite(void *_iosm, const void *buf, size_t count)
File state machine to handle archive I/O and system call's.
File name and stat information.
const char const bson int num