Here's a small update:
On 01/22/16 21:18, Martijn van Duren wrote:
3) 3_vi_remove_progname.diff: Don't keep a copy of the progname in
memory, use getprogname instead.
Attached without the setprogname. The reason I included setprogname was because getprogname in the manpage doesn't mention that it always returns a stripped version and it's a strict requirement of vi.
4) 4_vi_remove_tail.diff: Use basename instead of tail
The second basename is inside #define DEBUG, so I missed it in the compile check. Furthermore it's only used for msgq, which uses it it vsnprint and I can't imagine it can hurt in there. As for the input, it's used with a variable file and __FILE__, so it shouldn't cause any more harm than tail.
5) 5_vi_remove_v_strdup.diff: Use strdup instead of v_strdup.
Attached an updated patch that returns the msgq calls. I don't feel comfortable towards these, since msgq calls GET_SPACE_GOTO, which might call REALLOC if the buffer isn't big enough, which in turn calls msgq again, which ...
diff --git a/cl/cl_main.c b/cl/cl_main.c
index 11bdabb..34e9548 100644
--- a/cl/cl_main.c
+++ b/cl/cl_main.c
@@ -36,7 +36,7 @@ sigset_t __sigblockset;                               /* 
GLOBAL: Blocked signals. */
 
 static void       cl_func_std(GS *);
 static CL_PRIVATE *cl_init(GS *);
-static GS        *gs_init(char *);
+static GS        *gs_init(void);
 static int        setsig(int, struct sigaction *, void (*)(int));
 static void       sig_end(GS *);
 static void       term_init(char *);
@@ -60,7 +60,7 @@ main(int argc, char *argv[])
                abort();
 
        /* Create and initialize the global structure. */
-       __global_list = gp = gs_init(argv[0]);
+       __global_list = gp = gs_init();
 
        /* Create and initialize the CL_PRIVATE structure. */
        clp = cl_init(gp);
@@ -141,22 +141,15 @@ main(int argc, char *argv[])
  *     Create and partially initialize the GS structure.
  */
 static GS *
-gs_init(char *name)
+gs_init(void)
 {
        GS *gp;
-       char *p;
-
-       /* Figure out what our name is. */
-       if ((p = strrchr(name, '/')) != NULL)
-               name = p + 1;
 
        /* Allocate the global structure. */
        CALLOC_NOMSG(NULL, gp, 1, sizeof(GS));
        if (gp == NULL)
                err(1, NULL);
 
-
-       gp->progname = name;
        return (gp);
 }
 
diff --git a/common/gs.h b/common/gs.h
index d1e94de..c47173f 100644
--- a/common/gs.h
+++ b/common/gs.h
@@ -55,8 +55,6 @@ typedef enum { KEY_VEOF, KEY_VERASE, KEY_VKILL, KEY_VWERASE } 
scr_keyval_t;
  * Structure that describes global state of the running program.
  */
 struct _gs {
-       char    *progname;              /* Programe name. */
-
        int      id;                    /* Last allocated screen id. */
        TAILQ_HEAD(_dqh, _scr) dq;      /* Displayed screens. */
        TAILQ_HEAD(_hqh, _scr) hq;      /* Hidden screens. */
diff --git a/common/main.c b/common/main.c
index 25decbf..028e1c7 100644
--- a/common/main.c
+++ b/common/main.c
@@ -95,12 +95,12 @@ editor(GS *gp, int argc, char *argv[])
 
        /* Set initial screen type and mode based on the program name. */
        readonly = 0;
-       if (!strcmp(gp->progname, "ex") || !strcmp(gp->progname, "nex"))
+       if (!strcmp(getprogname(), "ex") || !strcmp(getprogname(), "nex"))
                LF_INIT(SC_EX);
        else {
                /* Nview, view are readonly. */
-               if (!strcmp(gp->progname, "nview") ||
-                   !strcmp(gp->progname, "view"))
+               if (!strcmp(getprogname(), "nview") ||
+                   !strcmp(getprogname(), "view"))
                        readonly = 1;
                
                /* Vi is the default. */
@@ -121,11 +121,11 @@ editor(GS *gp, int argc, char *argv[])
        F_SET(gp, G_SNAPSHOT);
 
        pmode = MODE_EX;
-       if (!strcmp(gp->progname, "ex"))
+       if (!strcmp(getprogname(), "ex"))
                pmode = MODE_EX;
-       else if (!strcmp(gp->progname, "vi"))
+       else if (!strcmp(getprogname(), "vi"))
                pmode = MODE_VI;
-       else if (!strcmp(gp->progname, "view"))
+       else if (!strcmp(getprogname(), "view"))
                pmode = MODE_VIEW;
 
        while ((ch = getopt(argc, argv, optstr[pmode])) != -1)
diff --git a/common/recover.c b/common/recover.c
index 6d6f1e1..c7f9796 100644
--- a/common/recover.c
+++ b/common/recover.c
@@ -397,8 +397,8 @@ rcv_mailfile(SCR *sp, int issync, char *cp_path)
            " was editing a file named ", t, " on the machine ",
            host, ", when it was saved for recovery. ",
            "You can recover most, if not all, of the changes ",
-           "to this file using the -r option to ", gp->progname, ":\n\n\t",
-           gp->progname, " -r ", t);
+           "to this file using the -r option to ", getprogname(), ":\n\n\t",
+           getprogname(), " -r ", t);
        if (len > sizeof(buf) - 1) {
 lerr:          msgq(sp, M_ERR, "Recovery file buffer overrun");
                goto err;
@@ -555,7 +555,7 @@ rcv_list(SCR *sp)
 next:          (void)fclose(fp);
        }
        if (found == 0)
