From 88b091510b35c6e2195f4a3e0638a90b3b53023a Mon Sep 17 00:00:00 2001 From: Chris Allegretta Date: Thu, 17 May 2001 11:35:43 +0000 Subject: [PATCH] Changed all string allocations to charalloc(), new function designed to take nmalloc argument but call calloc based on (char *) size. git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@661 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- ChangeLog | 3 +++ cut.c | 18 +++++++++--------- files.c | 24 ++++++++++++------------ nano.c | 35 +++++++++++++++++------------------ proto.h | 3 ++- rcfile.c | 2 +- search.c | 10 +++++----- utils.c | 13 +++++++------ winio.c | 4 ++-- 9 files changed, 58 insertions(+), 54 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1608c376..67856081 100644 --- a/ChangeLog +++ b/ChangeLog @@ -20,6 +20,9 @@ Cvs code - - Added config.guess and config.sub to distribution because, apparently, newer autoconf/automakes can't live without them. - Various spelling updates by David Lawrence Ramsey. + - Changed all string allocations to charalloc(), new function + designed to take nmalloc argument but call calloc based on + (char *) size. - configure.in: - New option, --enable-nanorc, which allows people to have a .nanorc initialization file and set options normally used on the command diff --git a/cut.c b/cut.c index 487a92cb..69c7a0b3 100644 --- a/cut.c +++ b/cut.c @@ -66,13 +66,13 @@ void cut_marked_segment(filestruct * top, int top_x, filestruct * bot, /* Set up the beginning of the cutbuffer */ tmp = copy_node(top); - tmpstr = nmalloc(strlen(&top->data[top_x]) + 1); + tmpstr = charalloc(strlen(&top->data[top_x]) + 1); strcpy(tmpstr, &top->data[top_x]); free(tmp->data); tmp->data = tmpstr; /* Chop off the end of the first line */ - tmpstr = nmalloc(top_x + 1); + tmpstr = charalloc(top_x + 1); strncpy(tmpstr, top->data, top_x); free(top->data); top->data = tmpstr; @@ -90,7 +90,7 @@ void cut_marked_segment(filestruct * top, int top_x, filestruct * bot, if (next == NULL) return; /* Now, paste bot[bot_x] into top[top_x] */ - tmpstr = nmalloc(strlen(top->data) + strlen(&bot->data[bot_x])); + tmpstr = charalloc(strlen(top->data) + strlen(&bot->data[bot_x])); strncpy(tmpstr, top->data, top_x); strcpy(&tmpstr[top_x], &bot->data[bot_x]); free(top->data); @@ -182,7 +182,7 @@ int do_cut_text(void) tmp = copy_node(current); newsize = abs(mark_beginx - current_x) + 1; - tmpstr = nmalloc(newsize + 1); + tmpstr = charalloc(newsize + 1); if (current_x < mark_beginx) { strncpy(tmpstr, ¤t->data[current_x], newsize); memmove(¤t->data[current_x], @@ -237,7 +237,7 @@ int do_cut_text(void) } else { add_to_cutbuffer(fileptr); fileage = make_new_node(NULL); - fileage->data = nmalloc(1); + fileage->data = charalloc(1); fileage->data[0] = '\0'; current = fileage; } @@ -300,7 +300,7 @@ int do_uncut_text(void) /* If there's only one line in the cutbuffer */ if (cutbuffer->next == NULL) { tmpstr = - nmalloc(strlen(current->data) + strlen(cutbuffer->data) + + charalloc(strlen(current->data) + strlen(cutbuffer->data) + 1); strncpy(tmpstr, current->data, current_x); strcpy(&tmpstr[current_x], cutbuffer->data); @@ -315,13 +315,13 @@ int do_uncut_text(void) } else { /* yuck -- no kidding! */ tmp = current->next; /* New beginning */ - tmpstr = nmalloc(current_x + strlen(newbuf->data) + 1); + tmpstr = charalloc(current_x + strlen(newbuf->data) + 1); strncpy(tmpstr, current->data, current_x); strcpy(&tmpstr[current_x], newbuf->data); totsize += strlen(newbuf->data) + strlen(newend->data) + 1; /* New end */ - tmpstr2 = nmalloc(strlen(newend->data) + + tmpstr2 = charalloc(strlen(newend->data) + strlen(¤t->data[current_x]) + 1); strcpy(tmpstr2, newend->data); strcat(tmpstr2, ¤t->data[current_x]); @@ -371,7 +371,7 @@ int do_uncut_text(void) if (marked_cut == 2 && current_x != strlen(current->data)) { tmp = make_new_node(current); - tmp->data = nmalloc(strlen(¤t->data[current_x]) + 1); + tmp->data = charalloc(strlen(¤t->data[current_x]) + 1); strcpy(tmp->data, ¤t->data[current_x]); splice_node(current, tmp, current->next); null_at(current->data, current_x); diff --git a/files.c b/files.c index 2c688d05..ac626ba3 100644 --- a/files.c +++ b/files.c @@ -54,7 +54,7 @@ void load_file(void) void new_file(void) { fileage = nmalloc(sizeof(filestruct)); - fileage->data = nmalloc(1); + fileage->data = charalloc(1); strcpy(fileage->data, ""); fileage->prev = NULL; fileage->next = NULL; @@ -96,7 +96,7 @@ filestruct *read_line(char *buf, filestruct * prev, int *line1ins) filestruct *fileptr; fileptr = nmalloc(sizeof(filestruct)); - fileptr->data = nmalloc(strlen(buf) + 2); + fileptr->data = charalloc(strlen(buf) + 2); strcpy(fileptr->data, buf); if (*line1ins) { @@ -137,7 +137,7 @@ int read_file(int fd, char *filename) filestruct *fileptr = current, *tmp = NULL; int line1ins = 0; - buf = nmalloc(bufx); + buf = charalloc(bufx); buf[0] = '\0'; if (fileptr != NULL && fileptr->prev != NULL) { @@ -390,7 +390,7 @@ int write_file(char *name, int tmp) } /* Don't follow symlink. Create new file. */ else { - buf = nmalloc(strlen(realname) + 8); + buf = charalloc(strlen(realname) + 8); strncpy(buf, realname, strlen(realname)+1); strcat(buf, ".XXXXXX"); if ((fd = mkstemp(buf)) == -1) { @@ -603,7 +603,7 @@ char *real_dir_from_tilde(char *buf) if (getenv("HOME") != NULL) { free(dirtmp); - dirtmp = nmalloc(strlen(buf) + 2 + strlen(getenv("HOME"))); + dirtmp = charalloc(strlen(buf) + 2 + strlen(getenv("HOME"))); sprintf(dirtmp, "%s%s", getenv("HOME"), &buf[1]); @@ -627,7 +627,7 @@ char *real_dir_from_tilde(char *buf) if (userdata != NULL) { /* User found */ free(dirtmp); - dirtmp = nmalloc(strlen(buf) + 2 + strlen(userdata->pw_dir)); + dirtmp = charalloc(strlen(buf) + 2 + strlen(userdata->pw_dir)); sprintf(dirtmp, "%s%s", userdata->pw_dir, &buf[i]); } @@ -702,7 +702,7 @@ char **username_tab_completion(char *buf, int *num_matches) * This makes a lot more sense to me (Chris) this way... */ - matchline = nmalloc(strlen(userdata->pw_name) + 2); + matchline = charalloc(strlen(userdata->pw_name) + 2); sprintf(matchline, "~%s", userdata->pw_name); matches[*num_matches] = matchline; ++*num_matches; @@ -734,7 +734,7 @@ char **cwd_tab_completion(char *buf, int *num_matches) /* Okie, if there's a / in the buffer, strip out the directory part */ if (strcmp(buf, "") && strstr(buf, "/")) { - dirName = nmalloc(strlen(buf) + 1); + dirName = charalloc(strlen(buf) + 1); tmp = buf + strlen(buf); while (*tmp != '/' && tmp != buf) tmp--; @@ -793,7 +793,7 @@ char **cwd_tab_completion(char *buf, int *num_matches) * This makes a lot more sense to me (Chris) this way... */ tmp2 = NULL; - tmp2 = nmalloc(strlen(next->d_name) + 1); + tmp2 = charalloc(strlen(next->d_name) + 1); strcpy(tmp2, next->d_name); matches[*num_matches] = tmp2; ++*num_matches; @@ -958,7 +958,7 @@ char *input_tab(char *buf, int place, int *lastWasTab, int *newplace) if (longestname > COLS - 1) longestname = COLS - 1; - foo = nmalloc(longestname + 5); + foo = charalloc(longestname + 5); /* Print the list of matches */ for (i = 0, col = 0; i < num_matches; i++) { @@ -1063,7 +1063,7 @@ char **browser_init(char *path, int *longest, int *numents) while ((next = readdir(dir)) != NULL) { if (!strcmp(next->d_name, ".")) continue; - filelist[i] = nmalloc(strlen(next->d_name) + strlen(path) + 2); + filelist[i] = charalloc(strlen(next->d_name) + strlen(path) + 2); if (!strcmp(path, "/")) snprintf(filelist[i], strlen(next->d_name) + strlen(path) + 1, @@ -1158,7 +1158,7 @@ char *do_browser(char *inpath) path = mallocstrcpy(path, inpath); filelist = browser_init(path, &longest, &numents); - foo = nmalloc(longest + 8); + foo = charalloc(longest + 8); /* Sort the list by directory first, then alphabetically */ qsort(filelist, numents, sizeof(char *), diralphasort); diff --git a/nano.c b/nano.c index 61c3f554..bcccaf24 100644 --- a/nano.c +++ b/nano.c @@ -118,7 +118,7 @@ void die(char *msg, ...) i = write_file(name, 1); } else { - char *buf = nmalloc(strlen(filename) + 6); + char *buf = charalloc(strlen(filename) + 6); strcpy(buf, filename); strcat(buf, ".save"); i = write_file(buf, 1); @@ -160,7 +160,7 @@ void clear_filename(void) { if (filename != NULL) free(filename); - filename = nmalloc(1); + filename = charalloc(1); filename[0] = 0; } @@ -187,7 +187,7 @@ void global_init(void) if (fill < MIN_FILL_LENGTH) die_too_small(); - hblank = nmalloc(COLS + 1); + hblank = charalloc(COLS + 1); memset(hblank, ' ', COLS); hblank[COLS] = 0; } @@ -224,7 +224,7 @@ filestruct *copy_node(filestruct * src) filestruct *dst; dst = nmalloc(sizeof(filestruct)); - dst->data = nmalloc(strlen(src->data) + 1); + dst->data = charalloc(strlen(src->data) + 1); dst->next = src->next; dst->prev = src->prev; @@ -606,12 +606,12 @@ int do_enter(filestruct * inptr) current_x++; totsize++; } - newnode->data = nmalloc(strlen(tmp) + extra + 1); + newnode->data = charalloc(strlen(tmp) + extra + 1); strncpy(newnode->data, current->data, extra); strcpy(&newnode->data[extra], tmp); } } else { - newnode->data = nmalloc(strlen(tmp) + 1); + newnode->data = charalloc(strlen(tmp) + 1); strcpy(newnode->data, tmp); } *tmp = 0; @@ -809,7 +809,7 @@ void do_wrap(filestruct * inptr, char input_char) down = 1; } - temp->data = nmalloc(strlen(&inptr->data[current_word_start]) + 1); + temp->data = charalloc(strlen(&inptr->data[current_word_start]) + 1); strcpy(temp->data, &inptr->data[current_word_start]); inptr->data = nrealloc(inptr->data, last_word_end + 2); inptr->data[last_word_end + 1] = 0; @@ -817,7 +817,7 @@ void do_wrap(filestruct * inptr, char input_char) /* Category 1b: one word on the line and word not taking up whole line (i.e. there are spaces at the beginning of the line) */ if (last_word_end == -1) { - temp->data = nmalloc(strlen(&inptr->data[current_word_start]) + 1); + temp->data = charalloc(strlen(&inptr->data[current_word_start]) + 1); strcpy(temp->data, &inptr->data[current_word_start]); /* Inside word, remove it from original, and move cursor to right spot. */ @@ -848,7 +848,7 @@ void do_wrap(filestruct * inptr, char input_char) /* Case 2a: cursor before word at wrap point. */ if (current_x < current_word_start) { temp->data = - nmalloc(strlen(&inptr->data[current_word_start]) + 1); + charalloc(strlen(&inptr->data[current_word_start]) + 1); strcpy(temp->data, &inptr->data[current_word_start]); if (!isspace((int) input_char)) { @@ -871,7 +871,7 @@ void do_wrap(filestruct * inptr, char input_char) else if ((current_x >= current_word_start) && (current_x <= (current_word_end + 1))) { temp->data = - nmalloc(strlen(&inptr->data[current_word_start]) + 1); + charalloc(strlen(&inptr->data[current_word_start]) + 1); strcpy(temp->data, &inptr->data[current_word_start]); down = 1; @@ -907,7 +907,7 @@ void do_wrap(filestruct * inptr, char input_char) /* Case 2c: cursor past word at wrap point. */ else { temp->data = - nmalloc(strlen(&inptr->data[current_word_start]) + 1); + charalloc(strlen(&inptr->data[current_word_start]) + 1); strcpy(temp->data, &inptr->data[current_word_start]); down = 1; @@ -931,8 +931,7 @@ void do_wrap(filestruct * inptr, char input_char) /* Plus one for the space which concatenates the two lines together plus 1 for \0. */ char *p = - nmalloc((strlen(temp->data) + strlen(inptr->next->data) + 2) - * sizeof(char)); + charalloc((strlen(temp->data) + strlen(inptr->next->data) + 2)); if (ISSET(AUTOINDENT)) { int non = 0; @@ -991,7 +990,7 @@ void do_wrap(filestruct * inptr, char input_char) spc++; totsize++; } - t = nmalloc(strlen(temp->data) + extra + 1); + t = charalloc(strlen(temp->data) + extra + 1); strncpy(t, inptr->data, extra); strcpy(t + extra, temp->data); free(temp->data); @@ -1342,7 +1341,7 @@ int do_int_speller(char *tempfile_name) return FALSE; } - read_buff = nmalloc(pipe_buff_size + 1); + read_buff = charalloc(pipe_buff_size + 1); /* Process the returned spelling errors */ @@ -1960,7 +1959,7 @@ int do_justify(void) current->data[i] = '\0'; len2 = strlen(current->data + i + 1); - tmpline->data = nmalloc(len2 + 1); + tmpline->data = charalloc(len2 + 1); /* Skip the white space in current. */ memcpy(tmpline->data, current->data + i + 1, len2); @@ -2078,7 +2077,7 @@ void help_init(void) free(help_text); /* Allocate space for the help text */ - help_text = nmalloc(allocsize); + help_text = charalloc(allocsize); /* Now add the text we want */ strcpy(help_text, help_text_init); @@ -2332,7 +2331,7 @@ int main(int argc, char *argv[]) break; #ifndef DISABLE_SPELLER case 's': - alt_speller = nmalloc(strlen(optarg) + 1); + alt_speller = charalloc(strlen(optarg) + 1); strcpy(alt_speller, optarg); break; #endif diff --git a/proto.h b/proto.h index 3b26ef38..0be932dc 100644 --- a/proto.h +++ b/proto.h @@ -124,7 +124,6 @@ void center_cursor(void); void bottombars(shortcut s[], int slen); void blank_statusbar_refresh(void); void *nmalloc (size_t howmuch); -void *ncalloc (size_t howmuch, size_t size); void *mallocstrcpy(char *dest, char *src); void wrap_reset(void); void display_main_list(void); @@ -168,6 +167,8 @@ int do_first_line(void), do_last_line(void); int do_replace(void), do_help(void), do_enter_void(void); int keypad_on(WINDOW * win, int newval); +char *charalloc (size_t howmuch); + #ifndef DISABLE_BROWSER char *do_browser(char *path); struct stat filestat(const char *path); diff --git a/rcfile.c b/rcfile.c index b361df2b..fb8ca1fd 100644 --- a/rcfile.c +++ b/rcfile.c @@ -216,7 +216,7 @@ void do_rcfile(void) if (getenv("HOME") == NULL) return; - nanorc = nmalloc(strlen(getenv("HOME")) + 10); + nanorc = charalloc(strlen(getenv("HOME")) + 10); sprintf(nanorc, "%s/.nanorc", getenv("HOME")); if (stat(nanorc, &fileinfo) == -1) { diff --git a/search.c b/search.c index 57ce24b2..4b1eef93 100644 --- a/search.c +++ b/search.c @@ -55,11 +55,11 @@ void regexp_cleanup(void) void search_init_globals(void) { if (last_search == NULL) { - last_search = nmalloc(1); + last_search = charalloc(1); last_search[0] = 0; } if (last_replace == NULL) { - last_replace = nmalloc(1); + last_replace = charalloc(1); last_replace[0] = 0; } } @@ -79,7 +79,7 @@ int search_init(int replacing) search_init_globals(); - buf = nmalloc(strlen(last_search) + 5); + buf = charalloc(strlen(last_search) + 5); buf[0] = 0; /* Okay, fun time. backupstring is our holder for what is being @@ -447,7 +447,7 @@ char *replace_line(void) } /* Create buffer */ - copy = nmalloc(new_line_size); + copy = charalloc(new_line_size); /* Head of Original Line */ strncpy(copy, current->data, current_x); @@ -628,7 +628,7 @@ int do_replace(void) } if (ISSET(PICO_MODE)) { - buf = nmalloc(strlen(last_replace) + 5); + buf = charalloc(strlen(last_replace) + 5); if (strcmp(last_replace, "")) { if (strlen(last_replace) > (COLS / 3)) { strncpy(buf, last_replace, COLS / 3); diff --git a/utils.c b/utils.c index f331acb1..2655f722 100644 --- a/utils.c +++ b/utils.c @@ -99,17 +99,18 @@ void *nmalloc(size_t howmuch) return r; } -/* We're going to need this too */ -void *ncalloc(size_t howmuch, size_t size) +/* We're going to need this too - Hopefully this will minimize + the transition cost of moving to the apropriate function. */ +char *charalloc(size_t howmuch) { void *r; /* Panic save? */ - if (!(r = calloc(howmuch, size))) + if (!(r = calloc(howmuch, sizeof (char *)))) die(_("nano: calloc: out of memory!")); - return r; + return (char *) r; } void *nrealloc(void *ptr, size_t howmuch) @@ -141,7 +142,7 @@ void *mallocstrcpy(char *dest, char *src) return(dest); } - dest = nmalloc(strlen(src) + 1); + dest = charalloc(strlen(src) + 1); strcpy(dest, src); return dest; @@ -152,7 +153,7 @@ void *mallocstrcpy(char *dest, char *src) void new_magicline(void) { filebot->next = nmalloc(sizeof(filestruct)); - filebot->next->data = nmalloc(1); + filebot->next->data = charalloc(1); filebot->next->data[0] = '\0'; filebot->next->prev = filebot; filebot->next->next = NULL; diff --git a/winio.c b/winio.c index 88ec03f8..3661c45a 100644 --- a/winio.c +++ b/winio.c @@ -265,7 +265,7 @@ int nanogetstr(int allowtabs, char *buf, char *def, shortcut s[], int slen, int shift = 0; #endif - inputbuf = nmalloc(strlen(def) + 1); + inputbuf = charalloc(strlen(def) + 1); inputbuf[0] = 0; x_left = strlen(buf); @@ -930,7 +930,7 @@ void update_line(filestruct * fileptr, int index) realdata = fileptr->data; len = strlen(realdata); - fileptr->data = nmalloc(xpt(fileptr, len) + 1); + fileptr->data = charalloc(xpt(fileptr, len) + 1); pos = 0; for (i = 0; i < len; i++) {