home *** CD-ROM | disk | FTP | other *** search
- /* @(#)ssh.c 1.20 92/03/11 (HaB)
- *
- * NAME:
- * ssh
- *
- * SYNTAX:
- * ssh [-h]
- * [-x] < shar_archive
- * [-v]
- *
- * OPTIONS:
- * -h Display usage information.
- *
- * -x Extract files in archive instead
- * of spliting it into parts.
- *
- * -v Display version information.
- *
- * Without any options ssh splits archives
- * into extractable 'PartXX' files.
- *
- * DESCRIPTION:
- * Splits, strips and extracts appended shell
- * archives and stores the result in 'PartXX'
- * files (not when extracting).
- *
- * NOTES:
- * The program should work on all archives created
- * using 'shar' (or equals) provided that they have
- * not been changed since they were first generated.
- *
- * BUGS:
- * I have noticed that when the archives contains
- * archives themselves (happens sometimes) it does
- * not work properly.
- *
- * DATE:
- * 1992-03-11
- *
- * AUTHOR:
- * Hans C. Beckerus
- * etxerus@james.ericsson.se
- *
- * DISCLAIMER:
- * This program is free to distribute to anyone aslong
- * as the code is not changed in anyway without me
- * knowing about it.
- *
- * /HaB :-)
- */
-
- #include <stdio.h>
- #include <string.h>
- #include <malloc.h>
-
- #define SEARCH 0
- #define START 1
- #define INSIDE 2
- #define MSTEP 80 /* Allocation steps */
- #define SEARCHP 5 /* No. of search patterns */
-
- #ifndef SHELL
- #define SHELL "/bin/sh" /* Just in case... */
- #endif
-
- #ifdef SYSV /* HPUX/SYSV */
- #define nl_fprintf fprintf
- #define nl_sprintf sprintf
- #define nl_strcmp strcmp
- size_t msize;
- #endif
-
- #ifdef SUN /* SunOS/Berkeley */
- unsigned int msize;
- #endif
-
- #ifdef SCCSID
- char sccsid[] = "@(#)ssh.c 1.20 92/03/11 (HaB) etxerus@james.ericsson.se";
- #endif
-
- enum boolean { /* Boolean constants */
- FALSE,
- TRUE
- };
-
- char *pattern[] = { /* Add more patterns here if needed. */
- "# this is a shell archive", /* NOTE! Remember to increase SEARCHP. */
- "# this is part",
- "#!/bin/sh",
- "# !/bin/sh",
- "#! /bin/sh"
- };
-
- /* usage:
- *
- * Display usage information and exit with status rc.
- *
- */
-
- void usage (rc)
-
- int rc; /* Return code */
-
- {
- puts ("\nUsage: ssh [-h]");
- puts (" [-x] < shar_archive");
- puts (" [-v]\n");
- puts ("Options:");
- puts (" -h - This help text.");
- puts (" -x - Extract files in archive instead");
- puts (" of spliting it into parts.");
- puts (" -v - Display version information.\n");
- puts (" Without any options ssh splits archives");
- puts (" into extractable 'PartXX' files.\n");
- exit (rc);
- }
-
- void main (argn, argv)
-
- int argn;
- char *argv[];
-
- {
- FILE *fr = stdin; /* Input filepointer */
- FILE *fw; /* Output filepointer */
- FILE *pipe; /* Stream pipe */
- int state = SEARCH; /* The current state */
- int fc = 0; /* File part counter */
- int extract = FALSE; /* Extract/write flag */
- char *s; /* Read line */
- char fout[7]; /* Output filenames */
- register j = 0; /* Counter */
- register c; /* Read character */
-
- /* Check the arguments if any */
- while (--argn) {
- argv++;
- if (!(strcmp (*argv, "-h", 2))) { /* Help screen */
- if (!(argn-1)) /* Single option */
- usage (0);
- }
- else if (!(strcmp (*argv, "-x"))) { /* Extract files */
- if (!(argn-1)) { /* Single option */
- extract = TRUE;
- continue;
- }
- }
- else if (!(strcmp (*argv, "-v"))) {
- if (!(argn-1)) { /* Single option */
- puts ("ssh 1.20 (bugs to etxerus@james.ericsson.se)");
- exit (0);
- }
- }
- usage (1);
- }
-
- msize = MSTEP;
- s = malloc (msize); /* Allocate buffer */
-
- while ((c = getc (fr)) != EOF) {
- if (c != '\n') { /* Check for EOL */
- s[j++] = c;
- if (j == msize) {
- msize += MSTEP;
- if ((s = realloc (s, msize)) == NULL) {
- fprintf (stderr, "ssh: Allocation error, cannot continue.\n");
- exit (1);
- }
- }
- }
- else {
- s[j] = '\0'; /* One line has been read */
- switch (state) {
- case SEARCH:
- for (j = 0; j < SEARCHP; j++) {
- if (!(strncasecmp (s, pattern[j], strlen (pattern[j])))) {
- state = START;
- break;
- }
- }
- if (state != START)
- break;
-
- case START: /* Start writing or extracting */
- if (!extract) {
- sprintf (fout, "Part%.2d", ++fc);
- fw = fopen (fout, "w");
- fprintf (fw, "%s\n", s);
- }
- else {
- if ((pipe = popen (SHELL, "w")) == NULL) {
- puts ("ssh: Cannot create process.\n");
- exit (1);
- }
- fprintf (pipe, "%s\n", s);
- }
- state = INSIDE;
- break;
-
- case INSIDE:
- if (!(strcmp (s, "exit 0"))) { /* Look for end */
- if (!extract) {
- fprintf (fw, "%s\n", s);
- fclose (fw);
- }
- else {
- fprintf (pipe, "%s\n", s);
- pclose (pipe);
- }
- state = SEARCH;
- }
- else {
- if (extract)
- fprintf (pipe, "%s\n", s);
- else
- fprintf (fw, "%s\n", s);
- }
- break;
- }
- j = 0; /* Reset counter */
- }
- }
- }
-