Hello tech@,
Attached are 6 patches to do some initial cleanup in vi (these
complement the perr patch that got comitted earlier). They are intended
to be applied in sequential order, because there may be slight overlap,
but they (almost) all do more or less the same thing: Remove custom code
with their libc counterpart.
1) 1_vi_prefer_errx.diff: don't use fprintf+exit if we can call errx
2) 2_vi_remove_v_estr.diff: v_estr is practically warn and warnx combined.
3) 3_vi_remove_progname.diff: Don't keep a copy of the progname in
memory, use getprogname instead.
4) 4_vi_remove_tail.diff: Use basename instead of tail
5) 5_vi_remove_v_strdup.diff: Use strdup instead of v_strdup.
6) 6_vi_use_tmp.diff: /var/tmp is dead, long live /tmp
The first four patches have also been accepted by the nvi2 project.
I haven't offered the fifth patch, since they also included a v_wstrdup
function which takes CHAR_T, which in their code can be an u_char or a
wchar_t depending on the compile options and I don't want to get
entangled in there.
The 6th patch should speak for itself.
Martijn
diff --git cl/cl_main.c cl/cl_main.c
index 8cf9045..ed4abda 100644
--- cl/cl_main.c
+++ cl/cl_main.c
@@ -39,7 +39,7 @@ static CL_PRIVATE *cl_init(GS *);
static GS *gs_init(char *);
static int setsig(int, struct sigaction *, void (*)(int));
static void sig_end(GS *);
-static void term_init(char *, char *);
+static void term_init(char *);
/*
* main --
@@ -73,7 +73,7 @@ main(int argc, char *argv[])
*/
if ((ttype = getenv("TERM")) == NULL)
ttype = "unknown";
- term_init(gp->progname, ttype);
+ term_init(ttype);
/* Add the terminal type to the global structure. */
if ((OG_D_STR(gp, GO_TERM) =
@@ -211,7 +211,7 @@ tcfail: err(1, "tcgetattr");
* Initialize terminal information.
*/
static void
-term_init(char *name, char *ttype)
+term_init(char *ttype)
{
int err;
@@ -219,13 +219,10 @@ term_init(char *name, char *ttype)
setupterm(ttype, STDOUT_FILENO, &err);
switch (err) {
case -1:
- (void)fprintf(stderr,
- "%s: No terminal database found\n", name);
+ errx(1, "No terminal database found\n");
exit (1);
case 0:
- (void)fprintf(stderr,
- "%s: %s: unknown terminal type\n", name, ttype);
- exit (1);
+ errx(1, "%s: unknown terminal type\n", ttype);
}
}
diff --git common/main.c common/main.c
index f0e2093..25decbf 100644
--- common/main.c
+++ common/main.c
@@ -17,6 +17,7 @@
#include <sys/time.h>
#include <bitstring.h>
+#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
@@ -32,8 +33,7 @@
#ifdef DEBUG
static void attach(GS *);
#endif
-static void v_estr(char *, int, char *);
-static int v_obsolete(char *, char *[]);
+static int v_obsolete(char *[]);
/*
* editor --
@@ -108,7 +108,7 @@ editor(GS *gp, int argc, char *argv[])
}
/* Convert old-style arguments into new-style ones. */
- if (v_obsolete(gp->progname, argv))
+ if (v_obsolete(argv))
return (1);
/* Parse the arguments. */
@@ -136,8 +136,7 @@ editor(GS *gp, int argc, char *argv[])
* We should support multiple -c options.
*/
if (gp->c_option != NULL) {
- v_estr(gp->progname, 0,
- "only one -c command may be specified.");
+ warnx("only one -c command may be specified.");
return (1);
}
gp->c_option = optarg;
@@ -152,8 +151,7 @@ editor(GS *gp, int argc, char *argv[])
attach(gp);
break;
default:
- v_estr(gp->progname, 0,
- "-D requires s or w argument.");
+ warnx("-D requires s or w argument.");
return (1);
}
break;
@@ -173,7 +171,7 @@ editor(GS *gp, int argc, char *argv[])
break;
case 'r': /* Recover. */
if (flagchk == 't') {
- v_estr(gp->progname, 0,
+ warnx(
"only one of -r and -t may be specified.");
return (1);
}
@@ -188,7 +186,7 @@ editor(GS *gp, int argc, char *argv[])
#ifdef DEBUG
case 'T': /* Trace. */
if ((gp->tracefp = fopen(optarg, "w")) == NULL) {
- v_estr(gp->progname, errno, optarg);
+ warn("%s", optarg);
goto err;
}
(void)fprintf(gp->tracefp,
@@ -197,13 +195,12 @@ editor(GS *gp, int argc, char *argv[])
#endif
case 't': /* Tag. */
if (flagchk == 'r') {
- v_estr(gp->progname, 0,
+ warnx(
"only one of -r and -t may be specified.");
return (1);
}
if (flagchk == 't') {
- v_estr(gp->progname, 0,
- "only one tag file may be specified.");
+ warnx("only one tag file may be specified.");
return (1);
}
flagchk = 't';
@@ -236,7 +233,7 @@ editor(GS *gp, int argc, char *argv[])
* If not reading from a terminal, it's like -s was specified.
*/
if (silent && !LF_ISSET(SC_EX)) {
- v_estr(gp->progname, 0, "-s option is only applicable to ex.");
+ warnx("-s option is only applicable to ex.");
goto err;
}
if (LF_ISSET(SC_EX) && F_ISSET(gp, G_SCRIPTED))
@@ -363,7 +360,7 @@ editor(GS *gp, int argc, char *argv[])
l = strlen(sp->frp->name) + 1;
MALLOC_NOMSG(sp, *--argv, l);
if (*argv == NULL) {
- v_estr(gp->progname, errno, NULL);
+ warn(NULL);
goto err;
}
(void)strlcpy(*argv, sp->frp->name, l);
@@ -534,7 +531,7 @@ v_end(GS *gp)
* Convert historic arguments into something getopt(3) will like.
*/
static int
-v_obsolete(char *name, char *argv[])
+v_obsolete(char *argv[])
{
size_t len;
char *p;
@@ -572,7 +569,7 @@ v_obsolete(char *name, char *argv[])
if (argv[0][1] == '\0') {
argv[0] = strdup("-s");
if (argv[0] == NULL) {
-nomem: v_estr(name, errno, NULL);
+nomem: warn(NULL);
return (1);
}
} else
@@ -592,7 +589,7 @@ attach(GS *gp)
char ch;
if ((fd = open(_PATH_TTY, O_RDONLY, 0)) < 0) {
- v_estr(gp->progname, errno, _PATH_TTY);
+ warn("%s", _PATH_TTY);
return;
}
@@ -609,14 +606,3 @@ attach(GS *gp)
(void)close(fd);
}
#endif
-
-static void
-v_estr(char *name, int eno, char *msg)
-{
- (void)fprintf(stderr, "%s", name);
- if (msg != NULL)
- (void)fprintf(stderr, ": %s", msg);
- if (eno)
- (void)fprintf(stderr, ": %s", strerror(errno));
- (void)fprintf(stderr, "\n");
-}
diff --git cl/cl_main.c cl/cl_main.c
index ed4abda..cc48ad3 100644
--- cl/cl_main.c
+++ cl/cl_main.c
@@ -19,6 +19,7 @@
#include <err.h>
#include <errno.h>
#include <fcntl.h>
+#include <libgen.h>
#include <paths.h>
#include <signal.h>
#include <stdio.h>
@@ -36,7 +37,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 *);
@@ -59,8 +60,10 @@ main(int argc, char *argv[])
if (reenter++)
abort();
+ /* Set progname for compatibility reasons */
+ setprogname(basename(argv[0]));
/* 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 +144,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 common/gs.h common/gs.h
index d1e94de..c47173f 100644
--- common/gs.h
+++ 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 common/main.c common/main.c
index 25decbf..028e1c7 100644
--- common/main.c
+++ 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 common/recover.c common/recover.c
index 6d6f1e1..c7f9796 100644
--- common/recover.c
+++ 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 common/log.c common/log.c
index a423c55..7d19db8 100644
--- common/log.c
+++ common/log.c
@@ -18,6 +18,7 @@
#include <bitstring.h>
#include <errno.h>
#include <fcntl.h>
+#include <libgen.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
@@ -635,7 +636,7 @@ log_err(SCR *sp, char *file, int line)
{
EXF *ep;
- msgq(sp, M_SYSERR, "%s/%d: log put error", tail(file), line);
+ msgq(sp, M_SYSERR, "%s/%d: log put error", basename(file), line);
ep = sp->ep;
(void)ep->log->close(ep->log);
if (!log_init(sp, ep))
diff --git common/util.c common/util.c
index ffb3a27..2c32fc5 100644
--- common/util.c
+++ common/util.c
@@ -97,22 +97,6 @@ nonblank(SCR *sp, recno_t lno, size_t *cnop)
}
/*
- * tail --
- * Return tail of a path.
- *
- * PUBLIC: char *tail(char *);
- */
-char *
-tail(char *path)
-{
- char *p;
-
- if ((p = strrchr(path, '/')) == NULL)
- return (path);
- return (p + 1);
-}
-
-/*
* v_strdup --
* Strdup for wide character strings with an associated length.
*
diff --git include/com_extern.h include/com_extern.h
index 35fd59a..4160a4c 100644
--- include/com_extern.h
+++ 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 *tail(char *);
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);
diff --git vi/vs_refresh.c vi/vs_refresh.c
index 5f316a1..e8befe0 100644
--- vi/vs_refresh.c
+++ vi/vs_refresh.c
@@ -17,6 +17,7 @@
#include <bitstring.h>
#include <ctype.h>
+#include <libgen.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
@@ -473,7 +474,7 @@ adjust: if (!O_ISSET(sp, O_LEFTRIGHT) &&
/* Sanity checking. */
if (CNO >= len && len != 0) {
msgq(sp, M_ERR, "Error: %s/%d: cno (%u) >= len (%u)",
- tail(__FILE__), __LINE__, CNO, len);
+ baneame(__FILE__), __LINE__, CNO, len);
return (1);
}
#endif
diff --git common/screen.c common/screen.c
index 701b87c..a5cece5 100644
--- common/screen.c
+++ common/screen.c
@@ -76,15 +76,15 @@ 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)
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)
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)
goto mem;
sp->repl_len = orig->repl_len;
if (orig->newl_len) {
diff --git common/seq.c common/seq.c
index 2703d18..beb1874 100644
--- common/seq.c
+++ common/seq.c
@@ -54,7 +54,7 @@ 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) {
sv_errno = errno;
goto mem1;
}
@@ -75,14 +75,14 @@ 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) {
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) {
sv_errno = errno;
goto mem3;
}
@@ -92,7 +92,7 @@ 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) {
sv_errno = errno;
free(qp->input);
mem3: if (qp->name != NULL)
diff --git common/util.c common/util.c
index 2c32fc5..9a33e45 100644
--- common/util.c
+++ 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 ex/ex_args.c ex/ex_args.c
index 9a1952f..1e16c8d 100644
--- ex/ex_args.c
+++ ex/ex_args.c
@@ -86,7 +86,7 @@ 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)
return (1);
*ap = NULL;
@@ -297,7 +297,7 @@ 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) {
free(s_argv);
return (NULL);
}
@@ -305,7 +305,7 @@ 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) {
while (--ap >= s_argv)
free(*ap);
free(s_argv);
diff --git ex/ex_file.c ex/ex_file.c
index 0cc25fa..b5cd3f2 100644
--- ex/ex_file.c
+++ ex/ex_file.c
@@ -44,7 +44,7 @@ ex_file(SCR *sp, EXCMD *cmdp)
frp = sp->frp;
/* Make sure can allocate enough space. */
- if ((p = v_strdup(sp,
+ if ((p = strndup(
cmdp->argv[0]->bp, cmdp->argv[0]->len)) == NULL)
return (1);
diff --git ex/ex_init.c ex/ex_init.c
index cfa2ccc..5e08459 100644
--- ex/ex_init.c
+++ ex/ex_init.c
@@ -283,14 +283,14 @@ 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)
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)
return (1);
ecp->if_lno = 1;
F_SET(ecp, E_NAMEDISCARD);
diff --git ex/ex_read.c ex/ex_read.c
index b61a56b..26143ee 100644
--- ex/ex_read.c
+++ ex/ex_read.c
@@ -214,7 +214,7 @@ 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) {
free(sp->frp->name);
sp->frp->name = p;
diff --git ex/ex_write.c ex/ex_write.c
index 23f1f7f..c079a63 100644
--- ex/ex_write.c
+++ ex/ex_write.c
@@ -226,8 +226,8 @@ 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,
- cmdp->argv[1]->bp, cmdp->argv[1]->len)) != NULL) {
+ if ((p = strndup(cmdp->argv[1]->bp,
+ cmdp->argv[1]->len)) != NULL) {
free(sp->frp->name);
sp->frp->name = p;
}
diff --git include/com_extern.h include/com_extern.h
index 4160a4c..7b7ad0c 100644
--- include/com_extern.h
+++ 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 vi/v_init.c vi/v_init.c
index a360a68..be64671 100644
--- vi/v_init.c
+++ vi/v_init.c
@@ -57,7 +57,7 @@ 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)
return (1);
nvip->lastckey = ovip->lastckey;
diff --git build/pathnames.h build/pathnames.h
index 6797458..3eea916 100644
--- build/pathnames.h
+++ build/pathnames.h
@@ -3,6 +3,6 @@
#define _PATH_EXRC ".exrc"
#define _PATH_NEXRC ".nexrc"
-#define _PATH_PRESERVE "/var/tmp/vi.recover"
+#define _PATH_PRESERVE "/tmp/vi.recover"
#define _PATH_SYSEXRC "/etc/vi.exrc"
#define _PATH_TAGS "tags"