home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / os2 / insp110p.zip / DUTILSRC.ZIP / tic2desc.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  4KB  |  201 lines

  1. /*
  2.  * $Id$
  3.  * make descriptions file from .TIC files in current directory
  4.  *
  5.  * $Log$
  6.  */
  7.  
  8. # include <stdio.h>
  9. # include <stddef.h>
  10. # include <stdlib.h>
  11. # include <ctype.h>
  12. # include <string.h>
  13. # include <memory.h>
  14. # include <dos.h>
  15. # include <direct.h>
  16. # include <errno.h>
  17.  
  18. # include "dosdir.h"
  19. # ifndef MAX_PATH
  20. #  define MAX_PATH 128
  21. # endif
  22.  
  23. # define DESCFILE "files.bbs"
  24. # define argused(x) (x = x)
  25.  
  26.  
  27.  
  28. char descfile[MAX_PATH] = DESCFILE;
  29.  
  30. int line = 0;
  31. int    desconly = 0;
  32.  
  33. static char
  34. *skip_whi (char *s)
  35. {
  36.     while (isspace (*s))
  37.         ++s;
  38.     return s;
  39. }
  40.  
  41. static int
  42. match (const char* s, const char *v)
  43. {
  44.     int l = strlen (v);
  45.     return memcmp (s, v, l);
  46. }
  47.  
  48.  
  49. int
  50. gatherdesc (FILE **fp, char* spec)
  51. {
  52.     int             j, l, carg;
  53.     FILE            *ip;
  54.     char            cwd[MAX_PATH];
  55.     struct FFIND    ff;
  56.  
  57.     GETCWD (cwd, MAX_PATH);
  58.     l = strlen (cwd);
  59.     if (l && cwd[l-1] != '\\')
  60.         cwd[l++] = '\\';
  61.     strcpy (cwd + l, spec);
  62.     j = FINDFIRST (cwd, _A_NORMAL, &ff);
  63.     while (j == 0)
  64.     {
  65.         if (strcmp (f_name(&ff), descfile) != 0)
  66.         {
  67.  
  68.             strcpy (cwd + l, f_name(&ff));
  69.             ip = fopen (cwd, "r");
  70.             if (ip == NULL)
  71.                 fprintf (stderr, "tic2desc: Open %s - %s\n", f_name(&ff), strerror (errno));
  72.             else
  73.             {
  74.                 int lines, gotone;
  75.                 char aname[25], faddr[25], fname[25], fdesc[256], buf[512];
  76.                 char fname1[25];
  77.  
  78.                 *aname = *fname = *faddr = *fdesc = '\0';
  79.                 for (lines = gotone = 0; !gotone && lines++ < 15 && fgets (buf, 512, ip) != NULL; )
  80.                 {
  81.                     char* p = strchr (buf, '\n');
  82.                     if (p != NULL)
  83.                         *p = '\0';
  84.  
  85.                     if (match (buf, "Area") == 0)
  86.                     {
  87.                         strncpy (aname, skip_whi (buf+4), 24);
  88.                         aname[24] = '\0';
  89.                     }
  90.                     else if (match (buf, "From") == 0)
  91.                     {
  92.                         strncpy (faddr, skip_whi (buf + 4), 24);
  93.                         faddr[24] = '\0';
  94.                     }
  95.                     else if (match (buf, "File") == 0)
  96.                     {
  97.                         strncpy (fname, skip_whi (buf + 4), 24);
  98.                         fname[24] = '\0';
  99.                     }
  100.                     else if (match (buf, "Desc") == 0)
  101.                     {
  102.                         strncpy (fdesc, skip_whi (buf + 4), 255);
  103.                         fdesc[255] = '\0';
  104.                     }
  105.                 }
  106.  
  107.                 if (*fname)
  108.                 {
  109.                     strlwr (fname);
  110.                     strupr (aname);
  111.  
  112.                     if (*fp == NULL)
  113.                     {
  114.                         char * pp, bak[MAX_PATH];
  115.  
  116.                         strcpy (bak, descfile);
  117.                         pp = strrchr (bak, '.');
  118.                         if (pp == NULL)
  119.                             pp = bak + strlen(bak);
  120.                         strcpy (pp, ".BAK");
  121.                         remove (bak);
  122.                         rename (descfile, bak);
  123.                         *fp = fopen (descfile, "w");
  124.                         if (*fp == NULL)
  125.                         {
  126.                             fprintf (stderr, "tic2desc: Can't create '%s' = %s\n", descfile, strerror(errno));
  127.                             rename (bak, descfile);
  128.                             break;
  129.                         }
  130.                     }
  131.  
  132.                     strcpy (fname1, fname);
  133.                     strupr (fname1);
  134.                     if (desconly)
  135.                     {
  136.                         fprintf (*fp, "%-13s%s\n", f_name(&ff), fname);
  137.                         fprintf (*fp, "%-13s%s\n", fname1,      fdesc);
  138.                     }
  139.                     else
  140.                     {
  141.                         fprintf (*fp, "%-13s%-9s%-13sFrom %s\n", f_name(&ff), aname, fname, faddr);
  142.                         fprintf (*fp, "%-13s%-9s%-13s%s\n",      fname1,      aname, fname, fdesc);
  143.                     }
  144.                     printf (".");
  145.  
  146.                 }
  147.                 else
  148.                     fprintf (stderr, "tic2desc: No filename in %s\n", f_name(&ff));
  149.  
  150.                 fclose (ip);
  151.             }
  152.         }
  153.         j = FINDNEXT (&ff);
  154.     }
  155.     FINDCLOSE (&ff);
  156.     return 0;
  157. }
  158.  
  159.  
  160.  
  161. int
  162. main (int argc, char *argv[])
  163. {
  164.     int r = 0;
  165.     int carg;
  166.     FILE *fp;
  167.  
  168.     for (carg = 0; ++carg < argc; )
  169.     {
  170.         char *argp = argv[carg];
  171.         if (*argp == '-' || *argp == '/')
  172.         {
  173.             ++argp;
  174.             switch (tolower (*argp))
  175.             {
  176.             case 'f':            /* Filename for descriptions */
  177.                 if (!*++argp)
  178.                     argp = argv[++carg];
  179.                 strcpy (descfile, argp);
  180.                 break;
  181.             case 'd':            /* Descriptions only */
  182.                 ++desconly;
  183.                 break;
  184.             }
  185.         }
  186.     }
  187.  
  188.     argused(argc);
  189.     argused(argv);
  190.     fp = NULL;
  191.     r |= gatherdesc (&fp, "*.bad");
  192.     r |= gatherdesc (&fp, "*.tic");
  193.     if (fp != NULL)
  194.     {
  195.         fclose (fp);
  196.         printf ("\n");
  197.     }
  198.     return r;
  199. }
  200.  
  201.