home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume28 / ssh / part01 / ssh.c < prev   
Encoding:
C/C++ Source or Header  |  1992-03-07  |  3.5 KB  |  146 lines

  1. /* @(#)ssh.c 1.12 92/03/05 (HaB)
  2.  *
  3.  * NAME:
  4.  *    ssh
  5.  *
  6.  * SYNTAX:
  7.  *    ssh < filename
  8.  * 
  9.  * DESCRIPTION:
  10.  *    Splits and strips appended shell archives and
  11.  *    stores the result in 'partXX' files.
  12.  *    
  13.  * NOTES:
  14.  *    The program should work on all archives created
  15.  *    using 'shar' (or equals) provided that they have
  16.  *    not been changed since they were first generated.  
  17.  *
  18.  * DATE:
  19.  *    1992-03-05
  20.  *
  21.  * AUTHOR:
  22.  *    Hans C. Beckerus
  23.  *    etxerus@james.ericsson.se
  24.  *
  25.  * DISCLAIMER:
  26.  *    This program is free to distribute to anyone aslong 
  27.  *    as the code is not changed in anyway without me
  28.  *    knowing about it.  
  29.  *
  30.  *                                            /HaB :-)
  31.  */
  32.  
  33. #include <stdio.h>
  34. #include <string.h>
  35.  
  36. #define SEARCH   0
  37. #define START    1
  38. #define INSIDE   2
  39. #define MSTEP    80     /* Allocation steps       */
  40. #define SEARCHP  5      /* No. of search patterns */ 
  41.  
  42. #ifdef HP     /* Can be used for Turbo C/C++ */
  43. #include <stdlib.h>
  44. size_t msize;
  45. #endif
  46.  
  47. #ifdef SUN
  48. #include <malloc.h>
  49. unsigned int msize;
  50. #endif
  51.  
  52. char sccsid[] = "@(#)ssh.c 1.12  92/03/05 (HaB)  etxerus@james.ericsson.se";
  53.  
  54. char *pattern[] = {                 /* Add more patterns here if needed.   */ 
  55.     "# This is a shell archive",    /* NOTE! Remember to increase SEARCHP. */ 
  56.     "# This is part",
  57.     "#!/bin/sh",
  58.     "# !/bin/sh",
  59.     "#! /bin/sh"
  60. };
  61.  
  62. /*  ssearchp:
  63.  *
  64.  *  Searches string s for pattern p.
  65.  *  Returns index on success else -1.
  66.  *
  67.  */
  68.  
  69. int ssearchp (s, p)
  70.  
  71. char *s;     /* String to search      */
  72. char *p;     /* Pattern to search for */
  73.  
  74. {
  75.     register i, j, k;     /* Counters */
  76.  
  77.     for (i = 0; s[i] != '\0'; i++) {
  78.     for (j = i, k = 0; p[k] != '\0' && s[j] == p[k]; j++, k++)
  79.             ;
  80.     if (k > 0 && p[k] == '\0')     /* Pattern found */
  81.         return i;
  82.     }
  83.     return -1;    /* Pattern not found */
  84. }
  85.  
  86. main ()
  87. {
  88.     FILE     *fr = stdin;         /* Input filepointer  */
  89.     FILE     *fw;                 /* Output filepointer */
  90.     int       state = SEARCH;     /* The current state  */
  91.     int       fc = 0;             /* File part counter  */
  92.     char     *s;                  /* Read line          */
  93.     char      fout[7];            /* Output filenames   */
  94.     register  j = 0;              /* Counter            */
  95.     register  c;                  /* Read character     */
  96.  
  97.     msize = MSTEP;
  98.     s = malloc (msize);     /* Allocate buffer */
  99.  
  100.     while ((c = getc (fr)) != EOF) {
  101.     if (c != '\n') {     /* Check for EOL */
  102.         s[j++] = c;
  103.             if (j == msize) {
  104.                 msize += MSTEP;
  105.                 if ((s = realloc (s, msize)) == NULL) {
  106.                     puts ("ssh: Allocation error, cannot continue.");
  107.                     exit (1);
  108.                 }
  109.             }
  110.         }
  111.     else {
  112.         s[j] = '\0';     /* One line has been read */
  113.         switch (state) {
  114.         case SEARCH:
  115.             for (j = 0; j < SEARCHP;) {
  116.             if (ssearchp (s, pattern[j++]) != -1) {
  117.                 state = START;
  118.                 continue;
  119.             }
  120.             }
  121.             if (state != START)
  122.             break;
  123.  
  124.         case START:     /* Start writing to file */
  125.             sprintf (fout, "part%.2d", ++fc);
  126.             fw = fopen (fout, "w");
  127.             fprintf (fw, "%s\n", s);
  128.                     state = INSIDE;
  129.             break;
  130.  
  131.                 case INSIDE:
  132.             if (!(strcmp (s, "exit 0"))) {     /* Look for end */
  133.                         fprintf (fw, "%s\n", s);
  134.                         fclose (fw);
  135.                         state = SEARCH;
  136.                     }
  137.                     else
  138.             fprintf (fw, "%s\n", s);
  139.             break;
  140.             }
  141.             j = 0;    /* Reset counter */
  142.         }
  143.     }
  144. }
  145.  
  146.