home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff284.lzh / ARPTools / src / Head.c < prev    next >
C/C++ Source or Header  |  1989-11-27  |  4KB  |  212 lines

  1. /*
  2.           Head - Display the first lines of a file.
  3.  
  4.           Original effort by Fabio Rossetti. Inspired by the head program
  5.           by Gary Brant found on <>< 179.
  6.  
  7.       (c) 1989 by Fabio Rossetti.
  8.  
  9.       To compile under Lattice C v5.0x use:
  10.  
  11.         lc  -O -v -cus head
  12.         blink lib:cres.o head.o to head lib lib:a.lib lib:lc.lib sd nd
  13.  
  14. */
  15.  
  16. #include <exec/types.h>
  17. #include <exec/memory.h>
  18. #include <exec/libraries.h>
  19. #include <libraries/dos.h>
  20. #include <libraries/dosextens.h>
  21. #include <libraries/arpbase.h>
  22. #include <arpfunctions.h>
  23. #include <proto/exec.h>
  24. #include <proto/dos.h>        
  25.  
  26.  
  27. struct ArpBase *ArpBase=NULL;
  28. struct Process *Pr;
  29. LONG argc;
  30.  
  31. #define NARGS 4
  32. #define BFSIZE 256    
  33. #define FROM    argv[0]
  34. #define TO    argv[1]
  35. #define LIN    argv[2]
  36. #define BAN    argv[3]
  37. STRPTR argv[NARGS];
  38.  
  39. TEXT *Buf,*OBuf;
  40. TEXT Banner[256];
  41.  
  42. struct UserAnchor {
  43.     struct    AnchorPath    ua_AP;
  44.     BYTE    moremem[255];    /* extension */
  45. };
  46. struct    UserAnchor *Anchor=NULL;
  47.  
  48. /* Trick to keep code down to size */
  49. VOID MemCleanup()
  50. {
  51. }
  52.  
  53. /* shutdown routine */
  54. VOID Cleanup(r1,r2,msg)
  55. LONG r1,r2;
  56. STRPTR msg;
  57. {
  58.     if (msg) Puts(msg);
  59.     if (ArpBase) CloseLibrary((struct Library *)ArpBase);
  60.     Pr->pr_Result2 = r2;
  61.     exit(r1);
  62. }
  63.  
  64. VOID Head(Inp,Out,lines)
  65. BPTR Inp,Out;
  66. ULONG lines;
  67.  
  68. {
  69.  
  70.     REGISTER TEXT c;
  71.     REGISTER ULONG count,l=0,fl=0,i,obp=0;
  72.  
  73.     /* read until EOF */
  74.     while(count = Read(Inp,Buf,BFSIZE))    {
  75.  
  76.         /* check break */
  77.         if (SetSignal(0,0) & SIGBREAKF_CTRL_C) 
  78.             Cleanup(RETURN_WARN,NULL,"***Break");
  79.  
  80.         /* scan buffer */    
  81.         for(i=0; i < count; i++) {
  82.     
  83.             c = *(Buf+i);
  84.     
  85.             *(OBuf + obp++) = c;
  86.  
  87.             if (obp >= BFSIZE) {
  88.                 (VOID)Write(Out,OBuf,obp);
  89.                 obp = 0;}
  90.  
  91.             /* end of line ? */    
  92.             if (c == '\n' ) {
  93.                     l++;
  94.                     if (l >= lines) {
  95.                     if (obp) (VOID)Write(Out,OBuf,obp);
  96.                     obp = 0;
  97.                     fl=1;
  98.                     break;
  99.                     }
  100.                 }
  101.             }
  102.  
  103.     if (fl) break;
  104.     }
  105.     
  106.     if (obp) (VOID)Write(Out,OBuf,obp);
  107. }
  108.  
  109. VOID _main(Line)
  110. STRPTR Line;
  111. {
  112.  
  113.  
  114.  
  115.     BPTR infh,outfh;
  116.     LONG Lines,Result,i=0;
  117.     char **ArV;
  118.     Pr = (struct Process*)FindTask(NULL);
  119.     
  120.     if(!(ArpBase = (struct ArpBase*)OpenLibrary(ArpName,ArpVersion)))
  121.             Cleanup(RETURN_FAIL,ERROR_INVALID_RESIDENT_LIBRARY,NULL);
  122.     
  123.     /* parse command line */
  124.     for (argc=0;argc < NARGS ;++argc)
  125.         argv[argc] = (STRPTR) NULL;
  126.  
  127.     while(*Line > ' ')
  128.         ++Line;
  129.  
  130.     if((argc = GADS(++Line,
  131.         strlen(Line),
  132.         "Usage: Head [Files file1 file2 ...] [TO filename] [LIN linenumber] [BAN]",
  133.         argv,
  134.         "Files/...,TO/K,LIN/K,BAN/S" )) < 0)
  135.             Cleanup(RETURN_WARN,NULL,FROM);
  136.  
  137.  
  138.         if (!(Buf = ArpAllocMem(BFSIZE,MEMF_CLEAR)) ||
  139.         !(OBuf = ArpAllocMem(BFSIZE,MEMF_CLEAR)))
  140.                 Cleanup(RETURN_FAIL,ERROR_NO_FREE_STORE,"Error: no memory");
  141.  
  142.  
  143.     
  144.     if (LIN) {
  145.         Lines = Atol(LIN);
  146.         if((Errno == ERRBADINT) || (Lines <=0))
  147.             Cleanup(RETURN_ERROR,NULL,"Bad args");
  148.         }
  149.     else Lines = 10;
  150.  
  151.     if(TO) {
  152.             if(!(outfh=ArpOpen(TO,MODE_NEWFILE))) {
  153.                 Printf("Can't open %s\n",TO);
  154.                 Cleanup(RETURN_ERROR,ERROR_OBJECT_NOT_FOUND,NULL);
  155.                 }
  156.             }
  157.     else outfh = Output();
  158.  
  159.  
  160.     ArV = (char **)FROM;
  161.     if (ArV[0]) {
  162.  
  163.     if ( Anchor = (struct UserAnchor *)ArpAlloc( (ULONG)sizeof( *Anchor )) )
  164.     {
  165.         Anchor->ua_AP.ap_Length = 255;    /* Want full path built */
  166.         
  167.     }
  168.     else
  169.         Cleanup(RETURN_FAIL,ERROR_NO_FREE_STORE,"Error: no memory");
  170.     
  171.     while (ArV[i] != NULL) {
  172.  
  173.     Result = FindFirst(ArV[i],(struct AnchorPath*) Anchor);
  174.  
  175.     while (Result == 0) {
  176.  
  177.         if (Anchor->ua_AP.ap_Info.fib_DirEntryType < 0) {
  178.             if(!(infh=ArpOpen(Anchor->ua_AP.ap_Buf,MODE_OLDFILE))) {
  179.                 Printf("Can't open %s\n",FROM);
  180.                 Cleanup(RETURN_ERROR,ERROR_OBJECT_NOT_FOUND,NULL);
  181.                     }
  182.             else {
  183.                 if (BAN) {
  184.                     PathName(ArpLock(Anchor->ua_AP.ap_Buf,
  185.                     ACCESS_READ),Banner,255);
  186.                     FPrintf(outfh,"*** File %s ***\n",
  187.                     Banner);
  188.                     }
  189.             Head(infh,outfh,Lines);
  190.             }
  191.             }
  192.         else {
  193.             Printf("%s is a directory !\n",ArV[i]);
  194.             Cleanup(RETURN_ERROR,ERROR_OBJECT_WRONG_TYPE,NULL);
  195.             }
  196.         /*else 
  197.             Printf("Can't find %s\n",FROM);
  198.             Cleanup(RETURN_ERROR,ERROR_OBJECT_NOT_FOUND,NULL);
  199.             */
  200.  
  201.         Result = FindNext((struct AnchorPath*) Anchor );    
  202.         }
  203.         i++;
  204.         }
  205.     }
  206.     
  207.     else Head(Input(),outfh,Lines);
  208.  
  209.     Cleanup(NULL,NULL,NULL);
  210.  
  211.  
  212. }