home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / ftp-102.zip / ftape-1.02 / qic / reblock.c < prev    next >
C/C++ Source or Header  |  1992-10-12  |  2KB  |  90 lines

  1. /* Reblock a pipeline.
  2.    Copyright (C) 1992 David L. Brown, Jr.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.    
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.    
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; see the file COPYING.  If not, write to
  16.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /*
  19.  * reblock.c,v 1.2 1992/10/13 01:55:35 dbrown Exp
  20.  *
  21.  * reblock.c,v
  22.  * Revision 1.2  1992/10/13  01:55:35  dbrown
  23.  * Added FSF copyright.
  24.  *
  25.  * Revision 1.1  1992/10/12  05:16:14  dbrown
  26.  * Created.
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31.  
  32. main (int argc, char **argv)
  33. {
  34.   int value = -1;
  35.   char *ptr;
  36.   int size;
  37.   int len;
  38.   int write_cur;
  39.   int read_cur;
  40.   int done = 0;
  41.  
  42.   if (argc == 2)
  43.     value = atoi (argv[1]);
  44.  
  45.   if (value <= 0)
  46.     {
  47.       fprintf (stderr, "Usage: reblock kbytes\n");
  48.       exit (1);
  49.     }
  50.  
  51.   size = value * 1024;
  52.   ptr = (char *) malloc (size);
  53.   if (ptr == 0)
  54.     {
  55.       fprintf (stderr, "Cannot allocate %dk of data\n", value);
  56.       exit (1);
  57.     }
  58.  
  59.   while (!done)
  60.     {
  61.       /* Read as much as possible in. */
  62.       read_cur = 0;
  63.  
  64.       while (read_cur < size && !done)
  65.     {
  66.       len = read (0, ptr + read_cur, size - read_cur);
  67.       if (len <= 0)
  68.         done = 1;
  69.       else
  70.         read_cur += len;
  71.     }
  72.  
  73.       /* Now write this data out. */
  74.       write_cur = 0;
  75.       while (write_cur < read_cur)
  76.     {
  77.       len = write (1, ptr + write_cur, read_cur - write_cur);
  78.       if (len <= 0)
  79.         {
  80.           fprintf (stderr, "Reblock error: can't write");
  81.           exit (1);
  82.         }
  83.  
  84.       write_cur += len;
  85.     }
  86.     }
  87.  
  88.   exit (0);
  89. }
  90.