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

Cats

Name: Anonymous 2011-09-26 2:54

Tell me about your cat(s), /prog/.

Name: Anonymous 2011-09-26 3:08

$ whatis cat
cat (1)         - concatenate files and print on the standard output
$

Name: Anonymous 2011-09-26 3:30

my cat hsa a long neck and he picks fish out of the water w/ his beak.

Name: Anonymous 2011-09-26 3:58

>>2

LoseThos does not have streams, more or less.  It's supposed to be for recreational programming.  Streams and pipes are more often for admins.  In the actual act of programming and debugging, do you find you use them?  For video games, you don't.  For more adult things, you use printf() and cmd line, but pipes?  LoseThos has printf.

asm is a mark of IQ
interrupts are a mark of IQ
pipes are a mark of IQ
pipes and regexs are a mark of IQ
spelling and grammar is a mark of IQ
wit is not a mark of IQ -- wisdom.

They are pretty-much IQ by common concensus.

Name: Anonymous 2011-09-26 3:58

wisdom is not IQ

Name: Anonymous 2011-09-26 4:25

Chen

Name: Anonymous 2011-09-26 4:26

I opened the window and the sun came in, my cat sits on the table soaking up the sun

Name: Cudder !MhMRSATORI!FBeUS42x4uM+kgp 2011-09-26 4:42

https://gist.github.com/665971
All the cats you want.

Here's Anonix 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;
}

Name: Anonymous 2011-09-26 4:52

>>8
It... isn't seriously reading the data into memory?
That's just fucking stupid.

Name: Anonymous 2011-09-26 4:57

>>9
They all do that.

Name: VIPPER 2011-09-26 5:13

>>8
That looks really messy.

Name: Anonymous 2011-09-26 8:25

>>11
Of course it is. Is C

Name: Anonymous 2011-09-26 8:51

Everything is messy under the hood. You just don't know it if you don't write in a low-level language.

Name: Anonymous 2011-09-26 8:58

>>13
In fact i do. I'm now in the greener pastures of dynamic languages

Name: FrozenVoid 2011-09-26 9:32

>>13
I tend to check asm output for each change to get the feel of what treachery each compiler tries to do. I don't program in asm, but i have the tables/opcode latencies and their args/call/regs and i can handle checking small functions very closely for optimization.

Name: Anonymous 2011-09-26 9:55

>one-width indentations
>space indentations
>lack of spaces in syntax
>having options in cat (this is POSIX invalid)
>putting the asterisks next to the base type
>taking more than ~10 lines for cat
PIG DISGUSTING

Name: Anonymous 2011-09-26 10:06

>>16

I was referring to >>8 by the way.

Here's a better version:

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


>waaaah, you don't have -e!
Use printf. This is not cat's purpose.

>waaaah, you don't have -n!
Use printf. This is not cat's purpose.

>waaaah, you don't have --squeeze-blank/--show-tabs/--show-nonprinting/--gnu-bloat-option-frice!
Fuck you.

Name: Anonymous 2011-09-26 10:15

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

Name: Anonymous 2011-09-26 10:15

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

Name: Anonymous 2011-09-26 10:16

>>18

Fixed segfaulting loop.

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

Name: Anonymous 2011-09-26 10:16

>>20

Newlines are nice.

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

Name: Anonymous 2011-09-26 10:19

>>21

Flush your buffers between files.

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

Name: Anonymous 2011-09-26 10:21

>having options in cat (this is POSIX invalid)

http://pubs.opengroup.org/onlinepubs/009695399/utilities/cat.html

OPTIONS
The cat utility shall conform to the Base Definitions volume of IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.

The following option shall be supported:

-u Write bytes from the input file to the standard output without delay as each is read.

Name: Anonymous 2011-09-26 10:22

>>23
What if I have a file named ``-u''?

Name: Anonymous 2011-09-26 10:23

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

Name: Anonymous 2011-09-26 10:26

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.389s


I do wonder why GNU cat is so much faster than my cat.

Name: Anonymous 2011-09-26 10:26

>>24

you can use ``./-u''. I was so confused this one time I had to delete a file that began with a hyphen.

Name: Anonymous 2011-09-26 10:28

>>24
cat -- -u

http://pubs.opengroup.org/onlinepubs/009604499/basedefs/xbd_chap12.html

Guideline 10:
The argument -- should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the '-' character. The -- argument should not be used as an option or as an operand.
--

And if you're wondering "what if I have a file named '--'", that would be cat -- --.

>>17-22
These are going to be slow as fuck because they're all reading and writing one char at a time.

Name: Anonymous 2011-09-26 10:28

>>28
Oh, so that's why it can only do 72.1 MB/s. >>26

Name: Anonymous 2011-09-26 10:30

>>29
Time the Anonix version >>8. I bet it'll be at least twice as fast, if not more. GNU cat is doing some weird voodoo.

Name: Anonymous 2011-09-26 10:31

>>30
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


Your code is invalid C.

Name: Anonymous 2011-09-26 10:49

>>31
Not mine. It compiles fine here.


$ 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
$

Name: Anonymous 2011-09-26 10:52

>>32
That's because you're not using -Wall -Werror -Wextra -ansi -pedantic which is the minimum required to ensure good code.

Name: Anonymous 2011-09-26 11:03

>>33
Similarly, -O3 -ffast-math -march=native -mtune=native is required for benchmarks.

Name: Anonymous 2011-09-26 11:04

>>34
-O3 is bloat and cache-miss-happy. -ffast-math is unsafe. -march implicitly implies -mtune, making -mtune unnecessary.

Good flags:

-Wall -Werror -Wextra -ansi -pedantic -O2 -march=native

Name: Anonymous 2011-09-26 11:32

SUSSIX cat uses open, mmap and fstat.

I guess that's why it's so fast.

Name: Anonymous 2011-09-26 11:33

>>36
I wonder if cat could benefit from multi-threading.

Name: FrozenVoid 2011-09-26 11:50

>>37
Adding complexity is not justified just because you want that 5% speedup. You have to think about what you need to multithread, unlike some people who make 10 threads which eat up cpu time doing 1 threads work and decreasing OS performance.

Name: Anonymous 2011-09-26 12:15

>>17
if (argc == 1)
    goto no_arguments;


It's sort of like something in the PS3 supervisor's code:

if (gamec == 0)
    goto no_games;

Name: Anonymous 2011-09-26 12:17

>>37
>implying extra threads would make an I/O bound program go any faster

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