add sleep & date, thanks kamil

This commit is contained in:
Connor Lane Smith 2011-05-23 19:00:31 +01:00
parent 8e26716a5a
commit 474ee643ed
9 changed files with 84 additions and 5 deletions

View File

@ -1,6 +1,7 @@
MIT/X Consortium License MIT/X Consortium License
© 2011 Connor Lane Smith <cls@lubutu.com> © 2011 Connor Lane Smith <cls@lubutu.com>
© 2011 Kamil Cholewiński <harry666t@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),

View File

@ -1,6 +1,6 @@
include config.mk 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 OBJ = $(SRC:.c=.o) util.o
BIN = $(SRC:.c=) BIN = $(SRC:.c=)
MAN = $(SRC:.c=.1) MAN = $(SRC:.c=.1)

2
cat.1
View File

@ -3,7 +3,7 @@
cat \- concatenate files cat \- concatenate files
.SH SYNOPSIS .SH SYNOPSIS
.B cat .B cat
.RI [ files ...] .RI [ file ...]
.SH DESCRIPTION .SH DESCRIPTION
.B cat .B cat
reads each file in sequence and writes it to stdout. If no file is given, cat reads each file in sequence and writes it to stdout. If no file is given, cat

View File

@ -1,8 +1,8 @@
# sbase version # sbase version
VERSION = 0.0 VERSION = 0.0
CC = cc #CC = cc
#CC = musl-gcc CC = musl-gcc
CPPFLAGS = -D_BSD_SOURCE CPPFLAGS = -D_BSD_SOURCE
CFLAGS = -Os -ansi -Wall -pedantic $(CPPFLAGS) CFLAGS = -Os -ansi -Wall -pedantic $(CPPFLAGS)

20
date.1 Normal file
View File

@ -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.

32
date.c Normal file
View File

@ -0,0 +1,32 @@
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#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;
}

9
sleep.1 Normal file
View File

@ -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.

18
sleep.c Normal file
View File

@ -0,0 +1,18 @@
/* See LICENSE file for copyright and license details. */
#include <stdlib.h>
#include <unistd.h>
#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;
}

View File

@ -21,7 +21,6 @@ main(int argc, char *argv[])
int i; int i;
t = time(NULL); t = time(NULL);
for(i = 1; i < argc; i++) for(i = 1; i < argc; i++)
if(!strcmp(argv[i], "-c")) if(!strcmp(argv[i], "-c"))
cflag = true; cflag = true;