diff --git a/libio/Makefile b/libio/Makefile new file mode 100644 index 0000000..710caaa --- /dev/null +++ b/libio/Makefile @@ -0,0 +1,2 @@ +libio.o: libio.c libio.h + $(CC) -c -o libio.o libio.c diff --git a/libio/libio.c b/libio/libio.c index 35e988b..325270d 100644 --- a/libio/libio.c +++ b/libio/libio.c @@ -3,19 +3,11 @@ /* Backups of standard library systems for the odd scenario where there is no * standard library but there is the write(2) system call. */ -static int _isdigit(int c); -static int _strlen(char *s); - -/* Wrappers around write(2) */ -static int _write(int fd, char *s, int n); -static int _write1(int fd, int c); - -/* Normally in */ -static int -_isdigit(int c){ - return c >= '0' && c <= '9'; -} +#if defined USE_SYSTEM_STRING_H +# include +# define _strlen strlen +#else /* Normally in */ static int _strlen(char *s){ @@ -25,6 +17,22 @@ _strlen(char *s){ return i; } +#endif + +#if defined USE_SYSTEM_CTYPE_H +# include +# define _isdigit isdigit +#else +/* Normally in */ +static int +_isdigit(int c){ + return c >= '0' && c <= '9'; +} +#endif + +/* Wrappers around write(2) */ +static int _write(int fd, char *s, int n); +static int _write1(int fd, int c); /* Wrapper around write(2) - keeps re-running write(2) until the operation * succeeds in full. Usually unnecessary but still good. */ @@ -52,7 +60,7 @@ _write1(int fd, int c){ int fdprint(int fd, char *s){ - return _write(fd, s, strlen(s)); + return _write(fd, s, _strlen(s)); } int diff --git a/retval/Makefile b/retval/Makefile new file mode 100644 index 0000000..718a23c --- /dev/null +++ b/retval/Makefile @@ -0,0 +1,8 @@ +retval: retval.o ../libio/libio.o + $(CC) -o retval ../libio/libio.o retval.o + +retval.o: retval.c ../libio/libio.h + $(CC) -c -I../libio -o retval.o retval.c + +../libio/libio.o: ../libio/libio.c ../libio/libio.h + $(MAKE) -C ../libio diff --git a/retval/retval.c b/retval/retval.c index ca3d68d..130bb56 100644 --- a/retval/retval.c +++ b/retval/retval.c @@ -1,4 +1,5 @@ #include /* isdigit(3) */ +#include /* fprintf(3), stderr */ #include /* EX_USAGE */ #include /* write(2) */ #include "libio.h" /* fdprint(3), parse_uint(3) */ @@ -9,9 +10,7 @@ int main(int argc, char *argv[]){ unsigned int s; if(argc < 2){ -usage: write(2, "Usage: ", 7); - fdprint(2, argv[0] == NULL ? program_name : argv[0]); - write(2, " [status]\n", 10); +usage: fprintf(stderr, "Usage: %s [status]\n", argv[0] == NULL ? program_name : argv[0]); return EX_USAGE; }