diff options
author | tb <tb@cvs.openbsd.org> | 2016-03-07 22:49:46 +0000 |
---|---|---|
committer | tb <tb@cvs.openbsd.org> | 2016-03-07 22:49:46 +0000 |
commit | 83d1092dcaabbcf5daf5af2c72e3a97ecf30d2c6 (patch) | |
tree | 60df02891d2a686c57e3f540cf7c45ec86337e31 /games/fortune | |
parent | 13b1316938a89d6f72c8a68d61f279dde7d353a0 (diff) |
eliminate do_malloc() and do_free().
These are wrappers for malloc(3) and free(3) with NULL checks. do_free()
is pointless since free() already checks for NULL. do_malloc() is used
only three times, once asprintf(3) seems more appropriate, and for just
two calls the benefit of a custom wrapper is minimal.
ok millert@
Diffstat (limited to 'games/fortune')
-rw-r--r-- | games/fortune/fortune/fortune.c | 48 |
1 files changed, 9 insertions, 39 deletions
diff --git a/games/fortune/fortune/fortune.c b/games/fortune/fortune/fortune.c index 2b2cd3d6e65..4d9cd4242e8 100644 --- a/games/fortune/fortune/fortune.c +++ b/games/fortune/fortune/fortune.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fortune.c,v 1.54 2016/03/07 19:49:38 tb Exp $ */ +/* $OpenBSD: fortune.c,v 1.55 2016/03/07 22:49:45 tb Exp $ */ /* $NetBSD: fortune.c,v 1.8 1995/03/23 08:28:40 cgd Exp $ */ /*- @@ -113,8 +113,6 @@ int add_file(int, void all_forts(FILEDESC *, char *); char *copy(char *, char *); void display(FILEDESC *); -void do_free(void *); -void *do_malloc(size_t); int form_file_list(char **, int); int fortlen(void); void get_fort(void); @@ -384,11 +382,8 @@ add_file(int percent, char *file, char *dir, FILEDESC **head, FILEDESC **tail, path = file; was_malloc = 0; } else { - size_t len; - - len = strlen(dir) + strlen(file) + 2; - path = do_malloc(len); - snprintf(path, len, "%s/%s", dir, file); + if (asprintf(&path, "%s/%s", dir, file) == -1) + err(1, NULL); was_malloc = 1; } if ((isdir = is_dir(path)) && parent != NULL) { @@ -459,9 +454,9 @@ over: path); if (was_malloc) free(path); - do_free(fp->datfile); + free(fp->datfile); free((char *) fp); - do_free(offensive); + free(offensive); return 0; } /* @@ -497,7 +492,8 @@ new_fp(void) { FILEDESC *fp; - fp = do_malloc(sizeof *fp); + if ((fp = malloc(sizeof *fp)) == NULL) + err(1, NULL); fp->datfd = -1; fp->pos = POS_UNKNOWN; fp->inf = NULL; @@ -712,33 +708,6 @@ copy(char *str, char *suf) } /* - * do_malloc: - * Do a malloc, checking for NULL return. - */ -void * -do_malloc(size_t size) -{ - void *new; - - if ((new = malloc(size)) == NULL) { - (void) fprintf(stderr, "fortune: out of memory.\n"); - exit(1); - } - return new; -} - -/* - * do_free: - * Free malloc'ed space, if any. - */ -void -do_free(void *ptr) -{ - if (ptr != NULL) - free(ptr); -} - -/* * init_prob: * Initialize the fortune probabilities. */ @@ -1127,7 +1096,8 @@ find_matches(void) Fort_len = maxlen_in_list(File_list); DPRINTF(2, (stderr, "Maximum length is %zu\n", Fort_len)); /* extra length, "%\n" is appended */ - Fortbuf = do_malloc(Fort_len + 10); + if ((Fortbuf = malloc(Fort_len + 10)) == NULL) + err(1, NULL); Found_one = 0; matches_in_list(File_list); |