3 * kmk_sleep - suspend execution for an interval of time.
7 * Copyright (c) 2008-2009 knut st. osmundsen <bird-kBuild-spamix@anduin.net>
9 * This file is part of kBuild.
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 3 of the License, or
14 * (at your option) any later version.
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.
21 * You should have received a copy of the GNU General Public License
22 * along with kBuild. If not, see <http://www.gnu.org/licenses/>
26 /*******************************************************************************
28 *******************************************************************************/
42 #include "../kmkbuiltin.h"
45 static const char *name(const char *pszName
)
47 const char *psz
= strrchr(pszName
, '/');
48 #if defined(_MSC_VER) || defined(__OS2__)
49 const char *psz2
= strrchr(pszName
, '\\');
51 psz2
= strrchr(pszName
, ':');
52 if (psz2
&& (!psz
|| psz2
> psz
))
55 return psz
? psz
+ 1 : pszName
;
59 static int usage(FILE *pOut
, const char *argv0
)
63 "usage: %s <seconds>[s]\n"
64 " or: %s <milliseconds>ms\n"
65 " or: %s <minutes>m\n"
71 "Only integer values are accepted.\n"
73 argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
);
78 int kmk_builtin_sleep(int argc
, char **argv
, char **envp
)
82 unsigned long ulFactor
;
90 return usage(stderr
, argv
[0]);
93 if ( !strcmp(argv
[1], "-h")
94 || !strcmp(argv
[1], "-?")
95 || !strcmp(argv
[1], "-H")
96 || !strcmp(argv
[1], "--help"))
98 usage(stdout
, argv
[0]);
102 /* version request */
103 if ( !strcmp(argv
[1], "-V")
104 || !strcmp(argv
[1], "--version"))
106 printf("kmk_sleep - kBuild version %d.%d.%d (r%u)\n"
107 "Copyright (c) 2008-2009 knut st. osmundsen\n",
108 KBUILD_VERSION_MAJOR
, KBUILD_VERSION_MINOR
, KBUILD_VERSION_PATCH
,
114 * Try convert the argument to a time period.
115 * Allow spaces before, between and after the different parts.
117 pszInterval
= argv
[1];
118 while (isspace(*pszInterval
))
121 cMsToSleep
= strtol(pszInterval
, &pszSuff
, 0);
122 if (pszSuff
== pszInterval
)
124 fprintf(stderr
, "%s: malformed interval '%s'!\n", name(argv
[0]), pszInterval
);
128 while (isspace(*pszSuff
))
132 ulFactor
= 1000; /* s */
135 /* find the suffix length and check that it's only white space following it. */
138 while (pszSuff
[i
] && !isspace(pszSuff
[i
]))
143 if (!isspace(pszSuff
[i
]))
145 fprintf(stderr
, "%s: malformed interval '%s'!\n", name(argv
[0]), pszInterval
);
151 if (cchSuff
== 2 && !strncmp (pszSuff
, "ms", 2))
153 else if (cchSuff
== 1 && *pszSuff
== 's')
155 else if (cchSuff
== 1 && *pszSuff
== 'm')
157 else if (cchSuff
== 1 && *pszSuff
== 'h')
158 ulFactor
= 60*60*1000;
159 else if (cchSuff
== 1 && *pszSuff
== 'd')
160 ulFactor
= 24*60*60*1000;
163 fprintf(stderr
, "%s: unknown suffix '%.*s'!\n", name(argv
[0]), cchSuff
, pszSuff
);
169 cMsToSleep
*= ulFactor
;
170 if ((cMsToSleep
/ ulFactor
) != (unsigned long)lTmp
)
172 fprintf(stderr
, "%s: time interval overflow!\n", name(argv
[0]));
177 * Do the actual sleeping.
181 #if defined(_MSC_VER)
186 struct timespec TimeSpec
;
187 TimeSpec
.tv_nsec
= (cMsToSleep
% 1000) * 1000000;
188 TimeSpec
.tv_sec
= cMsToSleep
/ 1000;
189 nanosleep(&TimeSpec
, NULL
);