home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 143_01 / head.c < prev    next >
Text File  |  1985-11-14  |  3KB  |  136 lines

  1. /*
  2. %CC1 $1.C -X -E5000
  3. %CLINK $1 DIO WILDEXP -S
  4. %DELETE    $1.CRL 
  5. */
  6. /*********************************************************************
  7. *                HEAD                     *
  8. **********************************************************************
  9. *           COPYRIGHT 1983 EUGENE H. MALLORY             *
  10. *********************************************************************/
  11. #include "BDSCIO.H"
  12. #include "DIO.H"
  13.  
  14. int str_comp();
  15.  
  16. main(argc,argv)
  17.  
  18. int argc;
  19. char **argv;
  20.  
  21. BEGIN
  22.  
  23. int fid,c,i,pl;
  24. char fcb[BUFSIZ],doneflag,fname[MAXLINE],string[MAXLINE];
  25. int ii,jj,optionerr;
  26. char *ss;
  27.  
  28. dioinit(&argc,argv); /*    INITIALIZE DIO BUFFERS AND FLAGS */
  29.  
  30. /*********************************************************************
  31. ***          ARGUMENT PROCESSING                   ***
  32. *********************************************************************/
  33.  
  34.     pl = 10;
  35.     optionerr = FALSE;
  36.     
  37.     for (ii=argc-1;ii>0;ii--)
  38.         if (argv[ii][0] == '-')
  39.         BEGIN
  40.             for    (ss = &argv[ii][1]; *ss    != '\0';)
  41.             BEGIN
  42.                 switch (toupper(*ss++))
  43.                 BEGIN
  44.                     case 'L':
  45.                     pl= atoi(ss);
  46.                     break;
  47.                     case 'H':
  48.                     optionerr = TRUE;
  49.                     break;
  50.                     default:
  51.                     typef("HEAD: Illegal option %c.\n"
  52.                         ,*--ss);
  53.                     ss++;
  54.                     optionerr = TRUE;
  55.                     break;
  56.                 END
  57.                 while (isdigit(*ss)) ss++;
  58.             END
  59.             for    (jj=ii;jj<(argc-1);jj++) argv[jj] = argv[jj+1];
  60.             argc--;
  61.         END
  62.     
  63.  
  64.     if (optionerr) 
  65.         BEGIN
  66.         typef("HEAD:  Legal options are:\n");
  67.         typef("-Ln    Length of    head to    output.\n");
  68.         exit(0);
  69.         END
  70.         
  71.     if (pl < 1) error("HEAD: Length     less than 1.");
  72.     
  73. /*********************************************************************
  74. ***             END OF ARGUMENT PROCESSING               ***
  75. *********************************************************************/
  76. wildexp(&argc,&argv);
  77.  
  78. #ifdef C80
  79. qsort(&argv[1],argc-1,2,str_comp);
  80. #else
  81. qsort(&argv[1],argc-1,2,&str_comp); 
  82. #endif
  83.         
  84. while (TRUE)
  85.     BEGIN
  86.     doneflag = TRUE;
  87.     if (--argc > 0)
  88.         BEGIN
  89.         strcpy(fname,*++argv);
  90.         doneflag = FALSE;
  91.         END
  92.     else if    (DIOIN)    
  93.         BEGIN
  94.         doneflag = getstring(fname);
  95.         if (strlen(fname) == 0)    goto done;
  96.         fname[strlen(fname)-1] = 0;
  97.         END
  98.     if (doneflag) goto done;
  99.     if (strlen(fname) == 0)    goto newfile;
  100.     fid = fopen(fname,fcb);
  101.     if (fid    <= 0)
  102.         BEGIN
  103.         printf("Unable to open file \'%s\'.\n",fname);
  104.         goto newfile;
  105.         END
  106.     printf("FILE: %s\n\n",fname);
  107.     for (i=1;i<=pl;i++)
  108.         BEGIN
  109.         if (fgets(string,fcb)) puts(string);
  110.         else break;
  111.         END
  112.     printf("\n\n");
  113. newfile:
  114.     fclose(fcb);
  115.     END
  116. done:
  117.     dioflush();
  118.     exit(0);
  119. END
  120.  
  121. int str_comp(s,t)
  122. char **s, **t;
  123. BEGIN
  124.     char *s1, *t1;
  125.     int    i;
  126.     s1 = *s;
  127.     t1 = *t;
  128.     for (i=0;i<MAXLINE;i++)
  129.         BEGIN
  130.         if (t1[i] != s1[i]) 
  131.             return s1[i] - t1[i];
  132.         if (s1[i] == '\0')  return 0;
  133.         END
  134.     return 0;
  135. END
  136.