shell archiver written in C

Gary Perlman perlman at wivax.UUCP
Thu Dec 6 00:05:46 AEST 1984


Here is my shar.  It is somewhat faster than the shell script version:
       14.0 real         1.1 user         5.6 sys  (shell version)
        3.0 real         0.2 user         0.5 sys  (C version)
but another advantage is that it provides better diagnostics (I think).
I know there have been other versions posted, but I saw a recent request.
Gary Perlman/Wang Institute/Tyng Road/Tyngsboro, MA/01879/(617) 649-9731

#!/bin/sh-----cut here-----cut here-----cut here-----cut here-----
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	shar.1 #	shar.c 
echo shar: extracting shar.1
cat - << \SHAR_EOF > shar.1
.TH SHAR 1net "December 5, 1984"
.SH NAME
shar \- create storage archive for extraction by /bin/sh
.SH SYNOPSIS
.B shar
[-v] files
.SH DESCRIPTION
.I shar
prints its input files with special 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.
The -v (verbose) option prints feedback about
what shar is doing.
.SH AUTHOR
Gary Perlman
(based on a shell version by James Gosling)
SHAR_EOF
echo shar: extracting shar.c
cat - << \SHAR_EOF > shar.c
#include <stdio.h>

/*
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 main difference is that my version
does not affect any files because it prints to the standard
output.  Mine also has a -v (verbose) option.
*/


#define	DELIM           "SHAR_EOF"  /* put after each file */
#define	SHAR            "shar"      /* the name of this program */
#define	READ_PERMISSION 4           /* access permission */

int 	Verbose = 0; /* option to provide append/extract feedback */

main (argc, argv) char **argv;	
	{
	int 	status = 0;
	if (!strcmp (argv[1], "-v"))
		{
		Verbose = 1;
		argc--;
		argv++;
		}
	if (argc == 1)
		{
		fprintf (stderr, "%s: No input files\n", SHAR);
		fprintf (stderr, "USAGE: %s [-v] files > archive\n", SHAR);
		exit (1);
		}
	if (header (argc, argv))
		exit (2);
	while (--argc)
		status += shar (*++argv);
	exit (status);
	}

header (argc, argv)
char	**argv;
	{
	int 	i;
	int 	problems = 0;
	for (i = 1; i < argc; i++)
		if (access (argv[i], READ_PERMISSION))
			{
			fprintf (stderr, "%s: Can't read %s\n", SHAR, argv[i]);
			problems++;
			}
	if (problems) return (problems);
	puts ("#!/bin/sh-----cut here-----cut here-----cut here-----cut here-----");
	printf ("# %s:	Shell Archiver\n", SHAR);
	puts ("#	Run the following text with /bin/sh to create:");
	for (i = 1; i < argc; i++)
		printf ("#	%s ", argv[i]);
	putchar ('\n');
	return (0);
	}

shar (file)
char	*file;
	{
	char	line[BUFSIZ];
	FILE	*ioptr;
	if (ioptr = fopen (file, "r"))
		{
		if (Verbose)
			{
			fprintf (stderr, "%s: appending %s\n", SHAR, file);
			printf ("echo %s: extracting %s\n", SHAR, file);
			}
		printf ("cat - << \\%s > %s\n", DELIM, file);
		while (fgets (line, BUFSIZ, ioptr))
			fputs (line, stdout);
		(void) fclose (ioptr);
		puts (DELIM);
		return (0);
		}
	else
		{
		fprintf (stderr, "%s: Can't open %s\n", SHAR, file);
		return (1);
		}
	}
SHAR_EOF
a - shar.c



More information about the Comp.sources.unix mailing list