]> TLD Linux GIT Repositories - rc-scripts.git/blob - src/usleep.c
- from PLD
[rc-scripts.git] / src / usleep.c
1 /*
2  * usleep.c     Sleep for the specified number of microseconds
3  *
4  * Usage:       usleep [ microseconds ]
5  *
6  * Copyright 2001 Werner Fink, 2001 SuSE GmbH Nuernberg, Germany.
7  * Copyright 2005 Werner Fink, 2005 SUSE LINUX Products GmbH, Germany.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Author:      Werner Fink <werner@suse.de>
15  */
16
17 #ifndef  __USE_STRING_INLINES
18 # define __USE_STRING_INLINES
19 #endif
20 #ifdef   __NO_STRING_INLINES
21 # undef  __NO_STRING_INLINES
22 #endif
23 #include <libgen.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #ifdef _POSIX_PRIORITY_SCHEDULING
29 # include <sched.h>
30 #endif
31 #define USAGE           "Usage:\t%s [ microseconds ]\n", we_are
32
33 static char *we_are;
34 int main(int argc, char **argv)
35 {
36     unsigned long int usec = 1;
37
38     if (argc > 2)
39         goto err;
40
41     if (argc > 1) {
42         char *endptr;
43         usec = strtoul(argv[1], &endptr, 10);
44         if (*endptr != '\0')
45             goto err;
46     }
47
48     if (usec)
49         usleep(usec);
50 #ifdef _POSIX_PRIORITY_SCHEDULING
51     else
52         (void)sched_yield();
53 #endif
54     _exit(0);
55
56     /* Do this at the end for speed */
57 err:
58     we_are = basename(argv[0]);
59     fprintf(stderr, USAGE);
60
61     if (argc > 1 && *(argv[1]) == '-') {
62         argv[1]++;
63         if (!strcmp(argv[1], "-help") || *(argv[1]) == 'h' || *(argv[1]) == '?') {
64             fprintf(stderr, "Sleep for the specified number of microseconds.\n\n");
65             fprintf(stderr, "Help options:\n");
66             fprintf(stderr, "  -h, -?, --help    display this help and exit.\n");
67             exit (0);
68         }
69     }
70     exit (1);
71 }