| 1 | /* $Id: sleep.c 2019 2008-11-02 00:21:05Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * kmk_sleep - suspend execution for an interval of time.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2008 knut st. osmundsen <bird-src-spam@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 <stdio.h>
|
|---|
| 30 | #include <stdlib.h>
|
|---|
| 31 | #include <string.h>
|
|---|
| 32 | #include <ctype.h>
|
|---|
| 33 | #include <errno.h>
|
|---|
| 34 | #if defined(_MSC_VER)
|
|---|
| 35 | # include <Windows.h>
|
|---|
| 36 | #else
|
|---|
| 37 | # include <unistd.h>
|
|---|
| 38 | # include <time.h>
|
|---|
| 39 | #endif
|
|---|
| 40 |
|
|---|
| 41 | #include "../kmkbuiltin.h"
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | static const char *name(const char *pszName)
|
|---|
| 45 | {
|
|---|
| 46 | const char *psz = strrchr(pszName, '/');
|
|---|
| 47 | #if defined(_MSC_VER) || defined(__OS2__)
|
|---|
| 48 | const char *psz2 = strrchr(pszName, '\\');
|
|---|
| 49 | if (!psz2)
|
|---|
| 50 | psz2 = strrchr(pszName, ':');
|
|---|
| 51 | if (psz2 && (!psz || psz2 > psz))
|
|---|
| 52 | psz = psz2;
|
|---|
| 53 | #endif
|
|---|
| 54 | return psz ? psz + 1 : pszName;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 | static int usage(FILE *pOut, const char *argv0)
|
|---|
| 59 | {
|
|---|
| 60 | argv0 = name(argv0);
|
|---|
| 61 | fprintf(pOut,
|
|---|
| 62 | "usage: %s <seconds>[s]\n"
|
|---|
| 63 | " or: %s <milliseconds>ms\n"
|
|---|
| 64 | " or: %s <minutes>m\n"
|
|---|
| 65 | " or: %s <hours>h\n"
|
|---|
| 66 | " or: %s <days>d\n"
|
|---|
| 67 | " or: %s --help\n"
|
|---|
| 68 | " or: %s --version\n"
|
|---|
| 69 | "\n"
|
|---|
| 70 | "Only unsinged integer values are accepted.\n"
|
|---|
| 71 | ,
|
|---|
| 72 | argv0, argv0, argv0, argv0, argv0, argv0, argv0);
|
|---|
| 73 | return 1;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | int kmk_builtin_sleep(int argc, char **argv, char **envp)
|
|---|
| 78 | {
|
|---|
| 79 | long cMsToSleep;
|
|---|
| 80 | long lTmp;
|
|---|
| 81 | unsigned long ulFactor;
|
|---|
| 82 | char *pszInterval;
|
|---|
| 83 | char *pszSuff;
|
|---|
| 84 |
|
|---|
| 85 | /*
|
|---|
| 86 | * Parse arguments.
|
|---|
| 87 | */
|
|---|
| 88 | if (argc != 2)
|
|---|
| 89 | return usage(stderr, argv[0]);
|
|---|
| 90 |
|
|---|
| 91 | /* help request */
|
|---|
| 92 | if ( !strcmp(argv[1], "-h")
|
|---|
| 93 | || !strcmp(argv[1], "-?")
|
|---|
| 94 | || !strcmp(argv[1], "-H")
|
|---|
| 95 | || !strcmp(argv[1], "--help"))
|
|---|
| 96 | {
|
|---|
| 97 | usage(stdout, argv[0]);
|
|---|
| 98 | return 0;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /* version request */
|
|---|
| 102 | if ( !strcmp(argv[1], "-V")
|
|---|
| 103 | || !strcmp(argv[1], "--version"))
|
|---|
| 104 | {
|
|---|
| 105 | printf("kmk_sleep - kBuild version %d.%d.%d (r%u)\n"
|
|---|
| 106 | "Copyright (C) 2008 Knut St. Osmundsen\n",
|
|---|
| 107 | KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH,
|
|---|
| 108 | KBUILD_SVN_REV);
|
|---|
| 109 | return 0;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /*
|
|---|
| 113 | * Try convert the argument to a time period.
|
|---|
| 114 | * Allow spaces before, between and after the different parts.
|
|---|
| 115 | */
|
|---|
| 116 | pszInterval = argv[1];
|
|---|
| 117 | while (isspace(*pszInterval))
|
|---|
| 118 | pszInterval++;
|
|---|
| 119 |
|
|---|
| 120 | cMsToSleep = strtol(pszInterval, &pszSuff, 0);
|
|---|
| 121 | if (pszSuff == pszInterval)
|
|---|
| 122 | {
|
|---|
| 123 | fprintf(stderr, "%s: malformed interval '%s'!\n", name(argv[0]), pszInterval);
|
|---|
| 124 | return 1;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | while (isspace(*pszSuff))
|
|---|
| 128 | pszSuff++;
|
|---|
| 129 |
|
|---|
| 130 | if (!*pszSuff)
|
|---|
| 131 | ulFactor = 1000; /* s */
|
|---|
| 132 | else
|
|---|
| 133 | {
|
|---|
| 134 | /* find the suffix length and check that it's only white space following it. */
|
|---|
| 135 | int cchSuff;
|
|---|
| 136 | int i = 1;
|
|---|
| 137 | while (pszSuff[i] && !isspace(pszSuff[i]))
|
|---|
| 138 | i++;
|
|---|
| 139 | cchSuff = i;
|
|---|
| 140 | while (pszSuff[i])
|
|---|
| 141 | {
|
|---|
| 142 | if (!isspace(pszSuff[i]))
|
|---|
| 143 | {
|
|---|
| 144 | fprintf(stderr, "%s: malformed interval '%s'!\n", name(argv[0]), pszInterval);
|
|---|
| 145 | return 1;
|
|---|
| 146 | }
|
|---|
| 147 | i++;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | if (cchSuff == 2 && !strncmp (pszSuff, "ms", 2))
|
|---|
| 151 | ulFactor = 1;
|
|---|
| 152 | else if (cchSuff == 1 && *pszSuff == 's')
|
|---|
| 153 | ulFactor = 1000;
|
|---|
| 154 | else if (cchSuff == 1 && *pszSuff == 'm')
|
|---|
| 155 | ulFactor = 60*1000;
|
|---|
| 156 | else if (cchSuff == 1 && *pszSuff == 'h')
|
|---|
| 157 | ulFactor = 60*60*1000;
|
|---|
| 158 | else if (cchSuff == 1 && *pszSuff == 'd')
|
|---|
| 159 | ulFactor = 24*60*60*1000;
|
|---|
| 160 | else
|
|---|
| 161 | {
|
|---|
| 162 | fprintf(stderr, "%s: unknown suffix '%.*s'!\n", name(argv[0]), cchSuff, pszSuff);
|
|---|
| 163 | return 1;
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | lTmp = cMsToSleep;
|
|---|
| 168 | cMsToSleep *= ulFactor;
|
|---|
| 169 | if ((cMsToSleep / ulFactor) != (unsigned long)lTmp)
|
|---|
| 170 | {
|
|---|
| 171 | fprintf(stderr, "%s: time interval overflow!\n", name(argv[0]));
|
|---|
| 172 | return 1;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /*
|
|---|
| 176 | * Do the actual sleeping.
|
|---|
| 177 | */
|
|---|
| 178 | if (cMsToSleep > 0)
|
|---|
| 179 | {
|
|---|
| 180 | #if defined(_MSC_VER)
|
|---|
| 181 | Sleep(cMsToSleep);
|
|---|
| 182 | #else
|
|---|
| 183 | if (cMsToSleep)
|
|---|
| 184 | {
|
|---|
| 185 | struct timespec TimeSpec;
|
|---|
| 186 | TimeSpec.tv_nsec = (cMsToSleep % 1000) * 1000000;
|
|---|
| 187 | TimeSpec.tv_sec = cMsToSleep / 1000;
|
|---|
| 188 | nanosleep(&TimeSpec, NULL);
|
|---|
| 189 | }
|
|---|
| 190 | #endif
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | return 0;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|