aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/functions.c33
-rw-r--r--lib/getmountlist.c2
-rw-r--r--lib/lib.h7
3 files changed, 36 insertions, 6 deletions
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;