xref: /386bsd/usr/src/usr.bin/tar/list.c (revision a2142627)
1 /* List a tar archive.
2    Copyright (C) 1988, 1992, 1993 Free Software Foundation
3 
4 This file is part of GNU Tar.
5 
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)
9 any later version.
10 
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.
15 
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.  */
19 
20 /*
21  * List a tar archive.
22  *
23  * Also includes support routines for reading a tar archive.
24  *
25  * this version written 26 Aug 1985 by John Gilmore (ihnp4!hoptoad!gnu).
26  */
27 
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <sys/types.h>
31 #include <errno.h>
32 #ifndef STDC_HEADERS
33 extern int errno;
34 #endif
35 #include <time.h>
36 
37 #ifdef BSD42
38 #include <sys/file.h>
39 #else
40 #ifndef V7
41 #include <fcntl.h>
42 #endif
43 #endif
44 
45 #define	isodigit(c)	( ((c) >= '0') && ((c) <= '7') )
46 
47 #include "tar.h"
48 #include "port.h"
49 
50 extern FILE *msg_file;
51 
52 long from_oct ();		/* Decode octal number */
53 void demode ();			/* Print file mode */
54 void restore_saved_dir_info ();
55 PTR ck_malloc ();
56 
57 union record *head;		/* Points to current archive header */
58 struct stat hstat;		/* Stat struct corresponding */
59 int head_standard;		/* Tape header is in ANSI format */
60 
61 int check_exclude ();
62 void close_archive ();
63 void decode_header ();
64 int findgid ();
65 int finduid ();
66 void name_gather ();
67 int name_match ();
68 void names_notfound ();
69 void open_archive ();
70 void print_header ();
71 int read_header ();
72 void saverec ();
73 void skip_file ();
74 void skip_extended_headers ();
75 
76 extern char *quote_copy_string ();
77 
78 
79 /*
80  * Main loop for reading an archive.
81  */
82 void
83 read_and (do_something)
84      void (*do_something) ();
85 {
86   int status = 3;		/* Initial status at start of archive */
87   int prev_status;
88   extern time_t new_time;
89   char save_linkflag;
90 
91   name_gather ();		/* Gather all the names */
92   open_archive (1);		/* Open for reading */
93 
94   for (;;)
95     {
96       prev_status = status;
97       status = read_header ();
98       switch (status)
99 	{
100 
101 	case 1:		/* Valid header */
102 	  /* We should decode next field (mode) first... */
103 	  /* Ensure incoming names are null terminated. */
104 
105 	  if (!name_match (current_file_name)
106 	      || (f_new_files && hstat.st_mtime < new_time)
107 	      || (f_exclude && check_exclude (current_file_name)))
108 	    {
109 
110 	      int isextended = 0;
111 
112 	      if (head->header.linkflag == LF_VOLHDR
113 		  || head->header.linkflag == LF_MULTIVOL
114 		  || head->header.linkflag == LF_NAMES)
115 		{
116 		  (*do_something) ();
117 		  continue;
118 		}
119 	      if (f_show_omitted_dirs
120 		  && head->header.linkflag == LF_DIR)
121 		msg ("Omitting %s\n", current_file_name);
122 	      /* Skip past it in the archive */
123 	      if (head->header.isextended)
124 		isextended = 1;
125 	      save_linkflag = head->header.linkflag;
126 	      userec (head);
127 	      if (isextended)
128 		{
129 		  /*					register union record *exhdr;
130 
131 					for (;;) {
132 					    exhdr = findrec();
133 					    if (!exhdr->ext_hdr.isextended) {
134 					    	userec(exhdr);
135 					    	break;
136 					    }
137 					}
138 					userec(exhdr);*/
139 		  skip_extended_headers ();
140 		}
141 	      /* Skip to the next header on the archive */
142 	      if (save_linkflag != LF_DIR)
143 		skip_file ((long) hstat.st_size);
144 	      continue;
145 
146 	    }
147 
148 	  (*do_something) ();
149 	  continue;
150 
151 	  /*
152 			 * If the previous header was good, tell them
153 			 * that we are skipping bad ones.
154 			 */
155 	case 0:		/* Invalid header */
156 	  userec (head);
157 	  switch (prev_status)
158 	    {
159 	    case 3:		/* Error on first record */
160 	      msg ("Hmm, this doesn't look like a tar archive.");
161 	      /* FALL THRU */
162 	    case 2:		/* Error after record of zeroes */
163 	    case 1:		/* Error after header rec */
164 	      msg ("Skipping to next file header...");
165 	    case 0:		/* Error after error */
166 	      break;
167 	    }
168 	  continue;
169 
170 	case 2:		/* Record of zeroes */
171 	  userec (head);
172 	  status = prev_status;	/* If error after 0's */
173 	  if (f_ignorez)
174 	    continue;
175 	  /* FALL THRU */
176 	case EOF:		/* End of archive */
177 	  break;
178 	}
179       break;
180     };
181 
182   restore_saved_dir_info ();
183   close_archive ();
184   names_notfound ();		/* Print names not found */
185 }
186 
187 
188 /*
189  * Print a header record, based on tar options.
190  */
191 void
list_archive()192 list_archive ()
193 {
194   extern char *save_name;
195   int isextended = 0;		/* Flag to remember if head is extended */
196 
197   /* Save the record */
198   saverec (&head);
199 
200   /* Print the header record */
201   if (f_verbose)
202     {
203       if (f_verbose > 1)
204 	decode_header (head, &hstat, &head_standard, 0);
205       print_header ();
206     }
207 
208   if (f_gnudump && head->header.linkflag == LF_DUMPDIR)
209     {
210       size_t size, written, check;
211       char *data;
212       extern long save_totsize;
213       extern long save_sizeleft;
214 
215       userec (head);
216       if (f_multivol)
217 	{
218 	  save_name = current_file_name;
219 	  save_totsize = hstat.st_size;
220 	}
221       for (size = hstat.st_size; size > 0; size -= written)
222 	{
223 	  if (f_multivol)
224 	    save_sizeleft = size;
225 	  data = findrec ()->charptr;
226 	  if (data == NULL)
227 	    {
228 	      msg ("EOF in archive file?");
229 	      break;
230 	    }
231 	  written = endofrecs ()->charptr - data;
232 	  if (written > size)
233 	    written = size;
234 	  errno = 0;
235 	  check = fwrite (data, sizeof (char), written, msg_file);
236 	  userec ((union record *) (data + written - 1));
237 	  if (check != written)
238 	    {
239 	      msg_perror ("only wrote %ld of %ld bytes to file %s", check, written, current_file_name);
240 	      skip_file ((long) (size) - written);
241 	      break;
242 	    }
243 	}
244       if (f_multivol)
245 	save_name = 0;
246       saverec ((union record **) 0);	/* Unsave it */
247       fputc ('\n', msg_file);
248       fflush (msg_file);
249       return;
250 
251     }
252   saverec ((union record **) 0);/* Unsave it */
253   /* Check to see if we have an extended header to skip over also */
254   if (head->header.isextended)
255     isextended = 1;
256 
257   /* Skip past the header in the archive */
258   userec (head);
259 
260   /*
261  	 * If we needed to skip any extended headers, do so now, by
262  	 * reading extended headers and skipping past them in the
263 	 * archive.
264 	 */
265   if (isextended)
266     {
267       /*		register union record *exhdr;
268 
269 		for (;;) {
270 			exhdr = findrec();
271 
272 			if (!exhdr->ext_hdr.isextended) {
273 				userec(exhdr);
274 				break;
275 			}
276 			userec(exhdr);
277 		}*/
278       skip_extended_headers ();
279     }
280 
281   if (f_multivol)
282     save_name = current_file_name;
283   /* Skip to the next header on the archive */
284 
285   skip_file ((long) hstat.st_size);
286 
287   if (f_multivol)
288     save_name = 0;
289 }
290 
291 
292 /*
293  * Read a record that's supposed to be a header record.
294  * Return its address in "head", and if it is good, the file's
295  * size in hstat.st_size.
296  *
297  * Return 1 for success, 0 if the checksum is bad, EOF on eof,
298  * 2 for a record full of zeros (EOF marker).
299  *
300  * You must always userec(head) to skip past the header which this
301  * routine reads.
302  */
303 int
read_header()304 read_header ()
305 {
306   register int i;
307   register long sum, signed_sum, recsum;
308   register char *p;
309   register union record *header;
310   long from_oct ();
311   char **longp;
312   char *bp, *data;
313   int size, written;
314   static char *next_long_name, *next_long_link;
315   char *name;
316 
317 recurse:
318 
319   header = findrec ();
320   head = header;		/* This is our current header */
321   if (NULL == header)
322     return EOF;
323 
324   recsum = from_oct (8, header->header.chksum);
325 
326   sum = 0;
327   p = header->charptr;
328   for (i = sizeof (*header); --i >= 0;)
329     {
330       /*
331 		 * We can't use unsigned char here because of old compilers,
332 		 * e.g. V7.
333 		 */
334       signed_sum += *p;
335       sum += 0xFF & *p++;
336     }
337 
338   /* Adjust checksum to count the "chksum" field as blanks. */
339   for (i = sizeof (header->header.chksum); --i >= 0;)
340     {
341       sum -= 0xFF & header->header.chksum[i];
342       signed_sum -= (char) header->header.chksum[i];
343     }
344   sum += ' ' * sizeof header->header.chksum;
345   signed_sum += ' ' * sizeof header->header.chksum;
346 
347   if (sum == 8 * ' ')
348     {
349       /*
350 		 * This is a zeroed record...whole record is 0's except
351 		 * for the 8 blanks we faked for the checksum field.
352 		 */
353       return 2;
354     }
355 
356   if (sum != recsum && signed_sum != recsum)
357     return 0;
358 
359   /*
360 	 * Good record.  Decode file size and return.
361 	 */
362   if (header->header.linkflag == LF_LINK)
363     hstat.st_size = 0;		/* Links 0 size on tape */
364   else
365     hstat.st_size = from_oct (1 + 12, header->header.size);
366 
367   header->header.arch_name[NAMSIZ - 1] = '\0';
368   if (header->header.linkflag == LF_LONGNAME
369       || header->header.linkflag == LF_LONGLINK)
370     {
371       longp = ((header->header.linkflag == LF_LONGNAME)
372 	       ? &next_long_name
373 	       : &next_long_link);
374 
375       userec (header);
376       if (*longp)
377 	free (*longp);
378       bp = *longp = (char *) ck_malloc (hstat.st_size);
379 
380       for (size = hstat.st_size;
381 	   size > 0;
382 	   size -= written)
383 	{
384 	  data = findrec ()->charptr;
385 	  if (data == NULL)
386 	    {
387 	      msg ("Unexpected EOF on archive file");
388 	      break;
389 	    }
390 	  written = endofrecs ()->charptr - data;
391 	  if (written > size)
392 	    written = size;
393 
394 	  bcopy (data, bp, written);
395 	  bp += written;
396 	  userec ((union record *) (data + written - 1));
397 	}
398       goto recurse;
399     }
400   else
401     {
402       name = (next_long_name
403 	      ? next_long_name
404 	      : head->header.arch_name);
405       if (current_file_name)
406 	free (current_file_name);
407       current_file_name = ck_malloc (strlen (name) + 1);
408       strcpy (current_file_name, name);
409 
410       name = (next_long_link
411 	      ? next_long_link
412 	      : head->header.arch_linkname);
413       if (current_link_name)
414 	free (current_link_name);
415       current_link_name = ck_malloc (strlen (name) + 1);
416       strcpy (current_link_name, name);
417 
418       next_long_link = next_long_name = 0;
419       return 1;
420     }
421 }
422 
423 
424 /*
425  * Decode things from a file header record into a "struct stat".
426  * Also set "*stdp" to !=0 or ==0 depending whether header record is "Unix
427  * Standard" tar format or regular old tar format.
428  *
429  * read_header() has already decoded the checksum and length, so we don't.
430  *
431  * If wantug != 0, we want the uid/group info decoded from Unix Standard
432  * tapes (for extraction).  If == 0, we are just printing anyway, so save time.
433  *
434  * decode_header should NOT be called twice for the same record, since the
435  * two calls might use different "wantug" values and thus might end up with
436  * different uid/gid for the two calls.  If anybody wants the uid/gid they
437  * should decode it first, and other callers should decode it without uid/gid
438  * before calling a routine, e.g. print_header, that assumes decoded data.
439  */
440 void
decode_header(header,st,stdp,wantug)441 decode_header (header, st, stdp, wantug)
442      register union record *header;
443      register struct stat *st;
444      int *stdp;
445      int wantug;
446 {
447   long from_oct ();
448 
449   st->st_mode = from_oct (8, header->header.mode);
450   st->st_mode &= 07777;
451   st->st_mtime = from_oct (1 + 12, header->header.mtime);
452   if (f_gnudump)
453     {
454       st->st_atime = from_oct (1 + 12, header->header.atime);
455       st->st_ctime = from_oct (1 + 12, header->header.ctime);
456     }
457 
458   if (0 == strcmp (header->header.magic, TMAGIC))
459     {
460       /* Unix Standard tar archive */
461       *stdp = 1;
462       if (wantug)
463 	{
464 #ifdef NONAMES
465 	  st->st_uid = from_oct (8, header->header.uid);
466 	  st->st_gid = from_oct (8, header->header.gid);
467 #else
468 	  st->st_uid =
469 	    (*header->header.uname
470 	     ? finduid (header->header.uname)
471 	     : from_oct (8, header->header.uid));
472 	  st->st_gid =
473 	    (*header->header.gname
474 	     ? findgid (header->header.gname)
475 	     : from_oct (8, header->header.gid));
476 #endif
477 	}
478 #if defined(S_IFBLK) || defined(S_IFCHR)
479       switch (header->header.linkflag)
480 	{
481 	case LF_BLK:
482 	case LF_CHR:
483 	  st->st_rdev = makedev (from_oct (8, header->header.devmajor),
484 				 from_oct (8, header->header.devminor));
485 	}
486 #endif
487     }
488   else
489     {
490       /* Old fashioned tar archive */
491       *stdp = 0;
492       st->st_uid = from_oct (8, header->header.uid);
493       st->st_gid = from_oct (8, header->header.gid);
494       st->st_rdev = 0;
495     }
496 }
497 
498 
499 /*
500  * Quick and dirty octal conversion.
501  *
502  * Result is -1 if the field is invalid (all blank, or nonoctal).
503  */
504 long
from_oct(digs,where)505 from_oct (digs, where)
506      register int digs;
507      register char *where;
508 {
509   register long value;
510 
511   while (isspace (*where))
512     {				/* Skip spaces */
513       where++;
514       if (--digs <= 0)
515 	return -1;		/* All blank field */
516     }
517   value = 0;
518   while (digs > 0 && isodigit (*where))
519     {				/* Scan til nonoctal */
520       value = (value << 3) | (*where++ - '0');
521       --digs;
522     }
523 
524   if (digs > 0 && *where && !isspace (*where))
525     return -1;			/* Ended on non-space/nul */
526 
527   return value;
528 }
529 
530 
531 /*
532  * Actually print it.
533  *
534  * Plain and fancy file header block logging.
535  * Non-verbose just prints the name, e.g. for "tar t" or "tar x".
536  * This should just contain file names, so it can be fed back into tar
537  * with xargs or the "-T" option.  The verbose option can give a bunch
538  * of info, one line per file.  I doubt anybody tries to parse its
539  * format, or if they do, they shouldn't.  Unix tar is pretty random here
540  * anyway.
541  *
542  * Note that print_header uses the globals <head>, <hstat>, and
543  * <head_standard>, which must be set up in advance.  This is not very clean
544  * and should be cleaned up.  FIXME.
545  */
546 #define	UGSWIDTH	18	/* min width of User, group, size */
547 /* UGSWIDTH of 18 means that with user and group names <= 8 chars the columns
548    never shift during the listing.  */
549 #define	DATEWIDTH	19	/* Last mod date */
550 static int ugswidth = UGSWIDTH;	/* Max width encountered so far */
551 
552 void
print_header()553 print_header ()
554 {
555   char modes[11];
556   char *timestamp;
557   char uform[11], gform[11];	/* These hold formatted ints */
558   char *user, *group;
559   char size[24];		/* Holds a formatted long or maj, min */
560   time_t longie;		/* To make ctime() call portable */
561   int pad;
562   char *name;
563   extern long baserec;
564 
565   if (f_sayblock)
566     fprintf (msg_file, "rec %10d: ", baserec + (ar_record - ar_block));
567   /* annofile(msg_file, (char *)NULL); */
568 
569   if (f_verbose <= 1)
570     {
571       /* Just the fax, mam. */
572       char *name;
573 
574       name = quote_copy_string (current_file_name);
575       if (name == 0)
576 	name = current_file_name;
577       fprintf (msg_file, "%s\n", name);
578       if (name != current_file_name)
579 	free (name);
580     }
581   else
582     {
583       /* File type and modes */
584       modes[0] = '?';
585       switch (head->header.linkflag)
586 	{
587 	case LF_VOLHDR:
588 	  modes[0] = 'V';
589 	  break;
590 
591 	case LF_MULTIVOL:
592 	  modes[0] = 'M';
593 	  break;
594 
595 	case LF_NAMES:
596 	  modes[0] = 'N';
597 	  break;
598 
599 	case LF_LONGNAME:
600 	case LF_LONGLINK:
601 	  msg ("Visible longname error\n");
602 	  break;
603 
604 	case LF_SPARSE:
605 	case LF_NORMAL:
606 	case LF_OLDNORMAL:
607 	case LF_LINK:
608 	  modes[0] = '-';
609 	  if ('/' == current_file_name[strlen (current_file_name) - 1])
610 	    modes[0] = 'd';
611 	  break;
612 	case LF_DUMPDIR:
613 	  modes[0] = 'd';
614 	  break;
615 	case LF_DIR:
616 	  modes[0] = 'd';
617 	  break;
618 	case LF_SYMLINK:
619 	  modes[0] = 'l';
620 	  break;
621 	case LF_BLK:
622 	  modes[0] = 'b';
623 	  break;
624 	case LF_CHR:
625 	  modes[0] = 'c';
626 	  break;
627 	case LF_FIFO:
628 	  modes[0] = 'p';
629 	  break;
630 	case LF_CONTIG:
631 	  modes[0] = 'C';
632 	  break;
633 	}
634 
635       demode ((unsigned) hstat.st_mode, modes + 1);
636 
637       /* Timestamp */
638       longie = hstat.st_mtime;
639       timestamp = ctime (&longie);
640       timestamp[16] = '\0';
641       timestamp[24] = '\0';
642 
643       /* User and group names */
644       if (*head->header.uname && head_standard)
645 	{
646 	  user = head->header.uname;
647 	}
648       else
649 	{
650 	  user = uform;
651 	  (void) sprintf (uform, "%d",
652 			  from_oct (8, head->header.uid));
653 	}
654       if (*head->header.gname && head_standard)
655 	{
656 	  group = head->header.gname;
657 	}
658       else
659 	{
660 	  group = gform;
661 	  (void) sprintf (gform, "%d",
662 			  from_oct (8, head->header.gid));
663 	}
664 
665       /* Format the file size or major/minor device numbers */
666       switch (head->header.linkflag)
667 	{
668 #if defined(S_IFBLK) || defined(S_IFCHR)
669 	case LF_CHR:
670 	case LF_BLK:
671 	  (void) sprintf (size, "%d,%d",
672 			  major (hstat.st_rdev),
673 			  minor (hstat.st_rdev));
674 	  break;
675 #endif
676 	case LF_SPARSE:
677 	  (void) sprintf (size, "%ld",
678 			  from_oct (1 + 12, head->header.realsize));
679 	  break;
680 	default:
681 	  (void) sprintf (size, "%ld", (long) hstat.st_size);
682 	}
683 
684       /* Figure out padding and print the whole line. */
685       pad = strlen (user) + strlen (group) + strlen (size) + 1;
686       if (pad > ugswidth)
687 	ugswidth = pad;
688 
689       name = quote_copy_string (current_file_name);
690       if (!name)
691 	name = current_file_name;
692       fprintf (msg_file, "%s %s/%s %*s%s %s %s %s",
693 	       modes,
694 	       user,
695 	       group,
696 	       ugswidth - pad,
697 	       "",
698 	       size,
699 	       timestamp + 4, timestamp + 20,
700 	       name);
701 
702       if (name != current_file_name)
703 	free (name);
704       switch (head->header.linkflag)
705 	{
706 	case LF_SYMLINK:
707 	  name = quote_copy_string (current_link_name);
708 	  if (!name)
709 	    name = current_link_name;
710 	  fprintf (msg_file, " -> %s\n", name);
711 	  if (name != current_link_name)
712 	    free (name);
713 	  break;
714 
715 	case LF_LINK:
716 	  name = quote_copy_string (current_link_name);
717 	  if (!name)
718 	    name = current_link_name;
719 	  fprintf (msg_file, " link to %s\n", current_link_name);
720 	  if (name != current_link_name)
721 	    free (name);
722 	  break;
723 
724 	default:
725 	  fprintf (msg_file, " unknown file type '%c'\n",
726 		   head->header.linkflag);
727 	  break;
728 
729 	case LF_OLDNORMAL:
730 	case LF_NORMAL:
731 	case LF_SPARSE:
732 	case LF_CHR:
733 	case LF_BLK:
734 	case LF_DIR:
735 	case LF_FIFO:
736 	case LF_CONTIG:
737 	case LF_DUMPDIR:
738 	  putc ('\n', msg_file);
739 	  break;
740 
741 	case LF_VOLHDR:
742 	  fprintf (msg_file, "--Volume Header--\n");
743 	  break;
744 
745 	case LF_MULTIVOL:
746 	  fprintf (msg_file, "--Continued at byte %ld--\n", from_oct (1 + 12, head->header.offset));
747 	  break;
748 
749 	case LF_NAMES:
750 	  fprintf (msg_file, "--Mangled file names--\n");
751 	  break;
752 	}
753     }
754   fflush (msg_file);
755 }
756 
757 /*
758  * Print a similar line when we make a directory automatically.
759  */
760 void
pr_mkdir(pathname,length,mode)761 pr_mkdir (pathname, length, mode)
762      char *pathname;
763      int length;
764      int mode;
765 {
766   char modes[11];
767   char *name;
768   extern long baserec;
769 
770   if (f_verbose > 1)
771     {
772       /* File type and modes */
773       modes[0] = 'd';
774       demode ((unsigned) mode, modes + 1);
775 
776       if (f_sayblock)
777 	fprintf (msg_file, "rec %10d: ", baserec + (ar_record - ar_block));
778       /* annofile(msg_file, (char *)NULL); */
779       name = quote_copy_string (pathname);
780       if (!name)
781 	name = pathname;
782       fprintf (msg_file, "%s %*s %.*s\n",
783 	       modes,
784 	       ugswidth + DATEWIDTH,
785 	       "Creating directory:",
786 	       length,
787 	       pathname);
788       if (name != pathname)
789 	free (name);
790     }
791 }
792 
793 
794 /*
795  * Skip over <size> bytes of data in records in the archive.
796  */
797 void
skip_file(size)798 skip_file (size)
799      register long size;
800 {
801   union record *x;
802   extern long save_totsize;
803   extern long save_sizeleft;
804 
805   if (f_multivol)
806     {
807       save_totsize = size;
808       save_sizeleft = size;
809     }
810 
811   while (size > 0)
812     {
813       x = findrec ();
814       if (x == NULL)
815 	{			/* Check it... */
816 	  msg ("Unexpected EOF on archive file");
817 	  exit (EX_BADARCH);
818 	}
819       userec (x);
820       size -= RECORDSIZE;
821       if (f_multivol)
822 	save_sizeleft -= RECORDSIZE;
823     }
824 }
825 
826 void
skip_extended_headers()827 skip_extended_headers ()
828 {
829   register union record *exhdr;
830 
831   for (;;)
832     {
833       exhdr = findrec ();
834       if (!exhdr->ext_hdr.isextended)
835 	{
836 	  userec (exhdr);
837 	  break;
838 	}
839       userec (exhdr);
840     }
841 }
842 
843 /*
844  * Decode the mode string from a stat entry into a 9-char string and a null.
845  */
846 void
demode(mode,string)847 demode (mode, string)
848      register unsigned mode;
849      register char *string;
850 {
851   register unsigned mask;
852   register char *rwx = "rwxrwxrwx";
853 
854   for (mask = 0400; mask != 0; mask >>= 1)
855     {
856       if (mode & mask)
857 	*string++ = *rwx++;
858       else
859 	{
860 	  *string++ = '-';
861 	  rwx++;
862 	}
863     }
864 
865   if (mode & S_ISUID)
866     if (string[-7] == 'x')
867       string[-7] = 's';
868     else
869       string[-7] = 'S';
870   if (mode & S_ISGID)
871     if (string[-4] == 'x')
872       string[-4] = 's';
873     else
874       string[-4] = 'S';
875   if (mode & S_ISVTX)
876     if (string[-1] == 'x')
877       string[-1] = 't';
878     else
879       string[-1] = 'T';
880   *string = '\0';
881 }
882