Comparing fd with zero for validity is a bad idea. We have been bitten with this already and I wanted to prevent it in the future. Reference: /n/sources/patch/fd-zero Date: Mon Apr 23 11:16:37 CES 2012 Signed-off-by: paurea@lsub.org --- /sys/src/cmd/aux/gps/gpsfs.c Mon Apr 23 11:15:17 2012 +++ /sys/src/cmd/aux/gps/gpsfs.c Mon Apr 23 11:15:13 2012 @@ -573,12 +573,12 @@ void rtcset(long t) { - static int fd; + static int fd = -1; long r; int n; char buf[32]; - if(fd <= 0 && (fd = open("#r/rtc", ORDWR)) < 0){ + if(fd < 0 && (fd = open("#r/rtc", ORDWR)) < 0){ fprint(2, "Can't open #r/rtc: %r\n"); return; } --- /sys/src/cmd/aux/gps/util.c Mon Apr 23 11:15:21 2012 +++ /sys/src/cmd/aux/gps/util.c Mon Apr 23 11:15:18 2012 @@ -145,12 +145,12 @@ static void rtcset(long t) /* We may use this some day */ { - static int fd; + static int fd = -1; long r; int n; char buf[32]; - if(fd <= 0 && (fd = open("#r/rtc", ORDWR)) < 0){ + if(fd < 0 && (fd = open("#r/rtc", ORDWR)) < 0){ fprint(2, "Can't open #r/rtc: %r\n"); return; } --- /sys/src/cmd/ndb/dnresolve.c Mon Apr 23 11:15:28 2012 +++ /sys/src/cmd/ndb/dnresolve.c Mon Apr 23 11:15:23 2012 @@ -646,7 +646,7 @@ mp->qdcount = mp->ancount = mp->nscount = mp->arcount = 0; } -/* timed read of reply. sets srcip. ibuf must be 64K to handle tcp answers. */ +/* timed read of reply. sets srcip */ static int readnet(Query *qp, int medium, uchar *ibuf, uvlong endms, uchar **replyp, uchar *srcip) @@ -666,7 +666,7 @@ memset(srcip, 0, IPaddrlen); alarm(ms); if (medium == Udp) - if (qp->udpfd <= 0) + if (qp->udpfd < 0) dnslog("readnet: qp->udpfd closed"); else { len = read(qp->udpfd, ibuf, Udphdrsize+Maxudpin); @@ -683,7 +683,7 @@ if (!qp->tcpset) dnslog("readnet: tcp params not set"); fd = qp->tcpfd; - if (fd <= 0) + if (fd < 0) dnslog("readnet: %s: tcp fd unset for dest %I", qp->dp->name, qp->tcpip); else if (readn(fd, lenbuf, 2) != 2) { @@ -1003,7 +1003,7 @@ } close(nfd); - if (qp->udpfd <= 0) + if (qp->udpfd < 0) dnslog("mydnsquery: qp->udpfd %d closed", qp->udpfd); else { if (write(qp->udpfd, udppkt, len+Udphdrsize) != @@ -1527,6 +1527,7 @@ static ulong lastmount; /* use alloced buffers rather than ones from the stack */ + // ibuf = emalloc(Maxudpin+Udphdrsize); ibuf = emalloc(64*1024); /* max. tcp reply size */ obuf = emalloc(Maxudp+Udphdrsize); --- /sys/src/cmd/sam/mesg.c Mon Apr 23 11:15:33 2012 +++ /sys/src/cmd/sam/mesg.c Mon Apr 23 11:15:30 2012 @@ -86,9 +86,9 @@ void journal(int out, char *s) { - static int fd = 0; + static int fd = -1; - if(fd <= 0) + if(fd < 0) fd = create("/tmp/sam.out", 1, 0666L); fprint(fd, "%s%s\n", out? "out: " : "in: ", s); } @@ -393,7 +393,7 @@ if(f->name.s[0] == 0) error(Enoname); Strduplstr(&genstr, &f->name); - writef(f); + xwritef(f); break; case Tclose: