thard code list of plan 9 services in case they are not in /etc/services - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit b589fce2fbf05ae18a6f015240f87ce2a163521f
 (DIR) parent 26a5fd572556f6d875c18fff8e83ed5eed6cf8fa
 (HTM) Author: rsc <devnull@localhost>
       Date:   Fri, 11 Feb 2005 16:58:23 +0000
       
       hard code list of plan 9 services in case they are not in /etc/services
       
       Diffstat:
         M src/lib9/_p9dialparse.c             |      36 ++++++++++++++++++++++++++++---
         M src/lib9/create.c                   |       4 ++++
         M src/lib9/fcallfmt.c                 |      22 +++++++++++++++++-----
         M src/lib9/mkfile                     |       3 +++
         A src/lib9/netcrypt.c                 |      18 ++++++++++++++++++
         M src/lib9/open.c                     |       4 ++++
         A src/lib9/readcons.c                 |     102 +++++++++++++++++++++++++++++++
         A src/lib9/syslog.c                   |     119 +++++++++++++++++++++++++++++++
       
       8 files changed, 300 insertions(+), 8 deletions(-)
       ---
 (DIR) diff --git a/src/lib9/_p9dialparse.c b/src/lib9/_p9dialparse.c
       t@@ -10,6 +10,24 @@
        static char *nets[] = { "tcp", "udp", nil };
        #define CLASS(p) ((*(uchar*)(p))>>6)
        
       +static struct {
       +        char *net;
       +        char *service;
       +        int port;
       +} porttbl[] = {
       +        "tcp",        "9fs",        564,
       +        "tcp",        "whoami",        565,
       +        "tcp",        "guard",        566,
       +        "tcp",        "ticket",        567,
       +        "tcp",        "exportfs",        17007,
       +        "tcp",        "rexexec",        17009,
       +        "tcp",        "ncpu",        17010,
       +        "tcp",        "cpu",        17013,
       +        "tcp",        "venti",        17034,
       +        "tcp",        "wiki",        17035,
       +        "tcp",        "secstore",        5356,
       +};
       +
        static int
        parseip(char *host, u32int *pip)
        {
       t@@ -103,7 +121,7 @@ p9dialparse(char *addr, char **pnet, char **punix, u32int *phost, int *pport)
                if(strcmp(net, "unix") == 0)
                        goto Unix;
        
       -        if(strcmp(net, "tcp")!=0 && strcmp(net, "udp")!=0){
       +        if(strcmp(net, "tcp")!=0 && strcmp(net, "udp")!=0 && strcmp(net, "net") != 0){
                        werrstr("bad network %s!%s!%s", net, host, port);
                        return -1;
                }
       t@@ -129,7 +147,19 @@ p9dialparse(char *addr, char **pnet, char **punix, u32int *phost, int *pport)
                                        return 0;
                                }
                        }
       -                werrstr("unknown service %s", port);
       +        }
       +
       +        for(i=0; i<nelem(porttbl); i++){
       +                if(strcmp(net, "net") == 0 || strcmp(porttbl[i].net, net) == 0)
       +                if(strcmp(porttbl[i].service, port) == 0){
       +                        *pnet = porttbl[i].net;
       +                        *pport = porttbl[i].port;
       +                        return 0;
       +                }
       +        }
       +
       +        if(strcmp(net, "net") == 0){
       +                werrstr("unknown service net!*!%s", port);
                        return -1;
                }
        
       t@@ -149,6 +179,6 @@ p9dialparse(char *addr, char **pnet, char **punix, u32int *phost, int *pport)
                        *pport = ntohs(se->s_port);
                        return 0;
                }
       -        werrstr("unknown service %s", port);
       +        werrstr("unknown service %s!*!%s", net, port);
                return -1;
        }
 (DIR) diff --git a/src/lib9/create.c b/src/lib9/create.c
       t@@ -43,6 +43,10 @@ p9create(char *path, int mode, ulong perm)
                                umode |= O_EXCL;
                                mode &= ~OEXCL;
                        }
       +                if(mode&OAPPEND){
       +                        umode |= O_APPEND;
       +                        mode &= ~OAPPEND;
       +                }
                        if(mode){
                                werrstr("unsupported mode in create");
                                goto out;
 (DIR) diff --git a/src/lib9/fcallfmt.c b/src/lib9/fcallfmt.c
       t@@ -216,15 +216,26 @@ dumpsome(char *ans, char *e, char *buf, long count)
                if(count > DUMPL)
                        count = DUMPL;
                for(i=0; i<count && printable; i++)
       -                if((buf[i]<32 && buf[i] !='\n' && buf[i] !='\t') || (uchar)buf[i]>127)
       +                if((buf[i]!=0 && buf[i]<32) || (uchar)buf[i]>127)
                                printable = 0;
                p = ans;
                *p++ = '\'';
                if(printable){
       -                if(count > e-p-2)
       -                        count = e-p-2;
       -                memmove(p, buf, count);
       -                p += count;
       +                if(2*count > e-p-2)
       +                        count = (e-p-2)/2;
       +                for(i=0; i<count; i++){
       +                        if(buf[i] == 0){
       +                                *p++ = '\\';
       +                                *p++ = '0';
       +                        }else if(buf[i] == '\t'){
       +                                *p++ = '\\';
       +                                *p++ = 't';
       +                        }else if(buf[i] == '\n'){
       +                                *p++ = '\\';
       +                                *p++ = 'n';
       +                        }else
       +                                *p++ = buf[i];
       +                }
                }else{
                        if(2*count > e-p-2)
                                count = (e-p-2)/2;
       t@@ -237,5 +248,6 @@ dumpsome(char *ans, char *e, char *buf, long count)
                }
                *p++ = '\'';
                *p = 0;
       +        assert(p < e);
                return p - ans;
        }
 (DIR) diff --git a/src/lib9/mkfile b/src/lib9/mkfile
       t@@ -117,6 +117,7 @@ LIB9OFILES=\
                nan.$O\
                needsrcquote.$O\
                needstack.$O\
       +        netcrypt.$O\
                netmkaddr.$O\
                notify.$O\
                nrand.$O\
       t@@ -130,6 +131,7 @@ LIB9OFILES=\
                quote.$O\
                rand.$O\
                read9pmsg.$O\
       +        readcons.$O\
                readn.$O\
                rfork.$O\
                searchpath.$O\
       t@@ -139,6 +141,7 @@ LIB9OFILES=\
                strdup.$O\
                strecpy.$O\
                sysfatal.$O\
       +        syslog.$O\
                sysname.$O\
                time.$O\
                tokenize.$O\
 (DIR) diff --git a/src/lib9/netcrypt.c b/src/lib9/netcrypt.c
       t@@ -0,0 +1,18 @@
       +#include <u.h>
       +#include <libc.h>
       +#include <auth.h>
       +
       +int
       +netcrypt(void *key, void *chal)
       +{
       +        uchar buf[8], *p;
       +
       +        strncpy((char*)buf, chal, 7);
       +        buf[7] = '\0';
       +        for(p = buf; *p && *p != '\n'; p++)
       +                ;
       +        *p = '\0';
       +        encrypt(key, buf, 8);
       +        sprint(chal, "%.2ux%.2ux%.2ux%.2ux", buf[0], buf[1], buf[2], buf[3]);
       +        return 1;
       +}
 (DIR) diff --git a/src/lib9/open.c b/src/lib9/open.c
       t@@ -32,6 +32,10 @@ p9open(char *name, int mode)
                        umode |= O_NONBLOCK;
                        mode ^= ONONBLOCK;
                }
       +        if(mode&OAPPEND){
       +                umode |= O_APPEND;
       +                mode ^= OAPPEND;
       +        }
                if(mode){
                        werrstr("mode 0x%x not supported", mode);
                        return -1;
 (DIR) diff --git a/src/lib9/readcons.c b/src/lib9/readcons.c
       t@@ -0,0 +1,102 @@
       +#include <u.h>
       +#define NOPLAN9DEFINES
       +#include <libc.h>
       +#include <termios.h>
       +#include <sys/termios.h>
       +
       +static int
       +rawx(int fd, int echoing)
       +{
       +        int was;
       +        static struct termios ttmode;
       +
       +        if(echoing == -1)
       +                return -1;
       +
       +        if(tcgetattr(fd, &ttmode) < 0)
       +                return -1;
       +        was = (ttmode.c_lflag&(ECHO|ICANON));
       +        ttmode.c_lflag &= ~(ECHO|ICANON);
       +        ttmode.c_lflag |= echoing;
       +        if(tcsetattr(fd, TCSANOW, &ttmode) < 0)
       +                return -1;
       +        return was;
       +}
       +
       +char*
       +readcons(char *prompt, char *def, int secret)
       +{
       +        int fd, n, raw;
       +        char line[10];
       +        char *s, *t;
       +        int l;
       +
       +        if((fd = open("/dev/tty", ORDWR)) < 0)
       +                return nil;
       +
       +        raw = -1;
       +        if(secret){
       +                raw = rawx(fd, 0);
       +                if(raw == -1)
       +                        return nil;
       +        }
       +
       +        if(def)
       +                fprint(fd, "%s[%s]: ", prompt, def);
       +        else
       +                fprint(fd, "%s: ", prompt);
       +
       +        s = strdup("");
       +        if(s == nil)
       +                return nil;
       +
       +        for(;;){
       +                n = read(fd, line, 1);
       +                if(n < 0){
       +                Error:
       +                        if(secret){
       +                                rawx(fd, raw);
       +                                write(fd, "\n", 1);
       +                        }
       +                        close(fd);
       +                        free(s);
       +                        return nil;
       +                }
       +                if(n > 0 && line[0] == 0x7F)
       +                        goto Error;
       +                if(n == 0 || line[0] == 0x04 || line[0] == '\n' || line[0] == '\r'){
       +                        if(secret){
       +                                rawx(fd, raw);
       +                                write(fd, "\n", 1);
       +                        }
       +                        close(fd);
       +                        if(*s == 0 && def){
       +                                free(s);
       +                                s = strdup(def);
       +                        }
       +                        return s;
       +                }
       +                if(line[0] == '\b'){
       +                        if(strlen(s) > 0)
       +                                s[strlen(s)-1] = 0;
       +                }else if(line[0] == 0x15){        /* ^U: line kill */
       +                        if(def != nil)
       +                                fprint(fd, "\n%s[%s]: ", prompt, def);
       +                        else
       +                                fprint(fd, "\n%s: ", prompt);
       +                        s[0] = 0;
       +                }else{
       +                        l = strlen(s);
       +                        t = malloc(l+2);
       +                        if(t)
       +                                memmove(t, s, l);
       +                        memset(s, 'X', l);
       +                        free(s);
       +                        if(t == nil)
       +                                return nil;
       +                        t[l] = line[0];
       +                        t[l+1] = 0;
       +                        s = t;
       +                }
       +        }
       +}
 (DIR) diff --git a/src/lib9/syslog.c b/src/lib9/syslog.c
       t@@ -0,0 +1,119 @@
       +#include <u.h>
       +#include <libc.h>
       +
       +static struct
       +{
       +        int        fd;
       +        int        consfd;
       +        char        *name;
       +        Dir        *d;
       +        Dir        *consd;
       +        Lock        lk;
       +} sl =
       +{
       +        -1, -1,
       +};
       +
       +static void
       +_syslogopen(void)
       +{
       +        char buf[1024], *p;
       +
       +        if(sl.fd >= 0)
       +                close(sl.fd);
       +        snprint(buf, sizeof(buf), "#9/log/%s", sl.name);
       +        p = unsharp(buf);
       +        sl.fd = open(p, OWRITE|OCEXEC|OAPPEND);
       +        free(p);
       +}
       +
       +/*
       + * Print
       + *  sysname: time: mesg
       + * on /sys/log/logname.
       + * If cons or log file can't be opened, print on the system console, too.
       + */
       +void
       +syslog(int cons, char *logname, char *fmt, ...)
       +{
       +        char buf[1024];
       +        char *ctim, *p;
       +        va_list arg;
       +        int n;
       +        Dir *d;
       +        char err[ERRMAX];
       +
       +        err[0] = '\0';
       +        errstr(err, sizeof err);
       +        lock(&sl.lk);
       +
       +        /*
       +         *  paranoia makes us stat to make sure a fork+close
       +         *  hasn't broken our fd's
       +         */
       +        d = dirfstat(sl.fd);
       +        if(sl.fd < 0
       +           || sl.name == nil
       +           || strcmp(sl.name, logname)!=0
       +           || sl.d == nil
       +           || d == nil
       +           || d->dev != sl.d->dev
       +           || d->type != sl.d->type
       +           || d->qid.path != sl.d->qid.path){
       +                free(sl.name);
       +                sl.name = strdup(logname);
       +                if(sl.name == nil)
       +                        cons = 1;
       +                else{
       +                        _syslogopen();
       +                        if(sl.fd < 0)
       +                                cons = 1;
       +                        free(sl.d);
       +                        sl.d = d;
       +                        d = nil;        /* don't free it */
       +                }
       +        }
       +        free(d);
       +        if(cons){
       +                d = dirfstat(sl.consfd);
       +                if(sl.consfd < 0
       +                   || d == nil
       +                   || sl.consd == nil
       +                   || d->dev != sl.consd->dev
       +                   || d->type != sl.consd->type
       +                   || d->qid.path != sl.consd->qid.path){
       +                        sl.consfd = open("/dev/tty", OWRITE|OCEXEC);
       +                        free(sl.consd);
       +                        sl.consd = d;
       +                        d = nil;        /* don't free it */
       +                }
       +                free(d);
       +        }
       +
       +        if(fmt == nil){
       +                unlock(&sl.lk);
       +                return;
       +        }
       +
       +        ctim = ctime(time(0));
       +        werrstr(err);
       +        p = buf + snprint(buf, sizeof(buf)-1, "%s ", sysname());
       +        strncpy(p, ctim+4, 15);
       +        p += 15;
       +        *p++ = ' ';
       +        va_start(arg, fmt);
       +        p = vseprint(p, buf+sizeof(buf)-1, fmt, arg);
       +        va_end(arg);
       +        *p++ = '\n';
       +        n = p - buf;
       +
       +        if(sl.fd >= 0){
       +                seek(sl.fd, 0, 2);
       +                write(sl.fd, buf, n);
       +        }
       +
       +        if(cons && sl.consfd >=0)
       +                write(sl.consfd, buf, n);
       +
       +        unlock(&sl.lk);
       +}