home *** CD-ROM | disk | FTP | other *** search
- /* @(#)ssh.c 1.12 92/03/05 (HaB)
- *
- * NAME:
- * ssh
- *
- * SYNTAX:
- * ssh < filename
- *
- * DESCRIPTION:
- * Splits and strips appended shell archives and
- * stores the result in 'partXX' files.
- *
- * 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.
- *
- * DATE:
- * 1992-03-05
- *
- * 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>
-
- #define SEARCH 0
- #define START 1
- #define INSIDE 2
- #define MSTEP 80 /* Allocation steps */
- #define SEARCHP 5 /* No. of search patterns */
-
- #ifdef HP /* Can be used for Turbo C/C++ */
- #include <stdlib.h>
- size_t msize;
- #endif
-
- #ifdef SUN
- #include <malloc.h>
- unsigned int msize;
- #endif
-
- char sccsid[] = "@(#)ssh.c 1.12 92/03/05 (HaB) etxerus@james.ericsson.se";
-
- 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"
- };
-
- /* ssearchp:
- *
- * Searches string s for pattern p.
- * Returns index on success else -1.
- *
- */
-
- int ssearchp (s, p)
-
- char *s; /* String to search */
- char *p; /* Pattern to search for */
-
- {
- register i, j, k; /* Counters */
-
- for (i = 0; s[i] != '\0'; i++) {
- for (j = i, k = 0; p[k] != '\0' && s[j] == p[k]; j++, k++)
- ;
- if (k > 0 && p[k] == '\0') /* Pattern found */
- return i;
- }
- return -1; /* Pattern not found */
- }
-
- main ()
- {
- FILE *fr = stdin; /* Input filepointer */
- FILE *fw; /* Output filepointer */
- int state = SEARCH; /* The current state */
- int fc = 0; /* File part counter */
- char *s; /* Read line */
- char fout[7]; /* Output filenames */
- register j = 0; /* Counter */
- register c; /* Read character */
-
- 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) {
- puts ("ssh: Allocation error, cannot continue.");
- exit (1);
- }
- }
- }
- else {
- s[j] = '\0'; /* One line has been read */
- switch (state) {
- case SEARCH:
- for (j = 0; j < SEARCHP;) {
- if (ssearchp (s, pattern[j++]) != -1) {
- state = START;
- continue;
- }
- }
- if (state != START)
- break;
-
- case START: /* Start writing to file */
- sprintf (fout, "part%.2d", ++fc);
- fw = fopen (fout, "w");
- fprintf (fw, "%s\n", s);
- state = INSIDE;
- break;
-
- case INSIDE:
- if (!(strcmp (s, "exit 0"))) { /* Look for end */
- fprintf (fw, "%s\n", s);
- fclose (fw);
- state = SEARCH;
- }
- else
- fprintf (fw, "%s\n", s);
- break;
- }
- j = 0; /* Reset counter */
- }
- }
- }
-
-