home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / DevTools / SmartPackage / Sources / bsplit / bsplit.c next >
Encoding:
C/C++ Source or Header  |  1994-04-14  |  5.2 KB  |  289 lines

  1. #ifndef lint
  2. static char* rcs_id = "$Id: bsplit.c,v 1.1 1992/09/05 15:26:05 arrouye Exp arrouye $";
  3. #endif
  4.  
  5. /*
  6.  * $Header: /tmp_mnt/users/mistral2/arrouye/sevy-src/RCS/bsplit.c,v 1.1 1992/09/05 15:26:05 arrouye Exp arrouye $
  7.  * $Revision: 1.1 $ $State: Exp $
  8.  *
  9.  * $Author: arrouye $ $Date: 1992/09/05 15:26:05 $ $Locked$
  10.  *
  11.  */
  12.  
  13. /*
  14.  * $Log: bsplit.c,v $
  15.  * Revision 1.1  1992/09/05  15:26:05  arrouye
  16.  * Initial revision
  17.  *
  18.  * Changed Fri Apr 15 07:44:19 MET DST 1994 to support multiple sizes.
  19.  
  20.  *
  21.  */
  22.  
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <ctype.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28.  
  29. #include <sys/param.h>
  30.  
  31. #ifndef CHUNK
  32. #define CHUNK    (32 * 1024)
  33. #endif
  34.  
  35. #ifndef OUT
  36. #define OUT    "bx"
  37. #endif
  38.  
  39. #ifndef OUTSUFFIX
  40. #define OUTSUFFIX    "aaa"
  41. #endif
  42.  
  43. struct blksize_elem {
  44.     int blksize;
  45.     struct blksize_elem* next;
  46. };
  47.  
  48. static void usage(pname)
  49.     char* pname; {
  50.  
  51.     fprintf(stderr,
  52.     "usage: %s [-n] [-blksize[b|k|m]] [input|- [oprefix [osuffix]]]\n", pname);
  53.     exit(1);
  54. }
  55.  
  56. static int size(sz)
  57.     char* sz; {
  58.  
  59.     int thesize;
  60.  
  61.     for (thesize = 0; *sz && isdigit(*sz); ++sz) {
  62.     thesize = thesize * 10 + *sz - '0';
  63.     }
  64.  
  65.     if (*sz) {
  66.     if (sz[1]) {
  67.         thesize = -1;
  68.     } else {
  69.         switch (islower(*sz) ? *sz : tolower(*sz)) {
  70.             case 'm':
  71.             thesize = thesize * 1024;
  72.     
  73.             case 'k':
  74.             thesize = thesize * 1024;
  75.             break;
  76.     
  77.             case 'b':
  78.             break;
  79.     
  80.             default:
  81.             thesize = -1;
  82.             break;
  83.         }
  84.         }
  85.     }
  86.  
  87.     return thesize;
  88. }
  89.  
  90. static int bsplit(pname, ifile, oprefix, osuffix, blksz, alphabetic)
  91.     char* pname;
  92.     char* ifile;
  93.     char* oprefix;
  94.     char* osuffix;
  95.     struct blksize_elem* blksz;
  96.     int alphabetic; {
  97.  
  98.     char outfile[MAXPATHLEN]; 
  99.     char* outletter;
  100.  
  101.     char* pool;
  102.     int ninpool;
  103.     int res;
  104.  
  105.     int filenum = 1;
  106.     
  107.     int fd;
  108.     int fmdsk;
  109.  
  110.     int size = 0;
  111.     
  112.     struct blksize_elem sentinel;
  113.     
  114.     sentinel.next = blksz, blksz = &sentinel;
  115.     
  116.     if (strcmp(ifile, "-")) {
  117.         fd = open(ifile, O_RDONLY);
  118.         if (fd == -1) {
  119.         fprintf(stderr, "%s: cannot open %s\n", pname, ifile);
  120.         res = 4;
  121.         goto end;
  122.         }
  123.     } else {
  124.     fd = 0;
  125.     }
  126.  
  127.     if (alphabetic) {
  128.         strcpy(outfile, oprefix);
  129.         strcat(outfile, osuffix);
  130.         outletter = outfile + (strlen(outfile) - 1);
  131.         --*outletter;
  132.     }
  133.     
  134.     do {
  135.     int out;
  136.     int num;
  137.  
  138.     if (blksz->next) {
  139.         blksz = blksz->next;
  140.         
  141.         if (!(size = blksz->blksize)) {
  142.         fprintf(stderr, "%s: zero block size\n", pname);
  143.         return 7;
  144.         }
  145.     }
  146.     
  147.     for (ninpool = 1; size && !(pool = (char*) malloc(size));
  148.         ++ninpool, size /= 2)
  149.         ;
  150.     
  151.     if (!size) {
  152.         fprintf(stderr, "%s: not enough memory\n", pname);
  153.         return 3;
  154.     }
  155.     
  156.     if (alphabetic) {
  157.         char* theletter = outletter;
  158.         do {
  159.         if (++*theletter > 'z') {
  160.             *theletter = OUTSUFFIX[strlen(OUTSUFFIX) -
  161.                 (outletter - theletter) - 1];
  162.             --theletter;
  163.         } else {
  164.             break;
  165.         }
  166.         } while (outletter - theletter < strlen(OUTSUFFIX));
  167.     
  168.         if (outletter - theletter == strlen(OUTSUFFIX)) {
  169.         fprintf(stderr, "%s: cannot generate filename after %s\n",
  170.             pname, outfile);
  171.         res = 5;
  172.         goto end;
  173.         }
  174.     } else {
  175.         sprintf(outfile, "%s%d", oprefix, filenum++);
  176.     }
  177.     
  178.     out = creat(outfile, 0666);
  179.     if (out == -1) {
  180.         fprintf(stderr, "%s: cannot create %s\n", pname, outfile);
  181.         res = 6;
  182.         goto end;
  183.     }
  184.  
  185.     for (fmdsk = 1, num = ninpool; fmdsk && num; --num) {
  186.         if ((fmdsk = read(fd, pool, size)) > 0) {
  187.             write(out, pool, fmdsk);
  188.         } else if (fmdsk == -1) {
  189.         fprintf(stderr, "%s: error reading %s\n", pname, ifile);
  190.         res = 7;
  191.         goto end;
  192.         }
  193.     }
  194.  
  195.     close(out);
  196.     } while (fmdsk == size);
  197.  
  198.     close(fd);
  199.  
  200. end:
  201.     free(pool);
  202.     return res;
  203. }
  204.  
  205. main(argc, argv)
  206.     int argc;
  207.     char** argv; {
  208.  
  209.     char* pname = *argv++;
  210.     struct blksize_elem* blksz = (struct blksize_elem*) 0;
  211.     struct blksize_elem* lstsz = blksz;
  212.     char* ifile;
  213.     char* oprefix = OUT;
  214.     char* osuffix = OUTSUFFIX;
  215.  
  216.     int alphabetic = 1;
  217.     
  218.     if (*argv && !strcmp(*argv, "-n")) {
  219.         alphabetic = 0;
  220.     ++argv;
  221.     }
  222.     
  223.     while (*argv && **argv == '-' && (*argv)[1]) {
  224.     /* Block size */
  225.     
  226.     int thesize = size(*argv++ + 1);
  227.     
  228.     if (!thesize) {
  229.         fprintf(stderr, "%s: bad size %s\n", pname, blksz);
  230.         return 2;
  231.     } else if (thesize == -1) {
  232.         usage(pname);
  233.         } else {
  234.         struct blksize_elem* el =
  235.             (struct blksize_elem*) malloc(sizeof(struct blksize_elem));
  236.         
  237.         if (!el) {
  238.         fprintf(stderr, "%s: not enough memory\n", pname);
  239.         exit(3);
  240.         } else {
  241.             el->blksize = thesize;
  242.         el->next = (struct blksize_elem*) 0;
  243.         
  244.             if (lstsz) {
  245.             lstsz->next = el;
  246.         } else {
  247.             blksz = lstsz = el;
  248.         }
  249.         }
  250.     }
  251.     }
  252.  
  253.     if (!*argv) {
  254.     ifile = "-";
  255.     } else {
  256.     ifile = *argv++;
  257.     }
  258.  
  259.     if (*argv) {
  260.     oprefix = *argv++;
  261.     }
  262.  
  263.     if (*argv) {
  264.     osuffix = *argv++;
  265.     }
  266.     
  267.     if (*argv) {
  268.     usage(pname);
  269.     }
  270.  
  271.     if (!blksz) {
  272.     struct blksize_elem* el =
  273.         (struct blksize_elem*) malloc(sizeof(struct blksize_elem));
  274.     
  275.     if (!el) {
  276.         fprintf(stderr, "%s: not enough memory\n", pname);
  277.         exit(3);
  278.     } else {
  279.         el->blksize = CHUNK;
  280.         el->next = (struct blksize_elem*) 0;
  281.         
  282.         blksz = lstsz = el;
  283.     }
  284.     }
  285.     
  286.     return bsplit(pname, ifile, oprefix, osuffix, blksz, alphabetic);
  287. }
  288.  
  289.