Advertisement
VisualPaul

myhead

Mar 2nd, 2013
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4.  
  5. void incorrect_arg_fmt()
  6. {
  7.     fputs("incorrect arguments\n", stderr);
  8.     abort();
  9. }
  10.  
  11. int main(int argc, char **argv)
  12. {
  13.     int bytes = 2048;
  14.     int just_read;
  15.     void *buffer;
  16.  
  17.     if (argc == 2) {
  18.         if (sscanf(argv[1], "%d", &bytes) != 1)
  19.             incorrect_arg_fmt();
  20.     } else if (argc != 1) {
  21.         incorrect_arg_fmt();
  22.     }
  23.  
  24.     buffer = malloc(bytes);
  25.  
  26.     while (bytes != 0 && (just_read = read(0, buffer, bytes)) != 0) {
  27.         int delta;
  28.         if (just_read < bytes)
  29.             delta = just_read;
  30.         else
  31.             delta = bytes;
  32.         bytes -= delta;
  33.         write(1, buffer, delta);
  34.     }
  35.     free(buffer);
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement