tee: Added tee
[mutos-utils.git] / sleep.c
blob892de3d529c26604f3617edce6ca04504124e77c
1 /*
2 Copyright © 2013 Alastair Stuart
4 This program is open source software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
15 #include <ctype.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
20 #include <getopt.h>
21 #include <unistd.h>
23 #define VERSION "0.01"
25 void usage(char* program)
27 printf("Usage: %s [number]\n", program);
28 printf("Pauses for [number] seconds.\n"
29 "\n"
30 " --help Print this message.\n"
31 " --version Show version info.\n");
34 int main(int argc, char* argv[])
36 static struct option long_options[] = {
37 {"help", no_argument, NULL, 1},
38 {"version", no_argument, NULL, 2},
39 {NULL, 0, NULL, 0}
42 int c = 0;
43 while ((c = getopt_long(argc, argv, "R",
44 long_options, NULL)) != -1)
46 switch (c)
48 case 1:
49 usage(argv[0]);
50 return 0;
51 case 2:
52 printf("sleep (mutos) v"VERSION"\n");
53 return 0;
54 default:
55 fprintf(stderr,"Run '%s --help' for usage.\n",
56 argv[0]);
57 return 1;
61 if ((argc - optind) == 0) {
62 fprintf(stderr, "%s: missing operand\n"
63 "Run '%s --help' for usage.\n",
64 argv[0], argv[0]);
65 return 1;
68 if ((argc - optind) > 1) {
69 fprintf(stderr, "%s: too many operands\n"
70 "Run '%s --help' for usage.\n",
71 argv[0], argv[0]);
72 return 1;
75 // validate number
76 for (size_t i = 0; i < strlen(argv[optind]); i++)
78 if (!isdigit(argv[optind][i])) {
79 fprintf(stderr, "%s: invalid number: %s\n",
80 argv[0], argv[optind]);
81 return 1;
85 sleep(atoi(argv[optind]));