ftp.nice.ch/pub/next/unix/file/Bsplit.NIHS.bs.tar.gz#/Bsplit/bsplit.c

This is bsplit.c in view mode; [Download] [Up]

/*
 * bsplit.c - split files in manageable pieces.
 * usage is exactly like the split program.
 *
 * Written by C. Swiger 8/08/93
 */

#include <stdio.h>
#define DEFAULT_SIZE 50000
#define DEFAULT_PREFIX "x"

#ifndef MAXPATHLEN
#define MAXPATHLEN 200
#endif

char   *malloc ();

int
convint (str)
     char *str;
{
  int multiplier = 1;
  int arglen = strlen (str);

  if (arglen > 1) {
      switch (str[arglen - 1]) {
        case 'k':
          multiplier = 1024;
          str[arglen - 1] = '\0';
          break;
        case 'm':
          multiplier = 1024 * 1024;
          str[arglen - 1] = '\0';
          break;
      }
  }

  return atoi (str) * multiplier;
}


int
main (argc, argv)               /* bsplit - split binary file */
     char   *argv[];
{
    char   *buf;
    char   *myname;
    int     blocksize = DEFAULT_SIZE;
    int     level;
    int     got;
    int     file_number = 0;
    char    outfname[MAXPATHLEN + 1];
    char    outbase[MAXPATHLEN + 3];
    int     foundinname = 0;
    FILE   *infile = stdin;
    FILE   *outfile;

    myname = *argv;
    strcpy (outbase, DEFAULT_PREFIX);
    if (argc == 1) {
        fprintf (stderr,
                 "usage: %s [-size{km}] [file [prefix]]\n",
                 myname);
        exit(1);
    }

    while (--argc > 0) {
        argv++;

        if ((*argv)[0] == '-') {
            if ((*argv)[1] == '\0') {
                if (foundinname) {
                    fprintf (stderr,
                             "usage: %s [-size{km}] [file [prefix]]\n",
                             myname);
                    exit (1);
                }
                foundinname++;
            } else
              blocksize = convint(*argv + 1);
              if (blocksize < 1) {
                  fprintf (stderr,
                           "usage: %s [-size{km}] [file [prefix]]\n",
                           myname);
                  exit (1);
              }
        }

        else
          if (foundinname) {
              if (strlen (*argv) > MAXPATHLEN) {
                  fprintf (stderr, "%s: prefix too long\n",
                           myname);
                  exit (1);
              }
              strcpy (outbase, *argv);
          }
          else {
              if ((infile = fopen (*argv, "r")) == NULL) {
                  fprintf (stderr, "%s: cannot open %s\n",
                           myname, *argv);
                  exit (1);
              }
              foundinname++;
          }
    }

    if ((buf = malloc (blocksize)) == NULL) {
        fprintf (stderr, "%s: malloc failed\n", myname);
        exit (1);
    }

    level = 0;

    while (1) {
        got = read (fileno (infile), &buf[level], blocksize - level);
        level += got;
        if ((level < blocksize) && (got > 0))
          continue;
        if ((level == blocksize) || ((got == 0) && (level > 0)))
        {
            sprintf (outfname, "%s%c%c", outbase, file_number / 26 + 'a',
                     file_number % 26 + 'a');
            if ((outfile = fopen (outfname, "w")) == NULL)
            {
                fprintf (stderr, "%s: cannot create %s\n", myname,
                         outfname);
                exit (1);
            }
            if (write (fileno (outfile), buf, level) != level)
            {
                fprintf (stderr, "%s: write failed\n", myname);
                exit (1);
            }
            fclose (outfile);
            level = 0;
            file_number++;
        }
        if (got == 0)
          break;
    }
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.