1 /* Create a tar archive.
2 Copyright (C) 1985, 1992 Free Software Foundation
4 This file is part of GNU Tar.
6 GNU Tar is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Tar is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Tar; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 * Create a tar archive.
23 * Written 25 Aug 1985 by John Gilmore, ihnp4!hoptoad!gnu.
29 #include <sys/types.h>
52 #if defined(_POSIX_VERSION) || defined(DIRENT)
58 #define DP_NAMELEN(x) strlen((x)->d_name)
59 #endif /* _POSIX_VERSION or DIRENT */
60 #if !defined(_POSIX_VERSION) && !defined(DIRENT) && defined(BSD42)
62 #define DP_NAMELEN(x) (x)->d_namlen
63 #endif /* not _POSIX_VERSION and BSD42 */
66 #define DP_NAMELEN(x) (x)->d_namlen
69 #if defined(USG) && !defined(_POSIX_VERSION) && !defined(DIRENT)
71 #define DP_NAMELEN(x) strlen((x)->d_name)
72 #endif /* USG and not _POSIX_VERSION and not DIRENT */
74 extern struct stat hstat; /* Stat struct corresponding */
82 extern struct name *gnu_list_name;
85 * If there are no symbolic links, there is no lstat(). Use stat().
91 extern void print_header();
93 union record *start_header();
95 void add_symlink_mangle();
96 void blank_name_list();
101 void close_archive();
102 void collect_and_sort_names();
104 int deal_with_sparse();
105 void find_new_file_size();
106 void finish_header();
107 int finish_sparse_file();
110 int is_dot_or_dotdot();
116 void write_dir_file();
118 void write_mangled();
121 /* This code moved from tar.h since create.c is the only file that cares
122 about 'struct link's. This means that other files might not have to
123 include sys/types.h any more. */
133 struct link *linklist; /* Points to first link in list */
135 static nolinks; /* Gets set if we run out of RAM */
138 * "Scratch" space to store the information about a sparse file before
139 * writing the info into the header or extended header
141 /* struct sp_array *sparsearray;*/
143 /* number of elts storable in the sparsearray */
144 /*int sparse_array_size = 10;*/
150 char *name_from_list();
152 open_archive(0); /* Open for writing */
155 char *buf = ck_malloc(PATH_MAX);
158 collect_and_sort_names();
160 while(p=name_from_list())
162 /* if(!f_dironly) { */
164 while(p=name_from_list()) {
166 if(p[strlen(p)-1]!='/')
168 bufp=buf+strlen(buf);
169 for(q=gnu_list_name->dir_contents;q && *q;q+=strlen(q)+1) {
172 dump_file(buf,-1, 1);
181 dump_file(".", -1, 1);
183 do dump_file(p, -1, 1);
184 while (p = name_next(1));
197 * Dump a single file. If it's a directory, recurse.
198 * Result is 1 for success, 0 for failure.
199 * Sets global "hstat" to stat() output for this file.
202 dump_file (p, curdev, toplevel)
203 char *p; /* File name to dump */
204 int curdev; /* Device our parent dir was on */
205 int toplevel; /* Whether we are a toplevel call */
207 union record *header;
209 extern char *save_name; /* JF for multi-volume support */
210 extern long save_totsize;
211 extern long save_sizeleft;
214 extern time_t new_time;
215 int critical_error = 0;
216 /* int sparse_ind = 0;*/
219 if(f_confirm && !confirm("add",p))
223 * Use stat if following (rather than dumping) 4.2BSD's
224 * symbolic links. Otherwise, use lstat (which, on non-4.2
225 * systems, is #define'd to stat anyway.
227 #ifdef STX_HIDDEN /* AIX */
228 if (0 != f_follow_links ?
229 statx (p, &hstat, STATSIZE, STX_HIDDEN):
230 statx (p, &hstat, STATSIZE, STX_HIDDEN|STX_LINK))
232 if (0 != f_follow_links? stat(p, &hstat): lstat(p, &hstat))
236 msg_perror("can't add file %s",p);
238 if (!f_ignore_failed_read || critical_error)
244 if (S_ISHIDDEN (hstat.st_mode)) {
245 char *new = (char *)alloca (strlen (p) + 2);
254 /* See if we only want new files, and check if this one is too old to
255 put in the archive. */
258 && new_time>hstat.st_mtime
259 && !S_ISDIR(hstat.st_mode)
260 && (f_new_files>1 || new_time>hstat.st_ctime)) {
262 msg("%s: is unchanged; not dumped",p);
268 /* See if we are trying to dump the archive */
269 if(ar_dev && hstat.st_dev==ar_dev && hstat.st_ino==ar_ino) {
270 msg("%s is the archive; not dumped",p);
275 * Check for multiple links.
277 * We maintain a list of all such files that we've written so
278 * far. Any time we see another, we check the list and
279 * avoid dumping the data again if we've done it once already.
281 if (hstat.st_nlink > 1
282 && (S_ISREG(hstat.st_mode)
284 || S_ISCTG(hstat.st_mode)
287 || S_ISCHR(hstat.st_mode)
290 || S_ISBLK(hstat.st_mode)
293 || S_ISFIFO(hstat.st_mode)
296 register struct link *lp;
298 /* First quick and dirty. Hashing, etc later FIXME */
299 for (lp = linklist; lp; lp = lp->next) {
300 if (lp->ino == hstat.st_ino &&
301 lp->dev == hstat.st_dev) {
302 char *link_name = lp->name;
304 /* We found a link. */
306 header = start_header(p, &hstat);
312 while(!f_absolute_paths && *link_name == '/') {
313 static int link_warn = 0;
316 msg("Removing leading / from absolute links");
321 strncpy(header->header.linkname,
323 if(header->header.linkname[NAMSIZ-1]) {
325 extern char *find_mangled();
327 mangled=find_mangled(link_name);
328 msg("%s: link name too long: mangled to %s",link_name,mangled);
329 strncpy(header->header.linkname,mangled,NAMSIZ);
331 header->header.linkflag = LF_LINK;
332 finish_header(header);
333 /* FIXME: Maybe remove from list after all links found? */
336 if (unlink (p) == -1)
337 msg_perror ("cannot remove %s", p);
339 return; /* We dumped it */
343 /* Not found. Add it to the list of possible links. */
344 lp = (struct link *)malloc((unsigned)(sizeof(struct link)+strlen(p)));
348 "no memory for links, they will be dumped as separate files");
352 lp->ino = hstat.st_ino;
353 lp->dev = hstat.st_dev;
360 * This is not a link to a previously dumped file, so dump it.
362 if (S_ISREG(hstat.st_mode)
364 || S_ISCTG(hstat.st_mode)
368 int f; /* File descriptor */
371 register union record *start;
375 /* int end_nulls = 0; */
380 if (f_sparse_files) {
382 * JK - This is the test for sparseness: whether the
383 * "size" of the file matches the number of blocks
384 * allocated for it. If there is a smaller number
385 * of blocks that would be necessary to accommodate
386 * a file of this size, we have a sparse file, i.e.,
387 * at least one of those records in the file is just
390 #ifdef hpux /* Nice of HPUX to gratuitiously change it, huh? - mib */
391 if (hstat.st_size - (hstat.st_blocks * 1024) > 1024 )
393 if (hstat.st_size - (hstat.st_blocks * RECORDSIZE) > RECORDSIZE)
396 int filesize = hstat.st_size;
399 header = start_header(p, &hstat);
405 header->header.linkflag = LF_SPARSE;
409 * Call the routine that figures out the
410 * layout of the sparse file in question.
411 * UPPERBOUND is the index of the last
412 * element of the "sparsearray," i.e.,
413 * the number of elements it needed to
417 upperbound = deal_with_sparse(p, header);
420 * See if we'll need an extended header
423 if (upperbound > SPARSE_IN_HDR-1)
424 header->header.isextended++;
426 * We store the "real" file size so
427 * we can show that in case someone wants
428 * to list the archive, i.e., tar tvf <file>.
429 * It might be kind of disconcerting if the
430 * shrunken file size was the one that showed
433 to_oct((long) hstat.st_size, 1+12,
434 header->header.realsize);
437 * This will be the new "size" of the
438 * file, i.e., the size of the file
439 * minus the records of holes that we're
443 find_new_file_size(&filesize, upperbound);
444 hstat.st_size = filesize;
445 to_oct((long) filesize, 1+12,
446 header->header.size);
447 /* to_oct((long) end_nulls, 1+12,
448 header->header.ending_blanks);*/
450 for (i = 0; i < SPARSE_IN_HDR; i++) {
451 if (!sparsearray[i].numbytes)
453 to_oct(sparsearray[i].offset, 1+12,
454 header->header.sp[i].offset);
455 to_oct(sparsearray[i].numbytes, 1+12,
456 header->header.sp[i].numbytes);
462 upperbound=SPARSE_IN_HDR-1;
465 sizeleft = hstat.st_size;
466 /* Don't bother opening empty, world readable files. */
467 if (sizeleft > 0 || 0444 != (0444 & hstat.st_mode)) {
468 f = open(p, O_RDONLY|O_BINARY);
469 if (f < 0) goto badperror;
474 /* If the file is sparse, we've already taken care of this */
476 header = start_header(p, &hstat);
477 if (header == NULL) {
485 /* Mark contiguous files, if we support them */
486 if (f_standard && S_ISCTG(hstat.st_mode)) {
487 header->header.linkflag = LF_CONTIG;
490 isextended = header->header.isextended;
491 save_linkflag = header->header.linkflag;
492 finish_header(header);
496 /* register union record *exhdr;*/
497 /* int arraybound = SPARSE_EXT_HDR;*/
498 /* static */ int index_offset = SPARSE_IN_HDR;
500 extend: exhdr = findrec();
507 bzero(exhdr->charptr, RECORDSIZE);
508 for (i = 0; i < SPARSE_EXT_HDR; i++) {
509 if (i+index_offset > upperbound)
511 to_oct((long) sparsearray[i+index_offset].numbytes,
513 exhdr->ext_hdr.sp[i].numbytes);
514 to_oct((long) sparsearray[i+index_offset].offset,
516 exhdr->ext_hdr.sp[i].offset);
520 if (sum < upperbound)
522 if (index_offset+i < upperbound) {
524 exhdr->ext_hdr.isextended++;
529 if (save_linkflag == LF_SPARSE) {
530 if (finish_sparse_file(f, &sizeleft, hstat.st_size, p))
534 while (sizeleft > 0) {
538 save_sizeleft = sizeleft;
539 save_totsize = hstat.st_size;
543 bufsize = endofrecs()->charptr - start->charptr;
545 if (sizeleft < bufsize) {
546 /* Last read -- zero out area beyond */
547 bufsize = (int)sizeleft;
548 count = bufsize % RECORDSIZE;
550 bzero(start->charptr + sizeleft,
551 (int)(RECORDSIZE - count));
553 count = read(f, start->charptr, bufsize);
555 msg_perror("read error at byte %ld, reading\
556 %d bytes, in file %s", hstat.st_size - sizeleft, bufsize,p);
561 /* This is nonportable (the type of userec's arg). */
562 userec(start+(count-1)/RECORDSIZE);
564 if (count == bufsize) continue;
565 msg( "file %s shrunk by %d bytes, padding with zeros.", p, sizeleft);
566 goto padit; /* Short read */
577 if (unlink (p) == -1)
578 msg_perror ("cannot remove %s", p);
583 * File shrunk or gave error, pad out tape to match
584 * the size we specified in the header.
588 save_sizeleft=sizeleft;
590 bzero(start->charptr,RECORDSIZE);
592 sizeleft-=RECORDSIZE;
602 else if(S_ISLNK(hstat.st_mode))
606 hstat.st_size = 0; /* Force 0 size on symlink */
607 header = start_header(p, &hstat);
613 size = readlink(p, header->header.linkname, NAMSIZ);
614 if (size < 0) goto badperror;
615 if (size == NAMSIZ) {
616 char *buf = ck_malloc(PATH_MAX);
618 readlink(p,buf,PATH_MAX);
619 /* next_mangle(header->header.linkname); */
620 add_symlink_mangle(buf,p,header->header.linkname);
621 msg("symbolic link %s too long: mangling to %s",p, header->header.linkname);
622 /* size=strlen(header->header.linkname); */
625 header->header.linkname[size] = '\0';
626 header->header.linkflag = LF_SYMLINK;
627 finish_header(header); /* Nothing more to do to it */
630 if (unlink (p) == -1)
631 msg_perror ("cannot remove %s", p);
637 else if (S_ISDIR(hstat.st_mode))
640 register struct direct *d;
644 int our_device = hstat.st_dev;
646 /* Build new prototype name */
649 namebuf=ck_malloc(buflen+1);
650 strncpy(namebuf, p, buflen);
651 while (len >= 1 && '/' == namebuf[len-1])
652 len--; /* Delete trailing slashes */
653 namebuf[len++] = '/'; /* Now add exactly one back */
654 namebuf[len] = '\0'; /* Make sure null-terminated */
657 * Output directory header record with permissions
658 * FIXME, do this AFTER files, to avoid R/O dir problems?
659 * If old archive format, don't write record at all.
662 hstat.st_size = 0; /* Force 0 size on dir */
664 * If people could really read standard archives,
665 * this should be: (FIXME)
666 header = start_header(f_standard? p: namebuf, &hstat);
667 * but since they'd interpret LF_DIR records as
668 * regular files, we'd better put the / on the name.
670 header = start_header(namebuf, &hstat);
674 goto badfile; /* eg name too long */
678 header->header.linkflag = LF_DUMPDIR;
680 header->header.linkflag = LF_DIR;
682 /* If we're gnudumping, we aren't done yet so don't close it. */
684 finish_header(header); /* Done with directory header */
695 buf=gnu_list_name->dir_contents; /* FOO */
697 for(p_buf=buf;p_buf && *p_buf;) {
705 to_oct((long)totsize,1+12,header->header.size);
706 finish_header(header);
712 save_sizeleft=sizeleft;
713 save_totsize=totsize;
716 bufsize=endofrecs()->charptr - start->charptr;
717 if(sizeleft<bufsize) {
719 count=bufsize%RECORDSIZE;
721 bzero(start->charptr+sizeleft,RECORDSIZE-count);
723 bcopy(p_buf,start->charptr,bufsize);
726 userec(start+(bufsize-1)/RECORDSIZE);
733 /* Now output all the files in the directory */
736 return; /* Unless the cmdline said not to */
739 * See if we are crossing from one file system to another,
740 * and avoid doing so if the user only wants to dump one file system.
742 if (f_local_filesys && toplevel && curdev != hstat.st_dev) {
744 msg("%s: is on a different filesystem; not dumped",p);
753 msg_perror ("can't open directory %s",p);
755 msg("error opening directory %s",
761 /* Hack to remove "./" from the front of all the file names */
762 if (len == 2 && namebuf[0] == '.' && namebuf[1]=='/')
765 /* Should speed this up by cd-ing into the dir, FIXME */
766 while (NULL != (d=readdir(dirp))) {
768 if(is_dot_or_dotdot(d->d_name))
771 if (DP_NAMELEN(d) + len >= buflen) {
772 buflen=len+DP_NAMELEN(d);
773 namebuf=ck_realloc(namebuf,buflen+1);
774 /* namebuf[len]='\0';
775 msg("file name %s%s too long",
779 strcpy(namebuf+len, d->d_name);
780 if(f_exclude && check_exclude(namebuf))
782 dump_file(namebuf, our_device, 0);
791 else if (S_ISCHR(hstat.st_mode)) {
797 else if (S_ISBLK(hstat.st_mode)) {
802 /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK */
803 #if (_ISP__M68K == 0) && (_ISP__A88K == 0) && defined(S_ISFIFO)
804 else if (S_ISFIFO(hstat.st_mode)) {
810 else if (S_ISSOCK(hstat.st_mode)) {
817 if (!f_standard) goto unknown;
819 hstat.st_size = 0; /* Force 0 size */
820 header = start_header(p, &hstat);
824 goto badfile; /* eg name too long */
827 header->header.linkflag = type;
828 #if defined(S_IFBLK) || defined(S_IFCHR)
829 if (type != LF_FIFO) {
830 to_oct((long) major(hstat.st_rdev), 8,
831 header->header.devmajor);
832 to_oct((long) minor(hstat.st_rdev), 8,
833 header->header.devminor);
837 finish_header(header);
840 if (unlink (p) == -1)
841 msg_perror ("cannot remove %s", p);
846 msg("%s: Unknown file type; file ignored.", p);
850 finish_sparse_file(fd, sizeleft, fullsize, name)
857 char tempbuf[RECORDSIZE];
865 while (*sizeleft > 0) {
867 bzero(start->charptr, RECORDSIZE);
868 bufsize = sparsearray[sparse_ind].numbytes;
869 if (!bufsize) { /* we blew it, maybe */
870 msg("Wrote %ld of %ld bytes to file %s",
871 fullsize - *sizeleft, fullsize, name);
874 pos = lseek(fd, sparsearray[sparse_ind++].offset, 0);
876 * If the number of bytes to be written here exceeds
877 * the size of the temporary buffer, do it in steps.
879 while (bufsize > RECORDSIZE) {
881 count = read(fd, start->charptr+amt_read, RECORDSIZE-amt_read);
882 bufsize -= RECORDSIZE - amt_read;
886 bzero(start->charptr, RECORDSIZE);
889 count = read(fd, start->charptr, RECORDSIZE);
891 msg_perror("read error at byte %ld, reading %d bytes, in file %s",
892 fullsize - *sizeleft, bufsize, name);
898 nwritten += RECORDSIZE; /* XXX */
900 bzero(start->charptr, RECORDSIZE);
904 clear_buffer(tempbuf);
905 count = read(fd, tempbuf, bufsize);
906 bcopy(tempbuf, start->charptr, RECORDSIZE);
908 msg_perror("read error at byte %ld, reading %d bytes, in file %s",
909 fullsize - *sizeleft, bufsize, name);
912 /* if (amt_read >= RECORDSIZE) {
914 userec(start+(count-1)/RECORDSIZE);
915 if (count != bufsize) {
916 msg("file %s shrunk by %d bytes, padding with zeros.", name, sizeleft);
921 amt_read += bufsize;*/
922 nwritten += count; /* XXX */
928 /* printf ("Amount actually written is (I hope) %d.\n", nwritten); */
929 /* userec(start+(count-1)/RECORDSIZE);*/
941 * Make room for our scratch space -- initially is 10 elts long
943 sparsearray = (struct sp_array *) malloc(sp_array_size * sizeof(struct sp_array));
944 for (i = 0; i < sp_array_size; i++) {
945 sparsearray[i].offset = 0;
946 sparsearray[i].numbytes = 0;
953 * Okay, we've got a sparse file on our hands -- now, what we need to do is
954 * make a pass through the file and carefully note where any data is, i.e.,
955 * we want to find how far into the file each instance of data is, and how
956 * many bytes are there. We store this information in the sparsearray,
957 * which will later be translated into header information. For now, we use
958 * the sparsearray as convenient storage.
960 * As a side note, this routine is a mess. If I could have found a cleaner
961 * way to do it, I would have. If anyone wants to find a nicer way to do
965 /* There is little point in trimming small amounts of null data at the */
966 /* head and tail of blocks -- it's ok if we only avoid dumping blocks */
967 /* of complete null data */
969 deal_with_sparse(name, header, nulls_at_end)
971 union record *header;
976 /* long save_offset;*/
978 /* int current_size = hstat.st_size;*/
981 char buf[RECORDSIZE];
983 int read_last_data = 0; /* did we just read the last record? */
987 header->header.isextended = 0;
989 * Can't open the file -- this problem will be caught later on,
992 if ((fd = open(name, O_RDONLY)) < 0)
998 while ((cc = read(fd, buf, sizeof buf)) != 0) {
1000 if (sparse_ind > sp_array_size-1) {
1003 * realloc the scratch area, since we've run out of room --
1005 sparsearray = (struct sp_array *)
1006 realloc(sparsearray,
1007 2 * sp_array_size * (sizeof(struct sp_array)));
1010 if (cc == sizeof buf) {
1011 if (zero_record(buf)) {
1013 sparsearray[sparse_ind++].numbytes
1017 } else { /* !zero_record(buf) */
1023 sparsearray[sparse_ind].offset
1027 } else if (cc < sizeof buf) {
1028 /* This has to be the last bit of the file, so this */
1029 /* is somewhat shorter than the above. */
1030 if (!zero_record(buf)) {
1034 sparsearray[sparse_ind].offset
1044 sparsearray[sparse_ind++].numbytes = numbytes;
1047 return sparse_ind - 1;
1051 * Just zeroes out the buffer so we don't confuse ourselves with leftover
1060 for (i = 0; i < RECORDSIZE; i++)
1064 #if 0 /* I'm leaving this as a monument to Joy Kendall, who wrote it -mib */
1067 * This routine takes a character array, and tells where within that array
1068 * the data can be found. It skips over any zeros, and sets the first
1069 * non-zero point in the array to be the "start", and continues until it
1070 * finds non-data again, which is marked as the "end." This routine is
1071 * mainly for 1) seeing how far into a file we must lseek to data, given
1072 * that we have a sparse file, and 2) determining the "real size" of the
1073 * file, i.e., the number of bytes in the sparse file that are data, as
1074 * opposed to the zeros we are trying to skip.
1076 where_is_data(from, to, buffer)
1082 register int save_to = *to;
1083 int amidst_data = 0;
1090 if (*from < 16) /* don't bother */
1092 /* keep going to make sure there isn't more real
1093 data in this record */
1094 while (i < RECORDSIZE) {
1102 else if (buffer[i]) {
1108 if (i == RECORDSIZE)
1116 /* Note that this routine is only called if zero_record returned true */
1117 #if 0 /* But we actually don't need it at all. */
1118 where_is_data (from, to, buffer)
1124 for (fp = buffer; ! *fp; fp++)
1126 for (tp = buffer + RECORDSIZE - 1; ! *tp; tp--)
1128 *from = fp - buffer;
1129 *to = tp - buffer + 1;
1136 * Takes a recordful of data and basically cruises through it to see if
1137 * it's made *entirely* of zeros, returning a 0 the instant it finds
1138 * something that is a non-zero, i.e., useful data.
1146 for (i = 0; i < RECORDSIZE; i++)
1147 if (buffer[i] != '\000')
1153 find_new_file_size(filesize, highest_index)
1160 for (i = 0; sparsearray[i].numbytes && i <= highest_index; i++)
1161 *filesize += sparsearray[i].numbytes;
1165 * Make a header block for the file name whose stat info is st .
1166 * Return header pointer for success, NULL if the name is too long.
1169 start_header(name, st)
1171 register struct stat *st;
1173 register union record *header;
1175 header = (union record *) findrec();
1176 bzero(header->charptr, sizeof(*header)); /* XXX speed up */
1179 * Check the file name and put it in the record.
1181 if(!f_absolute_paths) {
1182 static int warned_once = 0;
1187 msg("Removing drive spec from names in the archive");
1190 while ('/' == *name) {
1191 name++; /* Force relative path */
1193 msg("Removing leading / from absolute path names in the archive.");
1196 strncpy(header->header.name, name, NAMSIZ);
1197 if (header->header.name[NAMSIZ-1]) {
1200 /* next_mangle(header->header.name); */
1201 add_mangle(name,header->header.name);
1202 msg("%s: is too long: mangling to %s", name, header->header.name);
1205 to_oct((long) (st->st_mode & 07777),
1206 8, header->header.mode);
1207 to_oct((long) st->st_uid, 8, header->header.uid);
1208 to_oct((long) st->st_gid, 8, header->header.gid);
1209 to_oct((long) st->st_size, 1+12, header->header.size);
1210 to_oct((long) st->st_mtime, 1+12, header->header.mtime);
1211 /* header->header.linkflag is left as null */
1213 to_oct((long) st->st_atime, 1+12, header->header.atime);
1214 to_oct((long) st->st_ctime, 1+12, header->header.ctime);
1218 /* Fill in new Unix Standard fields if desired. */
1220 header->header.linkflag = LF_NORMAL; /* New default */
1221 strcpy(header->header.magic, TMAGIC); /* Mark as Unix Std */
1222 /* Fill in the POSIX TVERSION field after magic. */
1223 header->header.magic[6] = header->header.magic[7] = '0';
1224 finduname(header->header.uname, st->st_uid);
1225 findgname(header->header.gname, st->st_gid);
1232 * Finish off a filled-in header block and write it out.
1233 * We also print the file name and/or full info if verbose is on.
1236 finish_header(header)
1237 register union record *header;
1239 register int i, sum;
1242 bcopy(CHKBLANKS, header->header.chksum, sizeof(header->header.chksum));
1245 p = header->charptr;
1246 for (i = sizeof(*header); --i >= 0; ) {
1248 * We can't use unsigned char here because of old compilers,
1255 * Fill in the checksum field. It's formatted differently
1256 * from the other fields: it has [6] digits, a null, then a
1257 * space -- rather than digits, a space, then a null.
1258 * We use to_oct then write the null in over to_oct's space.
1259 * The final space is already there, from checksumming, and
1260 * to_oct doesn't modify it.
1262 * This is a fast way to do:
1263 * (void) sprintf(header->header.chksum, "%6o", sum);
1265 to_oct((long) sum, 8, header->header.chksum);
1266 header->header.chksum[6] = '\0'; /* Zap the space */
1271 extern union record *head; /* Points to current tape header */
1272 extern int head_standard; /* Tape header is in ANSI format */
1274 /* These globals are parameters to print_header, sigh */
1276 /* hstat is already set up */
1277 head_standard = f_standard;
1286 * Quick and dirty octal conversion.
1287 * Converts long "value" into a "digs"-digit field at "where",
1288 * including a trailing space and room for a null. "digs"==3 means
1289 * 1 digit, a space, and room for a null.
1291 * We assume the trailing null is already there and don't fill it in.
1292 * This fact is used by start_header and finish_header, so don't change it!
1294 * This should be equivalent to:
1295 * (void) sprintf(where, "%*lo ", digs-2, value);
1296 * except that sprintf fills in the trailing null and we don't.
1299 to_oct(value, digs, where)
1300 register long value;
1302 register char *where;
1305 --digs; /* Trailing null slot is left alone */
1306 where[--digs] = ' '; /* Put in the space, though */
1308 /* Produce the digits -- at least one */
1310 where[--digs] = '0' + (char)(value & 7); /* one octal digit */
1312 } while (digs > 0 && value != 0);
1314 /* Leading spaces, if necessary */
1316 where[--digs] = ' ';
1322 * Write the EOT record(s).
1323 * We actually zero at least one record, through the end of the block.
1324 * Old tar writes garbage after two zeroed records -- and PDtar used to.
1335 bufsize = endofrecs()->charptr - p->charptr;
1336 bzero(p->charptr, bufsize);