catrm

'cat' a file, then optionally 'rm' it. I just felt like writing C.
Log | Files | Refs | Atom | README

fcat.c (841B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 /* fcat copies data from file at `src` and appends it to
      5  to the file at `dst`, in 4kb (4096) chunks.
      6  returns EXIT_SUCCESS or EXIT_FAILURE. */
      7 int fcat(FILE *src, FILE *dst)
      8 {
      9         int ret = EXIT_FAILURE;
     10         size_t srcsiz;
     11         char buf[4096];
     12         size_t r, w, total = 0;
     13 
     14         fseek(src, 0, SEEK_END);
     15 	srcsiz = ftell(src);
     16         rewind(src);
     17 
     18         do {
     19                 r = fread(buf, sizeof(char), sizeof(buf), src);
     20                 if (ferror(src))
     21 			goto ABORT;
     22                 else {
     23                         w = fwrite(buf, sizeof(char), r, dst);
     24                         if (ferror(dst))
     25 				goto ABORT;
     26                         total += w;
     27                 }
     28         } while (!feof(src));
     29 
     30         if (total == srcsiz)
     31 		ret = EXIT_SUCCESS;
     32 
     33 ABORT:
     34         return ret;
     35 }
     36