summaryrefslogtreecommitdiff
path: root/usr.bin/mg
diff options
context:
space:
mode:
authorKjell Wooding <kjell@cvs.openbsd.org>2005-12-20 06:17:37 +0000
committerKjell Wooding <kjell@cvs.openbsd.org>2005-12-20 06:17:37 +0000
commitdc74e33818e7e64e60fca295fbaff47f4e5c206f (patch)
tree3fe3db244af810766dc26d5eb01c7d115d960c11 /usr.bin/mg
parent9039a07b0e8eda34651f5dde950c2f6b36f53852 (diff)
Clean up the ugly casted frees. In one case, this meant eliminating a nasty
struct/union/casting nightmare when building the list of names for filename completion. In particular, be consistent about strduping and freeing the list data.
Diffstat (limited to 'usr.bin/mg')
-rw-r--r--usr.bin/mg/buffer.c6
-rw-r--r--usr.bin/mg/def.h4
-rw-r--r--usr.bin/mg/echo.c14
-rw-r--r--usr.bin/mg/extend.c12
-rw-r--r--usr.bin/mg/file.c5
-rw-r--r--usr.bin/mg/fileio.c29
-rw-r--r--usr.bin/mg/funmap.c6
-rw-r--r--usr.bin/mg/line.c8
-rw-r--r--usr.bin/mg/macro.c6
-rw-r--r--usr.bin/mg/yank.c6
10 files changed, 47 insertions, 49 deletions
diff --git a/usr.bin/mg/buffer.c b/usr.bin/mg/buffer.c
index dd8db834f31..0332f583045 100644
--- a/usr.bin/mg/buffer.c
+++ b/usr.bin/mg/buffer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: buffer.c,v 1.53 2005/12/13 06:01:26 kjell Exp $ */
+/* $OpenBSD: buffer.c,v 1.54 2005/12/20 06:17:35 kjell Exp $ */
/* This file is in the public domain. */
@@ -184,7 +184,7 @@ killbuffer(struct buffer *bp)
rec = next;
}
- free((char *)bp->b_bname); /* Release name block */
+ free(bp->b_bname); /* Release name block */
free(bp); /* Release buffer block */
return (TRUE);
}
@@ -486,7 +486,7 @@ bfind(const char *bname, int cflag)
return (NULL);
}
if ((lp = lalloc(0)) == NULL) {
- free((char *) bp->b_bname);
+ free(bp->b_bname);
free(bp);
return (NULL);
}
diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h
index 04a6bfa9198..bf9c9b7cb7f 100644
--- a/usr.bin/mg/def.h
+++ b/usr.bin/mg/def.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: def.h,v 1.81 2005/12/13 07:20:13 kjell Exp $ */
+/* $OpenBSD: def.h,v 1.82 2005/12/20 06:17:36 kjell Exp $ */
/* This file is in the public domain. */
@@ -173,7 +173,7 @@ struct list {
struct buffer *x_bp; /* l_bp is used by struct line */
struct list *l_nxt;
} l_p;
- const char *l_name;
+ char *l_name;
};
/*
diff --git a/usr.bin/mg/echo.c b/usr.bin/mg/echo.c
index f10ae02cebb..8a39b1bceeb 100644
--- a/usr.bin/mg/echo.c
+++ b/usr.bin/mg/echo.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: echo.c,v 1.42 2005/12/13 06:01:27 kjell Exp $ */
+/* $OpenBSD: echo.c,v 1.43 2005/12/20 06:17:36 kjell Exp $ */
/* This file is in the public domain. */
@@ -99,7 +99,7 @@ eyesno(const char *sp)
maclcur = lp->l_bp;
maclcur->l_fp = lp->l_fp;
- free((char *)lp);
+ free(lp);
}
#endif /* !NO_MACRO */
if ((rep[0] == 'y' || rep[0] == 'Y') &&
@@ -594,7 +594,7 @@ complt_list(int flags, char *buf, int cpos)
int oldhue = tthue;
char *linebuf;
size_t linesize, len;
- const char *cp;
+ char *cp;
lh = NULL;
@@ -700,7 +700,8 @@ complt_list(int flags, char *buf, int cpos)
linebuf[0] = '\0';
width = 0;
}
- len = strlcat(linebuf, lh2->l_name + preflen, linesize);
+ len = strlcat(linebuf, lh2->l_name + preflen,
+ linesize);
width += maxwidth;
if (len < width && width < linesize) {
/* pad so the objects nicely line up */
@@ -923,6 +924,7 @@ free_file_list(struct list *lp)
while (lp) {
next = lp->l_next;
+ free(lp->l_name);
free(lp);
lp = next;
}
@@ -937,14 +939,16 @@ copy_list(struct list *lp)
while (lp) {
current = (struct list *)malloc(sizeof(struct list));
if (current == NULL) {
+ /* Free what we have allocated so far */
for (current = last; current; current = nxt) {
nxt = current->l_next;
+ free(current->l_name);
free(current);
}
return (NULL);
}
current->l_next = last;
- current->l_name = lp->l_name;
+ current->l_name = strdup(lp->l_name);
last = current;
lp = lp->l_next;
}
diff --git a/usr.bin/mg/extend.c b/usr.bin/mg/extend.c
index 217318ad870..a18c7d094a6 100644
--- a/usr.bin/mg/extend.c
+++ b/usr.bin/mg/extend.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: extend.c,v 1.43 2005/12/20 05:04:28 kjell Exp $ */
+/* $OpenBSD: extend.c,v 1.44 2005/12/20 06:17:36 kjell Exp $ */
/* This file is in the public domain. */
@@ -562,7 +562,7 @@ extend(int f, int n)
macro[macrocount - 1].m_funct = funct;
maclcur = lp->l_bp;
maclcur->l_fp = lp->l_fp;
- free((char *)lp);
+ free(lp);
}
#endif /* !NO_MACRO */
return ((*funct)(f, n));
@@ -881,10 +881,10 @@ excline(char *line)
if ((curmap = name_map(lp->l_text)) == NULL) {
ewprintf("No such mode: %s", lp->l_text);
status = FALSE;
- free((char *)lp);
+ free(lp);
goto cleanup;
}
- free((char *)lp);
+ free(lp);
bind = BINDARG;
break;
default:
@@ -925,10 +925,10 @@ cleanup:
lp = maclcur->l_fp;
while (lp != maclcur) {
np = lp->l_fp;
- free((char *)lp);
+ free(lp);
lp = np;
}
- free((char *)lp);
+ free(lp);
return (status);
}
diff --git a/usr.bin/mg/file.c b/usr.bin/mg/file.c
index 79e8fcfd57e..a6ed24c7aac 100644
--- a/usr.bin/mg/file.c
+++ b/usr.bin/mg/file.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: file.c,v 1.49 2005/12/20 05:04:28 kjell Exp $ */
+/* $OpenBSD: file.c,v 1.50 2005/12/20 06:17:36 kjell Exp $ */
/* This file is in the public domain. */
@@ -495,8 +495,7 @@ filewrite(int f, int n)
p++;
else
p = curbp->b_fname;
- if (curbp->b_bname)
- free((char *)curbp->b_bname);
+ free(curbp->b_bname);
curbp->b_bname = strdup(p);
curbp->b_flag &= ~(BFBAK | BFCHG);
upmodes(curbp);
diff --git a/usr.bin/mg/fileio.c b/usr.bin/mg/fileio.c
index 9cc81311d42..d3429f22cdb 100644
--- a/usr.bin/mg/fileio.c
+++ b/usr.bin/mg/fileio.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fileio.c,v 1.67 2005/12/20 05:04:28 kjell Exp $ */
+/* $OpenBSD: fileio.c,v 1.68 2005/12/20 06:17:36 kjell Exp $ */
/* This file is in the public domain. */
@@ -396,11 +396,6 @@ copy(char *frname, char *toname)
return (TRUE);
}
-struct filelist {
- struct list fl_l;
- char fl_name[NFILEN + 2];
-};
-
/*
* return list of file names that match the name in buf.
*/
@@ -411,8 +406,8 @@ make_file_list(char *buf)
int len, preflen, ret;
DIR *dirp;
struct dirent *dent;
- struct list *last;
- struct filelist *current;
+ struct list *last, *current;
+ char fl_name[NFILEN + 2];
char prefixx[NFILEN + 1];
/*
@@ -514,19 +509,19 @@ make_file_list(char *buf)
isdir = 1;
}
- current = malloc(sizeof(struct filelist));
- if (current == NULL)
- break;
-
- ret = snprintf(current->fl_name, sizeof(current->fl_name),
+ if ((current = malloc(sizeof(struct list))) == NULL) {
+ free_file_list(last);
+ return (NULL);
+ }
+ ret = snprintf(fl_name, sizeof(fl_name),
"%s%s%s", prefixx, dent->d_name, isdir ? "/" : "");
- if (ret < 0 || ret >= sizeof(current->fl_name)) {
+ if (ret < 0 || ret >= sizeof(fl_name)) {
free(current);
continue;
}
- current->fl_l.l_next = last;
- current->fl_l.l_name = current->fl_name;
- last = (struct list *) current;
+ current->l_next = last;
+ current->l_name = strdup(fl_name);
+ last = current;
}
closedir(dirp);
diff --git a/usr.bin/mg/funmap.c b/usr.bin/mg/funmap.c
index 8954c4bd29a..9b541b61bd5 100644
--- a/usr.bin/mg/funmap.c
+++ b/usr.bin/mg/funmap.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: funmap.c,v 1.22 2005/12/13 06:01:27 kjell Exp $ */
+/* $OpenBSD: funmap.c,v 1.23 2005/12/20 06:17:36 kjell Exp $ */
/*
* Copyright (c) 2001 Artur Grabowski <art@openbsd.org>. All rights reserved.
*
@@ -273,7 +273,7 @@ struct list *
complete_function_list(const char *fname)
{
struct funmap *fn;
- struct list *head, *el;
+ struct list *head, *el;
int len;
len = strlen(fname);
@@ -284,7 +284,7 @@ complete_function_list(const char *fname)
free_file_list(head);
return (NULL);
}
- el->l_name = fn->fn_name;
+ el->l_name = strdup(fn->fn_name);
el->l_next = head;
head = el;
}
diff --git a/usr.bin/mg/line.c b/usr.bin/mg/line.c
index f1bf4973c3c..3178bcc6b30 100644
--- a/usr.bin/mg/line.c
+++ b/usr.bin/mg/line.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: line.c,v 1.36 2005/12/20 05:04:28 kjell Exp $ */
+/* $OpenBSD: line.c,v 1.37 2005/12/20 06:17:36 kjell Exp $ */
/* This file is in the public domain. */
@@ -508,7 +508,7 @@ ldelnewline(void)
lp1->l_used += lp2->l_used;
lp1->l_fp = lp2->l_fp;
lp2->l_fp->l_bp = lp1;
- free((char *)lp2);
+ free(lp2);
return (TRUE);
}
if ((lp3 = lalloc(lp1->l_used + lp2->l_used)) == NULL)
@@ -535,8 +535,8 @@ ldelnewline(void)
wp->w_marko += lp1->l_used;
}
}
- free((char *)lp1);
- free((char *)lp2);
+ free(lp1);
+ free(lp2);
return (TRUE);
}
diff --git a/usr.bin/mg/macro.c b/usr.bin/mg/macro.c
index 90d39bc259e..74e888bd785 100644
--- a/usr.bin/mg/macro.c
+++ b/usr.bin/mg/macro.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: macro.c,v 1.10 2005/11/18 20:56:53 deraadt Exp $ */
+/* $OpenBSD: macro.c,v 1.11 2005/12/20 06:17:36 kjell Exp $ */
/* This file is in the public domain. */
@@ -37,9 +37,9 @@ definemacro(int f, int n)
if (maclhead != NULL) {
for (lp1 = maclhead->l_fp; lp1 != maclhead; lp1 = lp2) {
lp2 = lp1->l_fp;
- free((char *)lp1);
+ free(lp1);
}
- free((char *)lp1);
+ free(lp1);
}
if ((maclhead = lp1 = lalloc(0)) == NULL)
diff --git a/usr.bin/mg/yank.c b/usr.bin/mg/yank.c
index dd1a692153f..17962610ed8 100644
--- a/usr.bin/mg/yank.c
+++ b/usr.bin/mg/yank.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: yank.c,v 1.1 2005/11/22 05:02:44 kjell Exp $ */
+/* $OpenBSD: yank.c,v 1.2 2005/12/20 06:17:36 kjell Exp $ */
/* This file is in the public domain. */
@@ -30,7 +30,7 @@ void
kdelete(void)
{
if (kbufp != NULL) {
- free((char *)kbufp);
+ free(kbufp);
kbufp = NULL;
kstart = kused = ksize = 0;
}
@@ -84,7 +84,7 @@ kgrow(int dir)
nstart = (dir == KBACK) ? (kstart + KBLOCK) : (KBLOCK / 4);
bcopy(&(kbufp[kstart]), &(nbufp[nstart]), (int)(kused - kstart));
if (kbufp != NULL)
- free((char *)kbufp);
+ free(kbufp);
kbufp = nbufp;
ksize += KBLOCK;
kused = kused - kstart + nstart;