-               (void)printf("%s: No files to recover\n", sp->gp->progname);
+               (void)printf("%s: No files to recover\n", getprogname());
        (void)closedir(dirp);
        return (0);
 }
diff --git a/common/screen.c b/common/screen.c
index 701b87c..911e337 100644
--- a/common/screen.c
+++ b/common/screen.c
@@ -76,16 +76,22 @@ screen_init(GS *gp, SCR *orig, SCR **spp)
                /* Retain searching/substitution information. */
                sp->searchdir = orig->searchdir == NOTSET ? NOTSET : FORWARD;
                if (orig->re != NULL && (sp->re =
-                   v_strdup(sp, orig->re, orig->re_len)) == NULL)
+                   strndup(orig->re, orig->re_len)) == NULL) {
+                       msgq(sp, M_SYSERR, NULL);
                        goto mem;
+               }
                sp->re_len = orig->re_len;
                if (orig->subre != NULL && (sp->subre =
-                   v_strdup(sp, orig->subre, orig->subre_len)) == NULL)
+                   strndup(orig->subre, orig->subre_len)) == NULL) {
+                       msgq(sp, M_SYSERR, NULL);
                        goto mem;
+               }
                sp->subre_len = orig->subre_len;
                if (orig->repl != NULL && (sp->repl =
-                   v_strdup(sp, orig->repl, orig->repl_len)) == NULL)
+                   strndup(orig->repl, orig->repl_len)) == NULL) {
+                       msgq(sp, M_SYSERR, NULL);
                        goto mem;
+               }
                sp->repl_len = orig->repl_len;
                if (orig->newl_len) {
                        len = orig->newl_len * sizeof(size_t);
diff --git a/common/seq.c b/common/seq.c
index 2703d18..b09cfb3 100644
--- a/common/seq.c
+++ b/common/seq.c
@@ -54,7 +54,8 @@ seq_set(SCR *sp, CHAR_T *name, size_t nlen, CHAR_T *input, 
size_t ilen,
                if (output == NULL || olen == 0) {
                        p = NULL;
                        olen = 0;
-               } else if ((p = v_strdup(sp, output, olen)) == NULL) {
+               } else if ((p = strndup(output, olen)) == NULL) {
+                       msgq(sp, M_SYSERR, NULL);
                        sv_errno = errno;
                        goto mem1;
                }
@@ -75,14 +76,16 @@ seq_set(SCR *sp, CHAR_T *name, size_t nlen, CHAR_T *input, 
size_t ilen,
        /* Name. */
        if (name == NULL || nlen == 0)
                qp->name = NULL;
-       else if ((qp->name = v_strdup(sp, name, nlen)) == NULL) {
+       else if ((qp->name = strndup(name, nlen)) == NULL) {
+               msgq(sp, M_SYSERR, NULL);
                sv_errno = errno;
                goto mem2;
        }
        qp->nlen = nlen;
 
        /* Input. */
-       if ((qp->input = v_strdup(sp, input, ilen)) == NULL) {
+       if ((qp->input = strndup(input, ilen)) == NULL) {
+               msgq(sp, M_SYSERR, NULL);
                sv_errno = errno;
                goto mem3;
        }
@@ -92,7 +95,8 @@ seq_set(SCR *sp, CHAR_T *name, size_t nlen, CHAR_T *input, 
size_t ilen,
        if (output == NULL) {
                qp->output = NULL;
                olen = 0;
-       } else if ((qp->output = v_strdup(sp, output, olen)) == NULL) {
+       } else if ((qp->output = strndup(output, olen)) == NULL) {
+               msgq(sp, M_SYSERR, NULL);
                sv_errno = errno;
                free(qp->input);
 mem3:          if (qp->name != NULL)
diff --git a/common/util.c b/common/util.c
index 2c32fc5..9a33e45 100644
--- a/common/util.c
+++ b/common/util.c
@@ -97,25 +97,6 @@ nonblank(SCR *sp, recno_t lno, size_t *cnop)
 }
 
 /*
- * v_strdup --
- *     Strdup for wide character strings with an associated length.
- *
- * PUBLIC: CHAR_T *v_strdup(SCR *, const CHAR_T *, size_t);
- */
-CHAR_T *
-v_strdup(SCR *sp, const CHAR_T *str, size_t len)
-{
-       CHAR_T *copy;
-
-       MALLOC(sp, copy, len + 1);
-       if (copy == NULL)
-               return (NULL);
-       memcpy(copy, str, len * sizeof(CHAR_T));
-       copy[len] = '\0';
-       return (copy);
-}
-
-/*
  * nget_uslong --
  *      Get an unsigned long, checking for overflow.
  *
diff --git a/ex/ex_args.c b/ex/ex_args.c
index 9a1952f..acc6640 100644
--- a/ex/ex_args.c
+++ b/ex/ex_args.c
@@ -86,8 +86,10 @@ ex_next(SCR *sp, EXCMD *cmdp)
                for (ap = sp->argv,
                    argv = cmdp->argv; argv[0]->len != 0; ++ap, ++argv)
                        if ((*ap =
-                           v_strdup(sp, argv[0]->bp, argv[0]->len)) == NULL)
+                           strndup(argv[0]->bp, argv[0]->len)) == NULL) {
+                               msgq(sp, M_SYSERR, NULL);
                                return (1);
+                       }
                *ap = NULL;
 
                /* Switch to the first file. */
@@ -297,7 +299,8 @@ ex_buildargv(SCR *sp, EXCMD *cmdp, char *name)
                return (NULL);
 
        if (cmdp == NULL) {
-               if ((*ap = v_strdup(sp, name, strlen(name))) == NULL) {
+               if ((*ap = strdup(name)) == NULL) {
+                       msgq(sp, M_SYSERR, NULL);
                        free(s_argv);
                        return (NULL);
                }
@@ -305,7 +308,8 @@ ex_buildargv(SCR *sp, EXCMD *cmdp, char *name)
        } else
                for (argv = cmdp->argv; argv[0]->len != 0; ++ap, ++argv)
                        if ((*ap =
-                           v_strdup(sp, argv[0]->bp, argv[0]->len)) == NULL) {
+                           strndup(argv[0]->bp, argv[0]->len)) == NULL) {
+                               msgq(sp, M_SYSERR, NULL);
                                while (--ap >= s_argv)
                                        free(*ap);
                                free(s_argv);
diff --git a/ex/ex_file.c b/ex/ex_file.c
index 0cc25fa..92c6ea5 100644
--- a/ex/ex_file.c
+++ b/ex/ex_file.c
@@ -44,9 +44,11 @@ ex_file(SCR *sp, EXCMD *cmdp)
                frp = sp->frp;
 
                /* Make sure can allocate enough space. */
-               if ((p = v_strdup(sp,
-                   cmdp->argv[0]->bp, cmdp->argv[0]->len)) == NULL)
+               if ((p = strndup(
+                   cmdp->argv[0]->bp, cmdp->argv[0]->len)) == NULL) {
+                       msgq(sp, M_SYSERR, NULL);
                        return (1);
+               }
 
                /* If already have a file name, it becomes the alternate. */
                if (!F_ISSET(frp, FR_TMPFILE))
diff --git a/ex/ex_init.c b/ex/ex_init.c
index cfa2ccc..a3f91ab 100644
--- a/ex/ex_init.c
+++ b/ex/ex_init.c
@@ -283,15 +283,19 @@ ex_run_str(SCR *sp, char *name, char *str, size_t len, 
int ex_flags,
        if (nocopy)
                ecp->cp = str;
        else
-               if ((ecp->cp = v_strdup(sp, str, len)) == NULL)
+               if ((ecp->cp = strndup(str, len)) == NULL) {
+                       msgq(sp, M_SYSERR, NULL);
                        return (1);
+               }
        ecp->clen = len;
 
        if (name == NULL)
                ecp->if_name = NULL;
        else {
-               if ((ecp->if_name = v_strdup(sp, name, strlen(name))) == NULL)
+               if ((ecp->if_name = strdup(name)) == NULL) {
+                       msgq(sp, M_SYSERR, NULL);
                        return (1);
+               }
                ecp->if_lno = 1;
                F_SET(ecp, E_NAMEDISCARD);
        }
diff --git a/ex/ex_read.c b/ex/ex_read.c
index b61a56b..f56a2aa 100644
--- a/ex/ex_read.c
+++ b/ex/ex_read.c
@@ -214,8 +214,9 @@ ex_read(SCR *sp, EXCMD *cmdp)
                         */
                        if (F_ISSET(sp->frp, FR_TMPFILE) &&
                            !F_ISSET(sp->frp, FR_EXNAMED)) {
-                               if ((p = v_strdup(sp, cmdp->argv[1]->bp,
+                               if ((p = strndup(cmdp->argv[1]->bp,
                                    cmdp->argv[1]->len)) != NULL) {
+                                       msgq(sp, M_SYSERR, NULL);
                                        free(sp->frp->name);
                                        sp->frp->name = p;
                                }
diff --git a/ex/ex_write.c b/ex/ex_write.c
index 23f1f7f..6db4a12 100644
--- a/ex/ex_write.c
+++ b/ex/ex_write.c
@@ -226,8 +226,9 @@ exwr(SCR *sp, EXCMD *cmdp, enum which cmd)
                 */
                if (F_ISSET(sp->frp, FR_TMPFILE) &&
                    !F_ISSET(sp->frp, FR_EXNAMED)) {
-                       if ((p = v_strdup(sp,
+                       if ((p = strndup(
                            cmdp->argv[1]->bp, cmdp->argv[1]->len)) != NULL) {
+                               msgq((sp), M_SYSERR, NULL);
                                free(sp->frp->name);
                                sp->frp->name = p;
                        }
diff --git a/include/com_extern.h b/include/com_extern.h
index 4160a4c..7b7ad0c 100644
--- a/include/com_extern.h
+++ b/include/com_extern.h
@@ -105,7 +105,6 @@ int seq_save(SCR *, FILE *, char *, seq_t);
 int e_memcmp(CHAR_T *, EVENT *, size_t);
 void *binc(SCR *, void *, size_t *, size_t);
 int nonblank(SCR *, recno_t, size_t *);
-CHAR_T *v_strdup(SCR *, const CHAR_T *, size_t);
 enum nresult nget_uslong(u_long *, const char *, char **, int);
 enum nresult nget_slong(long *, const char *, char **, int);
 void TRACE(SCR *, const char *, ...);
diff --git a/vi/v_init.c b/vi/v_init.c
index a360a68..dca150c 100644
--- a/vi/v_init.c
+++ b/vi/v_init.c
@@ -57,8 +57,10 @@ v_screen_copy(SCR *orig, SCR *sp)
 
                /* Copy the paragraph/section information. */
                if (ovip->ps != NULL && (nvip->ps =
-                   v_strdup(sp, ovip->ps, strlen(ovip->ps))) == NULL)
+                   strdup(ovip->ps)) == NULL) {
+                       msgq(sp, M_SYSERR, NULL);
                        return (1);
+               }
 
                nvip->lastckey = ovip->lastckey;
                nvip->csearchdir = ovip->csearchdir;

Reply via email to