1 /* $Id: sleep.c 2413 2010-09-11 17:43:04Z 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 "../kmkbuiltin.h"
43 
44 
name(const char * pszName)45 static const char *name(const char *pszName)
46 {
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;
56 }
57 
58 
usage(FILE * pOut,const char * argv0)59 static int usage(FILE *pOut,  const char *argv0)
60 {
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"
72             ,
73             argv0, argv0, argv0, argv0, argv0, argv0, argv0);
74     return 1;
75 }
76 
77 
kmk_builtin_sleep(int argc,char ** argv,char ** envp)78 int kmk_builtin_sleep(int argc, char **argv, char **envp)
79 {
80     long cMsToSleep;
81     long lTmp;
82     unsigned long ulFactor;
83     char *pszInterval;
84     char *pszSuff;
85 
86     /*
87      * Parse arguments.
88      */
89     if (argc != 2)
90         return usage(stderr, argv[0]);
91 
92     /* help request */
93     if (   !strcmp(argv[1], "-h")
94         || !strcmp(argv[1], "-?")
95         || !strcmp(argv[1], "-H")
96         || !strcmp(argv[1], "--help"))
97     {
98         usage(stdout, argv[0]);
99         return 0;
100     }
101 
102     /* version request */
103     if (   !strcmp(argv[1], "-V")
104         || !strcmp(argv[1], "--version"))
105     {
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;
111     }
112 
113     /*
114      * Try convert the argument to a time period.
115      * Allow spaces before, between and after the different parts.
116      */
117     pszInterval = argv[1];
118     while (isspace(*pszInterval))
119         pszInterval++;
120 
121     cMsToSleep = strtol(pszInterval, &pszSuff, 0);
122     if (pszSuff == pszInterval)
123     {
124         fprintf(stderr, "%s: malformed interval '%s'!\n", name(argv[0]), pszInterval);
125         return 1;
126     }
127 
128     while (isspace(*pszSuff))
129         pszSuff++;
130 
131     if (!*pszSuff)
132         ulFactor = 1000; /* s */
133     else
134     {
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])
142         {
143             if (!isspace(pszSuff[i]))
144             {
145                 fprintf(stderr, "%s: malformed interval '%s'!\n", name(argv[0]), pszInterval);
146                 return 1;
147             }
148             i++;
149         }
150 
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
162         {
163             fprintf(stderr, "%s: unknown suffix '%.*s'!\n", name(argv[0]), cchSuff, pszSuff);
164             return 1;
165         }
166     }
167 
168     lTmp = cMsToSleep;
169     cMsToSleep *= ulFactor;
170     if ((cMsToSleep / ulFactor) != (unsigned long)lTmp)
171     {
172         fprintf(stderr, "%s: time interval overflow!\n", name(argv[0]));
173         return 1;
174     }
175 
176     /*
177      * Do the actual sleeping.
178      */
179     if (cMsToSleep > 0)
180     {
181 #if defined(_MSC_VER)
182         Sleep(cMsToSleep);
183 #else
184         if (cMsToSleep)
185         {
186             struct timespec TimeSpec;
187             TimeSpec.tv_nsec = (cMsToSleep % 1000) * 1000000;
188             TimeSpec.tv_sec  =  cMsToSleep / 1000;
189             nanosleep(&TimeSpec, NULL);
190         }
191 #endif
192     }
193 
194     return 0;
195 }
196 
197