*BSD News Article 48512


Return to BSD News archive

Newsgroups: comp.unix.bsd.freebsd.misc
Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!simtel!news.kei.com!bloom-beacon.mit.edu!uhog.mit.edu!nntp.club.cc.cmu.edu!cantaloupe.srv.cs.cmu.edu!rochester!rit!dpc6787
From: dpc6787@cs.rit.edu (David P Cutter)
Subject: Re: File Cut for a binary file
Message-ID: <1995Aug8.114215.4560@cs.rit.edu>
Sender: news@cs.rit.edu (USENET News Admin)
Nntp-Posting-Host: arizona
Organization: Rochester Institute of Technology, Rochester, NY
References: <RPANCHOL.95Aug6204025@westphal.ch.intel.com>
Date: Tue, 8 Aug 1995 11:42:15 GMT
Lines: 154

In article <RPANCHOL.95Aug6204025@westphal.ch.intel.com> rpanchol@westphal.ch.intel.com (Ranju Pancholi -FT-~) writes:
>I wish to find out if there exists routines to
>cut a binary file ( around 20 Meg ) into smaller
>segments of a specified size AND after transportation
>to another location JOIN them back into the original
>binary file ?
>
>thanks in advance...
>--
>- Ranju
>
>-----------------------------------------------------------------------------
>|                                |                                          |
>| Ranju Pancholi                 |    email: rpanchol@sedona.intel.com      |
>| Intel, Corp.                   |    Phone: (602) 554-1697                 |
>| 5000 W. Chandler Blvd.         |                                          |
>| Chandler, AZ  85226            !                                          | 
>-----------------------------------------------------------------------------

This is small enough to post it.  I can't take credit for it - I've had it
for a few years.

-Dave

/*
 * bsplit.c - split binary files in manageable pieces.
 * usage is exactly like the split program.
 *
 * This program was written from scratch, without looking at the
 * sources of split.
 *
 * Copyright (C) 1988 P. Knoppers
 *                    Bilderdijkhof 59
 *                    2624 ZG  Delft
 *                    The Netherlands
 */

char copy0[] = "Copyright (C) 1988 P. Knoppers";
char copy1[] = "Permission to use and distribute copies of this";
char copy2[] = "program WITH SOURCE is granted to anyone, provided";
char copy3[] = "that it is NOT CHANGED in any way.";

#include <stdio.h>
#define DEFSIZE 50000
#define DEFPREFIX "x"
#define MAXNAME 200

char   *malloc ();

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

    myname = *argv;
    strcpy (outbase, DEFPREFIX);
    while (--argc > 0)
    {
	argv++;
	if ((*argv)[0] == '-')
	{
	    if ((*argv)[1] == '\0')
	    {
		if (foundinname != 0)
		{
		    fprintf (stderr,
			    "usage: %s [-size] [file [prefix]]\n",
			    myname);
		    exit (1);
		}
		foundinname++;
	    }
	    else
		if (sscanf (*argv, "-%d", &bulksize) != 1)
		{
		    fprintf (stderr,
			    "usage: %s [-size] [file [prefix]]\n",
			    myname);
		    exit (1);
		}
	}
	else
	    if (foundinname != 0)
	    {
		if (strlen (*argv) > MAXNAME)
		{
		    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 (bulksize)) == NULL)
    {
	fprintf (stderr, "%s: malloc failed\n", myname);
	exit (1);
    }
    level = 0;
    while (1)
    {
	got = read (fileno (infile), &buf[level], bulksize - level);
	level += got;
	if ((level < bulksize) && (got > 0))
	    continue;
	if ((level == bulksize) || ((got == 0) && (level > 0)))
	{
	    sprintf (outfname, "%s%c%c", outbase, fno / 26 + 'a',
		    fno % 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;
	    fno++;
	}
	if (got == 0)
	    break;
    }
}
-- 
Work:  dpc@grail.com  (General Railway Signal Corp.)              (MIME Capable)
School:  dpc6787@cs.rit.edu (Rochester Institute of Technology) 
Home:  dpc@tristen.rochester.ny.us                                (MIME Capable)
WWW:  http://www.cs.rit.edu/~dpc6787/