home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / compiler / small_c / cb / sources / cpy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-08-18  |  3.3 KB  |  143 lines

  1.  
  2. /*
  3. ** cpy.c -- copy named files to standard output
  4. **
  5. ** Copyright 1982 J. E. Hendrix.  All rights reserved.
  6. */
  7. #include <stdio.h>
  8. #include "tools.h"
  9. #define NOCCARGC
  10. #define MAXARG 12
  11. int fin, i, j;
  12. int status;
  13. int binary, striplf, stripcr, from, to;
  14. char name[MAXFN], inclext[MAXFN];
  15. main(argc, argv) int argc, *argv; {
  16.   auxbuf(stdout, 4096);
  17.   doargs(argc, argv);
  18.   if((binary==YES)&(inclext[0]!=NULL)) {
  19.     fputs("cannot include files during binary copy", stderr);
  20.     abort(7);
  21.     }
  22.   fin=99;
  23.   i=0;
  24.   while(getarg(++i, name, MAXFN, argc, argv)!=EOF) {
  25.     if((name[0]=='-')|(name[0]==EXTMARK)) continue;
  26.     if((fin=fopen(name, "r"))==NULL) cant(name);
  27.     if(binary) bcopy(fin, stdout);
  28.     else       fcopy(fin, stdout);
  29.     fclose(fin);
  30.     }
  31.   if(fin==99) {
  32.     if(binary) bcopy(stdin, stdout);
  33.     else       fcopy(stdin, stdout);
  34.     }
  35.   fclose(stdout);
  36.   }
  37.  
  38. doargs(argc, argv) int argc, *argv; {
  39.   int len;
  40.   char arg[MAXARG], error;
  41.   inclext[0]=from=to=NULL;
  42.   binary=striplf=stripcr=error=NO;
  43.   i=0;
  44.   while(getarg(++i, arg, MAXARG, argc, argv)!=EOF) {
  45.     if(arg[0]==EXTMARK) {
  46.       j=0;
  47.       while(inclext[j]=arg[j]) ++j;
  48.       continue;
  49.       }
  50.     if(arg[0]!='-') continue;
  51.     if(arg[2]==NULL) {
  52.       if(same(arg[1], 'b')) binary=YES;
  53.       else error=YES;
  54.       }
  55.     else if(arg[4]==NULL) {
  56.       if(same(arg[1], 'n')) {
  57.         if(same(arg[2], 'c') & same(arg[3], 'r'))
  58.           stripcr=binary=YES;
  59.         else if(same(arg[2], 'l') & same(arg[3], 'f'))
  60.           striplf=binary=YES;
  61.         else error=YES;
  62.         }
  63.       else error=YES;
  64.       }
  65.     else if(same(arg[1], 't')) {
  66.       binary=YES;
  67.       len=utoi(arg+2, &from);
  68.       if((len<1)|(arg[len+2]!=',')) error=YES;
  69.       else {
  70.         j=len+3;
  71.         len=utoi(arg+j, &to);
  72.         if((len<1)|(arg[len+j]!=NULL)) error=YES;
  73.         }
  74.       }
  75.     else error=YES;
  76.     if(error) {
  77.       fputs("usage: CPY [file]... [.?] [-B] [-NCR] [-NLF] [-T#,#]\n", stderr);
  78.       abort(7);
  79.       }
  80.     }
  81.   }
  82.  
  83. bcopy(in, out) int in, out; {
  84.   char c[1];
  85.   while(YES) {
  86.     status=read(in, c, 1);
  87.     if(status==0) break;
  88.     if(isatty(in)&(c[0]==4)) break;
  89.     if(status < 1) {
  90.       fputs("input error\n", stderr);
  91.       fclose(out);
  92.       abort(7);
  93.       }
  94.     if((c[0]==CR)&(stripcr)) continue;
  95.     if((c[0]==LF)&(striplf)) continue;
  96.     if((from!=to)&((c[0]&255)==from)) c[0]=to;
  97.     status=write(out, c, 1);
  98.     if(status < 1) {
  99.       fputs("output error\n", stderr);
  100.       fclose(out);
  101.       abort(7);
  102.       }
  103.     poll(YES);
  104.     }
  105.   }
  106.  
  107. fcopy(in, out) int in, out; {
  108.   int i, loc, in2;
  109.   char buf[MAXLINE+1], str[MAXLINE+1];
  110.   while(fgets(buf, MAXLINE+1, in)!=NULL) {
  111.     poll(YES);
  112.     if(inclext[0]==NULL) {
  113.       sout(buf, out);
  114.       continue;
  115.       }
  116.     loc=0;
  117.     getwrd(buf, &loc, str);
  118.     if((lexcmp(str, "#include")!=0) &&
  119.        (lexcmp(str, ".so")!=0)) {
  120.       sout(buf, out);
  121.       continue;
  122.       }
  123.     getwrd(buf, &loc, str);
  124.     strip(str);
  125.     i=0;
  126.     while((str[i]!=EXTMARK) && str[i]) ++i;
  127.     if(inclext[1] && lexcmp(str+i, inclext)) {
  128.       sout(buf, out);
  129.       continue;
  130.       }
  131.     if((in2=fopen(str, "r"))==NULL) cant(str);
  132.     fcopy(in2, out);
  133.     fclose(in2);
  134.     }
  135.   }
  136.  
  137. #include "out.c"
  138. #include "cant.c"
  139. #include "same.c"
  140. #include "strip.c"
  141. #include "getwrd.c"
  142.  
  143.