+static union block *
+start_header (const char *name, struct tar_stat_info *st)
+{
+ union block *header;
+
+ name = safer_name_suffix (name, 0);
+ assign_string (&st->file_name, name);
+
+ if (sizeof header->header.name <= strlen (name))
+ {
+ if (archive_format == POSIX_FORMAT)
+ xheader_store ("path", st);
+ else
+ write_long (name, GNUTYPE_LONGNAME);
+ }
+
+ header = find_next_block ();
+ memset (header->buffer, 0, sizeof (union block));
+
+ assign_string (¤t_stat_info.file_name, name);
+
+ strncpy (header->header.name, name, NAME_FIELD_SIZE);
+ header->header.name[NAME_FIELD_SIZE - 1] = '\0';
+
+ /* Override some stat fields, if requested to do so. */
+
+ if (owner_option != (uid_t) -1)
+ st->stat.st_uid = owner_option;
+ if (group_option != (gid_t) -1)
+ st->stat.st_gid = group_option;
+ if (mode_option)
+ st->stat.st_mode = ((st->stat.st_mode & ~MODE_ALL)
+ | mode_adjust (st->stat.st_mode, mode_option));
+
+ /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
+ for a few tars and came up with the following interoperability
+ matrix:
+
+ WRITER
+ 1 2 3 4 5 6 7 8 9 READER
+ . . . . . . . . . 1 = SunOS 4.2 tar
+ # . . # # . . # # 2 = NEC SVR4.0.2 tar
+ . . . # # . . # . 3 = Solaris 2.1 tar
+ . . . . . . . . . 4 = GNU tar 1.11.1
+ . . . . . . . . . 5 = HP-UX 8.07 tar
+ . . . . . . . . . 6 = Ultrix 4.1
+ . . . . . . . . . 7 = AIX 3.2
+ . . . . . . . . . 8 = Hitachi HI-UX 1.03
+ . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
+
+ . = works
+ # = ``impossible file type''
+
+ The following mask for old archive removes the `#'s in column 4
+ above, thus making GNU tar both a universal donor and a universal
+ acceptor for Paul's test. */
+
+ if (archive_format == V7_FORMAT)
+ MODE_TO_CHARS (st->stat.st_mode & MODE_ALL, header->header.mode);
+ else
+ MODE_TO_CHARS (st->stat.st_mode, header->header.mode);
+
+ if (st->stat.st_uid > MAXOCTAL7 && archive_format == POSIX_FORMAT)
+ xheader_store ("uid", st);
+ else
+ UID_TO_CHARS (st->stat.st_uid, header->header.uid);
+
+ if (st->stat.st_gid > MAXOCTAL7 && archive_format == POSIX_FORMAT)
+ xheader_store ("gid", st);
+ else
+ GID_TO_CHARS (st->stat.st_gid, header->header.gid);
+
+ if (st->stat.st_size > MAXOCTAL11 && archive_format == POSIX_FORMAT)
+ xheader_store ("size", st);
+ else
+ OFF_TO_CHARS (st->stat.st_size, header->header.size);
+
+ TIME_TO_CHARS (st->stat.st_mtime, header->header.mtime);
+
+ /* FIXME */
+ if (S_ISCHR (st->stat.st_mode)
+ || S_ISBLK (st->stat.st_mode))
+ {
+ st->devmajor = major (st->stat.st_rdev);
+ st->devminor = minor (st->stat.st_rdev);
+
+ if (st->devmajor > MAXOCTAL7 && archive_format == POSIX_FORMAT)
+ xheader_store ("devmajor", st);
+ else
+ MAJOR_TO_CHARS (st->devmajor, header->header.devmajor);
+
+ if (st->devminor > MAXOCTAL7 && archive_format == POSIX_FORMAT)
+ xheader_store ("devminor", st);
+ else
+ MAJOR_TO_CHARS (st->devminor, header->header.devminor);
+ }
+ else
+ {
+ MAJOR_TO_CHARS (0, header->header.devmajor);
+ MINOR_TO_CHARS (0, header->header.devminor);
+ }
+
+ if (archive_format == POSIX_FORMAT)
+ {
+ xheader_store ("atime", st);
+ xheader_store ("ctime", st);
+ }
+ else if (incremental_option)
+ if (archive_format == OLDGNU_FORMAT)
+ {
+ TIME_TO_CHARS (st->stat.st_atime, header->oldgnu_header.atime);
+ TIME_TO_CHARS (st->stat.st_ctime, header->oldgnu_header.ctime);
+ }
+
+ header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
+
+ switch (archive_format)
+ {
+ case V7_FORMAT:
+ break;
+
+ case OLDGNU_FORMAT:
+ /* Overwrite header->header.magic and header.version in one blow. */
+ strcpy (header->header.magic, OLDGNU_MAGIC);
+ break;
+
+ case POSIX_FORMAT:
+ case GNU_FORMAT:
+ strncpy (header->header.magic, TMAGIC, TMAGLEN);
+ strncpy (header->header.version, TVERSION, TVERSLEN);
+ break;
+
+ default:
+ abort ();
+ }
+
+ if (archive_format == V7_FORMAT || numeric_owner_option)
+ {
+ /* header->header.[ug]name are left as the empty string. */
+ }
+ else
+ {
+ uid_to_uname (st->stat.st_uid, &st->uname);
+ gid_to_gname (st->stat.st_gid, &st->gname);
+
+ if (archive_format == POSIX_FORMAT
+ && strlen (st->uname) > UNAME_FIELD_SIZE)
+ xheader_store ("uname", st);
+ else
+ UNAME_TO_CHARS (st->uname, header->header.uname);
+
+ if (archive_format == POSIX_FORMAT
+ && strlen (st->gname) > GNAME_FIELD_SIZE)
+ xheader_store ("gname", st);
+ else
+ GNAME_TO_CHARS (st->gname, header->header.gname);
+ }
+
+ return header;
+}
+
+/* Finish off a filled-in header block and write it out. We also
+ print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
+ is not negative, is the block ordinal of the first record for this
+ file, which may be a preceding long name or long link record. */
+void
+finish_header (union block *header, off_t block_ordinal)
+{
+ size_t i;
+ int sum;
+ char *p;
+
+ /* Note: It is important to do this before the call to write_extended(),
+ so that the actual ustar header is printed */
+ if (verbose_option
+ && header->header.typeflag != GNUTYPE_LONGLINK
+ && header->header.typeflag != GNUTYPE_LONGNAME
+ && header->header.typeflag != XHDTYPE
+ && header->header.typeflag != XGLTYPE)
+ {
+ /* These globals are parameters to print_header, sigh. */
+
+ current_header = header;
+ /* current_stat_info is already set up. */
+ current_format = archive_format;
+ print_header (block_ordinal);
+ }
+
+ header = write_extended (header, XHDTYPE);
+
+ memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum);
+
+ sum = 0;
+ p = header->buffer;
+ for (i = sizeof *header; i-- != 0; )
+ /* We can't use unsigned char here because of old compilers, e.g. V7. */
+ sum += 0xFF & *p++;
+
+ /* Fill in the checksum field. It's formatted differently from the
+ other fields: it has [6] digits, a null, then a space -- rather than
+ digits, then a null. We use to_chars.
+ The final space is already there, from
+ checksumming, and to_chars doesn't modify it.
+
+ This is a fast way to do:
+
+ sprintf(header->header.chksum, "%6o", sum); */
+
+ uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7);
+
+ set_next_block_after (header);
+}
+\f
+/* Sparse file processing. */
+
+/* Takes a blockful of data and basically cruises through it to see if
+ it's made *entirely* of zeros, returning a 0 the instant it finds
+ something that is a nonzero, i.e., useful data. */
+static int
+zero_block_p (char *buffer)
+{
+ int counter;