home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / useful / dist / util / misc / bsplit / bsplit.posting < prev    next >
Text File  |  1993-08-30  |  5KB  |  178 lines

  1. Article 343 of comp.sources.misc:
  2. Path: fishpond!nud!asuvax!noao!ncar!tank!nic.MR.NET!hal!ncoast!allbery
  3. From: knop@dutesta.UUCP (Peter Knoppers, Delft Univ. of Technology)
  4. Newsgroups: comp.sources.misc
  5. Subject: v05i083: bsplit - a split-like program for binary files
  6. Message-ID: <1238@dutesta.UUCP>
  7. Date: 14 Dec 88 03:10:06 GMT
  8. Sender: allbery@ncoast.UUCP
  9. Reply-To: knop@dutesta.UUCP (Peter Knoppers, Delft Univ. of Technology)
  10. Organization: DELFT UNIVERSITY OF TECHNOLOGY Faculty of Electrical Engineering Computer architecture and Digital Technique Mekelweg 4   -   2628 CD  Delft
  11. Lines: 162
  12. Approved: allbery@ncoast.UUCP
  13.  
  14. Posting-number: Volume 5, Issue 83
  15. Submitted-by: "Peter Knoppers, Delft Univ. of Technology" <knop@dutesta.UUCP>
  16. Archive-name: bsplit
  17.  
  18. Below follows my program bsplit.c. It is a short and simple program
  19. that we use rather often. Manual is not included, as the use is
  20. almost identical to that of the un*x split program. As furnished
  21. it compiles and works on the systems that I tried. It may not
  22. work on systems where ints are 16 bits. For local use you may
  23. change this program into anything you like.
  24.  
  25. Modified versions must not be re-distributed. Distribution of the
  26. original version with diff is OK. If someone really improves this,
  27. mail the diffs to me, so that I can re-post the really-improved 
  28. version. The copyright message is there to prevent uncontrolled
  29. spreading of many slightly different versions.
  30.  
  31. My present email address is knop@dutesta.UUCP, this will soon
  32. change to knop@duteca.UUCP.
  33.  
  34. #! /bin/sh
  35. # This file was wrapped with "dummyshar".  "sh" this file to extract.
  36. # Contents:  bsplit.c
  37. echo extracting 'bsplit.c'
  38. if test -f 'bsplit.c' -a -z "$1"; then echo Not overwriting 'bsplit.c'; else
  39. sed 's/^X//' << \EOF > 'bsplit.c'
  40. X/*
  41. X * bsplit.c - split binary files in manageable pieces.
  42. X * usage is exactly like the split program.
  43. X *
  44. X * This program was written from scratch, without looking at the
  45. X * sources of split.
  46. X *
  47. X * Copyright (C) 1988 P. Knoppers
  48. X *                    Bilderdijkhof 59
  49. X *                    2624 ZG  Delft
  50. X *                    The Netherlands
  51. X */
  52. X
  53. Xchar copy0[] = "Copyright (C) 1988 P. Knoppers";
  54. Xchar copy1[] = "Permission to use and distribute copies of this";
  55. Xchar copy2[] = "program WITH SOURCE is granted to anyone, provided";
  56. Xchar copy3[] = "that it is NOT CHANGED in any way.";
  57. X
  58. X#include <stdio.h>
  59. X#define DEFSIZE 50000
  60. X#define DEFPREFIX "x"
  61. X#define MAXNAME 200
  62. X
  63. Xchar   *malloc ();
  64. X
  65. Xmain (argc, argv)        /* bsplit - split binary file */
  66. Xchar   *argv[];
  67. X{
  68. X    char   *buf;
  69. X    char   *myname;
  70. X    int     bulksize = DEFSIZE;
  71. X    int     level;
  72. X    int     got;
  73. X    int     fno = 0;
  74. X    char    outfname[MAXNAME + 1];
  75. X    char    outbase[MAXNAME + 3];
  76. X    int     foundinname = 0;
  77. X    FILE * infile = stdin;
  78. X    FILE * outfile;
  79. X
  80. X    myname = *argv;
  81. X    strcpy (outbase, DEFPREFIX);
  82. X    while (--argc > 0)
  83. X    {
  84. X    argv++;
  85. X    if ((*argv)[0] == '-')
  86. X    {
  87. X        if ((*argv)[1] == '\0')
  88. X        {
  89. X        if (foundinname != 0)
  90. X        {
  91. X            fprintf (stderr,
  92. X                "usage: %s [-size] [file [prefix]]\n",
  93. X                myname);
  94. X            exit (1);
  95. X        }
  96. X        foundinname++;
  97. X        }
  98. X        else
  99. X        if (sscanf (*argv, "-%d", &bulksize) != 1)
  100. X        {
  101. X            fprintf (stderr,
  102. X                "usage: %s [-size] [file [prefix]]\n",
  103. X                myname);
  104. X            exit (1);
  105. X        }
  106. X    }
  107. X    else
  108. X        if (foundinname != 0)
  109. X        {
  110. X        if (strlen (*argv) > MAXNAME)
  111. X        {
  112. X            fprintf (stderr, "%s: prefix too long\n",
  113. X                myname);
  114. X            exit (1);
  115. X        }
  116. X        strcpy (outbase, *argv);
  117. X        }
  118. X        else
  119. X        {
  120. X        if ((infile = fopen (*argv, "r")) == NULL)
  121. X        {
  122. X            fprintf (stderr, "%s: cannot open %s\n",
  123. X                myname, *argv);
  124. X            exit (1);
  125. X        }
  126. X        foundinname++;
  127. X        }
  128. X    }
  129. X
  130. X    if ((buf = malloc (bulksize)) == NULL)
  131. X    {
  132. X    fprintf (stderr, "%s: malloc failed\n", myname);
  133. X    exit (1);
  134. X    }
  135. X    level = 0;
  136. X    while (1)
  137. X    {
  138. X    got = read (fileno (infile), &buf[level], bulksize - level);
  139. X    level += got;
  140. X    if ((level < bulksize) && (got > 0))
  141. X        continue;
  142. X    if ((level == bulksize) || ((got == 0) && (level > 0)))
  143. X    {
  144. X        sprintf (outfname, "%s%c%c", outbase, fno / 26 + 'a',
  145. X            fno % 26 + 'a');
  146. X        if ((outfile = fopen (outfname, "w")) == NULL)
  147. X        {
  148. X        fprintf (stderr, "%s: cannot create %s\n", myname,
  149. X            outfname);
  150. X        exit (1);
  151. X        }
  152. X        if (write (fileno (outfile), buf, level) != level)
  153. X        {
  154. X        fprintf (stderr, "%s: write failed\n", myname);
  155. X        exit (1);
  156. X        }
  157. X        fclose (outfile);
  158. X        level = 0;
  159. X        fno++;
  160. X    }
  161. X    if (got == 0)
  162. X        break;
  163. X    }
  164. X}
  165. EOF
  166. chars=`wc -c < 'bsplit.c'`
  167. if test $chars !=    2804; then echo 'bsplit.c' is $chars characters, should be    2804 characters!; fi
  168. fi
  169. exit 0
  170. -- 
  171.  _____      __  __  __  __   ____   _____   _____   ______  _____    _____ 
  172. |  _  \    |  |/  ||  \|  | /    \ |  _  \ |  _  \ |   ___||  _  \  /  ___|
  173. |   __/ _  |     < |      ||  ||  ||   __/ |   __/ |   >__ |     <  \__  \
  174. |__|   |_| |__|\__||__|\__| \____/ |__|    |__|    |______||__|\__||_____/
  175. P. Knoppers, Delft Univ. of Technology, The Netherlands - knop@dutesta.UUCP
  176.  
  177.  
  178.