Name: Anonymous 2011-09-26 2:54
Tell me about your cat(s), /prog/.
cats you want.cat:/* @cat.c */
/* NOTE: GNU's cat will not cat a file into itself. Should we check for this?
Possibility of infinite loop here. POSIX doesn't specify what happens if
standard input happens to be standard output (cat to temp. file and then
write that to the output? don't write what was already written?) */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define CATBUF_SIZE 65536 /* increase this if you want OMG MOAR SPEED! */
char catbuf[CATBUF_SIZE];
int main(int argc, char** argv) {
int uflag = 0, retval = 0, i = 1, c;
FILE *input;
while((c=getopt(argc,argv,"u"))!=-1)
if(c=='u') {
uflag=1;
retval=setvbuf(stdout,NULL,_IONBF,0);
} else {
fprintf(stderr,"usage: %s [-u] file...\n",argv[0]);
return 1;
}
if(optind>=argc) { /* catting stdin */
input=stdin;
goto cat_stdin;
}
i=optind;
while(argv[i]) {
int l;
if(strcmp(argv[i],"-")) {
if(!(input=fopen(argv[i++],"rb"))) {
fprintf(stderr,"%s: could not open %s: %s\n",argv[0],argv[i-1],strerror(errno));
retval=1;
continue;
}
} else {
input=stdin;
i++;
}
cat_stdin:
if(uflag)
retval|=setvbuf(input, NULL, _IONBF, 0);
while(l=fread(catbuf,1,CATBUF_SIZE,input)) {
if(fwrite(catbuf,1,l,stdout)!=l) {
fprintf(stderr,"%s: error writing to stdout: %s\n",argv[0],strerror(errno));
retval=1;
break;
}
}
if(ferror(input))
fprintf(stderr,"%s: error reading %s: %s\n",argv[0],argv[i-1],strerror(errno));
if(input!=stdin)
retval|=fclose(input);
}
return retval;
}
#include <stdio.h>
int main(int argc, char **argv) {
int i = 0, c;
FILE *f = stdin;
if (argc == 1)
goto no_arguments;
while (i++ < argc) {
f = fopen(argv[i], "rb");
no_arguments:
while ((c = fgetc(f)) != EOF)
putchar(c);
}
return 0;
}-e!-n!--squeeze-blank/--show-tabs/--show-nonprinting/--gnu-bloat-option-frice!#include <stdio.h>
int main(int argc, char **argv) {
int i = 0, c;
FILE *f = stdin;
if (argc == 1)
goto no_arguments;
while (i++ < argc) {
f = fopen(argv[i], "rb");
if (!f) {
fprintf("cat: error opening %s", argv[i]);
return 1;
}
no_arguments:
while ((c = fgetc(f)) != EOF)
putchar(c);
}
return 0;
}
#include <stdio.h>
int main(int argc, char **argv) {
int i = 0, c;
FILE *f = stdin;
if (argc == 1)
goto no_arguments;
while (i++ < argc) {
f = fopen(argv[i], "rb");
if (!f) {
fprintf(stderr, "cat: error opening %s", argv[i]);
return 1;
}
no_arguments:
while ((c = fgetc(f)) != EOF)
putchar(c);
}
return 0;
}
#include <stdio.h>
int main(int argc, char **argv) {
int i = 0, c;
FILE *f = stdin;
if (argc == 1)
goto no_arguments;
while (++i < argc) {
f = fopen(argv[i], "rb");
if (!f) {
fprintf(stderr, "cat: error opening %s", argv[i]);
return 1;
}
no_arguments:
while ((c = fgetc(f)) != EOF)
putchar(c);
}
return 0;
}
#include <stdio.h>
int main(int argc, char **argv) {
int i = 0, c;
FILE *f = stdin;
if (argc == 1)
goto no_arguments;
while (++i < argc) {
f = fopen(argv[i], "rb");
if (!f) {
fprintf(stderr, "cat: error opening %s\n", argv[i]);
return 1;
}
no_arguments:
while ((c = fgetc(f)) != EOF)
putchar(c);
}
return 0;
}
#include <stdio.h>
int main(int argc, char **argv) {
int i = 0, c;
FILE *f = stdin;
if (argc == 1)
goto no_arguments;
while (++i < argc) {
f = fopen(argv[i], "rb");
if (!f) {
fprintf(stderr, "cat: error opening %s\n", argv[i]);
return 1;
}
no_arguments:
while ((c = fgetc(f)) != EOF)
putchar(c);
fflush(stdout);
}
return 0;
}
mercury tmp # time dd if=/dev/zero bs=4096 count=1048576 | cat > /dev/null
1048576+0 records in
1048576+0 records out
4294967296 bytes (4.3 GB) copied, 1.77577 s, 2.4 GB/s
real 0m1.776s
user 0m0.218s
sys 0m1.576s
mercury tmp # time dd if=/dev/zero bs=4096 count=1048576 | ./a.out > /dev/null
1048576+0 records in
1048576+0 records out
4294967296 bytes (4.3 GB) copied, 59.5882 s, 72.1 MB/s
real 0m59.590s
user 0m57.176s
sys 0m2.389scat -- -ucat -- --.mercury tmp # cc -xc -Wall -Werror -Wextra -ansi -pedantic -
[code goes here lol]
cc1: warnings being treated as errors
<stdin>: In function 'main':
<stdin>:19:2: error: implicit declaration of function 'getopt'
<stdin>:27:5: error: 'optind' undeclared (first use in this function)
<stdin>:27:5: note: each undeclared identifier is reported only once for each function it appears in
<stdin>:48:3: error: suggest parentheses around assignment used as truth value
<stdin>:49:32: error: comparison between signed and unsigned integer expressions
$ cc cat.c
$ time dd if=/dev/zero bs=4096 count=1048576 | cat > /dev/null
1048576+0 records in
1048576+0 records out
4294967296 bytes (4.3 GB) copied, 1.65333 s, 2.6 GB/s
real 0m1.670s
user 0m0.196s
sys 0m3.096s
$ time dd if=/dev/zero bs=4096 count=1048576 | ./a.out > /dev/null
1048576+0 records in
1048576+0 records out
4294967296 bytes (4.3 GB) copied, 2.23426 s, 1.9 GB/s
real 0m2.237s
user 0m0.240s
sys 0m4.080s
$
-Wall -Werror -Wextra -ansi -pedantic which is the minimum required to ensure good code.
if (argc == 1)
goto no_arguments;if (gamec == 0)
goto no_games;