coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

coreutils/src/sleep.c (60 lines, 1.4 KiB) -rw-r--r-- blame download

01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
// sleep.c, version 1.0.2
// OMKOV coreutils implementation of POSIX sleep
// Copyright (C) 2020, Jakob Wakeling
// MIT Licence

#include "util/error.h"
#include "util/optget.h"

#include <unistd.h>

#include <stdio.h>
#include <stdint.h>

#define VERSION "1.0.2"

static struct lop lops[] = {
	{ "help",    ARG_NUL, 256 },
	{ "version", ARG_NUL, 257 },
	{ NULL, 0, 0 }
};

static void hlp(void);
static void ver(void);

int main(int ac, char *av[]) { A0 = av[0];
	struct opt opt = OPTGET_INIT; opt.str = ""; opt.lops = lops;
	for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) {
	case 256: { hlp(); return 0; }
	case 257: { ver(); return 0; }
	default: { return 1; }
	}
	
	if (opt.ind == ac) { error(1, "missing operand"); }
	
	register unsigned n = 0, d; register char *p = av[1];
	for (; *p >= '0' && *p <= '9'; ++p) {
		d = (unsigned)*p - '0';
		if (n > (UINTMAX_MAX - d) / 10) { break; }
		n = n * 10 + d;
	}
	
	if (*p) { error(1, "%s: invalid time interval", av[1]); }
	
	return (int)sleep(n);
}

static void hlp(void) {
	puts("sleep - suspend execution for an interval\n");
	puts("usage: sleep time\n");
	puts("options:");
	puts("  --help     Display help information");
	puts("  --version  Display version information");
}

static void ver(void) {
	puts("OMKOV coreutils sleep, version " VERSION);
	puts("Copyright (C) 2020, Jakob Wakeling");
	puts("MIT Licence (https://opensource.org/licenses/MIT)");
}