1 | /* $Id: sleep.c 2003 2008-10-29 23:15:18Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kmk_sleep - suspend execution for an interval of time.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2008 knut st. osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * This file is part of kBuild.
|
---|
10 | *
|
---|
11 | * kBuild is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kBuild is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with kBuild; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <stdlib.h>
|
---|
33 | #include <string.h>
|
---|
34 | #include <ctype.h>
|
---|
35 | #include <errno.h>
|
---|
36 | #if defined(_MSC_VER)
|
---|
37 | # include <Windows.h>
|
---|
38 | #else
|
---|
39 | # include <unistd.h>
|
---|
40 | # include <time.h>
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #include "../kmkbuiltin.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | static const char *name(const char *pszName)
|
---|
47 | {
|
---|
48 | const char *psz = strrchr(pszName, '/');
|
---|
49 | #if defined(_MSC_VER) || defined(__OS2__)
|
---|
50 | const char *psz2 = strrchr(pszName, '\\');
|
---|
51 | if (!psz2)
|
---|
52 | psz2 = strrchr(pszName, ':');
|
---|
53 | if (psz2 && (!psz || psz2 > psz))
|
---|
54 | psz = psz2;
|
---|
55 | #endif
|
---|
56 | return psz ? psz + 1 : pszName;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | static int usage(FILE *pOut, const char *argv0)
|
---|
61 | {
|
---|
62 | argv0 = name(argv0);
|
---|
63 | fprintf(pOut,
|
---|
64 | "usage: %s <seconds>[s]\n"
|
---|
65 | " or: %s <milliseconds>ms\n"
|
---|
66 | " or: %s <minutes>m\n"
|
---|
67 | " or: %s <hours>h\n"
|
---|
68 | " or: %s <days>d\n"
|
---|
69 | " or: %s --help\n"
|
---|
70 | " or: %s --version\n"
|
---|
71 | "\n"
|
---|
72 | "Only unsinged integer values are accepted.\n"
|
---|
73 | ,
|
---|
74 | argv0, argv0, argv0, argv0, argv0, argv0, argv0);
|
---|
75 | return 1;
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | int kmk_builtin_sleep(int argc, char **argv, char **envp)
|
---|
80 | {
|
---|
81 | long cMsToSleep;
|
---|
82 | long lTmp;
|
---|
83 | unsigned long ulFactor;
|
---|
84 | char *pszInterval;
|
---|
85 | char *pszSuff;
|
---|
86 |
|
---|
87 | /*
|
---|
88 | * Parse arguments.
|
---|
89 | */
|
---|
90 | if (argc != 2)
|
---|
91 | return usage(stderr, argv[0]);
|
---|
92 |
|
---|
93 | /* help request */
|
---|
94 | if ( !strcmp(argv[1], "-h")
|
---|
95 | || !strcmp(argv[1], "-?")
|
---|
96 | || !strcmp(argv[1], "-H")
|
---|
97 | || !strcmp(argv[1], "--help"))
|
---|
98 | {
|
---|
99 | usage(stdout, argv[0]);
|
---|
100 | return 0;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /* version request */
|
---|
104 | if ( !strcmp(argv[1], "-V")
|
---|
105 | || !strcmp(argv[1], "--version"))
|
---|
106 | {
|
---|
107 | printf("kmk_sleep - kBuild version %d.%d.%d (r%u)\n"
|
---|
108 | "Copyright (C) 2008 Knut St. Osmundsen\n",
|
---|
109 | KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH,
|
---|
110 | KBUILD_SVN_REV);
|
---|
111 | return 0;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * Try convert the argument to a time period.
|
---|
116 | * Allow spaces before, between and after the different parts.
|
---|
117 | */
|
---|
118 | pszInterval = argv[1];
|
---|
119 | while (isspace(*pszInterval))
|
---|
120 | pszInterval++;
|
---|
121 |
|
---|
122 | cMsToSleep = strtol(pszInterval, &pszSuff, 0);
|
---|
123 | if (pszSuff == pszInterval)
|
---|
124 | {
|
---|
125 | fprintf(stderr, "%s: malformed interval '%s'!\n", name(argv[0]), pszInterval);
|
---|
126 | return 1;
|
---|
127 | }
|
---|
128 |
|
---|
129 | while (isspace(*pszSuff))
|
---|
130 | pszSuff++;
|
---|
131 |
|
---|
132 | if (!*pszSuff)
|
---|
133 | ulFactor = 1000; /* s */
|
---|
134 | else
|
---|
135 | {
|
---|
136 | /* find the suffix length and check that it's only white space following it. */
|
---|
137 | int cchSuff;
|
---|
138 | int i = 1;
|
---|
139 | while (pszSuff[i] && !isspace(pszSuff[i]))
|
---|
140 | i++;
|
---|
141 | cchSuff = i;
|
---|
142 | while (pszSuff[i])
|
---|
143 | {
|
---|
144 | if (!isspace(pszSuff[i]))
|
---|
145 | {
|
---|
146 | fprintf(stderr, "%s: malformed interval '%s'!\n", name(argv[0]), pszInterval);
|
---|
147 | return 1;
|
---|
148 | }
|
---|
149 | i++;
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (cchSuff == 2 && !strncmp (pszSuff, "ms", 2))
|
---|
153 | ulFactor = 1;
|
---|
154 | else if (cchSuff == 1 && *pszSuff == 's')
|
---|
155 | ulFactor = 1000;
|
---|
156 | else if (cchSuff == 1 && *pszSuff == 'm')
|
---|
157 | ulFactor = 60*1000;
|
---|
158 | else if (cchSuff == 1 && *pszSuff == 'h')
|
---|
159 | ulFactor = 60*60*1000;
|
---|
160 | else if (cchSuff == 1 && *pszSuff == 'd')
|
---|
161 | ulFactor = 24*60*60*1000;
|
---|
162 | else
|
---|
163 | {
|
---|
164 | fprintf(stderr, "%s: unknown suffix '%.*s'!\n", name(argv[0]), cchSuff, pszSuff);
|
---|
165 | return 1;
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | lTmp = cMsToSleep;
|
---|
170 | cMsToSleep *= ulFactor;
|
---|
171 | if ((cMsToSleep / ulFactor) != (unsigned long)lTmp)
|
---|
172 | {
|
---|
173 | fprintf(stderr, "%s: time interval overflow!\n", name(argv[0]));
|
---|
174 | return 1;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /*
|
---|
178 | * Do the actual sleeping.
|
---|
179 | */
|
---|
180 | if (cMsToSleep > 0)
|
---|
181 | {
|
---|
182 | #if defined(_MSC_VER)
|
---|
183 | Sleep(cMsToSleep);
|
---|
184 | #else
|
---|
185 | if (cMsToSleep)
|
---|
186 | {
|
---|
187 | struct timespec TimeSpec;
|
---|
188 | TimeSpec.tv_nsec = (cMsToSleep % 1000) * 1000000;
|
---|
189 | TimeSpec.tv_sec = cMsToSleep / 1000;
|
---|
190 | nanosleep(&TimeSpec, NULL);
|
---|
191 | }
|
---|
192 | #endif
|
---|
193 | }
|
---|
194 |
|
---|
195 | return 0;
|
---|
196 | }
|
---|
197 |
|
---|