home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume05 / bsplit < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  5.1 KB

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