*: Updated copyright to 2009 and normalized name & email.
[kbuild-mirror.git] / src / kmk / kmkbuiltin / sleep.c
blob7b52a7104be6ee30c79898727ae020bdfa1126d6
1 /* $Id$ */
2 /** @file
3 * kmk_sleep - suspend execution for an interval of time.
4 */
6 /*
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 /*******************************************************************************
27 * Header Files *
28 *******************************************************************************/
29 #include "config.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <errno.h>
35 #if defined(_MSC_VER)
36 # include <Windows.h>
37 #else
38 # include <unistd.h>
39 # include <time.h>
40 #endif
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, '\\');
50 if (!psz2)
51 psz2 = strrchr(pszName, ':');
52 if (psz2 && (!psz || psz2 > psz))
53 psz = psz2;
54 #endif
55 return psz ? psz + 1 : pszName;
59 static int usage(FILE *pOut, const char *argv0)
61 argv0 = name(argv0);
62 fprintf(pOut,
63 "usage: %s <seconds>[s]\n"
64 " or: %s <milliseconds>ms\n"
65 " or: %s <minutes>m\n"
66 " or: %s <hours>h\n"
67 " or: %s <days>d\n"
68 " or: %s --help\n"
69 " or: %s --version\n"
70 "\n"
71 "Only integer values are accepted.\n"
73 argv0, argv0, argv0, argv0, argv0, argv0, argv0);
74 return 1;
78 int kmk_builtin_sleep(int argc, char **argv, char **envp)
80 long cMsToSleep;
81 long lTmp;
82 unsigned long ulFactor;
83 char *pszInterval;
84 char *pszSuff;
87 * Parse arguments.
89 if (argc != 2)
90 return usage(stderr, argv[0]);
92 /* help request */
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]);
99 return 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,
109 KBUILD_SVN_REV);
110 return 0;
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))
119 pszInterval++;
121 cMsToSleep = strtol(pszInterval, &pszSuff, 0);
122 if (pszSuff == pszInterval)
124 fprintf(stderr, "%s: malformed interval '%s'!\n", name(argv[0]), pszInterval);
125 return 1;
128 while (isspace(*pszSuff))
129 pszSuff++;
131 if (!*pszSuff)
132 ulFactor = 1000; /* s */
133 else
135 /* find the suffix length and check that it's only white space following it. */
136 int cchSuff;
137 int i = 1;
138 while (pszSuff[i] && !isspace(pszSuff[i]))
139 i++;
140 cchSuff = i;
141 while (pszSuff[i])
143 if (!isspace(pszSuff[i]))
145 fprintf(stderr, "%s: malformed interval '%s'!\n", name(argv[0]), pszInterval);
146 return 1;
148 i++;
151 if (cchSuff == 2 && !strncmp (pszSuff, "ms", 2))
152 ulFactor = 1;
153 else if (cchSuff == 1 && *pszSuff == 's')
154 ulFactor = 1000;
155 else if (cchSuff == 1 && *pszSuff == 'm')
156 ulFactor = 60*1000;
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;
161 else
163 fprintf(stderr, "%s: unknown suffix '%.*s'!\n", name(argv[0]), cchSuff, pszSuff);
164 return 1;
168 lTmp = cMsToSleep;
169 cMsToSleep *= ulFactor;
170 if ((cMsToSleep / ulFactor) != (unsigned long)lTmp)
172 fprintf(stderr, "%s: time interval overflow!\n", name(argv[0]));
173 return 1;
177 * Do the actual sleeping.
179 if (cMsToSleep > 0)
181 #if defined(_MSC_VER)
182 Sleep(cMsToSleep);
183 #else
184 if (cMsToSleep)
186 struct timespec TimeSpec;
187 TimeSpec.tv_nsec = (cMsToSleep % 1000) * 1000000;
188 TimeSpec.tv_sec = cMsToSleep / 1000;
189 nanosleep(&TimeSpec, NULL);
191 #endif
194 return 0;