Skip to content

Instantly share code, notes, and snippets.

@alcides
Created September 4, 2011 21:02
Show Gist options
  • Save alcides/1193507 to your computer and use it in GitHub Desktop.
Save alcides/1193507 to your computer and use it in GitHub Desktop.
A cat clone in naïve C.
#include <stdlib.h>
#include <stdio.h>
char *buffer;
void read(char* fname) {
FILE *fp = fopen(fname, "r");
if (fp != NULL) {
fseek(fp, 0L, SEEK_END);
long s = ftell(fp);
rewind(fp);
buffer = malloc(s);
fread(buffer, s, 1, fp);
fwrite(buffer, s, 1, stdout);
if (fp != NULL) fclose(fp);
free(buffer);
}
}
int main(int argc, char** argv) {
int f;
for(f=1; f<argc; f++) {
read(argv[f]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment