home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 150_01 / ldir.c < prev    next >
Text File  |  1985-09-07  |  12KB  |  462 lines

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