aboutsummaryrefslogtreecommitdiff
path: root/user/bin/sleep.c
diff options
context:
space:
mode:
authornthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
committernthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
commitc63f340d90800895f007de64b7d2d14624263331 (patch)
tree2c0849fa597dd6da831c8707b6f2603403778d7b /user/bin/sleep.c
Created student weenix repository
Diffstat (limited to 'user/bin/sleep.c')
-rw-r--r--user/bin/sleep.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/user/bin/sleep.c b/user/bin/sleep.c
new file mode 100644
index 0000000..6b0d485
--- /dev/null
+++ b/user/bin/sleep.c
@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define SECONDS_TO_MICROSECONDS 1000000
+
+void help(int argc, char *argv[])
+{
+ fprintf(stderr, "usage: %s [-u (micrseconds)] <time>\n", argv[0]);
+ exit(1);
+}
+
+int main(int argc, char *argv[])
+{
+ if (argc > 3 || argc == 1)
+ {
+ help(argc, argv);
+ }
+
+ uint64_t tm = 0;
+
+ int use_usec = 0;
+ if (argc > 2)
+ {
+ if (strcmp(argv[1], "-u") == 0)
+ {
+ tm = atoi(argv[2]);
+ use_usec = 1;
+ }
+ else
+ {
+ help(argc, argv);
+ }
+ }
+
+ if (!use_usec)
+ {
+ tm = atoi(argv[1]);
+ tm *= SECONDS_TO_MICROSECONDS;
+ }
+
+ return usleep(tm);
+} \ No newline at end of file