Switch to snprintf. - sam - An updated version of the sam text editor.
 (HTM) git clone git://vernunftzentrum.de/sam.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
 (DIR) commit 69c9fdc656233873a00c4281143f2b663f78885b
 (DIR) parent 19daa6ce19b0fd7e5abe0bf72a95ff8801b36717
 (HTM) Author: Rob King <jking@deadpixi.com>
       Date:   Tue,  4 Oct 2016 11:11:12 -0500
       
       Switch to snprintf.
       
       Diffstat:
         include/libc.h                      |       3 ---
         sam/address.c                       |       2 +-
         sam/error.c                         |       6 +++---
         sam/mesg.c                          |       2 +-
         sam/regexp.c                        |       2 +-
         sam/sam.c                           |       8 +++-----
         sam/unix.c                          |       4 ++--
         samterm/mesg.c                      |       8 ++------
         samterm/unix.c                      |      39 +++++++------------------------
       
       9 files changed, 22 insertions(+), 52 deletions(-)
       ---
 (DIR) diff --git a/include/libc.h b/include/libc.h
       @@ -10,14 +10,11 @@
        #define runetochar(s, r) (wctomb((s), (r)))
        #define runelen(r)       (wctomb(NULL, (r)))
        
       -#define sprint              sprintf
        #define dup(a,b)            dup2(a,b)
        #define seek(a,b,c)         lseek(a,b,c)
        #define create(name, mode, perm)    creat(name, perm)
        #define exec(a,b)           execv(a,b)
        
       -#define getuser() (getenv("LOGNAME") ? getenv("LOGNAME") : "unknown")
       -
        /*
         * new rune routines
         */
 (DIR) diff --git a/sam/address.c b/sam/address.c
       @@ -147,7 +147,7 @@ filematch(File *f, String *r)
            String *t;
        
            c = Strtoc(&f->name);
       -    sprint(buf, "%c%c%c %s\n", " '"[f->state==Dirty],
       +    snprintf(buf, sizeof(buf) - 1, "%c%c%c %s\n", " '"[f->state==Dirty],
                "-+"[f->rasp!=0], " ."[f==curfile], c);
            free(c);
            t = tmpcstr(buf);
 (DIR) diff --git a/sam/error.c b/sam/error.c
       @@ -70,7 +70,7 @@ error(Err s)
        {
            char buf[512];
        
       -    sprint(buf, "?%s", emsg[s]);
       +    snprintf(buf, sizeof(buf) - 1, "?%s", emsg[s]);
            hiccough(buf);
        }
        
       @@ -79,7 +79,7 @@ error_s(Err s, char *a)
        {
            char buf[512];
        
       -    sprint(buf, "?%s \"%s\"", emsg[s], a);
       +    snprintf(buf, sizeof(buf) - 1, "?%s \"%s\"", emsg[s], a);
            hiccough(buf);
        }
        
       @@ -88,7 +88,7 @@ error_c(Err s, int c)
        {
            char buf[512];
        
       -    sprint(buf, "?%s `%c'", emsg[s], c);
       +    snprintf(buf, sizeof(buf) - 1, "?%s `%c'", emsg[s], c);
            hiccough(buf);
        }
        
 (DIR) diff --git a/sam/mesg.c b/sam/mesg.c
       @@ -88,7 +88,7 @@ void
        journaln(int out, int64_t n)
        {
            char buf[32];
       -    sprint(buf, PRId64, n);
       +    snprintf(buf, sizeof(buf) - 1, PRId64, n);
            journal(out, buf);
        }
        #else
 (DIR) diff --git a/sam/regexp.c b/sam/regexp.c
       @@ -237,7 +237,7 @@ cant(char *s)
        {
            char buf[100];
        
       -    sprint(buf, "regexp: can't happen: %s", s);
       +    snprintf(buf, sizeof(buf) - 1, "regexp: can't happen: %s", s);
            panic(buf);
        }
        
 (DIR) diff --git a/sam/sam.c b/sam/sam.c
       @@ -100,9 +100,7 @@ main(int argc, char *argv[])
            Strinit0(&wd);
            tempfile.listptr = emalloc(0);
            Strinit0(&plan9cmd);
       -    home = getenv("HOME");
       -    if(home == 0)
       -        home = "/";
       +    home = getenv("HOME") ? getenv("HOME") : "/";
            shpath = getenv("SHELL") ? getenv("SHELL") : shpath;
            sh = basename(shpath);
            if(!dflag)
       @@ -152,7 +150,7 @@ rescue(void)
                if(f==cmd || f->nrunes==0 || f->state!=Dirty)
                    continue;
                if(io == -1){
       -            sprint(buf, "%s/sam.save", home);
       +            snprintf(buf, sizeof(buf) - 1, "%s/sam.save", home);
                    io = create(buf, 1, 0700);
                    if(io<0)
                        return;
       @@ -170,7 +168,7 @@ rescue(void)
                    buf[sizeof buf-1] = 0;
                    free(c);
                }else
       -            sprint(buf, "nameless.%d", nblank++);
       +            snprintf(buf, sizeof(buf) - 1, "nameless.%d", nblank++);
                dprintf(io, "samsave %s <<'---%s'\n", buf, buf);
                addr.r.p1 = 0, addr.r.p2 = f->nrunes;
                writeio(f);
 (DIR) diff --git a/sam/unix.c b/sam/unix.c
       @@ -164,10 +164,10 @@ void
        dprint(char *z, ...)
        {
            va_list args;
       -    char buf[BLOCKSIZE];
       +    char buf[BLOCKSIZE + 1] = {0};
        
            va_start(args, z);
       -    vsprintf(buf, z, args);
       +    vsnprintf(buf, BLOCKSIZE, z, args);
            termwrite(buf);
            va_end(args);
        }
 (DIR) diff --git a/samterm/mesg.c b/samterm/mesg.c
       @@ -294,19 +294,15 @@ inmesg(Hmesg type, int count)
                outT0(Tack);
                break;
        
       -#ifndef NOFIFO
            case Hextcmd:
       -        if (exname != NULL)
       -        {
       +        if (exname[0]){
                    int fifofd = open(exname, O_WRONLY);
       -            if (fifofd >= 0)
       -            {
       +            if (fifofd >= 0){
                        dprintf(fifofd, "%511s", (char *)indata);
                        close(fifofd);
                    }
                }
                break;
       -#endif
        
            case Hexit:
                outT0(Texit);
 (DIR) diff --git a/samterm/unix.c b/samterm/unix.c
       @@ -10,7 +10,7 @@
        #include <errno.h>
        #include <signal.h>
        
       -char *exname = NULL;
       +char exname[PATH_MAX + 1] = {0};
        static char *fallbacks[] = {
            "*scrollForwardR: true",
            "*geometry: 740x780",
       @@ -59,50 +59,30 @@ dumperrmsg(int count, int type, int count0, int c)
        void
        removeextern(void)
        {
       -    if (exname) {
       -        (void)unlink(exname);
       -        exname = 0;
       -    }
       +    unlink(exname);
       +    exname[0] = 0;
        }
       -/*
       - *  some systems do not support non-blocking i/o on named pipes
       - *  or do not provide working POSIX interfaces to the pipes.
       - *  in that case, add the name of the system to the 'ifdef' that
       - *  disables the code at the beginning of the function.
       - *  The external 'B' command will not work.
       - */
        
        void
        extstart(void)
        {
       -    if (nofifo)
       -        return;
       -
       -#ifndef NOFIFO
            extern char *machine;
       -    char    *user;
       -    char    *home;
            int fd;
            int flags;
        
       -    user = getenv("LOGNAME") ? getenv("LOGNAME") : getenv("USER") ? getenv("USER") : "unknown";
       -    home = getenv("HOME");
       -
       -    if (home == NULL)
       +    if (nofifo || !getenv("HOME"))
                return;
        
       -    exname = (char *)alloc(4 + 6 + strlen(home) + 1 + strlen(user) + 1 + strlen(machine) + 100);
       -    sprint(exname, "%s/.sam.%s", home, machine);
       +    snprintf(exname, PATH_MAX, "%s/.sam.%s", getenv("HOME"), machine);
        
            /* Make the named pipe. */
       -    if (mkfifo(exname, 0600) == -1) {
       +    if (mkfifo(exname, 0600) == -1){
                struct stat statb;
       -        extern int  errno;
        
                if (errno != EEXIST || stat(exname, &statb) == -1)
                    return;
        
       -        if (!S_ISFIFO(statb.st_mode)) {
       +        if (!S_ISFIFO(statb.st_mode)){
                    removeextern();
                    if (mkfifo(exname, 0600) == -1)
                        return;
       @@ -118,10 +98,10 @@ extstart(void)
            /*
             * Turn off no-delay and provide ourselves as a lingering
             * writer so as not to get end of file on read.
       -         */ 
       +     */ 
            flags = fcntl(fd, F_GETFL, 0);
            if (flags == -1 || fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == -1
       -            || open(exname, O_WRONLY) == -1) {
       +            || open(exname, O_WRONLY) == -1){
                (void)close(fd);
                removeextern();
                return;
       @@ -129,5 +109,4 @@ extstart(void)
        
            estart(Eextern, fd, 8192);
            atexit(removeextern);
       -#endif
        }