/bin/cat

From C

(Redirected from Cat)
Jump to: navigation, search

A reimplementation of the /bin/cat unix tool, without options

   #include <stdio.h>
   int main(void)
   {
     int c;
     while ((c = getc(stdin)) != EOF)
       putc(c, stdout);
     return 0;
   }
   #include <stdio.h>
   int main(void)
   {
     char buf[BUFSIZ];
     while (fgets(buf, sizeof buf, stdin))
         fputs(buf, stdout);
     return 0;
   }
   /* Wrong, because:
   printf '\0' | cat | wc -c; printf '\0' | ./a.out | wc -c
   */
   #include <stdio.h>
   int main(void)
   {
     char buf[BUFSIZ];
     size_t n;
     while ((n = fread(buf, 1, sizeof buf, stdin)) > 0)
       fwrite(buf, 1, n, stdout);
     return 0;
   }
Personal tools