home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 185_01 / pull.c < prev    next >
Text File  |  1985-08-21  |  4KB  |  180 lines

  1. /* Program to extract 1 file from a Novosielski .lbr file
  2.  * Tom Burnett      10/08/85
  3.  */
  4.  
  5. #define    VERSION        1
  6. #define REVISION    0
  7. #define MOD_DATE    "10/08/85"
  8.  
  9. /* This program will extract a member of a LBR file...
  10.  
  11.    USE: PULL <library> <member>
  12.    COMPILE/LINK: cc pull.c
  13.          as pull.asm
  14.          ln pull.o c.lib
  15.   Based on Ltype by S. Kluger 01-13-83
  16. and
  17.    Vers 3.0   3-1-84
  18.    Upgrade to lattice c ms dos P. H. Mack.
  19.    Change to Aztec C 10/08/85 Tom Burnett
  20. */
  21.  
  22. #include "stdio.h"
  23.  
  24. char curdsk, fcb[36];
  25. char fnam[12], libnam[16], dirbuf[128], *dirp;
  26. int  fd, dirsiz, filsiz;
  27. FILE *fp1,*fopen();
  28.  
  29. #define OK    0
  30. #define ERROR -1
  31.  
  32. /************************************************
  33.  main
  34. *************************************************/
  35.  
  36. main(argc,argv)
  37. int argc;
  38. char **argv;
  39. {
  40.     fprintf(stderr,"\nPULL vers:%d.%02d  %s\n\r\n",
  41.     VERSION,REVISION,MOD_DATE);
  42.  
  43.     if (argc != 3) {
  44.         fprintf(stderr,"usage PULL LIBRARY.LBR FILENAME\n\n");
  45.         exit(1);
  46.         }
  47.     opnlib(argv[1]);
  48.  
  49.     if (fndmem(argv[2]) == ERROR) erxit("\nMember not in LBR file!\n\r");
  50.  
  51.     fprintf(stderr,"\nFile '%s' present - %d sectors.\n",argv[2],filsiz);
  52.     fprintf(stderr,"extracting file to default drive....\n");
  53.  
  54.     if ((fp1 = fopen(argv[2],"w")) == NULL) {
  55.         fprintf(stderr,"cannot create -> %s\n",argv[2]);
  56.         exit(1);
  57.         }
  58.  
  59.     extract();
  60. }
  61.  
  62. /************************************************
  63.  Extract function 
  64. *************************************************/
  65.  
  66. extract()
  67.  
  68. {
  69.     int    j;
  70.     int    c;
  71.  
  72.     dirsiz = filsiz;
  73.     do
  74.     {
  75.         reload();
  76.  
  77.         for (j=0; j<128; j++){
  78.  
  79.             fputc(*dirp,fp1);
  80.             dirp++;
  81.         }
  82.     }while(dirsiz != 0);
  83. }
  84.  
  85. /************************************************
  86.  open library file
  87. *************************************************/
  88.  
  89.  
  90. opnlib(file)
  91. char *file;
  92. {
  93.     char l, *npnt;
  94.  
  95.     strcpy(libnam,file);
  96.     l = matchr(libnam,'.');
  97.     if (l == 0) strcat(libnam, ".LBR");
  98.     fcbinit(libnam,fcb);    /* build name in fcb */
  99.     movmem(fcb,fnam,12);    /* get from dfcb2 for log */
  100.  
  101.     if(strcmp(fcb+9, "LBR")){
  102.         fprintf(stderr,"got %s\n",libnam);
  103.         erxit("Invalid file spec, MUST be type .LBR\n");
  104.     }
  105.  
  106.     fd = open(libnam,0x8000);
  107.     if(fd == -1) erxit("Library file not found.\n");
  108. }
  109.  
  110. /************************************************
  111.  find library member
  112. *************************************************/
  113.  
  114. fndmem(file)
  115. char *file;
  116. {
  117.     char dnam[16], fname[36];
  118.     long int    floc;
  119.  
  120.     fcbinit(file,fname);
  121.     read(fd,dirbuf,128);
  122.     dirp = dirbuf;
  123.     dirsiz = *(dirp+14);
  124.     dirp += 32;
  125.  
  126.     do{
  127.         if (*dirp == 255) return(ERROR);
  128.         if (*dirp == 0){
  129.             strcpy(dnam, dirp+1);
  130.             dnam[11]=0;
  131.             if(strcmp(dnam, fname+1) == 0){
  132.                 filsiz = (*(dirp+14)) + ((*(dirp+15)) * 256);
  133.                 floc=(*(dirp+12)) + ((*(dirp+13)) * 256);
  134.                 lseek(fd,floc *128,0);
  135.                 return(OK);
  136.             }
  137.         }
  138.         dirp += 32;
  139.         if(dirp > dirbuf+128) reload();
  140.     }
  141.     while(dirsiz);
  142.     return(ERROR);
  143. }
  144.  
  145. /************************************************
  146.  reload
  147. *************************************************/
  148.  
  149. reload()
  150. {
  151.     read(fd,dirbuf,128);
  152.     dirp = dirbuf;
  153.     dirsiz--;
  154. }
  155.  
  156. /************************************************
  157.  match char
  158. *************************************************/
  159.  
  160. matchr(st,ch)
  161. char *st,ch;
  162. {
  163.     int i;
  164.     for(i=0; st[i]; i++){
  165.         if(st[i] == ch) return(i);
  166.     }
  167.     return(0);
  168. }
  169.  
  170. /************************************************
  171.  error exit
  172. *************************************************/
  173.  
  174. erxit(strg)
  175. char *strg;
  176. {
  177.     fprintf(stderr,strg);
  178.     exit();
  179. }
  180.