home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / lambda / soundpot / p / ut-c.lbr / SPLIT.CZ / SPLIT.C
Encoding:
C/C++ Source or Header  |  1993-10-25  |  3.5 KB  |  130 lines

  1. /* split.c -- UTOOL.  Read STDIN and write it out in n-line chunks to  files
  2.  
  3.      author: David H. Wolen
  4.      last change:   3/31/83
  5.  
  6.      usage:    split [-n] [outname]
  7.  
  8.                split <infile
  9.                split <infile -66 chunk
  10.  
  11.      options:  n       - number of  lines per file; default 100
  12.                outname - filename for output files; default "temp"
  13.                          (program assigns extentions .a ... .z).
  14.  
  15.      input:    STDIN
  16.      output:   1 to 26 files
  17.  
  18.      notes:    (1)  output files are named outname.a ... outname.z if
  19.                     outname is given as an argument.  Otherwise, they're
  20.                     named temp.a ... temp.z (on the default drive).
  21.                (2)  If the input is large, the 26th file may have n lines
  22.                     written to it before end of input is reached.  In that
  23.                     case, the rest of the input will also be
  24.                     transferred to it.
  25.                (3)  outname can't begin with "-"
  26.                (4)  outname can contain a drive prefix; e.g. a:outname
  27.  
  28.      linkage:  a:clink split -f dio -ca (uses deff3.crl)
  29. */
  30.  
  31. #include "a:bdscio.h"
  32. #include "dio.h"
  33.  
  34. #define  STDIN  0
  35. #define  DLINES 100      /* default lines per output file */
  36. #define  DNAME  "temp"   /* default name for output files */
  37.  
  38. main(argc,argv)
  39. int  argc;
  40. char *argv[];
  41. {
  42.      int  nl, i;
  43.      char fnamprfx[20], filename[20];
  44.  
  45.      dioinit(&argc,argv);
  46.  
  47.      /* process options */
  48.  
  49.      --argc; ++argv;
  50.  
  51.      if(argc == 0)      /* default nl and name */
  52.           {nl=DLINES;
  53.           strcpy(fnamprfx,DNAME);
  54.           }
  55.      else if(argc == 1 && *argv[0] == '-')   /* default name */
  56.           {nl=atoi(*argv) *(-1);
  57.           if(nl <= 0)
  58.                error("split: bad lines per file argument");
  59.           strcpy(fnamprfx,DNAME);
  60.           }
  61.      else if(argc == 1 && *argv[0] != '-')   /* default lines per  file */
  62.           {nl=DLINES;
  63.           strcpy(fnamprfx,*argv);
  64.           }
  65.      else if(argc == 2)       /* no defaults */
  66.           {nl=atoi(*argv) *(-1);
  67.           if(nl <= 0)
  68.                error("split: bad lines per file argument");
  69.           strcpy(fnamprfx,*++argv);
  70.           }
  71.      else
  72.           error("usage: split [-n] [outname]");
  73.  
  74.  
  75.      /* main logic */
  76.  
  77.      for(i=0; i < 26; i++)
  78.           {makename(fnamprfx,i,filename);
  79.           if( !spcopy(nl,filename,i))
  80.                break;
  81.           }
  82.  
  83.      dioflush();
  84. }
  85.  
  86.  
  87.  
  88. /* makename -- make filename of the form fnamprfx.c where c= a ... z  */
  89. makename(prefix,index,outname)
  90. char *prefix, *outname;
  91. int  index;
  92. {
  93.      char suffix[3], *letters;
  94.  
  95.      letters="abcdefghijklmnopqrstuvwxyz";  /* poor man's static */
  96.  
  97.      strcpy(outname,prefix);
  98.      suffix[0]='.';
  99.      suffix[1]=letters[index];
  100.      suffix[2]='\0';
  101.      strcat(outname,suffix);
  102. }
  103.  
  104.  
  105.  
  106. /* spcopy -- copy nl lines from STDIN to outname */
  107. spcopy(nl,outname,ifile)
  108. int  nl, ifile;
  109. char *outname;
  110. {
  111.      char line[MAXLINE], obuf[BUFSIZ];
  112.      int  gotit, i;
  113.  
  114.      if(fcreat(outname,obuf) == ERROR)
  115.           error("split: can't create output file");
  116.  
  117.      for(i=0; i < nl; i++)
  118.           {if(!(gotit=fgets(line,STDIN)))
  119.                break;
  120.           fputs(line,obuf);
  121.           }
  122.  
  123.      if(gotit && ifile >= 25)      /* put rest of input on last output file */
  124.           while((gotit=fgets(line,STDIN)))
  125.                fputs(line,obuf);
  126.  
  127.      fefc(obuf);
  128.      return(gotit);
  129. }
  130.