Fix calls to {m,c,re}alloc so that they use x{m,c,re}alloc instead of

segfaulting or handling errors the same way themselves.
This commit is contained in:
Matt Kraai 2000-09-13 02:46:14 +00:00
parent b89075298e
commit 322ae93a5e
25 changed files with 70 additions and 108 deletions

14
ar.c
View File

@ -246,7 +246,7 @@ static headerL_t *getHeaders(int srcFd, headerL_t *head, int funct)
headerL_t *list; headerL_t *list;
off_t initialOffset; off_t initialOffset;
list = (headerL_t *) malloc(sizeof(headerL_t)); list = (headerL_t *) xmalloc(sizeof(headerL_t));
initialOffset=lseek(srcFd, 0, SEEK_CUR); initialOffset=lseek(srcFd, 0, SEEK_CUR);
if (checkArMagic(srcFd)==TRUE) if (checkArMagic(srcFd)==TRUE)
ar=TRUE; ar=TRUE;
@ -258,7 +258,7 @@ static headerL_t *getHeaders(int srcFd, headerL_t *head, int funct)
if (tar==TRUE) { if (tar==TRUE) {
while(readTarHeader(srcFd, list)==TRUE) { while(readTarHeader(srcFd, list)==TRUE) {
off_t tarOffset; off_t tarOffset;
list->next = (headerL_t *) malloc(sizeof(headerL_t)); list->next = (headerL_t *) xmalloc(sizeof(headerL_t));
*list->next = *head; *list->next = *head;
*head = *list; *head = *list;
@ -282,7 +282,7 @@ static headerL_t *getHeaders(int srcFd, headerL_t *head, int funct)
if (readArEntry(srcFd, list) == FALSE) if (readArEntry(srcFd, list) == FALSE)
return(head); return(head);
} }
list->next = (headerL_t *) malloc(sizeof(headerL_t)); list->next = (headerL_t *) xmalloc(sizeof(headerL_t));
*list->next = *head; *list->next = *head;
*head = *list; *head = *list;
/* recursive check for sub-archives */ /* recursive check for sub-archives */
@ -349,9 +349,9 @@ extern int ar_main(int argc, char **argv)
return (FALSE); return (FALSE);
} }
optind++; optind++;
entry = (headerL_t *) malloc(sizeof(headerL_t)); entry = (headerL_t *) xmalloc(sizeof(headerL_t));
header = (headerL_t *) malloc(sizeof(headerL_t)); header = (headerL_t *) xmalloc(sizeof(headerL_t));
extractList = (headerL_t *) malloc(sizeof(headerL_t)); extractList = (headerL_t *) xmalloc(sizeof(headerL_t));
header = getHeaders(srcFd, header, funct); header = getHeaders(srcFd, header, funct);
/* find files to extract or display */ /* find files to extract or display */
@ -359,7 +359,7 @@ extern int ar_main(int argc, char **argv)
/* only handle specified files */ /* only handle specified files */
while(optind < argc) { while(optind < argc) {
if ( (entry = findEntry(header, argv[optind])) != NULL) { if ( (entry = findEntry(header, argv[optind])) != NULL) {
entry->next = (headerL_t *) malloc(sizeof(headerL_t)); entry->next = (headerL_t *) xmalloc(sizeof(headerL_t));
*entry->next = *extractList; *entry->next = *extractList;
*extractList = *entry; *extractList = *entry;
} }

View File

@ -246,7 +246,7 @@ static headerL_t *getHeaders(int srcFd, headerL_t *head, int funct)
headerL_t *list; headerL_t *list;
off_t initialOffset; off_t initialOffset;
list = (headerL_t *) malloc(sizeof(headerL_t)); list = (headerL_t *) xmalloc(sizeof(headerL_t));
initialOffset=lseek(srcFd, 0, SEEK_CUR); initialOffset=lseek(srcFd, 0, SEEK_CUR);
if (checkArMagic(srcFd)==TRUE) if (checkArMagic(srcFd)==TRUE)
ar=TRUE; ar=TRUE;
@ -258,7 +258,7 @@ static headerL_t *getHeaders(int srcFd, headerL_t *head, int funct)
if (tar==TRUE) { if (tar==TRUE) {
while(readTarHeader(srcFd, list)==TRUE) { while(readTarHeader(srcFd, list)==TRUE) {
off_t tarOffset; off_t tarOffset;
list->next = (headerL_t *) malloc(sizeof(headerL_t)); list->next = (headerL_t *) xmalloc(sizeof(headerL_t));
*list->next = *head; *list->next = *head;
*head = *list; *head = *list;
@ -282,7 +282,7 @@ static headerL_t *getHeaders(int srcFd, headerL_t *head, int funct)
if (readArEntry(srcFd, list) == FALSE) if (readArEntry(srcFd, list) == FALSE)
return(head); return(head);
} }
list->next = (headerL_t *) malloc(sizeof(headerL_t)); list->next = (headerL_t *) xmalloc(sizeof(headerL_t));
*list->next = *head; *list->next = *head;
*head = *list; *head = *list;
/* recursive check for sub-archives */ /* recursive check for sub-archives */
@ -349,9 +349,9 @@ extern int ar_main(int argc, char **argv)
return (FALSE); return (FALSE);
} }
optind++; optind++;
entry = (headerL_t *) malloc(sizeof(headerL_t)); entry = (headerL_t *) xmalloc(sizeof(headerL_t));
header = (headerL_t *) malloc(sizeof(headerL_t)); header = (headerL_t *) xmalloc(sizeof(headerL_t));
extractList = (headerL_t *) malloc(sizeof(headerL_t)); extractList = (headerL_t *) xmalloc(sizeof(headerL_t));
header = getHeaders(srcFd, header, funct); header = getHeaders(srcFd, header, funct);
/* find files to extract or display */ /* find files to extract or display */
@ -359,7 +359,7 @@ extern int ar_main(int argc, char **argv)
/* only handle specified files */ /* only handle specified files */
while(optind < argc) { while(optind < argc) {
if ( (entry = findEntry(header, argv[optind])) != NULL) { if ( (entry = findEntry(header, argv[optind])) != NULL) {
entry->next = (headerL_t *) malloc(sizeof(headerL_t)); entry->next = (headerL_t *) xmalloc(sizeof(headerL_t));
*entry->next = *extractList; *entry->next = *extractList;
*extractList = *entry; *extractList = *entry;
} }

View File

@ -246,11 +246,11 @@ char** username_tab_completion(char* command, int *num_matches)
char** exe_n_cwd_tab_completion(char* command, int *num_matches) char** exe_n_cwd_tab_completion(char* command, int *num_matches)
{ {
char *dirName; char *dirName;
char **matches = (char **) NULL; char **matches;
DIR *dir; DIR *dir;
struct dirent *next; struct dirent *next;
matches = malloc( sizeof(char*)*50); matches = xmalloc( sizeof(char*)*50);
/* Stick a wildcard onto the command, for later use */ /* Stick a wildcard onto the command, for later use */
strcat( command, "*"); strcat( command, "*");
@ -273,7 +273,7 @@ char** exe_n_cwd_tab_completion(char* command, int *num_matches)
/* See if this matches */ /* See if this matches */
if (check_wildcard_match(next->d_name, command) == TRUE) { if (check_wildcard_match(next->d_name, command) == TRUE) {
/* Cool, found a match. Add it to the list */ /* Cool, found a match. Add it to the list */
matches[*num_matches] = malloc(strlen(next->d_name)+1); matches[*num_matches] = xmalloc(strlen(next->d_name)+1);
strcpy( matches[*num_matches], next->d_name); strcpy( matches[*num_matches], next->d_name);
++*num_matches; ++*num_matches;
//matches = realloc( matches, sizeof(char*)*(*num_matches)); //matches = realloc( matches, sizeof(char*)*(*num_matches));
@ -302,7 +302,7 @@ void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len)
/* Make a local copy of the string -- up /* Make a local copy of the string -- up
* to the position of the cursor */ * to the position of the cursor */
matchBuf = (char *) calloc(BUFSIZ, sizeof(char)); matchBuf = (char *) xcalloc(BUFSIZ, sizeof(char));
strncpy(matchBuf, command, cursor); strncpy(matchBuf, command, cursor);
tmp=matchBuf; tmp=matchBuf;
@ -670,8 +670,8 @@ extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
if (!h) { if (!h) {
/* No previous history -- this memory is never freed */ /* No previous history -- this memory is never freed */
h = his_front = malloc(sizeof(struct history)); h = his_front = xmalloc(sizeof(struct history));
h->n = malloc(sizeof(struct history)); h->n = xmalloc(sizeof(struct history));
h->p = NULL; h->p = NULL;
h->s = strdup(command); h->s = strdup(command);
@ -682,7 +682,7 @@ extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
history_counter++; history_counter++;
} else { } else {
/* Add a new history command -- this memory is never freed */ /* Add a new history command -- this memory is never freed */
h->n = malloc(sizeof(struct history)); h->n = xmalloc(sizeof(struct history));
h->n->p = h; h->n->p = h;
h->n->n = NULL; h->n->n = NULL;

View File

@ -102,12 +102,8 @@ do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
u_short unicode; u_short unicode;
maxct = tailsz; /* more than enough */ maxct = tailsz; /* more than enough */
up = (struct unipair *) malloc(maxct * sizeof(struct unipair)); up = (struct unipair *) xmalloc(maxct * sizeof(struct unipair));
if (!up) {
errorMsg("Out of memory?\n");
exit(1);
}
for (glyph = 0; glyph < fontsize; glyph++) { for (glyph = 0; glyph < fontsize; glyph++) {
while (tailsz >= 2) { while (tailsz >= 2) {
unicode = (((u_short) inbuf[1]) << 8) + inbuf[0]; unicode = (((u_short) inbuf[1]) << 8) + inbuf[0];

View File

@ -67,11 +67,7 @@ int loadkmap_main(int argc, char **argv)
exit(FALSE); exit(FALSE);
} }
ibuff = (u_short *) malloc(ibuffsz); ibuff = (u_short *) xmalloc(ibuffsz);
if (!ibuff) {
errorMsg("Out of memory.\n");
exit(FALSE);
}
for (i = 0; i < MAX_NR_KEYMAPS; i++) { for (i = 0; i < MAX_NR_KEYMAPS; i++) {
if (flags[i] == 1) { if (flags[i] == 1) {

View File

@ -90,7 +90,7 @@ extern int ln_main(int argc, char **argv)
if (linkIntoDirFlag == TRUE) { if (linkIntoDirFlag == TRUE) {
char *baseName = get_last_path_component(*argv); char *baseName = get_last_path_component(*argv);
linkName = (char *)malloc(strlen(dirName)+strlen(baseName)+2); linkName = (char *)xmalloc(strlen(dirName)+strlen(baseName)+2);
strcpy(linkName, dirName); strcpy(linkName, dirName);
if(dirName[strlen(dirName)-1] != '/') if(dirName[strlen(dirName)-1] != '/')
strcat(linkName, "/"); strcat(linkName, "/");

View File

@ -64,7 +64,7 @@ static const int max = 1024;
static Line *line_alloc() static Line *line_alloc()
{ {
Line *self; Line *self;
self = malloc(1 * sizeof(Line)); self = xmalloc(1 * sizeof(Line));
return self; return self;
} }
@ -76,9 +76,6 @@ static Line *line_newFromFile(FILE * src)
if ((cstring = get_line_from_file(src))) { if ((cstring = get_line_from_file(src))) {
self = line_alloc(); self = line_alloc();
if (self == NULL) {
return NULL;
}
self->data = cstring; self->data = cstring;
self->next = NULL; self->next = NULL;
return self; return self;
@ -173,10 +170,7 @@ static List *list_sort(List * self, Compare * compare)
Line *line; Line *line;
/* mallocate array of Line*s */ /* mallocate array of Line*s */
self->sorted = (Line **) malloc(self->len * sizeof(Line *)); self->sorted = (Line **) xmalloc(self->len * sizeof(Line *));
if (self->sorted == NULL) {
return NULL;
}
/* fill array w/ List's contents */ /* fill array w/ List's contents */
i = 0; i = 0;
@ -294,4 +288,4 @@ int sort_main(int argc, char **argv)
return(0); return(0);
} }
/* $Id: sort.c,v 1.20 2000/07/16 20:57:15 kraai Exp $ */ /* $Id: sort.c,v 1.21 2000/09/13 02:46:13 kraai Exp $ */

View File

@ -87,12 +87,12 @@ int tail_stream(int fd)
ssize_t f_size=0; ssize_t f_size=0;
bs=BUFSIZ; bs=BUFSIZ;
line=malloc(bs); line=xmalloc(bs);
while(1) { while(1) {
bytes_read=read(fd,line,bs); bytes_read=read(fd,line,bs);
if(bytes_read<=0) if(bytes_read<=0)
break; break;
buffer=realloc(buffer,f_size+bytes_read); buffer=xrealloc(buffer,f_size+bytes_read);
memcpy(&buffer[f_size],line,bytes_read); memcpy(&buffer[f_size],line,bytes_read);
filelocation=f_size+=bytes_read; filelocation=f_size+=bytes_read;
} }
@ -150,8 +150,8 @@ int tail_stream(int fd)
void add_file(char *name) void add_file(char *name)
{ {
++n_files; ++n_files;
files = realloc(files, n_files); files = xrealloc(files, n_files);
files[n_files - 1] = (char *) malloc(strlen(name) + 1); files[n_files - 1] = (char *) xmalloc(strlen(name) + 1);
strcpy(files[n_files - 1], name); strcpy(files[n_files - 1], name);
} }
@ -268,13 +268,13 @@ int tail_main(int argc, char **argv)
units=-11; units=-11;
if(units>0) if(units>0)
units--; units--;
fd=malloc(sizeof(int)*n_files); fd=xmalloc(sizeof(int)*n_files);
if (n_files == 1) if (n_files == 1)
#ifndef BB_FEATURE_SIMPLE_TAIL #ifndef BB_FEATURE_SIMPLE_TAIL
if (!verbose) if (!verbose)
#endif #endif
show_headers = 0; show_headers = 0;
buffer=malloc(BUFSIZ); buffer=xmalloc(BUFSIZ);
for (test = 0; test < n_files; test++) { for (test = 0; test < n_files; test++) {
if (show_headers) if (show_headers)
printf("==> %s <==\n", files[test]); printf("==> %s <==\n", files[test]);

View File

@ -555,9 +555,7 @@ static void
initialize_group_array () initialize_group_array ()
{ {
ngroups = getgroups(0, NULL); ngroups = getgroups(0, NULL);
if ((group_array = realloc(group_array, ngroups * sizeof(gid_t))) == NULL) group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
fatalError("Out of space\n");
getgroups(ngroups, group_array); getgroups(ngroups, group_array);
} }

View File

@ -44,7 +44,6 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> /* for realloc() */
#include <unistd.h> /* for getopt() */ #include <unistd.h> /* for getopt() */
#include <regex.h> #include <regex.h>
#include <string.h> /* for strdup() */ #include <string.h> /* for strdup() */
@ -457,7 +456,7 @@ static void add_cmd_str(const char *cmdstr)
continue; continue;
} }
/* grow the array */ /* grow the array */
sed_cmds = realloc(sed_cmds, sizeof(struct sed_cmd) * (++ncmds)); sed_cmds = xrealloc(sed_cmds, sizeof(struct sed_cmd) * (++ncmds));
/* zero new element */ /* zero new element */
memset(&sed_cmds[ncmds-1], 0, sizeof(struct sed_cmd)); memset(&sed_cmds[ncmds-1], 0, sizeof(struct sed_cmd));
/* load command string into new array element, get remainder */ /* load command string into new array element, get remainder */
@ -481,7 +480,7 @@ static void load_cmd_file(char *filename)
/* if a line ends with '\' it needs the next line appended to it */ /* if a line ends with '\' it needs the next line appended to it */
while (line[strlen(line)-2] == '\\' && while (line[strlen(line)-2] == '\\' &&
(nextline = get_line_from_file(cmdfile)) != NULL) { (nextline = get_line_from_file(cmdfile)) != NULL) {
line = realloc(line, strlen(line) + strlen(nextline) + 1); line = xrealloc(line, strlen(line) + strlen(nextline) + 1);
strcat(line, nextline); strcat(line, nextline);
free(nextline); free(nextline);
} }

View File

@ -40,7 +40,7 @@ extern int which_main(int argc, char **argv)
if (!path_list) if (!path_list)
path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"; path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin";
path_parsed = malloc (strlen(path_list) + 1); path_parsed = xmalloc (strlen(path_list) + 1);
strcpy (path_parsed, path_list); strcpy (path_parsed, path_list);
/* Replace colons with zeros in path_parsed and count them */ /* Replace colons with zeros in path_parsed and count them */

2
ln.c
View File

@ -90,7 +90,7 @@ extern int ln_main(int argc, char **argv)
if (linkIntoDirFlag == TRUE) { if (linkIntoDirFlag == TRUE) {
char *baseName = get_last_path_component(*argv); char *baseName = get_last_path_component(*argv);
linkName = (char *)malloc(strlen(dirName)+strlen(baseName)+2); linkName = (char *)xmalloc(strlen(dirName)+strlen(baseName)+2);
strcpy(linkName, dirName); strcpy(linkName, dirName);
if(dirName[strlen(dirName)-1] != '/') if(dirName[strlen(dirName)-1] != '/')
strcat(linkName, "/"); strcat(linkName, "/");

View File

@ -102,12 +102,8 @@ do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
u_short unicode; u_short unicode;
maxct = tailsz; /* more than enough */ maxct = tailsz; /* more than enough */
up = (struct unipair *) malloc(maxct * sizeof(struct unipair)); up = (struct unipair *) xmalloc(maxct * sizeof(struct unipair));
if (!up) {
errorMsg("Out of memory?\n");
exit(1);
}
for (glyph = 0; glyph < fontsize; glyph++) { for (glyph = 0; glyph < fontsize; glyph++) {
while (tailsz >= 2) { while (tailsz >= 2) {
unicode = (((u_short) inbuf[1]) << 8) + inbuf[0]; unicode = (((u_short) inbuf[1]) << 8) + inbuf[0];

View File

@ -67,11 +67,7 @@ int loadkmap_main(int argc, char **argv)
exit(FALSE); exit(FALSE);
} }
ibuff = (u_short *) malloc(ibuffsz); ibuff = (u_short *) xmalloc(ibuffsz);
if (!ibuff) {
errorMsg("Out of memory.\n");
exit(FALSE);
}
for (i = 0; i < MAX_NR_KEYMAPS; i++) { for (i = 0; i < MAX_NR_KEYMAPS; i++) {
if (flags[i] == 1) { if (flags[i] == 1) {

View File

@ -273,7 +273,7 @@ mount_one(char *blockDevice, char *directory, char *filesystemType,
numfilesystems = ioctl (fd, DEVMTAB_COUNT_FILESYSTEMS); numfilesystems = ioctl (fd, DEVMTAB_COUNT_FILESYSTEMS);
if (numfilesystems<0) if (numfilesystems<0)
fatalError("\nDEVMTAB_COUNT_FILESYSTEMS: %s\n", strerror (errno)); fatalError("\nDEVMTAB_COUNT_FILESYSTEMS: %s\n", strerror (errno));
fslist = (struct k_fstype *) calloc ( numfilesystems, sizeof(struct k_fstype)); fslist = (struct k_fstype *) xcalloc ( numfilesystems, sizeof(struct k_fstype));
/* Grab the list of available filesystems */ /* Grab the list of available filesystems */
status = ioctl (fd, DEVMTAB_GET_FILESYSTEMS, fslist); status = ioctl (fd, DEVMTAB_GET_FILESYSTEMS, fslist);
@ -343,7 +343,7 @@ extern int mount_main(int argc, char **argv)
numfilesystems = ioctl (fd, DEVMTAB_COUNT_MOUNTS); numfilesystems = ioctl (fd, DEVMTAB_COUNT_MOUNTS);
if (numfilesystems<0) if (numfilesystems<0)
fatalError( "\nDEVMTAB_COUNT_MOUNTS: %s\n", strerror (errno)); fatalError( "\nDEVMTAB_COUNT_MOUNTS: %s\n", strerror (errno));
mntentlist = (struct k_mntent *) calloc ( numfilesystems, sizeof(struct k_mntent)); mntentlist = (struct k_mntent *) xcalloc ( numfilesystems, sizeof(struct k_mntent));
/* Grab the list of mounted filesystems */ /* Grab the list of mounted filesystems */
if (ioctl (fd, DEVMTAB_GET_MOUNTS, mntentlist)<0) if (ioctl (fd, DEVMTAB_GET_MOUNTS, mntentlist)<0)

View File

@ -229,7 +229,7 @@ extern int ps_main(int argc, char **argv)
* some new processes start up while we wait. The kernel will * some new processes start up while we wait. The kernel will
* just ignore any extras if we give it too many, and will trunc. * just ignore any extras if we give it too many, and will trunc.
* the list if we give it too few. */ * the list if we give it too few. */
pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t)); pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
pid_array[0] = num_pids+10; pid_array[0] = num_pids+10;
/* Now grab the pid list */ /* Now grab the pid list */

2
ps.c
View File

@ -229,7 +229,7 @@ extern int ps_main(int argc, char **argv)
* some new processes start up while we wait. The kernel will * some new processes start up while we wait. The kernel will
* just ignore any extras if we give it too many, and will trunc. * just ignore any extras if we give it too many, and will trunc.
* the list if we give it too few. */ * the list if we give it too few. */
pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t)); pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
pid_array[0] = num_pids+10; pid_array[0] = num_pids+10;
/* Now grab the pid list */ /* Now grab the pid list */

5
sed.c
View File

@ -44,7 +44,6 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> /* for realloc() */
#include <unistd.h> /* for getopt() */ #include <unistd.h> /* for getopt() */
#include <regex.h> #include <regex.h>
#include <string.h> /* for strdup() */ #include <string.h> /* for strdup() */
@ -457,7 +456,7 @@ static void add_cmd_str(const char *cmdstr)
continue; continue;
} }
/* grow the array */ /* grow the array */
sed_cmds = realloc(sed_cmds, sizeof(struct sed_cmd) * (++ncmds)); sed_cmds = xrealloc(sed_cmds, sizeof(struct sed_cmd) * (++ncmds));
/* zero new element */ /* zero new element */
memset(&sed_cmds[ncmds-1], 0, sizeof(struct sed_cmd)); memset(&sed_cmds[ncmds-1], 0, sizeof(struct sed_cmd));
/* load command string into new array element, get remainder */ /* load command string into new array element, get remainder */
@ -481,7 +480,7 @@ static void load_cmd_file(char *filename)
/* if a line ends with '\' it needs the next line appended to it */ /* if a line ends with '\' it needs the next line appended to it */
while (line[strlen(line)-2] == '\\' && while (line[strlen(line)-2] == '\\' &&
(nextline = get_line_from_file(cmdfile)) != NULL) { (nextline = get_line_from_file(cmdfile)) != NULL) {
line = realloc(line, strlen(line) + strlen(nextline) + 1); line = xrealloc(line, strlen(line) + strlen(nextline) + 1);
strcat(line, nextline); strcat(line, nextline);
free(nextline); free(nextline);
} }

View File

@ -246,11 +246,11 @@ char** username_tab_completion(char* command, int *num_matches)
char** exe_n_cwd_tab_completion(char* command, int *num_matches) char** exe_n_cwd_tab_completion(char* command, int *num_matches)
{ {
char *dirName; char *dirName;
char **matches = (char **) NULL; char **matches;
DIR *dir; DIR *dir;
struct dirent *next; struct dirent *next;
matches = malloc( sizeof(char*)*50); matches = xmalloc( sizeof(char*)*50);
/* Stick a wildcard onto the command, for later use */ /* Stick a wildcard onto the command, for later use */
strcat( command, "*"); strcat( command, "*");
@ -273,7 +273,7 @@ char** exe_n_cwd_tab_completion(char* command, int *num_matches)
/* See if this matches */ /* See if this matches */
if (check_wildcard_match(next->d_name, command) == TRUE) { if (check_wildcard_match(next->d_name, command) == TRUE) {
/* Cool, found a match. Add it to the list */ /* Cool, found a match. Add it to the list */
matches[*num_matches] = malloc(strlen(next->d_name)+1); matches[*num_matches] = xmalloc(strlen(next->d_name)+1);
strcpy( matches[*num_matches], next->d_name); strcpy( matches[*num_matches], next->d_name);
++*num_matches; ++*num_matches;
//matches = realloc( matches, sizeof(char*)*(*num_matches)); //matches = realloc( matches, sizeof(char*)*(*num_matches));
@ -302,7 +302,7 @@ void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len)
/* Make a local copy of the string -- up /* Make a local copy of the string -- up
* to the position of the cursor */ * to the position of the cursor */
matchBuf = (char *) calloc(BUFSIZ, sizeof(char)); matchBuf = (char *) xcalloc(BUFSIZ, sizeof(char));
strncpy(matchBuf, command, cursor); strncpy(matchBuf, command, cursor);
tmp=matchBuf; tmp=matchBuf;
@ -670,8 +670,8 @@ extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
if (!h) { if (!h) {
/* No previous history -- this memory is never freed */ /* No previous history -- this memory is never freed */
h = his_front = malloc(sizeof(struct history)); h = his_front = xmalloc(sizeof(struct history));
h->n = malloc(sizeof(struct history)); h->n = xmalloc(sizeof(struct history));
h->p = NULL; h->p = NULL;
h->s = strdup(command); h->s = strdup(command);
@ -682,7 +682,7 @@ extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
history_counter++; history_counter++;
} else { } else {
/* Add a new history command -- this memory is never freed */ /* Add a new history command -- this memory is never freed */
h->n = malloc(sizeof(struct history)); h->n = xmalloc(sizeof(struct history));
h->n->p = h; h->n->p = h;
h->n->n = NULL; h->n->n = NULL;

12
sort.c
View File

@ -64,7 +64,7 @@ static const int max = 1024;
static Line *line_alloc() static Line *line_alloc()
{ {
Line *self; Line *self;
self = malloc(1 * sizeof(Line)); self = xmalloc(1 * sizeof(Line));
return self; return self;
} }
@ -76,9 +76,6 @@ static Line *line_newFromFile(FILE * src)
if ((cstring = get_line_from_file(src))) { if ((cstring = get_line_from_file(src))) {
self = line_alloc(); self = line_alloc();
if (self == NULL) {
return NULL;
}
self->data = cstring; self->data = cstring;
self->next = NULL; self->next = NULL;
return self; return self;
@ -173,10 +170,7 @@ static List *list_sort(List * self, Compare * compare)
Line *line; Line *line;
/* mallocate array of Line*s */ /* mallocate array of Line*s */
self->sorted = (Line **) malloc(self->len * sizeof(Line *)); self->sorted = (Line **) xmalloc(self->len * sizeof(Line *));
if (self->sorted == NULL) {
return NULL;
}
/* fill array w/ List's contents */ /* fill array w/ List's contents */
i = 0; i = 0;
@ -294,4 +288,4 @@ int sort_main(int argc, char **argv)
return(0); return(0);
} }
/* $Id: sort.c,v 1.20 2000/07/16 20:57:15 kraai Exp $ */ /* $Id: sort.c,v 1.21 2000/09/13 02:46:13 kraai Exp $ */

12
tail.c
View File

@ -87,12 +87,12 @@ int tail_stream(int fd)
ssize_t f_size=0; ssize_t f_size=0;
bs=BUFSIZ; bs=BUFSIZ;
line=malloc(bs); line=xmalloc(bs);
while(1) { while(1) {
bytes_read=read(fd,line,bs); bytes_read=read(fd,line,bs);
if(bytes_read<=0) if(bytes_read<=0)
break; break;
buffer=realloc(buffer,f_size+bytes_read); buffer=xrealloc(buffer,f_size+bytes_read);
memcpy(&buffer[f_size],line,bytes_read); memcpy(&buffer[f_size],line,bytes_read);
filelocation=f_size+=bytes_read; filelocation=f_size+=bytes_read;
} }
@ -150,8 +150,8 @@ int tail_stream(int fd)
void add_file(char *name) void add_file(char *name)
{ {
++n_files; ++n_files;
files = realloc(files, n_files); files = xrealloc(files, n_files);
files[n_files - 1] = (char *) malloc(strlen(name) + 1); files[n_files - 1] = (char *) xmalloc(strlen(name) + 1);
strcpy(files[n_files - 1], name); strcpy(files[n_files - 1], name);
} }
@ -268,13 +268,13 @@ int tail_main(int argc, char **argv)
units=-11; units=-11;
if(units>0) if(units>0)
units--; units--;
fd=malloc(sizeof(int)*n_files); fd=xmalloc(sizeof(int)*n_files);
if (n_files == 1) if (n_files == 1)
#ifndef BB_FEATURE_SIMPLE_TAIL #ifndef BB_FEATURE_SIMPLE_TAIL
if (!verbose) if (!verbose)
#endif #endif
show_headers = 0; show_headers = 0;
buffer=malloc(BUFSIZ); buffer=xmalloc(BUFSIZ);
for (test = 0; test < n_files; test++) { for (test = 0; test < n_files; test++) {
if (show_headers) if (show_headers)
printf("==> %s <==\n", files[test]); printf("==> %s <==\n", files[test]);

4
test.c
View File

@ -555,9 +555,7 @@ static void
initialize_group_array () initialize_group_array ()
{ {
ngroups = getgroups(0, NULL); ngroups = getgroups(0, NULL);
if ((group_array = realloc(group_array, ngroups * sizeof(gid_t))) == NULL) group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
fatalError("Out of space\n");
getgroups(ngroups, group_array); getgroups(ngroups, group_array);
} }

View File

@ -273,7 +273,7 @@ mount_one(char *blockDevice, char *directory, char *filesystemType,
numfilesystems = ioctl (fd, DEVMTAB_COUNT_FILESYSTEMS); numfilesystems = ioctl (fd, DEVMTAB_COUNT_FILESYSTEMS);
if (numfilesystems<0) if (numfilesystems<0)
fatalError("\nDEVMTAB_COUNT_FILESYSTEMS: %s\n", strerror (errno)); fatalError("\nDEVMTAB_COUNT_FILESYSTEMS: %s\n", strerror (errno));
fslist = (struct k_fstype *) calloc ( numfilesystems, sizeof(struct k_fstype)); fslist = (struct k_fstype *) xcalloc ( numfilesystems, sizeof(struct k_fstype));
/* Grab the list of available filesystems */ /* Grab the list of available filesystems */
status = ioctl (fd, DEVMTAB_GET_FILESYSTEMS, fslist); status = ioctl (fd, DEVMTAB_GET_FILESYSTEMS, fslist);
@ -343,7 +343,7 @@ extern int mount_main(int argc, char **argv)
numfilesystems = ioctl (fd, DEVMTAB_COUNT_MOUNTS); numfilesystems = ioctl (fd, DEVMTAB_COUNT_MOUNTS);
if (numfilesystems<0) if (numfilesystems<0)
fatalError( "\nDEVMTAB_COUNT_MOUNTS: %s\n", strerror (errno)); fatalError( "\nDEVMTAB_COUNT_MOUNTS: %s\n", strerror (errno));
mntentlist = (struct k_mntent *) calloc ( numfilesystems, sizeof(struct k_mntent)); mntentlist = (struct k_mntent *) xcalloc ( numfilesystems, sizeof(struct k_mntent));
/* Grab the list of mounted filesystems */ /* Grab the list of mounted filesystems */
if (ioctl (fd, DEVMTAB_GET_MOUNTS, mntentlist)<0) if (ioctl (fd, DEVMTAB_GET_MOUNTS, mntentlist)<0)

View File

@ -1290,7 +1290,7 @@ extern pid_t* findPidByName( char* pidName)
* some new processes start up while we wait. The kernel will * some new processes start up while we wait. The kernel will
* just ignore any extras if we give it too many, and will trunc. * just ignore any extras if we give it too many, and will trunc.
* the list if we give it too few. */ * the list if we give it too few. */
pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t)); pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
pid_array[0] = num_pids+10; pid_array[0] = num_pids+10;
/* Now grab the pid list */ /* Now grab the pid list */
@ -1316,9 +1316,7 @@ extern pid_t* findPidByName( char* pidName)
if ((strstr(info.command_line, pidName) != NULL) if ((strstr(info.command_line, pidName) != NULL)
&& (strlen(pidName) == strlen(info.command_line))) { && (strlen(pidName) == strlen(info.command_line))) {
pidList=realloc( pidList, sizeof(pid_t) * (j+2)); pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
if (pidList==NULL)
fatalError(memory_exhausted);
pidList[j++]=info.pid; pidList[j++]=info.pid;
} }
} }
@ -1389,9 +1387,7 @@ extern pid_t* findPidByName( char* pidName)
if ((strstr(p, pidName) != NULL) if ((strstr(p, pidName) != NULL)
&& (strlen(pidName) == strlen(p))) { && (strlen(pidName) == strlen(p))) {
pidList=realloc( pidList, sizeof(pid_t) * (i+2)); pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
if (pidList==NULL)
fatalError(memory_exhausted);
pidList[i++]=strtol(next->d_name, NULL, 0); pidList[i++]=strtol(next->d_name, NULL, 0);
} }
} }
@ -1624,7 +1620,7 @@ extern char *get_line_from_file(FILE *file)
break; break;
/* grow the line buffer as necessary */ /* grow the line buffer as necessary */
if (idx > linebufsz-2) if (idx > linebufsz-2)
linebuf = realloc(linebuf, linebufsz += GROWBY); linebuf = xrealloc(linebuf, linebufsz += GROWBY);
linebuf[idx++] = (char)ch; linebuf[idx++] = (char)ch;
if ((char)ch == '\n') if ((char)ch == '\n')
break; break;

View File

@ -40,7 +40,7 @@ extern int which_main(int argc, char **argv)
if (!path_list) if (!path_list)
path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"; path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin";
path_parsed = malloc (strlen(path_list) + 1); path_parsed = xmalloc (strlen(path_list) + 1);
strcpy (path_parsed, path_list); strcpy (path_parsed, path_list);
/* Replace colons with zeros in path_parsed and count them */ /* Replace colons with zeros in path_parsed and count them */