From cd9dfc3b7b73715840b63180e2e4bfdb6e7ca9a4 Mon Sep 17 00:00:00 2001 From: landley Date: Wed, 18 Oct 2006 18:38:16 -0400 Subject: Next drop of toysh, plus more infratructure. --- lib/functions.c | 33 ++++++++++++++++++++++++++++----- lib/getmountlist.c | 2 +- lib/lib.h | 7 +++++++ 3 files changed, 36 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/functions.c b/lib/functions.c index 3d9a8aca..dd7c6c52 100644 --- a/lib/functions.c +++ b/lib/functions.c @@ -1,8 +1,10 @@ -/* vi: set ts=4 :*/ +/* vi: set sw=4 ts=4 :*/ /* functions.c - reusable stuff. * - * Functions with the x prefix never return failure, they either succeed or - * kill the program with an error message. + * Functions with the x prefix are wrappers for library functions. They either + * succeed or kill the program with an error message, but never return failure. + * They usually have the same arguments and return value as the function they + * wrap. */ #include "toys.h" @@ -19,6 +21,7 @@ void error_exit(char *msg, ...) exit(toys.exitval); } +// Like strncpy but always null terminated. void strlcpy(char *dest, char *src, size_t size) { strncpy(dest,src,size); @@ -30,9 +33,27 @@ void *xmalloc(size_t size) { void *ret = malloc(size); if (!ret) error_exit("xmalloc"); + + return ret; +} + +// Die unless we can allocate prezeroed memory. +void *xzalloc(size_t size) +{ + void *ret = xmalloc(size); + bzero(ret,size); + return ret; +} + +// Die unless we can change the size of an existing allocation, possibly +// moving it. (Notice different arguments from libc function.) +void xrealloc(void **ptr, size_t size) +{ + *ptr = realloc(*ptr, size); + if (!*ptr) error_exit("xrealloc"); } -// Die unless we can copy this string. +// Die unless we can allocate a copy of this string. void *xstrndup(char *s, size_t n) { void *ret = xmalloc(++n); @@ -41,9 +62,11 @@ void *xstrndup(char *s, size_t n) return ret; } -// Die unless we can exec argv[] +// Die unless we can exec argv[] (or run builtin command). Note that anything +// with a path isn't a builtin, so /bin/sh won't match the builtin sh. void *xexec(char **argv) { + toy_exec(argv); execvp(argv[0], argv); error_exit("No %s", argv[0]); } diff --git a/lib/getmountlist.c b/lib/getmountlist.c index a2b0698d..16b828bf 100644 --- a/lib/getmountlist.c +++ b/lib/getmountlist.c @@ -1,4 +1,4 @@ -/* vi: set ts=4 : */ +/* vi: set sw=4 ts=4 : */ #include "toys.h" diff --git a/lib/lib.h b/lib/lib.h index fa24c8d5..157db343 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -1,13 +1,20 @@ /* vi: set ts=4 :*/ +// functions.c void error_exit(char *msg, ...); void strlcpy(char *dest, char *src, size_t size); void *xmalloc(size_t size); +void *xzalloc(size_t size); +void xrealloc(void **ptr, size_t size); void *xstrndup(char *s, size_t n); void *xexec(char **argv); int xopen(char *path, int flags, int mode); FILE *xfopen(char *path, char *mode); +// llist.c +void llist_free(void *list, void (*freeit)(void *data)); + +// getmountlist.c struct mtab_list { struct mtab_list *next; char *dir; -- cgit v1.2.3