From 262f357fdd137895cfcdbc957da6332eecc47ba6 Mon Sep 17 00:00:00 2001 From: Connor Lane Smith Date: Wed, 25 May 2011 11:42:17 +0100 Subject: [PATCH] add head --- Makefile | 4 ++-- chown.c | 2 +- date.c | 6 ++++-- head.1 | 15 +++++++++++++++ head.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ sleep.c | 5 ++++- touch.c | 6 ++++-- 7 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 head.1 create mode 100644 head.c diff --git a/Makefile b/Makefile index b95b5f2..aa65696 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ include config.mk LIB = util/enmasse.o util/eprintf.o util/recurse.o -SRC = basename.c cat.c chown.c date.c dirname.c echo.c false.c grep.c ln.c \ - mkfifo.c pwd.c rm.c sleep.c tee.c touch.c true.c wc.c +SRC = basename.c cat.c chown.c date.c dirname.c echo.c false.c grep.c head.c \ + ln.c mkfifo.c pwd.c rm.c sleep.c tee.c touch.c true.c wc.c OBJ = $(SRC:.c=.o) $(LIB) BIN = $(SRC:.c=) MAN = $(SRC:.c=.1) diff --git a/chown.c b/chown.c index a6a2b3e..68719e9 100644 --- a/chown.c +++ b/chown.c @@ -49,7 +49,7 @@ main(int argc, char *argv[]) if(errno != 0) eprintf("getgrnam %s:", group); else if(!gr) - eprintf("getgrnam %s: no such user\n", group); + eprintf("getgrnam %s: no such group\n", group); } for(; optind < argc; optind++) chownpwgr(argv[optind]); diff --git a/date.c b/date.c index 9fb43a0..141c88d 100644 --- a/date.c +++ b/date.c @@ -8,7 +8,7 @@ int main(int argc, char *argv[]) { - char buf[BUFSIZ], c; + char buf[BUFSIZ], c, *end; char *fmt = "%c"; struct tm *now = NULL; time_t t; @@ -17,7 +17,9 @@ main(int argc, char *argv[]) while((c = getopt(argc, argv, "d:")) != -1) switch(c) { case 'd': - t = strtol(optarg, NULL, 0); + t = strtol(optarg, &end, 0); + if(*end != '\0') + eprintf("%s: not a number\n", optarg); break; default: exit(EXIT_FAILURE); diff --git a/head.1 b/head.1 new file mode 100644 index 0000000..37c30b8 --- /dev/null +++ b/head.1 @@ -0,0 +1,15 @@ +.TH HEAD 1 sbase\-VERSION +.SH NAME +head \- output first part of files +.SH SYNOPSIS +.B head +.RB [ \-n +.IR lines ] +.RI [ file ...] +.SH DESCRIPTION +.B head +writes the first 10 lines of each file to stdout. +.SH OPTIONS +.TP +.BI \-n " lines" +outputs the given number of lines. diff --git a/head.c b/head.c new file mode 100644 index 0000000..f836e3b --- /dev/null +++ b/head.c @@ -0,0 +1,47 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include +#include "util.h" + +static void head(FILE *, const char *, long); + +int +main(int argc, char *argv[]) +{ + char *end, c; + long n = 10; + FILE *fp; + + while((c = getopt(argc, argv, "n:")) != -1) + switch(c) { + case 'n': + n = strtol(optarg, &end, 0); + if(*end != '\0') + eprintf("%s: not a number\n", optarg); + break; + default: + exit(EXIT_FAILURE); + } + if(optind == argc) + head(stdin, "", n); + else for(; optind < argc; optind++) { + if(!(fp = fopen(argv[optind], "r"))) + eprintf("fopen %s:", argv[optind]); + head(fp, argv[optind], n); + fclose(fp); + } + return EXIT_SUCCESS; +} + +void +head(FILE *fp, const char *str, long n) +{ + char buf[BUFSIZ]; + int i; + + for(i = 0; i < n && fgets(buf, sizeof buf, fp); i++) + fputs(buf, stdout); + if(ferror(fp)) + eprintf("%s: read error:", str); +} diff --git a/sleep.c b/sleep.c index 39fbae8..e341c65 100644 --- a/sleep.c +++ b/sleep.c @@ -6,6 +6,7 @@ int main(int argc, char *argv[]) { + char *end; unsigned int seconds; if(getopt(argc, argv, "") != -1) @@ -13,7 +14,9 @@ main(int argc, char *argv[]) if(optind != argc-1) eprintf("usage: %s seconds\n", argv[0]); - seconds = atoi(argv[optind]); + seconds = strtol(argv[optind], &end, 0); + if(*end != '\0') + eprintf("%s: not a number\n", argv[optind]); while((seconds = sleep(seconds)) > 0) ; return EXIT_SUCCESS; diff --git a/touch.c b/touch.c index 4f0cfbf..903c632 100644 --- a/touch.c +++ b/touch.c @@ -17,7 +17,7 @@ static time_t t; int main(int argc, char *argv[]) { - char c; + char *end, c; t = time(NULL); while((c = getopt(argc, argv, "ct:")) != -1) @@ -26,7 +26,9 @@ main(int argc, char *argv[]) cflag = true; break; case 't': - t = strtol(optarg, NULL, 0); + t = strtol(optarg, &end, 0); + if(*end != '\0') + eprintf("%s: not a number\n", optarg); break; default: exit(EXIT_FAILURE);