Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Challenge:reimplement UNIX cat

Name: Anonymous 2015-02-23 16:31

Most elegant solution wins
reference bloated cats: https://gist.github.com/pete/665971

Name: Anonymous 2015-03-04 20:20

>>1

#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;
}

Name: Anonymous 2015-03-05 12:19

#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);
}
}

Name: Anonymous 2015-03-05 12:52

import autism
autism.unix_cat()

Name: Anonymous 2015-03-05 17:01

. ,.
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_ ) \
~-._~~~---._,____..--- \
~----"~ \

Name: Anonymous 2015-03-05 19:16

>>44
┈┈┈┈┈┈▕▔╲ TRIGGER WARNING: TEXT ART
┈┈┈┈┈┈┈▏▕
┈┈┈┈┈┈┈▏▕▂▂▂
▂▂▂▂▂▂╱┈▕▂▂▂▏
▉▉▉▉▉┈┈┈▕▂▂▂▏
▉▉▉▉▉┈┈┈▕▂▂▂▏ Anonymous liked this
▔▔▔▔▔▔╲▂▕▂▂▂

Name: moot 2015-07-11 18:46

bump

Name: Anonymous 2015-07-11 18:57

#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;
}

Name: Anonymous 2015-07-11 20:04

Name: Cudder !cXCudderUE 2015-07-11 23:44

>>47
WTF is this shit.

Name: Anonymous 2015-07-12 2:16

>>49
What's wrong with it?

Name: Anonymous 2015-07-12 7:37

>>50
fshut it.

Name: Anonymous 2015-07-12 9:00

just take the plan9 cat and rewrite it to work with <stdio.h>

#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;
}


there. done. now you can go back to masturbating to hentai

Name: Anonymous 2015-07-12 9:18

>>2
#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;
#define STDEND ; WSACleanup();return EXIT_SUCCESS;}
the poor fuck does a lot of stupid shit

the poor fuck's program doesn't even handle multiple files

Name: Cudder !cXCudderUE 2015-07-12 13:08

>>50
Non-POSIX headers and functions.
Trivial function used once.
Unnecessary global variable.

Anoncoreutils 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;
}

Name: Anonymous 2015-07-14 3:56

>>39
}
} else {
writer.write_u8(byte)
}.unwrap();
}
}
}
}

Paid by the line? Rust will do fine.

Name: Anonymous 2015-07-15 9:19

>>13
Let me fix for you.

#!/usr/bin/env perl
print while <>;

Name: Anonymous 2015-07-15 9:26

Cat reimplemented in perl oneliner.

perl -ne print

perl -ne print text-file.txt

echo "fuck you" | perl -ne print

Just werks

Name: Anonymous 2015-07-15 12:37

>>57
Please give a citation for your code's source.

Name: Anonymous 2015-07-15 21:27

main = interact id

Name: Anonymous 2015-07-15 22:15

>>59
How to interact with your id in Haskell.

Name: Anonymous 2015-07-16 12:33

>>59
doesn't handle the -v flag, 0/10

Name: Anonymous 2015-07-16 15:46

>>58
www.perltutorials.com/t/how-to-implement-a-cat-oneliner-in-perl-fuck-you

Name: Anonymous 2015-07-16 16:29

>>62
Doesn't load without JS.

Name: Anonymous 2016-06-08 18:57

>>62
Doesn't load even with JS.

Name: Anonymous 2016-06-08 21:32

Name: Anonymous 2016-06-08 22:35

>>65
Check 'em

Name: Anonymous 2016-06-08 23:06

>>48
>>66
Nice.

Name: Anonymous 2017-01-12 22:36

>>39
Now that's what I call a ``bloated cat"!

Name: Anonymous 2017-01-12 23:21

while(1) { putc(getc)); }

when it stops printing just hit Ctrl-c

Name: Anonymous 2017-01-12 23:34

>>69
Doesn't work on my machine.

Name: Anonymous 2017-01-13 0:33

is it possible to implement with pipe or dup2 or something?

Name: Anonymous 2017-01-13 22:16

>>69
0/10, does not catenate files.

>>71
No, somebody (cat) still has to actually move the data around. Dupping stdin on top of stdout does nothing because the file descriptors are local to the process.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List