home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / determcap / determcap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-01-18  |  3.0 KB  |  209 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. char tbuf[BUFSIZ * 16];        /* really big */
  5. char *input ();
  6. FILE *fp;
  7. int verbose = 0;
  8.  
  9. main (argc, argv)
  10. int argc;
  11. char **argv;
  12. {
  13.     verbose = (argc > 1);
  14.  
  15.     while (input (tbuf) != NULL)
  16.     {
  17.         if (tbuf[0] == '#')
  18.             comment ();
  19.         else
  20.             entry ();
  21.     }
  22. }
  23.  
  24. int usebuf = 0;
  25.  
  26. char *input (bp)
  27. char *bp;
  28. {
  29.     if (! usebuf)
  30.         return (gets (bp));
  31.     else
  32.     {
  33.         usebuf = 0;
  34.         return (tbuf);
  35.     }
  36. }
  37.  
  38. comment ()
  39. {
  40.     static int com_no = 1;
  41.     char name[20];
  42.  
  43.     sprintf (name, "comment.%d", com_no++);
  44.     if ((fp = fopen (name, "w")) == NULL)
  45.     {
  46.         fflush (stdout);
  47.         fprintf (stderr, "%s: could not open\n", name);
  48.         exit (1);
  49.     }
  50.  
  51.     do
  52.     {
  53.         fprintf (fp, "%s\n", tbuf);
  54.         if (input (tbuf) == NULL)
  55.         {
  56.             fclose (fp);
  57.             exit (0);
  58.         }
  59.     } while (tbuf[0] == '#');
  60.  
  61.     /* at this point, a non-comment is in the buffer */
  62.     usebuf = 1;
  63.     fclose (fp);
  64. }
  65.  
  66. char *getname ();
  67. char *getentry ();
  68.  
  69. entry ()
  70. {
  71.     int end = strlen (tbuf) - 1;
  72.     char *name, *fullname, *cp, *state;
  73.     char *index ();
  74.  
  75.     /* first, get the entire entry */
  76.     while (tbuf[end] == '\\')
  77.     {
  78.         if (input (& tbuf[end]) == NULL)
  79.         {
  80.             fflush (stdout);
  81.             fprintf (stderr, "stdin ended with a '\\\\'\n");
  82.             exit (1);
  83.         }
  84.         end = strlen (tbuf) - 1;
  85.     }
  86.     /* now pull it apart */
  87.  
  88.     state = tbuf;
  89.     fullname = getentry (& state);
  90.     name = getname (fullname);
  91.  
  92.     if (verbose)
  93.         printf ("%s\n", name);
  94.  
  95.     dodir (name);
  96.  
  97.     if ((fp = fopen (name, "w")) == NULL)
  98.     {
  99.         fflush (stdout);
  100.         fprintf (stderr, "%s: could not open\n", name);
  101.         exit (1);
  102.     }
  103.     fprintf (fp, "%s\n", fullname);
  104.     fclose (fp);
  105.  
  106.     while (cp = getentry (& state))
  107.         doentry (cp);
  108.  
  109.     if (chdir ("..") < 0)
  110.     {
  111.         perror ("chdir(\"..\")");
  112.         exit (1);
  113.     }
  114. }
  115.  
  116. char *getname (cp)
  117. register char *cp;
  118. {
  119.     static char shortname[100];
  120.     register int i = 0;
  121.  
  122.     while (*cp != '|')
  123.         cp++;
  124.  
  125.     for (cp++; *cp != '|' && *cp != ':'; cp++)
  126.         shortname[i++] = *cp;
  127.     shortname[i] = '\0';
  128.  
  129.     return (shortname);
  130. }
  131.  
  132. char *getentry (state)
  133. register char **state;
  134. {
  135.     char *cp;
  136.  
  137.     if (!state || ! *state || ! **state)
  138.         return (NULL);
  139.  
  140.     while (**state == '\t' || **state == ':')
  141.         (*state)++;
  142.  
  143.     cp = *state;
  144.  
  145.     while (**state && **state != ':')
  146.         (*state)++;
  147.  
  148.     **state = '\0';
  149.     (*state)++;
  150.  
  151.     if (verbose)
  152.         printf ("\t'%s'\n", cp);
  153.  
  154.     return (cp);
  155. }
  156.  
  157. dodir (dir)
  158. char *dir;
  159. {
  160.     char buf[100];
  161.  
  162.     if (mkdir (dir, 0755) < 0)
  163.     {
  164.         sprintf (buf, "mkdir (\"%s\")", dir);
  165.         perror (buf);
  166.         exit (1);
  167.     }
  168.     if (chdir (dir) < 0)
  169.     {
  170.         sprintf (buf, "chdir (\"%s\")", dir);
  171.         perror (buf);
  172.         exit (1);
  173.     }
  174. }
  175.  
  176. doentry (cp)
  177. char *cp;
  178. {
  179.     char name[3];
  180.  
  181.     name[0] = cp[0];
  182.     name[1] = cp[1];
  183.     name[2] = '\0';
  184.  
  185.     if ((fp = fopen (name, "w")) == NULL)
  186.     {
  187.         fflush (stdout);
  188.         fprintf (stderr, "%s: could not open\n", name);
  189.         exit (1);
  190.     }
  191.  
  192.     fprintf (fp, "%s\n", cp);
  193.     fclose (fp);
  194. }
  195.  
  196. #ifdef    NEED_MKDIR_SUB
  197. int
  198. mkdir(p, u)
  199.     char    *p;
  200.     int         u;
  201. {
  202.     char     buff[BUFSIZ];
  203.  
  204.     /* By playing with UMASK you can skip the chmod, but so it goes. */
  205.     (void)sprintf(buff, "mkdir %s && chmod %o %s", p, u, p);
  206.     return(system(buff) ? -1 : 0);
  207. }
  208. #endif    /* NEED_MKDIR_SUB */
  209.