coreutils

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

AuthorJamozed <[email protected]>
Date2020-06-26 11:26:39
Commite0fc3a5da1394796d0d96e6fb9ec1e589d68c46a
Parentb3fcfc938f3d168a7ec39e0ed747c37252efe204

sleep: Add POSIX sleep

Diffstat

M CMakeLists.txt | 1 +
M README.md | 1 +
A man/sleep.1 | 38 ++++++++++++++++++++++++++++++++++++++
A src/sleep.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

4 files changed, 133 insertions, 0 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 16551b5..d63b5c1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -20,6 +20,7 @@ ADD_EXECUTABLE(logname  ${CMAKE_SOURCE_DIR}/src/logname.c)
 ADD_EXECUTABLE(mkdir    ${CMAKE_SOURCE_DIR}/src/mkdir.c)
 ADD_EXECUTABLE(pwd      ${CMAKE_SOURCE_DIR}/src/pwd.c)
 ADD_EXECUTABLE(rmdir    ${CMAKE_SOURCE_DIR}/src/rmdir.c)
+ADD_EXECUTABLE(sleep    ${CMAKE_SOURCE_DIR}/src/sleep.c)
 ADD_EXECUTABLE(tee      ${CMAKE_SOURCE_DIR}/src/tee.c)
 ADD_EXECUTABLE(true     ${CMAKE_SOURCE_DIR}/src/true.c)
 ADD_EXECUTABLE(unlink   ${CMAKE_SOURCE_DIR}/src/unlink.c)
diff --git a/README.md b/README.md
index c4666d3..6c21bfd 100644
--- a/README.md
+++ b/README.md
@@ -19,6 +19,7 @@ UNIX-like systems.
 | mkdir            | Make directories                         | POSIX    |
 | pwd              | Print working directory                  | POSIX    |
 | rmdir            | Remove directories                       | POSIX    |
+| sleep            | Suspend execution for an interval        | POSIX    |
 | tee              | Duplicate standard input                 | POSIX    |
 | true             | Return true value                        | POSIX    |
 | unlink           | Remove a file using the unlink function  | POSIX    |
diff --git a/man/sleep.1 b/man/sleep.1
new file mode 100644
index 0000000..a8f4234
--- /dev/null
+++ b/man/sleep.1
@@ -0,0 +1,38 @@
+.TH SLEEP 1 2020-06-26 "OMKOV coreutils" "General Commands Manual"
+.SH NAME
+sleep \(em suspend execution for an interval
+.SH SYNOPSYS
+\fBsleep\fR \fItime\fR
+.SH DESCRIPTION
+Suspend execution for at least the number of seconds specified by \fItime\fR.
+.SH OPTIONS
+The following options are supported:
+.TP
+.B --help
+Display help information.
+.TP
+.B --version
+Display version information.
+.SH OPERANDS
+The following operand is supported:
+.TP
+.I time
+The number of seconds for which to suspend execution. Must be a positive decimal
+integer.
+.SH EXIT STATUS
+The following exit values will be returned:
+.TP
+\ 0
+Execution was successfully suspended for at least \fItime\fR seconds.
+.TP
+>0
+An error occurred.
+.SH STANDARDS
+The \fIsleep\fR utility is compliant with the IEEE Std 1003.2-1992 ("POSIX.2")
+specification.
+.SH COPYRIGHT
+.nf
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+OMKOV Permissive Licence (https://www.omkov.net/OLPE)
+.fi
diff --git a/src/sleep.c b/src/sleep.c
new file mode 100644
index 0000000..7bb5f9f
--- /dev/null
+++ b/src/sleep.c
@@ -0,0 +1,93 @@
+// sleep.c, version 1.0.0
+// OMKOV coreutils implementation of POSIX sleep
+// Copyright (C) 2020, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Permissive Licence, version 1.0
+
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimers.
+* Redistributions in binary form must reproduce the above copyright notice, this
+  list of conditions and the following disclaimers in the documentation and/or
+  other materials provided with the distribution.
+* Neither the names of the copyright holders, nor the names of its contributors
+  may be used to endorse or promote products derived from this Software without
+  specific prior written permission.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT
+HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
+*/
+
+#include "error.h"
+#include "optget.h"
+
+#include <unistd.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+
+static void help(void);
+static void version(void);
+
+int main(int argc, char *argv[]) {
+	lop_t lops[] = {
+		{ "help",    ARG_NUL, 256 },
+		{ "version", ARG_NUL, 257 },
+		{ NULL, 0, 0 }
+	};
+	
+	opt_t opt = OPTGET_INIT; opt.str = ""; opt.lops = lops; int o;
+	while ((o = optget(&opt, argv, 1)) != -1) switch (o) {
+	case 256: { help(); return 0; }
+	case 257: { version(); return 0; }
+	default: { return 1; }
+	}
+	
+	if (opt.ind == argc) { error(1, "%s: missing operand", argv[0]); }
+	
+	register uintmax_t n = 0, d; register char *p = argv[1];
+	for (; *p >= '0' && *p <= '9'; ++p) {
+		d = (uintmax_t)*p - '0';
+		if (n > (UINTMAX_MAX - d) / 10) { break; }
+		n = n * 10 + d;
+	}
+	
+	if (*p) { error(1, "%s: %s: invalid time interval", argv[0], argv[1]); }
+	
+	return (int)sleep(n);
+}
+
+static void help(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");
+	return;
+}
+
+static void version(void) {
+	puts("OMKOV coreutils sleep, version 1.0.0");
+	puts("Copyright (C) 2020, Jakob Wakeling");
+	puts("All rights reserved.");
+	puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)");
+	return;
+}