Mercurial > hg > toybox
changeset 70:a1b464bbef08
Add "echo". Has -n and -e (but not \0123 yet).
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 20 Jan 2007 18:04:20 -0500 |
parents | 530168fc253f |
children | 40103a3ddcb0 |
files | lib/functions.c lib/lib.h toys/Config.in toys/toylist.h |
diffstat | 4 files changed, 48 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/lib/functions.c Sat Jan 20 12:30:19 2007 -0500 +++ b/lib/functions.c Sat Jan 20 18:04:20 2007 -0500 @@ -138,6 +138,25 @@ return ret; } +void xprintf(char *format, ...) +{ + va_list va; + va_start(va, format); + + vprintf(format, va); + if (ferror(stdout)) perror_exit("write"); +} + +void xputc(char c) +{ + if (EOF == fputc(c, stdout)) perror_exit("write"); +} + +void xflush(void) +{ + if (fflush(stdout)) perror_exit("write");; +} + // 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) @@ -149,14 +168,14 @@ void xaccess(char *path, int flags) { - if (access(path, flags)) error_exit("Can't access '%s'\n", path); + if (access(path, flags)) perror_exit("Can't access '%s'\n", path); } // Die unless we can open/create a file, returning file descriptor. int xcreate(char *path, int flags, int mode) { int fd = open(path, flags, mode); - if (fd == -1) error_exit("No file %s\n", path); + if (fd == -1) perror_exit("No file %s\n", path); return fd; } @@ -170,7 +189,7 @@ FILE *xfopen(char *path, char *mode) { FILE *f = fopen(path, mode); - if (!f) error_exit("No file %s\n", path); + if (!f) perror_exit("No file %s\n", path); return f; } @@ -227,7 +246,7 @@ char *xgetcwd(void) { char *buf = getcwd(NULL, 0); - if (!buf) error_exit("xgetcwd"); + if (!buf) perror_exit("xgetcwd"); return buf; } @@ -439,7 +458,7 @@ char *xreadfile(char *name) { char *buf = readfile(name); - if (!buf) error_exit("xreadfile %s", name); + if (!buf) perror_exit("xreadfile %s", name); return buf; }
--- a/lib/lib.h Sat Jan 20 12:30:19 2007 -0500 +++ b/lib/lib.h Sat Jan 20 18:04:20 2007 -0500 @@ -38,6 +38,9 @@ void *xstrndup(char *s, size_t n); void *xstrdup(char *s); char *xmsprintf(char *format, ...); +void xprintf(char *format, ...); +void xputc(char c); +void xflush(void); void xexec(char **argv); void xaccess(char *path, int flags); int xcreate(char *path, int flags, int mode);
--- a/toys/Config.in Sat Jan 20 12:30:19 2007 -0500 +++ b/toys/Config.in Sat Jan 20 18:04:20 2007 -0500 @@ -57,6 +57,26 @@ -k Sets units back to 1024 bytes (the default without -P) +config ECHO + bool "echo" + default n + help + usage: echo [-ne] [args...] + + Write each argument to stdout, with one space between each, followed + by a newline. + + -n No trailing newline. + -e Process the following escape sequences: + \\ backslash + \a alert (beep/flash) + \b backspace + \c Stop output here (avoids trailing newline) + \f form feed + \n newline + \r carriage return + \t horizontal tab + \v vertical tab config HELLO bool "hello" default n
--- a/toys/toylist.h Sat Jan 20 12:30:19 2007 -0500 +++ b/toys/toylist.h Sat Jan 20 18:04:20 2007 -0500 @@ -72,6 +72,7 @@ USE_COUNT(NEWTOY(count, "", TOYFLAG_USR|TOYFLAG_BIN)) USE_TOYSH(NEWTOY(cd, NULL, TOYFLAG_NOFORK)) USE_DF(NEWTOY(df, "Pkt*a", TOYFLAG_USR|TOYFLAG_SBIN)) +USE_ECHO(NEWTOY(echo, "en", TOYFLAG_BIN)) USE_TOYSH(NEWTOY(exit, NULL, TOYFLAG_NOFORK)) USE_HELLO(NEWTOY(hello, NULL, TOYFLAG_USR)) USE_MKE2FS(NEWTOY(mke2fs, MKE2FS_OPTSTRING, TOYFLAG_SBIN))