VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/sleep.c@ 3387

Last change on this file since 3387 was 3192, checked in by bird, 6 years ago

kmkbuiltin: funnel output thru output.c (usually via err.c).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/* $Id: sleep.c 3192 2018-03-26 20:25:56Z bird $ */
2/** @file
3 * kmk_sleep - suspend execution for an interval of time.
4 */
5
6/*
7 * Copyright (c) 2008-2010 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
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 3 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, see <http://www.gnu.org/licenses/>
23 *
24 */
25
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
41
42#include "err.h"
43#include "../kmkbuiltin.h"
44
45
46static int kmk_builtin_sleep_usage(PKMKBUILTINCTX pCtx, int fIsErr)
47{
48 kmk_builtin_ctx_printf(pCtx, fIsErr,
49 "usage: %s <seconds>[s]\n"
50 " or: %s <milliseconds>ms\n"
51 " or: %s <minutes>m\n"
52 " or: %s <hours>h\n"
53 " or: %s <days>d\n"
54 " or: %s --help\n"
55 " or: %s --version\n"
56 "\n"
57 "Only integer values are accepted.\n"
58 ,
59 pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName,
60 pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName);
61 return 1;
62}
63
64
65int kmk_builtin_sleep(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
66{
67 long cMsToSleep;
68 long lTmp;
69 unsigned long ulFactor;
70 char *pszInterval;
71 char *pszSuff;
72
73 /*
74 * Parse arguments.
75 */
76 if (argc != 2)
77 return kmk_builtin_sleep_usage(pCtx, 1);
78
79 /* help request */
80 if ( !strcmp(argv[1], "-h")
81 || !strcmp(argv[1], "-?")
82 || !strcmp(argv[1], "-H")
83 || !strcmp(argv[1], "--help"))
84 {
85 kmk_builtin_sleep_usage(pCtx, 0);
86 return 0;
87 }
88
89 /* version request */
90 if ( !strcmp(argv[1], "-V")
91 || !strcmp(argv[1], "--version"))
92 {
93 printf("kmk_sleep - kBuild version %d.%d.%d (r%u)\n"
94 "Copyright (c) 2008-2009 knut st. osmundsen\n",
95 KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH,
96 KBUILD_SVN_REV);
97 return 0;
98 }
99
100 /*
101 * Try convert the argument to a time period.
102 * Allow spaces before, between and after the different parts.
103 */
104 pszInterval = argv[1];
105 while (isspace(*pszInterval))
106 pszInterval++;
107
108 cMsToSleep = strtol(pszInterval, &pszSuff, 0);
109 if (pszSuff == pszInterval)
110 return errx(pCtx, 1, "malformed interval '%s'!\n", pszInterval);
111
112 while (isspace(*pszSuff))
113 pszSuff++;
114
115 if (!*pszSuff)
116 ulFactor = 1000; /* s */
117 else
118 {
119 /* find the suffix length and check that it's only white space following it. */
120 int cchSuff;
121 int i = 1;
122 while (pszSuff[i] && !isspace(pszSuff[i]))
123 i++;
124 cchSuff = i;
125 while (pszSuff[i])
126 {
127 if (!isspace(pszSuff[i]))
128 return errx(pCtx, 1, "malformed interval '%s'!\n", pszInterval);
129 i++;
130 }
131
132 if (cchSuff == 2 && !strncmp (pszSuff, "ms", 2))
133 ulFactor = 1;
134 else if (cchSuff == 1 && *pszSuff == 's')
135 ulFactor = 1000;
136 else if (cchSuff == 1 && *pszSuff == 'm')
137 ulFactor = 60*1000;
138 else if (cchSuff == 1 && *pszSuff == 'h')
139 ulFactor = 60*60*1000;
140 else if (cchSuff == 1 && *pszSuff == 'd')
141 ulFactor = 24*60*60*1000;
142 else
143 return errx(pCtx, 1, "unknown suffix '%.*s'!\n", cchSuff, pszSuff);
144 }
145
146 lTmp = cMsToSleep;
147 cMsToSleep *= ulFactor;
148 if ((cMsToSleep / ulFactor) != (unsigned long)lTmp)
149 return errx(pCtx, 1, "time interval overflow!\n");
150
151 /*
152 * Do the actual sleeping.
153 */
154 if (cMsToSleep > 0)
155 {
156#if defined(_MSC_VER)
157 Sleep(cMsToSleep);
158#else
159 if (cMsToSleep)
160 {
161 struct timespec TimeSpec;
162 TimeSpec.tv_nsec = (cMsToSleep % 1000) * 1000000;
163 TimeSpec.tv_sec = cMsToSleep / 1000;
164 nanosleep(&TimeSpec, NULL);
165 }
166#endif
167 }
168
169 return 0;
170}
171
172#ifdef KMK_BUILTIN_STANDALONE
173int main(int argc, char **argv, char **envp)
174{
175 KMKBUILTINCTX Ctx = { "kmk_sleep", NULL };
176 return kmk_builtin_sleep(argc, argv, envp, &Ctx);
177}
178#endif
179
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use