Pick your poison
1
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;
2
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.
3
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);
4
Name:
Anonymous
2011-06-24 6:25
>>3
where the default case be, bro?
5
Name:
Anonymous
2011-06-24 6:49
6
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?
7
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);
8
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;
9
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.
10
Name:
Anonymous
2011-06-24 9:25
enum std {stdin,stdout,stderr}
f=freopen(_PATH_DEVNULL,fd?w:r,std[fd])
11
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();
12
Name:
Anonymous
2011-06-24 13:00
#ifndef CODE_TAGS_IN_USE
//fuck you ``faggot"
#error "fuck you ``faggot\""
#endif
13
Name:
Anonymous
2011-06-24 18:58
_PATH_DEVNULL ?
Motherfucking GNU
14
Name:
Anonymous
2011-06-24 19:04
>>13
#include <pathnames.h>
15
Name:
Anonymous
2011-06-24 19:11
>>12
\"
Those are not ``proper quotes''.
16
Name:
Anonymous
2011-06-24 19:22
17
Name:
Anonymous
2011-06-24 22:37
“These are”
18
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];
19
Name:
sage
2011-06-25 8:51
>>14
#include <paths.h>
Fuck GNU and their non-portable bullshit.
20
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.
21
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;
}