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

Pages: 1-

Pick your poison

Name: Anonymous 2011-06-24 5:46


 switch(fd)
 {
  case 0:
   f = freopen(_PATH_DEVNULL, r, stdin);
  break;
  case 1:
   f = freopen(_PATH_DEVNULL, w, stdout);
  break;
  case 2:
   f = freopen(_PATH_DEVNULL, w, stderr);
  break;
  default:
   f = NULL;
  break; 
 }



 f = (fd == 0) ? freopen(_PATH_DEVNULL, r, stdin)
               : (fd == 1) ? freopen(_PATH_DEVNULL, w, stdout)
                           : (fd == 2) ? freopen(_PATH_DEVNULL, w, stderr)
                                       : NULL;

Name: Anonymous 2011-06-24 6:01

The second snippet is a bit too Lispy to me. This sort of thing works great in macros however.

Name: Anonymous 2011-06-24 6:22

typedef struct {
char* mode;
FILE* stream;
} write_out;

write_out methods[3] = {
   { "r", stdin },
   { "w", stdout },
   { "w", stderr }
};

freopen(_PATH_DEVNULL, methods[fd].mode, methods[fd].stream);

Name: Anonymous 2011-06-24 6:25

>>3
where the default case be, bro?

Name: Anonymous 2011-06-24 6:49

Name: CL noob 2011-06-24 6:51

(if (eql fd 0)
  (freopen _PATH_DEVNULL r stdin)
  (if (eql fd 1)
    (freopen _PATH_DEVNULL w stdout)
    (if (eql fd 2)
      (freopen _PATH_DEVNULL w stderr))))


Did I do it right?

Name: Anonymous 2011-06-24 9:03

>>4
f = NULL;
if(fd >= 0 && fd < 3)
   f = freopen(_PATH_DEVNULL, methods[fd].mode, methods[fd].stream);

Name: Anonymous 2011-06-24 9:14

#define pd0 freopen(_PATH_DEVNULL,
f=!fd?pd0##r,stdin):fd==1?pd0##w,stdout):fd==2?pd0##w,stderr):NULL;

Name: Anonymous 2011-06-24 9:22

>>3
solution is very good, I think, elegant and efficient.

>>1
I like the switch-solution more because it looks clearer.

Name: Anonymous 2011-06-24 9:25

enum std {stdin,stdout,stderr}
f=freopen(_PATH_DEVNULL,fd?w:r,std[fd])

Name: Anonymous 2011-06-24 12:45

you program like a fag and your shits all retarded.
just do it like this:

if(x==1)gaysex();
if(x==2)fuckoff();
if(x==3)suckdicks();

Name: Anonymous 2011-06-24 13:00


#ifndef CODE_TAGS_IN_USE
//fuck you ``faggot"
#error "fuck you ``faggot\""
#endif

Name: Anonymous 2011-06-24 18:58

_PATH_DEVNULL ?

Motherfucking GNU

Name: Anonymous 2011-06-24 19:04

>>13
#include <pathnames.h>

Name: Anonymous 2011-06-24 19:11

>>12
\"
Those are not ``proper quotes''.

Name: Anonymous 2011-06-24 19:22

>>15
Yes, they are.

Name: Anonymous 2011-06-24 22:37

“These are”

Name: Anonymous 2011-06-25 7:53


f = NULL;
if (fd >= 0 && fd <= 2)
    f = ((FILE*[]){ freopen(_PATH_DEVNULL, r, stdin)
                  , freopen(_PATH_DEVNULL, w, stdout)
                  , freopen(_PATH_DEVNULL, w, stderr)
                  })[fd];

Name: sage 2011-06-25 8:51

>>14
#include <paths.h>
Fuck GNU and their non-portable bullshit.

Name: Anonymous 2011-06-26 5:55

>>7
>>18

Why would these be any better than the switch solution?
Looking at the assembly output of all, the switch solution fares better.

Name: Anonymous 2011-06-26 12:01


switch (fd) {
#define HAX(anus, m, s) case (anus): f = freopen(_PATH_DEVNULL, (m), (s)); break
    HAX(0, "r", stdin);
    HAX(1, "w", stdout);
    HAX(2, "w", stderr);
#undef HAX
    default:
        f = NULL;
}

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