home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0210 - 0219 / ibm0210-0219 / ibm0213.tar / ibm0213 / HAM_W32.ZIP / SAMPLES / BLKSIZE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-12  |  1.6 KB  |  51 lines

  1. /***************************************************************************/
  2. /*                                                                                                    */
  3. /*                                                                                                    */
  4. /*            Read from Stdin, Write to Stdout, using specified Blocksize            */
  5. /*        Copyright (c) 1994 by Hamilton Laboratories.  All rights reserved.    */
  6. /*                                                                                                    */
  7. /*                                                                                                    */
  8. /***************************************************************************/
  9.         
  10. /* blksize.c:  Accepts just one (optional) argument, the
  11.                blocksize desired, in bytes.  */
  12.  
  13. #include <windows.h>
  14. #include <stdlib.h>
  15.  
  16. void main( int argc, char **argv )
  17.    {
  18.    HANDLE Stdin = GetStdHandle(STD_INPUT_HANDLE),
  19.       Stdout = GetStdHandle(STD_OUTPUT_HANDLE);
  20.    char *buffer, *p;
  21.    DWORD length, blocksize, remaining;
  22.  
  23.    /* First arg is blocksize.  If missing, default to 512 bytes. */
  24.    blocksize = argc > 1 ? atoi(argv[1]) : 512;
  25.    buffer = malloc(blocksize);
  26.  
  27.    while (ReadFile(Stdin, buffer, blocksize, &length, NULL) && length)
  28.        {
  29.           if (remaining = blocksize - length)
  30.            {
  31.            /* Apparently reading from a pipe or the console and getting less
  32.                than a full blocksize number of characters.  Keep reading until
  33.                we have enough to for a whole blocksize to output (or until
  34.                 End-Of-File). */
  35.            p = buffer + length;
  36.            while (remaining &&
  37.                     ReadFile(Stdin, p, remaining, &length, NULL) && length)
  38.                {
  39.                remaining -= length;
  40.                p += length;
  41.                }
  42.            length = blocksize - remaining;
  43.            }
  44.       WriteFile(Stdout, buffer, length, &length, NULL);
  45.       }
  46.  
  47.    CloseHandle(Stdin);
  48.    CloseHandle(Stdout);
  49.    ExitProcess(0);
  50.    } 
  51.