fork download
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <errno.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8.  
  9. void
  10. cat(int f, char *s)
  11. {
  12. char buf[8192];
  13. long n;
  14.  
  15. while((n=read(f, buf, (long)sizeof buf))>0)
  16. if(write(1, buf, n)!=n)
  17. {
  18. printf("write error copying %s: %d\n", s, errno);
  19. exit(1);
  20. }
  21. if(n < 0)
  22. {
  23. printf("error reading %s: %d\n", s, errno);
  24. exit(1);
  25. }
  26. }
  27.  
  28. int
  29. main(int argc, char *argv[])
  30. {
  31. int f, i;
  32.  
  33. if(argc == 1)
  34. cat(0, "<stdin>");
  35. else for(i=1; i<argc; i++){
  36. f = open(argv[i], 0);
  37. if(f < 0)
  38. {
  39. printf("can't open %s: %d\n", argv[i], errno);
  40. exit(1);
  41. }
  42. else{
  43. cat(f, argv[i]);
  44. close(f);
  45. }
  46. }
  47. exit(0);
  48. }
  49.  
  50.  
  51.  
Success #stdin #stdout 0s 1784KB
stdin
Standard input is empty
stdout
Standard output is empty