13 #define _IOSM_INTERNAL
34 static int strntoul(
const char *str,
char **endptr,
42 buf = (
char *)
alloca(num + 1);
43 strncpy(buf, str, num);
46 ret = strtoul(buf, &end, base);
49 *endptr = ((
char *)str) + (end - buf);
51 *endptr = ((
char *)str) + strlen(buf);
58 #define _IOSMRC(_rc) \
59 if ((_rc) <= 0) return ((_rc) ? (int) -rc : IOSMERR_HDR_TRAILER)
61 static ssize_t
tarRead(
void * _iosm,
void * buf,
size_t count)
66 char * t = (
char *) buf;
70 fprintf(stderr,
"\ttarRead(%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);
106 char * t = (
char *)
xmalloc(nb);
107 ssize_t rc =
tarRead(iosm, t, nb);
117 fprintf(stderr,
"\ttarHeaderReadName(%p, %u, %p) rc 0x%x\n", _iosm, (
unsigned)len, fnp, (
unsigned)rc);
134 fprintf(stderr,
" tarHeaderRead(%p, %p)\n", iosm, st);
150 {
const unsigned char * hp = (
const unsigned char *) hdr;
156 memcpy(hdrchecksum, hdr->
checksum,
sizeof(hdrchecksum));
162 memset(checksum, (
int)
' ',
sizeof(checksum));
163 sprintf(checksum,
"%06o", (
unsigned) (sum & 07777777));
165 fprintf(stderr,
"\tmemcmp(\"%s\", \"%s\", %u)\n", hdrchecksum, checksum, (
unsigned)
sizeof(hdrchecksum));
166 if (memcmp(hdrchecksum, checksum,
sizeof(hdrchecksum)))
181 st->st_mode &= ~S_IFMT;
190 st->st_mode |= S_IFREG;
193 st->st_mode |= S_IFREG;
199 st->st_mode |= S_IFLNK;
202 st->st_mode |= S_IFCHR;
205 st->st_mode |= S_IFBLK;
208 st->st_mode |= S_IFDIR;
212 st->st_mode |= S_IFIFO;
240 st->st_ctime = st->st_atime = st->st_mtime;
247 st->st_rdev = st->st_dev;
253 if (iosm->
path == NULL && hdr->
name[0] !=
'\0') {
254 nb = strlen(hdr->
name);
256 memcpy(t, hdr->
name, nb);
273 fprintf(stderr,
"\t %06o%3d (%4d,%4d)%12lu %s\n\t-> %s\n",
274 (
unsigned)st->st_mode, (
int)st->st_nlink,
275 (
int)st->st_uid, (
int)st->st_gid, (
unsigned long)st->st_size,
281 static ssize_t
tarWrite(
void * _iosm,
const void *buf,
size_t count)
286 const char * s = (
const char *) buf;
291 fprintf(stderr,
"\t tarWrite(%p, %p[%u])\n", iosm, buf, (
unsigned)count);
297 if (s != iosm->
rdbuf)
298 memmove(iosm->
rdbuf, s + nb, iosm->
rdnb);
301 if (!rc && iosm->
rdnb != iosm->
wrnb)
309 #if defined(JBJ_WRITEPAD)
327 ssize_t rc =
tarWrite(_iosm, path, strlen(path));
329 #if !defined(JBJ_WRITEPAD)
338 fprintf(stderr,
"\ttarHeaderWriteName(%p, %s) rc 0x%x\n", _iosm, path, (
unsigned)rc);
355 const char * path = (iosm && iosm->
path ? iosm->
path :
"");
359 fprintf(stderr,
"\ttarHeaderWriteBlock(%p, %p) type %c\n", iosm, hdr, hdr->
typeflag);
361 fprintf(stderr,
"\t %06o%3d (%4d,%4d)%12lu %s\n",
362 (
unsigned)st->st_mode, (
int)st->st_nlink,
363 (
int)st->st_uid, (
int)st->st_gid, (
unsigned long)st->st_size,
370 {
const unsigned char * hp = (
const unsigned char *) hdr;
377 sprintf(hdr->
checksum,
"%06o", (
unsigned)(sum & 07777777));
379 fprintf(stderr,
"\thdrchksum \"%s\"\n", hdr->
checksum);
391 static const char * llname =
"././@LongLink";
393 const char * path = (iosm && iosm->
path ? iosm->
path :
"");
394 const char * lpath = (iosm && iosm->
lpath ? iosm->
lpath :
"");
401 fprintf(stderr,
" tarHeaderWrite(%p, %p)\n", iosm, st);
404 if (nb >
sizeof(hdr->
name)) {
405 memset(hdr, 0,
sizeof(*hdr));
406 strcpy(hdr->
name, llname);
407 sprintf(hdr->
mode,
"%07o", 0);
408 sprintf(hdr->
uid,
"%07o", 0);
409 sprintf(hdr->
gid,
"%07o", 0);
410 sprintf(hdr->
filesize,
"%011o", (
unsigned) (nb & 037777777777));
411 sprintf(hdr->
mtime,
"%011o", 0);
413 strncpy(hdr->
uname,
"root",
sizeof(hdr->
uname));
414 strncpy(hdr->
gname,
"root",
sizeof(hdr->
gname));
421 if (lpath && lpath[0] !=
'0') {
423 if (nb >
sizeof(hdr->
name)) {
424 memset(hdr, 0,
sizeof(*hdr));
426 sprintf(hdr->
mode,
"%07o", 0);
427 sprintf(hdr->
uid,
"%07o", 0);
428 sprintf(hdr->
gid,
"%07o", 0);
429 sprintf(hdr->
filesize,
"%011o", (
unsigned) (nb & 037777777777));
430 sprintf(hdr->
mtime,
"%011o", 0);
432 strncpy(hdr->
uname,
"root",
sizeof(hdr->
uname));
433 strncpy(hdr->
gname,
"root",
sizeof(hdr->
gname));
441 memset(hdr, 0,
sizeof(*hdr));
443 strncpy(hdr->
name, path,
sizeof(hdr->
name));
445 if (lpath && lpath[0] !=
'\0')
448 sprintf(hdr->
mode,
"%07o", (
unsigned int)(st->st_mode & 00007777));
449 sprintf(hdr->
uid,
"%07o", (
unsigned int)(st->st_uid & 07777777));
450 sprintf(hdr->
gid,
"%07o", (
unsigned int)(st->st_gid & 07777777));
452 sprintf(hdr->
filesize,
"%011o", (
unsigned) (st->st_size & 037777777777));
453 sprintf(hdr->
mtime,
"%011o", (
unsigned) (st->st_mtime & 037777777777));
458 else if (S_ISCHR(st->st_mode))
460 else if (S_ISBLK(st->st_mode))
462 else if (S_ISDIR(st->st_mode))
464 else if (S_ISFIFO(st->st_mode))
470 else if (S_ISREG(st->st_mode))
471 hdr->
typeflag = (lpath && lpath[0] !=
'\0' ?
'1' :
'0');
475 if (s == NULL) s =
"root";
478 if (s == NULL) s =
"root";
482 dev =
major((
unsigned)st->st_dev);
483 sprintf(hdr->
devMajor,
"%07o", (
unsigned) (dev & 07777777));
484 dev =
minor((
unsigned)st->st_dev);
485 sprintf(hdr->
devMinor,
"%07o", (
unsigned) (dev & 07777777));
491 #if !defined(JBJ_WRITEPAD)
505 fprintf(stderr,
" tarTrailerWrite(%p)\n", iosm);
509 #if defined(JBJ_WRITEPAD)
515 #if defined(JBJ_WRITEPAD)