lolamontes69

K+R Exercise7_8

Sep 25th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.81 KB | None | 0 0
  1. /* Write a program to print a set of files, starting each new one on a new page,
  2.  * with a title and a running page count for each file *
  3.  *
  4.  * maybe have a fixed page length (fill with blanks to end)
  5.  *
  6.  * if no -s flag and only one arg print file to stdout
  7.  *
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #define MAXLINE 100
  14. #define MAXCHAR 100
  15.  
  16. void printpageheader(char *line, FILE *iop);
  17. char *fgets1(char *s, int n, FILE *iop);
  18. int fputs1(char *s, FILE *iop);
  19.  
  20. main(int argc, char *argv[]) /* argv[n] doesnt overflow */
  21. {
  22.     char line[MAXLINE];
  23.     char *prog = argv[0];
  24.     char *filename;
  25.     FILE *fp;
  26.     FILE *fp1;
  27.     int c, screen = 0, pagenumber = 0, filewrite = 0, linenum = 5;
  28.     /* screen is to print to stdout */
  29.     while(--argc > 0 && (*++argv)[0] == '-')
  30.         while(c = *++argv[0])
  31.             switch(c) {
  32.             case 's':
  33.                 screen = 1;
  34.                 break;
  35.             case 'w':
  36.                 filewrite = 1;
  37.                 break;
  38.             case 'h':
  39.                 puts("Usage   : fileutil1 -s|w|h file1 file2 ...\n");
  40.                 puts("Examples: fileutil1 -w Output.txt Input1.txt Input2.txt");
  41.                 puts("          fileutil1 -s Input1.txt Input2.txt");
  42.                 puts("Options :");
  43.                 puts("      -s  print files to stdout");
  44.                 puts("      -w  print files to outputfile");
  45.                 puts("      -h  print this help menu\n");
  46.                 puts("Report bugs to <[email protected]>.");
  47.                 exit(0);
  48.             default:
  49.                 printf("fileutil1: illegal option %c\n", c);
  50.                 argc = 0;
  51.                 break;
  52.             }
  53.     if(argc < 1 || (!filewrite && !screen)) {
  54.         puts("Usage: fileutil1 -s|w|h file1 file2 ...\n");
  55.         exit(0);
  56.     } else if(argc==1) {
  57.             if(screen) {
  58.                 if((fp = fopen(*argv, "r")) == NULL) {   /* first open the FILE object */
  59.                     fprintf(stderr, "%s: can't open %s\n", prog, *argv);
  60.                     exit(1);
  61.                 } else fp1 = stdout;
  62.             } else {
  63.                 fp = stdin;
  64.                 fp1 = fopen(*argv, "w");
  65.             }
  66.             filename = *argv;
  67.             pagenumber += 1;
  68.             sprintf(line,"## File: %-60spage %3d ##\n",filename,pagenumber);
  69.             printpageheader(line, fp1);
  70.             while(fgets1(line, MAXCHAR, fp)) {
  71.                 fputs1(line, fp1);
  72.                 linenum +=1;
  73.                 if(linenum==24) {
  74.                     pagenumber+=1;
  75.                     sprintf(line,"## File: %-60spage %3d ##\n",filename,pagenumber);
  76.                     printpageheader(line, fp1);
  77.                     linenum=0;
  78.                 }
  79.             }
  80.             puts(" ");
  81.             exit(0);
  82.        
  83.     } else if(argc>1) {
  84.         if(screen)
  85.             fp1 = stdout;
  86.         else {
  87.             fp1 = fopen(*argv++, "w");
  88.             argc--;
  89.         }
  90.         for( ; argc>0; argc--) {
  91.             linenum = 5;
  92.             pagenumber += 1;
  93.             if((fp = fopen(*argv, "r")) == NULL) {  
  94.                 fprintf(stderr, "%s: can't open %s\n", prog, *argv);
  95.                 exit(1);
  96.             } else {
  97.                 filename = *argv;
  98.                 sprintf(line,"## File: %-60spage %3d ##\n",filename,pagenumber);
  99.                 printpageheader(line, fp1);
  100.                 while(fgets1(line, MAXCHAR, fp)) {
  101.                     fputs1(line, fp1);
  102.                     linenum +=1;
  103.                     if(linenum==30) {
  104.                         pagenumber+=1;
  105.                         sprintf(line,"## File: %-60spage %3d ##\n",filename,pagenumber);
  106.                         printpageheader(line, fp1);
  107.                         linenum=5;
  108.                     }
  109.                 }
  110.             }
  111.             for( ; linenum<=30; linenum++)
  112.                 fputs1("\n", fp1);
  113.             *argv++;
  114.         }
  115.         fclose(fp1);
  116.     }
  117.     puts(" ");
  118.     exit(0);
  119. }
  120.  
  121. /* printpageheader: print title and page number at top of each page */
  122. void printpageheader(char *line, FILE *iop)
  123. {
  124.     fputs1("################################################################################\n",iop);
  125.     fputs1(line,iop);
  126.     fputs1("################################################################################\n\n",iop);
  127. }
  128.  
  129. /* fgets1: get at most n chars from iop */
  130. char *fgets1(char *s, int n, FILE *iop)
  131. {
  132.     register int c;
  133.     register char *cs;
  134.  
  135.     cs = s;
  136.     while(--n > 0 && (c = getc(iop)) != EOF)
  137.         if((*cs++ = c) == '\n')
  138.             break;
  139.     *cs = '\0';
  140.     return (c == EOF && cs == s) ? NULL : s;
  141. }
  142.  
  143. /* fputs1: put string s on file iop */
  144. int fputs1(char *s, FILE *iop)
  145. {
  146.     int c;
  147.  
  148.     while(c = *s++)
  149.         putc(c, iop);
  150.     return ferror(iop) ? EOF : 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment