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

  1. /*-----------------------------------------------------------------*/
  2. /*  FILE NAME:      listfile                                       */
  3. /*  VERSION:        1.2                                            */
  4. /*  WRITTEN:        6th July, 1986                                 */
  5. /*  MODIFIED:       13th July, 1986                                */
  6. /*  --------        14th July, 1986                                */
  7. /*  COPYRIGHT:      Cogar Computer Services Pty. Ltd.              */
  8. /*                                                                 */
  9. /*  NOTE:           This programme is specifically written for     */
  10. /*  ----            BDS 'C' running under CP/M 2.2 and may need    */
  11. /*                  to be modified for other C's.                  */
  12. /*                                                                 */
  13. /*  PURPOSE:        Will list a text file on the line printer      */
  14. /*  -------         with page numbers and headings (the file name) */
  15. /*                  with a standard page length of 60 printed      */
  16. /*                  lines.   Note the equates may need to be       */
  17. /*                  changed if you are using 66-line paper instead */
  18. /*                  of A4, which allows 70 lines per page.         */
  19. /*                                                                 */
  20. /*  FUNCTIONS:      LISTC         Found in DEFF3                   */
  21. /*  ---------       ENTAB                                          */
  22. /*                                                                 */
  23. /*-----------------------------------------------------------------*/
  24. #include <bdscio.h>    /* See note, above                         */
  25. #include <pec.h>    /* Required for this file                  */
  26. /*-----------------------------------------------------------------*/
  27. #define VERSION "1.2"    /* The current Version No.                 */
  28. /*-----------------------------------------------------------------*/
  29. /*  SPECIFIC CONSTANTS                                             */
  30. /*-----------------------------------------------------------------*/
  31. #define SECTORS 64    /* Sectors in an 8K printer buffer         */
  32. #define PAGE_LENGTH 70    /* For A4 paper                            */
  33. #define PAGE_TOP 5    /* No. of lines to leave after header      */
  34. #define PAGE_BOTTOM 10    /* No. of lines to skip after last line    */
  35. #define    LINES PAGE_LENGTH-(PAGE_TOP+PAGE_BOTTOM) /* lines per page */
  36. #define EOF 26        /* CP/M end of file (?)  CONTROL-Z         */
  37. /*-----------------------------------------------------------------*/
  38.  
  39.  
  40. main(argc, argv)
  41. char *argv[];
  42. short argc;
  43. {
  44.     short i, j, k, n, page_no;    /* counters                */
  45.     short TAB_WIDTH;
  46.     TAB_WIDTH = 8;        /* Normal tab size                 */
  47.     int c;                /* for listing             */
  48.     char dma[128];    /* The DMA buffer for BDOS functions       */
  49.     char printer_buf[SECTORS*128]; /* The printer buffer       */
  50.     char *list_char;    /* Pointer to listing character    */
  51.     char file_name[15];
  52.     char type[4];        /* To see what is the file type.   */
  53.     short file;    /* The file descriptor                     */
  54. /*-----------------------------------------------------------------*/
  55. /*    The programme starts here                                  */
  56. /*    First check to see whether anything was entered from the   */
  57. /*      console as we need a specific file name of the form -      */
  58. /*                                                                 */
  59. /*        [d:]filename                                       */
  60. /*                                                                 */
  61. /*    where "d:" is the optional drive name.                     */
  62. /*-----------------------------------------------------------------*/
  63.     pec_clear();    /* clear the screen                        */
  64.     header();
  65.     
  66.     if(argc < 2) {
  67.     printf("We need to know the name of the file you want to\
  68.  list.\n");
  69.     printf("Give this in the form - \n\n");
  70.     printf("          [d:]filename\n\n");
  71.     printf("where 'd:' is an optional drive name and filename is\
  72.  specific.\n\n");
  73.     gets(up_str(file_name));
  74.     }
  75.     
  76.     else if(argc > 2) {
  77.     printf("Too many parameters given.   Only the filename is\
  78.  required.\n");
  79.     printf("Give this in the form - \n\n");
  80.     printf("          [d:]filename\n\n");
  81.     printf("where 'd:' is an optional drive name and filename is\
  82.  specific.\n");
  83.     exit();
  84.     }
  85.     
  86.     else strcpy(file_name, argv[1]);
  87.  
  88. /*-----------------------------------------------------------------*/
  89. /*  A file name has been obtained but, as this programme is only   */
  90. /*  ASCII files, we should at least check for things like "COM"    */
  91. /*  and "CRL" and "HEX" files.   Terminate the programme if any    */
  92. /*  of these is found to have been specified.                      */
  93. /*-----------------------------------------------------------------*/
  94.     i = j = 0;
  95.     while((c = file_name[i]) != '.' && i < 15)
  96.         i++;
  97.     i++;        /* Skip over the period                    */
  98.     while(c != '\0' && i < 15)
  99.     {
  100.         type[j] = file_name[i];
  101.         i++;
  102.         j++;
  103.     }
  104.  
  105.     if(!strcmp("COM",type))
  106.         message4(type);
  107.     else if(!strcmp("CRL",type))
  108.         message4(type);
  109.     else if(!strcmp("HEX",type))
  110.         message4(type);
  111.     else printf("\nNow listing -  %s", file_name);
  112.  
  113. /*  Now have a file name nominated so we should open this file     */
  114.  
  115.     if((file = open(file_name, 0)) != -1)
  116.         list_char = printer_buf;    /* point to buffer */
  117.     else message3();
  118.  
  119. /*  File has been opened, so print the page top                    */
  120.  
  121.     page_no = 1;        /* Starting value                  */
  122.     put_top(file_name,page_no);
  123.     n = 0;    /* Zero the line counter                           */
  124.     k = 0;    /* Zero line-character counter                     */
  125.     
  126.     while((i = read(file,printer_buf,SECTORS)) != 0)
  127.     {
  128.         j = 0;    /* Zero the character counter              */
  129.  
  130.         while((c = *list_char) != EOF && j < i*128)
  131.         {
  132.             k++;    /* Increment character counter     */
  133.             if( c == HT)
  134.                 k = entab(k, TAB_WIDTH);
  135.             else listc(c);
  136.             if(c == '\n')
  137.             {
  138.                 n++;
  139.                 k = 0;
  140.             }
  141. /*  Check for the end of page and go to next, if so                */
  142.  
  143.             if(n == LINES)
  144.             {
  145.                 put_bottom();
  146.                 n = 0;    /* Re-set the counters     */
  147.                 k = 0;
  148.                 page_no = page_no + 1;
  149.                 put_top(file_name,page_no);
  150.             }
  151.             list_char++;
  152.             j++;
  153.         if( k == 80)
  154.             k = 0;    /* Re-set the counter             */
  155.         }
  156.     list_char = printer_buf;    /* Re-set the pointer      */
  157.     }
  158.     close(file);
  159.     if(n < LINES)
  160.         last_page(n);
  161.     else printf("\nListing finished, return to CP/M.");
  162. }
  163. /*----------------------END OF MAIN ROUTINE------------------------*/
  164. /*                                                                 */
  165. /*  SUBROUTINES USED IN THIS PROGRAMME                             */
  166. /*-----------------------------------------------------------------*/
  167. header()
  168. {
  169.     printf("File listing programme - LISTFILE Version  %s\n\
  170. by Cogar Computer Services Pty. Ltd.\n", VERSION);
  171.      printf("Copyright 1986 - all rights reserved.\n\n");
  172. }
  173. /*-----------------------------------------------------------------*/
  174. message3()
  175. {
  176.     printf("\nUnable to open the file.");
  177.     exit();
  178. }
  179. /*-----------------------------------------------------------------*/
  180. message4(str)
  181. char str[4];
  182. {
  183.     printf("\nThis programme is unable to handle  %s  files.", str);
  184.     exit();
  185. }
  186. /*-----------------------------------------------------------------*/
  187. put_top(name,count)
  188. char *name;
  189. short count;
  190. {
  191.     short i;        /* Name counter                    */
  192.     int c;        /* To print out name                       */
  193.  
  194.     for(i = 0; c != '\0'; i++)
  195.         listc(c = name[i]);    /* Print file name         */
  196.     for(i = 0; i < (80 - strlen(name)) - 20; i++)
  197.         listc(SPACE);
  198.     lists("Page: \0");
  199.     listd(count);
  200.     for(i = 0; i < 5; i++)
  201.     {
  202.         listc(CR);    /* Space down 5 lines              */
  203.         listc(LF);
  204.     }
  205. }
  206. /*-----------------------------------------------------------------*/
  207. put_bottom()
  208. {
  209.     short i;
  210.     for(i = 0; i < PAGE_BOTTOM; i++)
  211.     {
  212.         listc(CR);    /* Space down five lines           */
  213.         listc(LF);
  214.     }
  215. }
  216. /*-----------------------------------------------------------------*/
  217. last_page(line_count)
  218. short line_count;
  219. {
  220.     short z;
  221.     listc(CR);
  222.  
  223.     for(z = 0; z < (LINES - line_count + PAGE_BOTTOM); z++)
  224.     {
  225.         listc(LF);    /* Space down to bottom of page    */
  226.     }
  227.     listc(LF);
  228.     printf("\n\nListing finished.  Return to CP/M.");
  229.     exit();
  230. }
  231. /*----------------------END OF SUBROUTINES-------------------------*/
  232. ter      */
  233.     }
  234.     close(file);
  235.     if(n < LINES)
  236.         last_page(n);
  237.     else printf("\nListing