Name: Anonymous 2015-02-23 16:31
Most elegant solution wins
reference bloated cats: https://gist.github.com/pete/665971
reference bloated cats: https://gist.github.com/pete/665971
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
// character-by-character concatination, in case the file is a terminal
void tty_cat(FILE *fp){
int c;
c = fgetc(fp);
while (!feof(fp)){
fputc(c, stdout);
c = fgetc(fp);
}
}
// read files by blocks to play nicer with syscalls and disk reads
void file_cat(FILE *fp){
size_t n_read;
uint8_t *buf = malloc(0x1000);
if (buf){
n_read = fread(buf, 1, 0x1000, fp);
while(n_read){
fwrite(buf, 1, n_read, stdout);
n_read = fread(buf, 1, 0x1000, fp);
}
}
}
void cat_thing(FILE *fp){
if (isatty(fileno(fp))){
tty_cat(fp);
} else {
file_cat(fp);
}
}
void print_help(){
printf(
"Usage: cat [OPTION] ... [FILE] ...\n"
" --help Show this help and exit\n"
" --version Output version information and exit\n"
);
}
int main(int argc, char *argv[]){
unsigned i;
if (argc > 1){
for (i = 1; i < argc; i++){
FILE *fp;
if (strcmp(argv[i], "-") == 0){
fp = stdin;
} else if (strcmp(argv[i], "--help") == 0){
print_help();
exit(0);
} else if (strcmp(argv[i], "--version") == 0){
printf("( =^'.'^=) meow\n");
exit(0);
} else {
fp = fopen(argv[i], "r");
}
if (!fp){
perror(argv[i]);
exit(1);
}
cat_thing(fp);
}
} else {
cat_thing(stdin);
}
return 0;
}
#include <u.h>
#include <libc.h>
static void slurp(int);
void
main(int argc, char **argv)
{
int *fd, i, x;
fd = malloc(sizeof(int) * argc);
if(fd == nil)
exits("No mem");
fd[0] = 0;
if(argc > 1){
argc--;
for(i = 0; argc - i != 0; i++)
fd[i] = open(argv[i], OREAD);
}else{
i = 1;
}
for(x = 0; x < i; x++)
slurp(fd[x]);
}
static void
slup(int x)
{
char buf[512];
int n;
if(x >= 0){
do{
n = read(x, buf, 512);
if(n > 0)
write(1, buf, n);
}while(n > 0);
}
}
import autism
autism.unix_cat()
. ,.
T."-._..---.._,-"/|
l|"-. _.v._ (" |
[l /.'_ \; _~"-.`-t
Y " _(o} _{o)._ ^.|
j T ,-<v>-. T ]
\ l ( /-^-\ ) ! !
\. \. "~" ./ /c-..,__
^r- .._ .- .-" `- . ~"--.
> \. \
] ^. \
3 . "> . Y -System V
,.__.--._ _j \ ~ . ; |
( ~"-._~"^._\ ^. ^._ I . l
"-._ ___ ~"-,_7 .Z-._ 7" Y ; \ _
/" "~-(r r _/_--._~-/ / /,.--^-._ / Y
"-._ '"~~~>-._~]>--^---./____,.^~ ^.^ !
~--._ ' Y---. \./
~~--._ l_ ) \
~-._~~~---._,____..--- \
~----"~ \
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "text.h"
#include "util.h"
static void
usage(void)
{
eprintf("usage: %s [-u] [file ...]\n", argv0);
}
int
main(int argc, char *argv[])
{
FILE *fp;
int ret = 0;
ARGBEGIN {
case 'u':
setbuf(stdout, NULL);
break;
default:
usage();
} ARGEND;
if (!argc) {
concat(stdin, "<stdin>", stdout, "<stdout>");
} else {
for (; *argv; argc--, argv++) {
if (!strcmp(*argv, "-")) {
*argv = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv);
ret = 1;
continue;
}
concat(fp, *argv, stdout, "<stdout>");
if (fp != stdin && fshut(fp, *argv))
ret = 1;
}
}
ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret;
}
#include <stdio.h>
void cat(FILE *f, char *s)
{
int c;
while ((c = getc(f)) != EOF)
if (putc(c, stdout) == EOF)
fprintf(stderr, "error copying %s\n", s);
if (ferror(f))
fprintf(stderr, "error reading %s\n", s);
}
int main(int argc, char *argv[])
{
FILE *f;
int i;
if (argc == 1)
cat(stdin, "<stdin>");
else for (i = 1; i < argc; i++)
if ((f = fopen(argv[i], "r")) == NULL)
fprintf(stderr, "can't open %s\n", argv[i]);
else {
cat(f, argv[i]);
fclose(f);
}
return 0;
}
#include "..\void.h"the poor fuck doesn't understand include paths
while(!feof(catfile))the poor fuck doesn't understand feof/ferror
#define STDSTART int main(int argc,char** argv){ rng8fastseed();;netstart;the poor fuck does a lot of stupid shit
#define STDEND ; WSACleanup();return EXIT_SUCCESS;}
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>
#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;
FILE *input;
if(argc>1 && !strcmp(*argv,"-u")) {
uflag=1;
retval=setvbuf(stdout,NULL,_IONBF,0);
i++;
}
if(i>=argc) { /* catting stdin */
input=stdin;
goto cat_stdin;
}
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;
}
perl -ne print
perl -ne print text-file.txt
echo "fuck you" | perl -ne print
main = interact id
while(1) { putc(getc)); }