shell archiver written in c
salmi
salmi at dicome.UUCP
Thu Mar 13 21:57:38 AEST 1986
there have been quite a few requests for the shar script in net.sources.wanted.
here is a version written in c that came off the net last year.
enjoy.....
-john
------------------------------cut here-----------------------------------------
/*
** cshar.c
*/
#ifndef lint
#ifdef RCSIDENT
static char *rcsid[] = {
"$Header: shar.c,v 1.3 84/12/09 19:38:53 thompson Exp $",
"$Locker: $"
};
#endif RCSIDENT
#endif lint
#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.
*/
/*
* I have made several mods to this program:
*
* 1) the -----Cut Here-----... now preceds the script.
* 2) the cat has been changed to a sed which removes a prefix
* character from the beginning of each line of the extracted
* file, this prefix character is added to each line of the archived
* files and is not the same as the first character of the
* file delimeter.
* 3) added several options:
* -c - add the -----Cut Here-----... line.
* -d'del' - change the file delimeter to del.
* -s - cause the resulting script to print the sum of
* the orignal file and the sum of the extracted
* file.
*
* Michael A. Thompson
* Dalhousie University
* Halifax, N.S., Canada.
*/
#define DELIM "SHAR_EOF"/* put after each file */
#define PREFIX1 'X' /* goes infront of each line */
#define PREFIX2 'Y' /* goes infront of each line if Delim
starts with PREFIX1 */
#define PREFIX (Delim[0] == PREFIX1 ? PREFIX2 : PREFIX1)
#define SHAR "shar" /* the name of this program */
#define READ_PERMISSION 4 /* access permission */
#define SUM "sum"
int Verbose = 0; /* option to provide append/extract
feedback */
int Sum = 0; /* option to provide sum checking */
char *Delim = DELIM; /* option to provide alternate delimeter
*/
int Cut = 0; /* option to provide cut mark */
FILE * popen ();
main (argc, argv) char **argv;
{
int status = 0;
while (argv[1][0] == '-') {
switch (argv[1][1]) {
case 'v':
Verbose = 1;
break;
case 's':
Sum = 1;
break;
case 'd':
if (argv[1][2])
Delim = &argv[1][2];
break;
case 'c':
Cut = 1;
break;
default:
fprintf (stderr, "%s: invalid argument\n", SHAR);
fprintf (stderr, "USAGE: %s [-v] [-s] [-d'delimeter'] [-c] files > archive\n", SHAR);
break;
}
argc--;
argv++;
}
if (argc == 1) {
fprintf (stderr, "%s: No input files\n", SHAR);
fprintf (stderr, "USAGE: %s [-v] [-s] [-d'delimeter'] [-c] files > archive\n", SHAR);
exit (1);
}
if (header (argc, argv))
exit (2);
while (--argc)
status += shar (*++argv);
puts ("exit");
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);
if (Cut)
puts ("-----Cut Here-----Cut Here-----Cut Here-----Cut Here-----");
puts ("#!/bin/sh");
printf ("# %s: Shell Archiver\n", SHAR);
puts ("# Run the following text with /bin/sh to create:");
for (i = 1; i < argc; i++)
printf ("# %s\n", argv[i]);
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 x - extracting %s\n", SHAR, file);
}
printf ("sed 's/^%c//' << '%s' > %s\n", PREFIX, Delim, file);
while (fgets (line, BUFSIZ, ioptr)) {
putc (PREFIX, stdout);
fputs (line, stdout);
}
(void) fclose (ioptr);
puts (Delim);
if (Sum) {
FILE * pfp;
char command[BUFSIZ];
sprintf (command, "%s %s", SUM, file);
if ((pfp = popen (command, "r"))) {
char sum[BUFSIZ];
fgets (sum, BUFSIZ, pfp);
sum[strlen (sum) - 1] = '\0';
printf ("echo 'Orignal Sum -> %s'\n", sum);
puts ("echo -n 'Current Sum -> '");
puts (command);
pclose (pfp);
}
}
return (0);
}
else {
fprintf (stderr, "%s: Can't open %s\n", SHAR, file);
return (1);
}
}
More information about the Comp.sources.unix
mailing list