home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / simtel / sigm / vols200 / vol228 / split.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-02-12  |  2.5 KB  |  187 lines

  1.  
  2. /*
  3. **    split.c
  4. **             public domain use only 
  5. **
  6. **    copyright 1984  Mark Ellington
  7. **
  8. **    This program splits a large file into two smaller files. 
  9. **    All input is on the command line;  the last argument is the
  10. **    number of bytes to go into the first output file.  The remainder
  11. **    of the input file goes into the second output file.  Study the code
  12. **    to see how this works. 
  13. **
  14. */
  15.  
  16. /*   split file into two smaller files  
  17. */
  18.  
  19.  
  20. #include printf.c
  21.  
  22.  
  23. int fptr, f1ptr, f2ptr;
  24. char temp[30];
  25. static char ins[100];
  26. int nbytes;
  27.  
  28. main(argc,argv)
  29. int argc; char *argv[];
  30. {
  31. char *s, *sc;
  32. char sv[3];
  33. char c;
  34.  
  35.     if (argc != 5) {
  36.         printf("\nusage: split [infilename.ext] "); 
  37.         printf("[outfile1.ext] [outfile2.ext] [# bytes]");
  38.                 exit();
  39.     }
  40.  
  41.     if ((fptr = fopen(argv[1],"r")) == 0) {
  42.         printf("\nCan't open %s\n",argv[1]);
  43.         exit();
  44.     }
  45.     printf("\n%s open to read",argv[1]);
  46.  
  47.     if ((f1ptr = fopen(argv[2],"w")) == 0) {
  48.         printf("\nCan't open %s\n",argv[2]);
  49.         exit();
  50.     }
  51.     printf("\n%s open to write\n",argv[2]);
  52.  
  53.     if ((f2ptr = fopen(argv[3],"w")) == 0) {
  54.         printf("\nCan't open %s\n",argv[3]);
  55.         exit();
  56.     }
  57.     printf("\n%s open to write\n",argv[3]);
  58.  
  59.     if (atoi(argv[4]) <= 0) {
  60.         printf("\n# bytes in first segment entered incorrectly");
  61.         exit();
  62.     }  
  63.  
  64.     filter(f1ptr,atoi(argv[4]));
  65.  
  66.     filter(f2ptr,30000);            
  67.  
  68.     printf("\n\nExiting split\n");
  69.  
  70.     fclose(fptr);
  71.     fclose(f1ptr);
  72.     fclose(f2ptr);
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  
  79. filter(ptr,nb)
  80. int ptr; int nb;
  81. {
  82.  
  83.     char *sptr;
  84.         int n;
  85.     int sumbytes = 0;
  86.  
  87.     while (fgets(fptr,ins) != 0) {
  88.             
  89.         for (n=0;n<100;n++) {
  90.                         ++sumbytes;
  91.                     if (ins[n] == '\0') break;
  92.         } 
  93.                     
  94.         puts(ins);
  95.         fputs(ptr,ins);
  96.         if (sumbytes >= nb) break;
  97.  
  98.     }
  99.  
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106. fgets(f,s)
  107. int f; char *s;
  108. {
  109.     char ch;
  110.  
  111.     while ((ch = getc(f)) != -1) {
  112.         *s++ = ch;
  113.         if (ch == '\n') {
  114.             *s = '\0';
  115.             return(1);
  116.         }
  117.     }
  118.     return(0);
  119. }    
  120.     
  121.  
  122.  
  123.  
  124. puts(s)
  125. char *s;
  126. {
  127.     while (*s) putchar(*s++);
  128. }
  129.  
  130.  
  131.  
  132.  
  133. fputs(f,s)
  134. int f; char *s;
  135. {
  136.     while(*s) putc(*s++,f);
  137. }
  138.  
  139.  
  140.  
  141. strcat(s1,s2)     /* concatenate two strings */
  142. char *s1, *s2;
  143. {
  144.     static char t[20]; 
  145.     char *tp;
  146.     tp = t;
  147.  
  148.     while (*s1) {
  149.         *tp++ = *s1++;
  150.     }
  151.     
  152.     do *tp++ = *s2; while (*s2++);
  153.  
  154.     return(t);
  155.  
  156. }
  157.  
  158.  
  159.  
  160. atoi(n) /* convert ascii string to integer */
  161. char *n;
  162. {
  163.     int val; 
  164.     char c;
  165.     int sign;
  166.     val=0;
  167.     sign=1;
  168.     while ((c = *n) == '\t' || c== ' ') ++n;
  169.     if (c== '-') {sign = -1; n++;}
  170.     while (isdigit(c = *n++)) val = val * 10 + c - '0';
  171.     return(sign*val);
  172. }
  173.  
  174.  
  175. isdigit(c)
  176. char c;
  177. {
  178.     return(c >= '0' && c <= '9');
  179. }
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.