Date: Mon, 15 Dec 86 14:29:25 est From: David Krowitz Subject: version of SHAR program for Alliant Here is the version of 'shar' for the Alliant library. The only difference from the Apollo version is a change in the documentation. -- David Krowitz ---------------------------------------------------------- #! /bin/sh # This is a shell archive, meaning: # 1. Remove everything above the #! /bin/sh line. # 2. Save the resulting text in a file. # 3. Execute the file with /bin/sh (not csh) to create the files: # shar.dir # This archive created: Mon Dec 15 14:27:15 1986 export PATH; PATH=/bin:$PATH if test ! -d 'shar.dir' then mkdir 'shar.dir' fi cd 'shar.dir' if test -f 'Makefile' then echo shar: will not over-write existing file "'Makefile'" else cat << \SHAR_EOF > 'Makefile' CFLAGS=-O shar: shar.o traverse.o getopt.o /bin/cc $(CFLAGS) -o shar shar.o traverse.o getopt.o traverse: /bin/cc -o traverse -DSTANDALONE traverse.c install: mv shar /ucla/air/srt/com/shar clean: /bin/rm *.o *.bak SHAR_EOF chmod +x 'Makefile' fi # end of overwriting check if test -f 'getopt.3' then echo shar: will not over-write existing file "'getopt.3'" else cat << \SHAR_EOF > 'getopt.3' .TH GETOPT 3 local .DA 25 March 1982 .SH NAME getopt \- get option letter from argv .SH SYNOPSIS .ft B int getopt(argc, argv, optstring) .br int argc; .br char **argv; .br char *optstring; .sp extern char *optarg; .br extern int optind; .ft .SH DESCRIPTION .I Getopt returns the next option letter in .I argv that matches a letter in .IR optstring . .I Optstring is a string of recognized option letters; if a letter is followed by a colon, the option is expected to have an argument that may or may not be separated from it by white space. .I Optarg is set to point to the start of the option argument on return from .IR getopt . .PP .I Getopt places in .I optind the .I argv index of the next argument to be processed. Because .I optind is external, it is normally initialized to zero automatically before the first call to .IR getopt . .PP When all options have been processed (i.e., up to the first non-option argument), .I getopt returns .BR EOF . The special option .B \-\- may be used to delimit the end of the options; .B EOF will be returned, and .B \-\- will be skipped. .SH SEE ALSO getopt(1) .SH DIAGNOSTICS .I Getopt prints an error message on .I stderr and returns a question mark .RB ( ? ) when it encounters an option letter not included in .IR optstring . .SH EXAMPLE The following code fragment shows how one might process the arguments for a command that can take the mutually exclusive options .B a and .BR b , and the options .B f and .BR o , both of which require arguments: .PP .RS .nf main(argc, argv) int argc; char **argv; { int c; extern int optind; extern char *optarg; \&. \&. \&. while ((c = getopt(argc, argv, "abf:o:")) != EOF) switch (c) { case 'a': if (bflg) errflg++; else aflg++; break; case 'b': if (aflg) errflg++; else bproc(); break; case 'f': ifile = optarg; break; case 'o': ofile = optarg; break; case '?': default: errflg++; break; } if (errflg) { fprintf(stderr, "Usage: ..."); exit(2); } for (; optind < argc; optind++) { \&. \&. \&. } \&. \&. \&. } .RE .PP A template similar to this can be found in .IR /usr/pub/template.c . .SH HISTORY Written by Henry Spencer, working from a Bell Labs manual page. Behavior believed identical to the Bell version. .SH BUGS It is not obvious how `\-' standing alone should be treated; this version treats it as a non-option argument, which is not always right. .PP Option arguments are allowed to begin with `\-'; this is reasonable but reduces the amount of error checking possible. .PP .I Getopt is quite flexible but the obvious price must be paid: there is much it could do that it doesn't, like checking mutually exclusive options, checking type of option arguments, etc. SHAR_EOF chmod +x 'getopt.3' fi # end of overwriting check if test -f 'getopt.c' then echo shar: will not over-write existing file "'getopt.c'" else cat << \SHAR_EOF > 'getopt.c' /* got this off net.sources */ #include /* * get option letter from argument vector */ int opterr = 1, /* useless, never set or used */ optind = 1, /* index into parent argv vector */ optopt; /* character checked for validity */ char *optarg; /* argument associated with option */ #define BADCH (int)'?' #define EMSG "" #define tell(s) fputs(*nargv,stderr);fputs(s,stderr); \ fputc(optopt,stderr);fputc('\n',stderr);return(BADCH); getopt(nargc,nargv,ostr) int nargc; char **nargv, *ostr; { static char *place = EMSG; /* option letter processing */ register char *oli; /* option letter list index */ char *index(); if(!*place) { /* update scanning pointer */ if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF); if (*place == '-') { /* found "--" */ ++optind; return(EOF); } } /* option letter okay? */ if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) { if(!*place) ++optind; tell(": illegal option -- "); } if (*++oli != ':') { /* don't need argument */ optarg = NULL; if (!*place) ++optind; } else { /* need an argument */ if (*place) optarg = place; /* no white space */ else if (nargc <= ++optind) { /* no arg */ place = EMSG; tell(": option requires an argument -- "); } else optarg = nargv[optind]; /* white space */ place = EMSG; ++optind; } return(optopt); /* dump back option letter */ } SHAR_EOF chmod +x 'getopt.c' fi # end of overwriting check if test -f 'shar.1' then echo shar: will not over-write existing file "'shar.1'" else cat << \SHAR_EOF > 'shar.1' .TH SHAR 1net "June 3, 1985" "UNIX User's Manual" "Wang Institute" .SH NAME shar \- create file storage archive for extraction by /bin/sh .SH SYNOPSIS .B shar [-abcsv] [-d delim] [-p prefix] files .SH DESCRIPTION .I shar prints its input files with special command lines around them to be used by the shell, .I /bin/sh , to extract them later. The output can be filtered through the shell to recreate copies of the original files. .PP .I shar allows directories to be named, and .I shar prints the necessary commands .ul (mkdir & cd) to create new directories and fill them. .I shar will emit commands to make executable plain files executable. .I shar will not allow existing files to be over-written; such files must be removed by the file extractor. .SH OPTIONS .de OP .TP .B -\\$1 .. .OP a All the options. The options: .B "-v -c -b -p X" are implied. .OP s Silent running. All checking and extra output is inhibited. .OP v Print verbose feedback messages about what .I shar is doing to be printed during extraction. Sizes of plain files are echoed to allow a simple validity check. .OP c Check file size on extraction by counting characters. An error message is reported to the person doing the extraction if the sizes don't match. One reason why the sizes may not match is that .I shar will append a newline to complete incomplete last lines; .I shar prints a message that mentions added newlines. Another reason why the sizes may not match is that some network mail programs remove non-whitespace control characters. .I shar prints a message that mentions control characters to the extractor. .OP b Extract files into basenames so that files with absolute path names are put into the current directory. This option has strange effects when directories are archived. .OP d delim Use this as the ``end of file'' delimiter instead of the default. The only reason to change it is if you suspect an file contains the default delimiter: .B SHAR_EOF. .OP p prefix Use this as the prefix to each line of the archived files. This is to make sure that special characters at the start of lines are not eaten up by programs like mailers. If this option is used, the files will be extracted with the stream editor .B sed rather than .B cat so it is more efficient and portable to avoid setting the prefix, though perhaps less safe if you don't know what is in the files. .SH "SEE ALSO tar(1), cpio(1), tp(1), sh(1) .SH AUTHOR Gary Perlman (based on a shell version by James Gosling, with additions motivated by Derek Zahn, Michael Thompson, H. Morrow Long, Fred Avolio, Gran Uddeborg, & Chuck Wegrzyn) .SH LIMITATIONS .I shar does not know anything about links between files or binary files. SHAR_EOF chmod +x 'shar.1' fi # end of overwriting check if test -f 'shar.c' then echo shar: will not over-write existing file "'shar.c'" else cat << \SHAR_EOF > 'shar.c' #include #include #include #include /*{ Shar puts readable text files together in a package from which they are easy to extract. The original version was a shell script posted to the net, shown below: #Date: Mon Oct 18 11:08:34 1982 #From: decvax!microsof!uw-beave!jim (James Gosling at CMU) AR=$1 shift for i do echo a - $i echo "echo x - $i" >>$AR echo "cat >$i <<'!Funky!Stuff!'" >>$AR cat $i >>$AR echo "!Funky!Stuff!" >>$AR done I rewrote this version in C to provide better diagnostics and to run faster. The major difference is that my version does not affect any files because it prints to the standard output. Mine also has several options. Gary Perlman/Wang Institute/Tyngsboro, MA/01879/(617) 649-9731 Many enhancements motivated by Michael Thompson. Directory archiving motivated by Derek Zahn @ wisconsin His version had some problems, so I wrote a general routine for traversing a directory hierarchy. It allows marching through a directory on old and new UNIX systems. }*/ /* COMMANDS */ #define EXTRACT "#! /bin/sh" /* magic exec string at shar file start */ #define PATH "/bin:$PATH" /* search path for programs */ #define CAT "cat"; /* /bin/cat */ #define SED "sed 's/^%s//'" /* /bin/sed removes Prefix from lines */ #define MKDIR "mkdir" /* make a new dirctory */ #define CHMOD "chmod +x" /* change file protection (for executables) */ #define CHDIR "cd" /* change current directory */ #define TEST "test" /* /bin/test files */ #define WC_C "wc -c <" /* counts chars in file */ #define ECHO "echo shar" /* echo a message to extractor */ main (argc, argv) char **argv; { int shar (); int optind; if ((optind = initial (argc, argv)) < 0) exit (1); if (header (argc, argv, optind)) exit (2); while (optind < argc) traverse (argv[optind++], shar); footer (); exit (0); } /* OPTIONS */ typedef int lgl; #define true ((lgl) 1) #define false ((lgl) 0) int Lastchar; /* the last character printed */ int Ctrlcount; /* how many bad control characters are in file */ #define USAGE "[-abcsv] [-p prefix] [-d delim] files > archive" #define OPTSTRING "abcsvp:d:" lgl Verbose = false; /* provide append/extract feedback */ lgl Basename = false; /* extract into basenames */ lgl Count = false; /* count characters to check transfer */ lgl Silent = false; /* turn off all verbosity */ char *Delim = "SHAR_EOF"; /* put after each file */ char Filter[100] = CAT; /* used to extract archived files */ char *Prefix = NULL; /* line prefix to avoid funny chars */ int /* returns the index of the first operand file */ initial (argc, argv) char **argv; { int errflg = 0; extern int optind; extern char *optarg; int C; while ((C = getopt (argc, argv, OPTSTRING)) != EOF) switch (C) { case 'v': Verbose = true; break; case 'c': Count = true; break; case 'b': Basename = true; break; case 'd': Delim = optarg; break; case 's': /* silent running */ Silent = true; Verbose = false; Count = false; Prefix = NULL; break; case 'a': /* all the options */ Verbose = true; Count = true; Basename = true; /* fall through to set prefix */ optarg = " X"; case 'p': (void) sprintf (Filter, SED, Prefix = optarg); break; default: errflg++; } if (errflg || optind == argc) { if (optind == argc) fprintf (stderr, "shar: No input files\n"); fprintf (stderr, "USAGE: shar %s\n", USAGE); return (-1); } return (optind); } header (argc, argv, optind) char **argv; { int i; lgl problems = false; long clock; char *ctime (); char *getenv (); char *NAME = getenv ("NAME"); char *ORG = getenv ("ORGANIZATION"); for (i = optind; i < argc; i++) if (access (argv[i], 4)) /* check read permission */ { fprintf (stderr, "shar: Can't read '%s'\n", argv[i]); problems++; } if (problems) return (problems); /* I have given up on putting a cut line in the archive. Too many people complained about having to remove it. puts ("-----cut here-----cut here-----cut here-----cut here-----"); */ puts (EXTRACT); puts ("# This is a shell archive, meaning:"); printf ("# 1. Remove everything above the %s line.\n", EXTRACT); puts ("# 2. Save the resulting text in a file."); puts ("# 3. Execute the file with /bin/sh (not csh) to create the files:"); for (i = optind; i < argc; i++) printf ("#\t%s\n", argv[i]); (void) time (&clock); printf ("# This archive created: %s", ctime (&clock)); if (NAME) printf ("# By:\t%s (%s)\n", NAME, ORG ? ORG : ""); printf ("export PATH; PATH=%s\n", PATH); return (0); } footer () { puts ("#\tEnd of shell archive"); puts ("exit 0"); } archive (input, output) char *input, *output; { char buf[BUFSIZ]; FILE *ioptr; if (ioptr = fopen (input, "r")) { if (Count == true) { Ctrlcount = 0; /* no bad control characters so far */ Lastchar = '\n'; /* simulate line start */ } printf ("%s << \\%s > '%s'\n", Filter, Delim, output); if (Prefix) { while (fgets (buf, BUFSIZ, ioptr)) { if (Prefix) outline (Prefix); outline (buf); } } else copyout (ioptr); /* thanks to H. Morrow Long (ittvax!long) for the next fix */ if (Lastchar != '\n') /* incomplete last line */ putchar ('\n'); /* Delim MUST begin new line! */ puts (Delim); if (Count == true && Lastchar != '\n') printf ("%s: a missing newline was added to \"'%s'\"\n", ECHO, input); if (Count == true && Ctrlcount) printf ("%s: %d control character%s may be missing from \"'%s'\"\n", ECHO, Ctrlcount, Ctrlcount > 1 ? "s" : "", input); (void) fclose (ioptr); return (0); } else { fprintf (stderr, "shar: Can't open '%s'\n", input); return (1); } } /* Copyout copies its ioptr almost as fast as possible except that it has to keep track of the last character printed. If the last character is not a newline, then shar has to add one so that the end of file delimiter is recognized by the shell. This checking costs about a 10% difference in user time. Otherwise, it is about as fast as cat. It also might count control characters. */ #define badctrl(c) (iscntrl (c) && !isspace (c)) copyout (ioptr) register FILE *ioptr; { register int C; register int L; register count; count = Count; while ((C = getc (ioptr)) != EOF) { if (count == true && badctrl (C)) Ctrlcount++; L = putchar (C); } Lastchar = L; } outline (s) register char *s; { if (*s) { while (*s) { if (Count == true && badctrl (*s)) Ctrlcount++; putchar (*s++); } Lastchar = *(s-1); } } #define FSIZE statbuf.st_size shar (file, type, pos) char *file; /* file or directory to be processed */ int type; /* either 'f' for file or 'd' for directory */ int pos; /* 0 going in to a file or dir, 1 going out */ { struct stat statbuf; char *basefile = file; if (!strcmp (file, ".")) return; if (stat (file, &statbuf)) FSIZE = 0; if (Basename == true) { while (*basefile) basefile++; /* go to end of name */ while (basefile > file && *(basefile-1) != '/') basefile--; } if (pos == 0) /* before the file starts */ { if (type == 'd') { printf ("if %s ! -d '%s'\n", TEST, basefile); printf ("then\n"); if (Verbose == true) printf (" %s: creating directory \"'%s'\"\n", ECHO, basefile); printf (" %s '%s'\n", MKDIR, basefile); printf ("fi\n"); if (Verbose == true) printf ("%s: entering directory \"'%s'\"\n", ECHO, basefile); printf ("%s '%s'\n", CHDIR, basefile); } else /* type == 'f' */ { if (Verbose == true) printf ("%s: extracting \"'%s'\" '(%d character%s)'\n", ECHO, basefile, FSIZE, FSIZE > 1 ? "s" : ""); if (Silent == false) /* this solution by G|ran Uddeborg */ { printf ("if %s -f '%s'\n", TEST, basefile); puts ("then"); printf (" %s: will not over-write existing file \"'%s'\"\n", ECHO, basefile); puts ("else"); } if (archive (file, basefile)) exit (-1); } } else /* pos == 1, after the file is archived */ { if (type == 'd') { if (Verbose == true) printf ("%s: done with directory \"'%s'\"\n", ECHO, basefile); printf ("%s ..\n", CHDIR); } else /* type == 'f' (plain file) */ { if (Count == true) { printf ("if %s %d -ne \"`%s '%s'`\"\n", TEST, FSIZE, WC_C, basefile); puts ("then"); printf (" %s: error transmitting \"'%s'\" ", ECHO, basefile); printf ("'(should have been %d character%s)'\n", FSIZE, FSIZE > 1 ? "s" : ""); puts ("fi"); } if (access (file, 1) == 0) /* executable -> chmod +x */ printf ("%s '%s'\n", CHMOD, basefile); if (Silent == false) { puts ("fi # end of overwriting check"); } } } } SHAR_EOF chmod +x 'shar.c' fi # end of overwriting check if test -f 'traverse.3' then echo shar: will not over-write existing file "'traverse.3'" else cat << \SHAR_EOF > 'traverse.3' .TH TRAVERSE 3WI "December 16, 1984" .SH NAME traverse \- recursively traverse a directory .SH SYNOPSIS .nf traverse (path, func) char *path; int (*func) (); func (path, filetype, position) char *path; .fi .SH DESCRIPTION traverse applies its argument function func to its argument file pathname path. If path is a directory, then traverse applies func to all its entries. This traversal is in depth first order so that files are processed in the order that they are stored in the directory. .PP The argument func should take three parameters: a file name, a file type, and a position. The call looks like this for directories: .ce (*func) (path, 'd', position); and like this for other files: .ce (*func) (path, 'f', position); The position is 0 when path is first encountered and 1 when traverse is done. This is used to allow processing before and after a directory is processed. .SH EXAMPLE .nf list (name, type, pos) char *name; { if (type == 'd') printf ("%s %s\en", pos ? "Leaving" : "Entering", name); else /* type == 'f' */ printf (" %s\en", name); } .fi .SH AUTHOR Gary Perlman .SH BUGS There are no diagnostics when directories cannot be searched. SHAR_EOF chmod +x 'traverse.3' fi # end of overwriting check if test -f 'traverse.c' then echo shar: will not over-write existing file "'traverse.c'" else cat << \SHAR_EOF > 'traverse.c' /*LINTLIBRARY*/ #include #include #include #ifdef MAXNAMLEN #define namedir(entry) (entry->d_name) #define MAXNAME 256 #else #define DIR FILE #define MAXNAME (DIRSIZ+2) #define opendir(path) fopen (path, "r") #define closedir(dirp) fclose (dirp) struct direct * readdir (dirp) DIR *dirp; { static struct direct entry; if (dirp == NULL) return (NULL); for (;;) { if (fread (&entry, sizeof (struct direct), 1, dirp) == 0) return (NULL); if (entry.d_ino) return (&entry); } } char *strncpy (); char * namedir (entry) struct direct *entry; { static char name[MAXNAME]; return (strncpy (name, entry->d_name, DIRSIZ)); } #endif #include #define isdir(path) (stat(path, &buf) ? 0 : (buf.st_mode&S_IFMT)==S_IFDIR) traverse (path, func) char *path; int (*func) (); { DIR *dirp; struct direct *entry; struct stat buf; int filetype = isdir (path) ? 'd' : 'f'; (*func) (path, filetype, 0); if (filetype == 'd') { if (chdir (path) == 0) { if (dirp = opendir (".")) { while (entry = readdir (dirp)) { char name[MAXNAME]; (void) strcpy (name, namedir (entry)); if (strcmp(name, ".") && strcmp(name, "..")) traverse (name, func); } (void) closedir(dirp); } (void) chdir (".."); } } (*func) (path, filetype, 1); } #ifdef STANDALONE static Indent = 0; tryverse (file, type, pos) char *file; { int in; if (pos == 0) { for (in = 0; in < Indent; in++) putchar ('\t'); if (type == 'd') { printf ("%s/\n", file); Indent++; } else puts (file); } else if (type == 'd') Indent--; } main (argc, argv) char **argv; { int tryverse (); char *root = argc > 1 ? argv[1] : "."; traverse (root, tryverse); } #endif SHAR_EOF chmod +x 'traverse.c' fi # end of overwriting check if test -f 'shar.hlp' then echo shar: will not over-write existing file "'shar.hlp'" else cat << \SHAR_EOF > 'shar.hlp' SHAR \- create file storage archive for extraction by /bin/sh SHAR [-abcsv] [-d delim] [-p prefix] files SSHAR prints its input files with special command lines around them to be used by the shell, /bin/sh , to extract them later. The output can be filtered through the shell to recreate copies of the original files. SHAR allows directories to be named, and prints the necessary commands (mkdir & cd) to create new directories and fill them. SHAR will emit commands to make executable plain files executable. SHAR will not allow existing files to be over-written; such files must be removed by the file extractor. OPTIONS ------- a - All the options. The options: "-v -c -b -p X" are implied. s - Silent running. All checking and extra output is inhibited. v - Print verbose feedback messages about what SHAR is doing to be printed during extraction. Sizes of plain files are echoed to allow a simple validity check. c - Check file size on extraction by counting characters. An error message is reported to the person doing the extraction if the sizes don't match. One reason why the sizes may not match is that SHAR will append a newline to complete incomplete last lines; SHAR prints a message that mentions added newlines. Another reason why the sizes may not match is that some network mail programs remove non-whitespace control characters. SHAR prints a message that mentions control characters to the extractor. b - Extract files into basenames so that files with absolute path names are put into the current directory. This option has strange effects when directories are archived. d delim - Use this as the ``end of file'' delimiter instead of the default. The only reason to change it is if you suspect an file contains the default delimiter: SHAR_EOF. p prefix - Use this as the prefix to each line of the archived files. This is to make sure that special characters at the start of lines are not eaten up by programs like mailers. If this option is used, the files will be extracted with the stream editor , 'sed', rather than with 'cat' so it is more efficient and portable to avoid setting the prefix, though perhaps less safe if you don't know what is in the files. SEE ALSO tar(1), cpio(1), tp(1), sh(1) AUTHOR Gary Perlman (based on a shell version by James Gosling, with additions motivated by Derek Zahn, Michael Thompson, H. Morrow Long, Fred Avolio, Gran Uddeborg, & Chuck Wegrzyn) LIMITATIONS ----------- SHAR does not know anything about links between files or binary files. SHAR_EOF chmod +x 'shar.hlp' fi # end of overwriting check cd .. # End of shell archive exit 0