From 474ee643edc05791771b9bf0308295b39da784e2 Mon Sep 17 00:00:00 2001 From: Connor Lane Smith Date: Mon, 23 May 2011 19:00:31 +0100 Subject: [PATCH] add sleep & date, thanks kamil --- LICENSE | 1 + Makefile | 2 +- cat.1 | 2 +- config.mk | 4 ++-- date.1 | 20 ++++++++++++++++++++ date.c | 32 ++++++++++++++++++++++++++++++++ sleep.1 | 9 +++++++++ sleep.c | 18 ++++++++++++++++++ touch.c | 1 - 9 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 date.1 create mode 100644 date.c create mode 100644 sleep.1 create mode 100644 sleep.c diff --git a/LICENSE b/LICENSE index eca3868..b374d1e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,7 @@ MIT/X Consortium License © 2011 Connor Lane Smith +© 2011 Kamil Cholewiński Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), diff --git a/Makefile b/Makefile index c4f437e..6dc10c4 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ include config.mk -SRC = basename.c cat.c echo.c false.c grep.c tee.c touch.c true.c wc.c +SRC = basename.c cat.c date.c echo.c false.c grep.c sleep.c tee.c touch.c true.c wc.c OBJ = $(SRC:.c=.o) util.o BIN = $(SRC:.c=) MAN = $(SRC:.c=.1) diff --git a/cat.1 b/cat.1 index 08a3027..db057c9 100644 --- a/cat.1 +++ b/cat.1 @@ -3,7 +3,7 @@ cat \- concatenate files .SH SYNOPSIS .B cat -.RI [ files ...] +.RI [ file ...] .SH DESCRIPTION .B cat reads each file in sequence and writes it to stdout. If no file is given, cat diff --git a/config.mk b/config.mk index d1c4d57..375d87f 100644 --- a/config.mk +++ b/config.mk @@ -1,8 +1,8 @@ # sbase version VERSION = 0.0 -CC = cc -#CC = musl-gcc +#CC = cc +CC = musl-gcc CPPFLAGS = -D_BSD_SOURCE CFLAGS = -Os -ansi -Wall -pedantic $(CPPFLAGS) diff --git a/date.1 b/date.1 new file mode 100644 index 0000000..e563ea2 --- /dev/null +++ b/date.1 @@ -0,0 +1,20 @@ +.TH DATE 1 sbase\-VERSION +.SH NAME +date \- print date and time +.SH SYNOPSIS +.B date +.RB [ \-d +.IR time ] +.RI [+ format ] +.SH DESCRIPTION +.B date +prints the date and time. If a +.I format +is given it is used to format the date as per +.BR strftime (3). +.SH OPTIONS +.TP +.BI \-d " time" +prints +.I time +instead of the system time, given as the number of seconds since the Unix epoch. diff --git a/date.c b/date.c new file mode 100644 index 0000000..5896f41 --- /dev/null +++ b/date.c @@ -0,0 +1,32 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include +#include +#include "util.h" + +int +main(int argc, char *argv[]) +{ + char buf[BUFSIZ]; + char *fmt = "%c"; + int i; + struct tm *now = NULL; + time_t t; + + t = time(NULL); + for(i = 1; i < argc; i++) + if(!strncmp("+", argv[i], 1)) + fmt = &argv[i][1]; + else if(!strcmp("-d", argv[i]) && i+1 < argc) + t = strtol(argv[++i], NULL, 0); + else + eprintf("usage: %s [-d time] [+format]\n", argv[0]); + now = localtime(&t); + if(!now) + eprintf("localtime failed\n"); + + strftime(buf, sizeof buf, fmt, now); + puts(buf); + return EXIT_SUCCESS; +} diff --git a/sleep.1 b/sleep.1 new file mode 100644 index 0000000..3ddab7e --- /dev/null +++ b/sleep.1 @@ -0,0 +1,9 @@ +.TH SLEEP 1 sbase\-VERSION +.SH NAME +sleep \- wait for a number of seconds +.SH SYNOPSIS +.B sleep +.I seconds +.SH DESCRIPTION +.B sleep +waits until the given number of seconds have elapsed. diff --git a/sleep.c b/sleep.c new file mode 100644 index 0000000..02fe791 --- /dev/null +++ b/sleep.c @@ -0,0 +1,18 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include "util.h" + +int +main(int argc, char *argv[]) +{ + unsigned int seconds; + + if(argc != 2) + eprintf("usage: %s seconds\n", argv[0]); + + seconds = atoi(argv[1]); + while((seconds = sleep(seconds)) > 0) + ; + return EXIT_SUCCESS; +} diff --git a/touch.c b/touch.c index c0e3599..13b51c9 100644 --- a/touch.c +++ b/touch.c @@ -21,7 +21,6 @@ main(int argc, char *argv[]) int i; t = time(NULL); - for(i = 1; i < argc; i++) if(!strcmp(argv[i], "-c")) cflag = true;