ftp.nice.ch/pub/next/unix/text/NeXT_French_Dictionary.3.1.08.I.bs.tar.gz#/NeXT_French_Dictionary3.1.08/src/next_francais/conv_iso_next/bsplit.c

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

/***********************************************************************
Split a binary file into pieces of a specified (or default) size.

Usage:
        bsplit [[-nnn] [-] filename]

A -nnn bytecount option sets the split piece size for subsequent
files.  Stdin is assumed if a bare - is given, or if no files are
specified.  Multiple filenames may be specified.

No check is made to see whether the output files (named with the input
file name and the suffixes "-001", "-002", etc.) exist.

[29-Aug-1990] Nelson H. F. Beebe
***********************************************************************/
#include <stdio.h>
#include <string.h>

#ifdef __STDC__

#ifndef __GNUC__
#include <stdlib.h>
#endif                          /* __GNUC__ */

#ifdef __COMPILER_KCC__
#define WB_FLAGS "wb8"
#define RB_FLAGS "rb8"
void bsplit();
#else                           /* NOT __COMPILER_KCC__ */
void bsplit(int size, char *name);
#endif                          /* __COMPILER_KCC__ */

#else                           /* NOT __STDC__ */
void bsplit();
#endif                          /* __STDC__ */

#ifdef VMS
#define RB_FLAGS        "rb","rfm=fix","bls=512","mrs=512"
#define WB_FLAGS        "wb","rfm=fix","bls=512","mrs=512"
#endif

#ifndef RB_FLAGS
#define RB_FLAGS "r"
#endif

#ifndef WB_FLAGS
#define WB_FLAGS "w"
#endif

#ifdef ABS
#undef ABS
#endif
#define ABS(n)          ( ((n) < 0) ? -(n) : (n) )
#define ROUNDABSUP(n)   ((ABS(n) + 511) & ~0x1ff)

#define DEFAULT_SIZE    32768
#define DEFAULT_DOS_SIZE    1457152
#define DEFAULT_MAC_SIZE    1436672
#define DEFAULT_NEXT_SIZE   1220608


int
main(argc, argv)
    int argc;
    char *argv[];
{
    int size;
    int k;
    int nfiles;

    size = DEFAULT_SIZE;

    for (nfiles = 0, k = 1; k < argc; ++k)
    {
        if (strcmp(argv[k], "-") == 0) /* bsplit stdin */
        {
            bsplit(ROUNDABSUP(size), (char *) NULL);
            nfiles++;
        }
        else if (*argv[k] == '-')       /* expect -nnn */
        {
	    if (argv[k][1] == 'h')       /* expect help */
            {
              printf("bsplit V1.0 April 94 by vamparys@litnext1.epfl.ch\n");
              printf("based on bsplit of Nelson H. F. Beebe [29-Aug-1990] \n");
              printf("    -h     help\n");
              printf("    -dos   floppy DOS size\n");
              printf("    -mac   floppy MAC size\n");
              printf("    -next  floppy NeXT size\n");
              printf("    -xxxx  size of xxxx\n");
              printf("    -      stdin is assumed\n");
              printf("No check is made to see whether the output files exist\n");
              printf("(suffixes -001 -002 ...)\n");
              exit(0);
            }else if (argv[k][1] == 'd') /* expect DOS */
            {
                size = DEFAULT_DOS_SIZE;
            }else if (argv[k][1] == 'n') /* expect NeXT */
            {
                size = DEFAULT_NEXT_SIZE;
            }else if (argv[k][1] == 'm') /* expect NeXT */
            {
                size = DEFAULT_MAC_SIZE;
            }else{
              (void) sscanf(&argv[1][1], "%d", &size);
              if (size < 512)     /* prevent nonsense values */
                size = DEFAULT_SIZE;
            }
        }
        else
        {
            bsplit(ROUNDABSUP(size), argv[k]);
            nfiles++;
        }
    }
    if (nfiles == 0)                    /* just process stdin */
        bsplit(ROUNDABSUP(size), (char *) NULL);
    exit(0);
    return (0);                 /* keep compilers happy */
}

void
bsplit(size, inname)
    int size;
    char *inname;
{
    register FILE *in;
    register FILE *out;
    char outname[512];
    register int n;
    int npart;
    register int c;

    if (inname == (char *) NULL)
    {
        in = stdin;
        inname = "stdin";
    }
    else
        in = fopen(inname, RB_FLAGS);
    if (in == (FILE *) NULL)
    {
        (void) fprintf(stderr, "?Cannot open file [%s]\n", inname);
        return;
    }
    (void) fprintf(stderr, "[%s]\n", inname);
    for (npart = 1; !feof(in); ++npart)
    {
        sprintf(outname, "%s-%03d", inname, npart);
        out = fopen(outname, WB_FLAGS);
        if (out == (FILE *) NULL)
        {
            (void) fprintf(stderr,
                           "?Cannot open file [%s]\n", outname);
            return;
        }
        (void) fprintf(stderr, "\t[%s]", outname);
        for (n = 0; n < size; ++n)
        {
            c = getc(in);
            if ((c == EOF) && feof(in))
                break;
            putc(c, out);
        }
        (void) fclose(out);
        (void) fprintf(stderr, " %d bytes [OK]\n", n);
    }
}

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