home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 164_01 / ldir.c < prev    next >
Text File  |  1984-04-08  |  10KB  |  420 lines

  1. /*    LDIR     Library Directory display program */
  2.  
  3. #define    VERSION        3 
  4. #define    REVISION    0
  5. #define MOD_DATE    "84-02-29"
  6.  
  7. /*
  8. Legal Notices:
  9.     Copyright (c) 1982, 1983 by Gary P. Novosielski.
  10.     All rights reserved.
  11.  
  12.     Permission is hereby granted for noncommercial use.
  13.     Use or duplication of this or any derivative work for
  14.     commercial advantage without prior written consent
  15.     of the author is prohibited.
  16.  
  17. LIFO Revision Summary:
  18.     3.00    84-29-84    Revised for use on IBM-PC running
  19.                 MS-DOS. Compiler = Lattice 'c'
  20.                 (Pete Mack)
  21.     2.20    83-10-13    Changed Kb size calculation to
  22.                 round upward.  Added max drive
  23.                 validation.  Moved copyright
  24.                 display to help section.
  25.     2.11    83-03-21    BDS 1.5 support.
  26.                 Size display in Kb.
  27.     2.10    82-12-09    Size display in sectors.
  28.     2.00    82-11-20    [Not released]
  29.     1.00    82-11-14    Initial source release
  30.         Gary P. Novosielski
  31.  
  32. Program Description:
  33.     This program is intended for use on RCPM systems to
  34.     allow callers to see the contents of the directories
  35.     of .LBR files on the system.  You probably won't need
  36.     it on your home system, since the -L function of LU.COM
  37.     provides this ability.  Since LU is not active on
  38.     remote systems, a program like this is necessary
  39.     to allow you to see member names in a library without
  40.     your having to download the library first.
  41. */
  42.  
  43. #include "stdio.h"
  44. #include "ctype.h"
  45.  
  46. /* Pseudo typedef's */
  47. #define FLAG        char
  48.  
  49. /* Number of displayed entries */
  50. #define NWIDE    4
  51.  
  52. /*         EXTERNALS */
  53. FILE   *lbrfile, *fopen();             /* fd for library file    */
  54. char lbrname[20];
  55.  
  56. #define FROM_START    0
  57.  
  58. FLAG lbropen;
  59. int  entries, freeent;
  60.  
  61. /* Entry Size */
  62. #define ESIZE        32
  63.  
  64. /* Entries per sector */
  65. #define EPS        (SECSIZ / ESIZE)
  66.  
  67. /* Structure of a directory entry */
  68. struct direntry
  69. {
  70.     char status;    /* Possible values */
  71. #define  ACTIVE            0x00
  72. #define  KILLED            0xFE
  73. #define     VIRGIN            0xFF
  74.     char id[8+3];     /* filename.ext */
  75.     unsigned indx;    /* Pointer to first sector */
  76.     unsigned size;    /* Size of member in sectors */
  77.     unsigned crc;    /* CRC check word */
  78.     /* Future expansion space */
  79. #define EXPSIZ            14
  80.     char expand[EXPSIZ];
  81. }
  82. *directory, *curentry;    /* two pointers to such a beast */
  83.  
  84. typedef struct direntry dirtype;
  85.  
  86. char sopt;        /* size option: S, N, or K */
  87. char *drmsg;        /* Max drive letter allowed */
  88.  
  89. /*        END OF EXTERNALS */
  90. #define SECSIZ    128
  91. #define OK    0
  92.  
  93. /************************************************
  94.  main
  95. *************************************************/
  96.  
  97. main (argc,argv)
  98. unsigned  argc;
  99. char *argv[];
  100. {
  101.     cprintf(
  102.       "Library DIRectory   Ver:%d.%02d   %s\n\r%s\n\r",
  103.       VERSION,REVISION,MOD_DATE,
  104.       "Press CTRL-S to pause; CTRL-C to cancel"
  105.       );
  106.  
  107.  
  108.     /*
  109.     The FIRST character of the following message is actually
  110.     used in the test for the maximum legal drive.  This will
  111.     allow sites which do not support a C compiler to easily
  112.     find and patch this value in the object code.
  113.     */
  114.     drmsg = "P: is highest valid drive";
  115.  
  116.  
  117.     /* Initialize flags */
  118.     sopt = 'K';            /* Default option setting */
  119.     lbropen = FALSE;        /* No library open */
  120.     directory = NULL;        /* No directory in memory */
  121.  
  122.  
  123.     if (argc < 2)        /* No command line arguments */
  124.     {
  125.     cputs("\n\rCopyright (c) 1982, 1983 by Gary P. Novosielski");
  126.     cputs("\n\r\nCorrect syntax is:");
  127.     cputs("\n\rLDIR [<option>] name1 [[<option>] [name2...[nameN]]]");
  128.     cputs("\n\r\nWhere:\tname1 through");
  129.     cputs("\n\r\tnameN\tare library names; default type: .LBR");
  130.     cputs("\n\rOptions:\n\r\t-n\tonly show names of members.");
  131.     cputs("\n\r\t-s\talso show size in sectors.");
  132.     cputs("\n\r\t-k\tlike -s, but size in Kbytes. (default)");
  133.     cputs("\n\rOption flags stay in effect for subsequent names.");
  134.     cputs("\n\rAmbiguous names are not permitted.");
  135.  
  136.  
  137.  
  138.     }
  139.     else
  140.     /* Process command line arguments */
  141.     while(--argc)
  142.     {
  143.         if (**(++argv) == '-')
  144.         procopt(*argv);
  145.         else if (!namel(*argv))
  146.         dirlist();
  147.         else
  148.         cprintf("\n\r%s not found on disk.\n\r",lbrname);
  149.  
  150.     }
  151.     /* End of command line.  Clean up and exit */
  152. }
  153. /* End of main function */
  154.  
  155. /************************************************
  156.  Open *name as the current library
  157. *************************************************/
  158.  
  159. namel(name)
  160. char *name;
  161. {
  162.     if (lbropen && close(lbrfile) == ERROR)
  163.     abend("\n\rCan't close library: %s",lbrname);
  164.     lbropen = FALSE;
  165.     if (isambig(name) || indexc(name,' ') != ERROR)
  166.     abend("\n\rBad library name: %s",name);
  167.     if (name[1] == ':' && *name > *drmsg)
  168.     abend("\n\r%s",drmsg);
  169.     strcpy(lbrname,name);
  170.     if (indexc(name,'.') == ERROR)
  171.     strcat(lbrname,".LBR");
  172.     if ((lbrfile = fopen(lbrname,"r")) != NULL)
  173.     {
  174.     cprintf("\n\rLibrary: %s has ",lbrname);
  175.     readdir();
  176.     }
  177.     else
  178.     return ERROR;
  179.     lbropen = TRUE;
  180.     cprintf ("%d entries, %d free:\n\r",entries,freeent);
  181.     return OK;
  182. }
  183.  
  184. /************************************************
  185.    Return flag saying if the requested number of memory bytes
  186.    are available.  Try to make them available if possible.
  187. *************************************************/
  188.  
  189. FLAG avail(request)
  190. unsigned request;    /* in bytes */
  191. {
  192.     char *ptr;
  193.     unsigned  *curavail, temp;
  194.     temp = 0;
  195.  
  196.     curavail = &temp;    /* Pseudo-static kluge */
  197.  
  198.     if(request < *curavail)
  199.     return TRUE;
  200.     if ((ptr = (char *) sbrk(++request - *curavail)) == ERROR)
  201.     return FALSE;
  202.  
  203.     /* If this is the first success, initialize pointer */
  204.     if (directory == NULL)
  205.     directory =  (dirtype *) ptr;
  206.  
  207.     *curavail = request; /* Modify static for next call */
  208.     return TRUE;
  209. }
  210.  
  211. /************************************************
  212. /* Read the directory into memory */
  213. *************************************************/
  214.  
  215. readdir()
  216. {
  217.     if (!avail(SECSIZ))
  218.     memerr();
  219.     fseek(lbrfile, 0, FROM_START);
  220.  
  221.     if (
  222.       fread(directory,128,1,lbrfile) != 1
  223.       || entcmp(directory,"\0           ")
  224.       || directory->indx
  225.       || !directory->size
  226.       )
  227.     abend("no directory");
  228.  
  229.     if (directory->size > 1)
  230.     {
  231.     if (!avail(SECSIZ * directory->size))
  232.         memerr();
  233.     if (fread(directory+EPS,128, directory->size - 1,lbrfile)
  234.       != directory->size - 1)
  235.         abend("a bad directory");
  236.     }
  237.  
  238.     freeent = entries = EPS * directory->size;
  239.  
  240.     for(
  241.       curentry = directory;
  242.       curentry->status != VIRGIN && freeent;
  243.       ++curentry
  244.       )
  245.     --freeent;
  246. }
  247.  
  248. /************************************************
  249.  memory error
  250. *************************************************/
  251.  
  252. memerr()
  253. {
  254.     abend("an absurdly huge directory");
  255. }
  256.  
  257. /************************************************
  258.   Search string *s for character c.  Return offset 
  259. *************************************************/
  260.  
  261. int indexc(s, c)
  262. char *s, c;
  263. {
  264.     int i;
  265.     for (i = 0; *s; i++) 
  266.     if(*s++ == c)
  267.         return i;
  268.     return ERROR;
  269. }
  270.  
  271. /************************************************
  272.   Return TRUE if s contains asterisk(s) or question(s)
  273. *************************************************/
  274.  
  275. isambig(s)
  276. char *s;
  277. {
  278.     if (indexc(s,'*') != ERROR || indexc(s,'?') != ERROR)
  279.     return TRUE;
  280.     return FALSE;
  281. }
  282.  
  283. /************************************************
  284.  Terminate program with error message 
  285. *************************************************/
  286.  
  287. abend(p1, p2, p3, p4)
  288. {
  289.     cprintf(p1, p2, p3, p4);
  290.     cputs("\n\r\nFor help, type LDIR alone.");
  291.     exit();
  292. }
  293.  
  294. /************************************************
  295.  compare string a to string b ignoring some bits of each
  296. *************************************************/
  297.  
  298. bitcmp(a, b, count, mask)
  299. char *a, *b, mask;
  300. int count;
  301. {
  302.     int r;
  303.     while(count--)
  304.     if (r = (*a++ & mask) - (*b++ & mask))
  305.         return r;
  306.     return 0;
  307. }
  308.  
  309. /************************************************
  310.  form a string in dst from a standard format name in src
  311. *************************************************/
  312.  
  313. formname(dst,src)
  314. char *dst, *src;
  315. {
  316.     int i,j;
  317.     j = 0;
  318.  
  319. /* Remove attributes first so compares will work */
  320.     for (i = 1; i < 12; i++)
  321.     src[i] &= 0x7F;
  322.     for (i = 1; i < 9; i++)
  323.     {
  324.     if (src[i] == ' ')
  325.         break;
  326.     dst[j++] = src[i];
  327.     }
  328.     if (src[9] != ' ')
  329.     dst[j++] = '.';
  330.     for (i = 9; i < 12; i++)
  331.     {
  332.     if (src[i] == ' ')
  333.         break;
  334.     dst[j++] = src[i];
  335.     }
  336.     dst[j] = '\0';
  337.     return dst;
  338. }
  339.  
  340. /************************************************
  341.    Compare two directory entries. Note that status is major
  342.    sort field.
  343. *************************************************/
  344.  
  345. entcmp(a,b)
  346. char *a, *b;
  347. {
  348.     int  i, r;
  349.  
  350.     for (i = (1+8+3); i--; a++, b++)
  351.     if ((r = *a - *b) && *b != '?')
  352.         return r;
  353.     return 0;
  354. }
  355.  
  356. /************************************************
  357.    List the directory of the current library, and return number
  358.    of free entries
  359. *************************************************/
  360.  
  361. dirlist()
  362. {
  363.     char name[20];
  364.     int  i;
  365.     unsigned del, act;
  366.  
  367.     curentry = directory;
  368.     for ((act = del = 0, i = entries - freeent); --i;)
  369.     {
  370.     if ((++curentry)->status == ACTIVE) 
  371.     {
  372.         if(!(act % NWIDE))  
  373.         cputs("\n\r");
  374.         formname(name, curentry);
  375.  
  376.         switch (sopt)
  377.         {
  378.         case 'S':    /* Size in sectors */
  379.         cprintf("%-12s%5ds ", name, curentry->size);
  380.         break;
  381.         case 'K':    /* Size in Kilobytes */
  382.         cprintf("%-12s%5dk ",name, (curentry->size+7)/8);
  383.         break;
  384.         case 'N':    /* Name only. More names per line */
  385.         cprintf("%-14s",name);
  386.         }
  387.         ++act;
  388.     }
  389.     else
  390.         ++del;
  391.     }
  392.     cprintf("\n\r Active entries: %u, Deleted: %u, Free: %u, Total: %u.\n\r",
  393.       ++act, del, freeent, entries);
  394.     return --act;
  395. }
  396.  
  397. /************************************************
  398.  Process option string (-xx)
  399. *************************************************/
  400.  
  401. procopt(s)
  402. char *s;
  403. {
  404.  
  405.     while(*(++s))
  406.     switch (*s)
  407.     {
  408.     case 'S':
  409.     case 'N':
  410.     case 'K':
  411.     case 's':
  412.     case 'n':
  413.     case 'k':
  414.         sopt = toupper(*s);
  415.         break;
  416.     default:
  417.         abend("'%c' is an invalid option",*s);
  418.     }
  419. }
  420.