| 1 | /* $Id: kmk_time.c 1796 2008-09-19 23:51:20Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * kmk_time - Time program execution.
|
|---|
| 4 | *
|
|---|
| 5 | * This is based on kmk/kmkbuiltin/redirect.c.
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | /*
|
|---|
| 9 | * Copyright (c) 2007-2008 knut st. osmundsen <bird-kBuild-spam@anduin.net>
|
|---|
| 10 | *
|
|---|
| 11 | * This file is part of kBuild.
|
|---|
| 12 | *
|
|---|
| 13 | * kBuild is free software; you can redistribute it and/or modify
|
|---|
| 14 | * it under the terms of the GNU General Public License as published by
|
|---|
| 15 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 16 | * (at your option) any later version.
|
|---|
| 17 | *
|
|---|
| 18 | * kBuild is distributed in the hope that it will be useful,
|
|---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 21 | * GNU General Public License for more details.
|
|---|
| 22 | *
|
|---|
| 23 | * You should have received a copy of the GNU General Public License
|
|---|
| 24 | * along with kBuild; if not, write to the Free Software
|
|---|
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 26 | *
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | /*******************************************************************************
|
|---|
| 31 | * Header Files *
|
|---|
| 32 | *******************************************************************************/
|
|---|
| 33 | #include <stdio.h>
|
|---|
| 34 | #include <stdlib.h>
|
|---|
| 35 | #include <string.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <fcntl.h>
|
|---|
| 38 | #include <time.h>
|
|---|
| 39 | #if defined(_MSC_VER)
|
|---|
| 40 | # include <io.h>
|
|---|
| 41 | # include <direct.h>
|
|---|
| 42 | # include <process.h>
|
|---|
| 43 | # include <Windows.h>
|
|---|
| 44 | #else
|
|---|
| 45 | # include <unistd.h>
|
|---|
| 46 | # include <sys/times.h>
|
|---|
| 47 | #endif
|
|---|
| 48 |
|
|---|
| 49 | #ifdef __OS2__
|
|---|
| 50 | # define INCL_BASE
|
|---|
| 51 | # include <os2.h>
|
|---|
| 52 | # ifndef LIBPATHSTRICT
|
|---|
| 53 | # define LIBPATHSTRICT 3
|
|---|
| 54 | # endif
|
|---|
| 55 | #endif
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 | static const char *name(const char *pszName)
|
|---|
| 59 | {
|
|---|
| 60 | const char *psz = strrchr(pszName, '/');
|
|---|
| 61 | #if defined(_MSC_VER) || defined(__OS2__)
|
|---|
| 62 | const char *psz2 = strrchr(pszName, '\\');
|
|---|
| 63 | if (!psz2)
|
|---|
| 64 | psz2 = strrchr(pszName, ':');
|
|---|
| 65 | if (psz2 && (!psz || psz2 > psz))
|
|---|
| 66 | psz = psz2;
|
|---|
| 67 | #endif
|
|---|
| 68 | return psz ? psz + 1 : pszName;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 | static int usage(FILE *pOut, const char *argv0)
|
|---|
| 73 | {
|
|---|
| 74 | fprintf(pOut,
|
|---|
| 75 | "usage: %s <program> [args]\n"
|
|---|
| 76 | " or: %s --help\n"
|
|---|
| 77 | " or: %s --version\n"
|
|---|
| 78 | ,
|
|---|
| 79 | argv0, argv0, argv0);
|
|---|
| 80 | return 1;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 | int main(int argc, char **argv)
|
|---|
| 85 | {
|
|---|
| 86 | int i;
|
|---|
| 87 | #if defined(_MSC_VER)
|
|---|
| 88 | FILETIME ftStart, ft;
|
|---|
| 89 | intptr_t rc;
|
|---|
| 90 | #else
|
|---|
| 91 | struct timeval tvStart, tv;
|
|---|
| 92 | pid_t pid;
|
|---|
| 93 | int rc;
|
|---|
| 94 | #endif
|
|---|
| 95 |
|
|---|
| 96 | /*
|
|---|
| 97 | * Parse arguments.
|
|---|
| 98 | */
|
|---|
| 99 | if (argc <= 1)
|
|---|
| 100 | return usage(stderr, name(argv[0]));
|
|---|
| 101 | for (i = 1; i < argc; i++)
|
|---|
| 102 | {
|
|---|
| 103 | char *psz = &argv[i][0];
|
|---|
| 104 | if (*psz++ != '-')
|
|---|
| 105 | break;
|
|---|
| 106 |
|
|---|
| 107 | if (*psz == '-')
|
|---|
| 108 | {
|
|---|
| 109 | /* '--' ? */
|
|---|
| 110 | if (!psz[1])
|
|---|
| 111 | {
|
|---|
| 112 | i++;
|
|---|
| 113 | break;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /* convert to short. */
|
|---|
| 117 | if (!strcmp(psz, "-help"))
|
|---|
| 118 | psz = "h";
|
|---|
| 119 | else if (!strcmp(psz, "-version"))
|
|---|
| 120 | psz = "V";
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | switch (*psz)
|
|---|
| 124 | {
|
|---|
| 125 | case 'h':
|
|---|
| 126 | usage(stdout, name(argv[0]));
|
|---|
| 127 | return 0;
|
|---|
| 128 |
|
|---|
| 129 | case 'V':
|
|---|
| 130 | printf("kmk_time - kBuild version %d.%d.%d (r%u)\n"
|
|---|
| 131 | "Copyright (C) 2007-2008 Knut St. Osmundsen\n",
|
|---|
| 132 | KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH,
|
|---|
| 133 | KBUILD_SVN_REV);
|
|---|
| 134 | return 0;
|
|---|
| 135 |
|
|---|
| 136 | default:
|
|---|
| 137 | fprintf(stderr, "%s: error: syntax error '%s'\n", name(argv[0]), argv[i]);
|
|---|
| 138 | return 1;
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /*
|
|---|
| 143 | * Make sure there's something to execute.
|
|---|
| 144 | */
|
|---|
| 145 | if (i >= argc)
|
|---|
| 146 | {
|
|---|
| 147 | fprintf(stderr, "%s: syntax error: nothing to execute!\n", name(argv[0]));
|
|---|
| 148 | return usage(stderr, name(argv[0]));
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /*
|
|---|
| 152 | * Execute the program (it's actually supposed to be a command I think, but wtf).
|
|---|
| 153 | */
|
|---|
| 154 | #if defined(_MSC_VER)
|
|---|
| 155 | /** @todo
|
|---|
| 156 | * We'll have to find the '--' in the commandline and pass that
|
|---|
| 157 | * on to CreateProcess or spawn. Otherwise, the argument qouting
|
|---|
| 158 | * is gonna be messed up.
|
|---|
| 159 | */
|
|---|
| 160 | GetSystemTimeAsFileTime(&ftStart);
|
|---|
| 161 | rc = _spawnvp(_P_WAIT, argv[i], &argv[i]);
|
|---|
| 162 | if (rc != -1)
|
|---|
| 163 | {
|
|---|
| 164 | unsigned _int64 iStart, iElapsed;
|
|---|
| 165 | GetSystemTimeAsFileTime(&ft);
|
|---|
| 166 |
|
|---|
| 167 | iStart = ftStart.dwLowDateTime | ((unsigned _int64)ftStart.dwHighDateTime << 32);
|
|---|
| 168 | iElapsed = ft.dwLowDateTime | ((unsigned _int64)ft.dwHighDateTime << 32);
|
|---|
| 169 | iElapsed -= iStart;
|
|---|
| 170 | iElapsed /= 10; /* to usecs */
|
|---|
| 171 |
|
|---|
| 172 | printf("%s: %um%u.%06us\n", name(argv[0]),
|
|---|
| 173 | (unsigned)(iElapsed / (60 * 1000000)),
|
|---|
| 174 | (unsigned)(iElapsed % (60 * 1000000)) / 1000000,
|
|---|
| 175 | (unsigned)(iElapsed % 1000000));
|
|---|
| 176 | }
|
|---|
| 177 | else
|
|---|
| 178 | {
|
|---|
| 179 | fprintf(stderr, "%s: error: _spawnvp(_P_WAIT, \"%s\", ...) failed: %s\n", name(argv[0]), argv[i], strerror(errno));
|
|---|
| 180 | rc = 1;
|
|---|
| 181 | }
|
|---|
| 182 | return rc;
|
|---|
| 183 |
|
|---|
| 184 | #else
|
|---|
| 185 | rc = 1;
|
|---|
| 186 | gettimeofday(&tvStart, NULL);
|
|---|
| 187 | pid = fork();
|
|---|
| 188 | if (pid > 0)
|
|---|
| 189 | {
|
|---|
| 190 | waitpid(pid, &rc, 0);
|
|---|
| 191 | gettimeofday(&tv, NULL);
|
|---|
| 192 |
|
|---|
| 193 | /* calc elapsed time */
|
|---|
| 194 | tv.tv_sec -= tvStart.tv_sec;
|
|---|
| 195 | if (tv.tv_usec > tvStart.tv_usec)
|
|---|
| 196 | tv.tv_usec -= tvStart.tv_usec;
|
|---|
| 197 | else
|
|---|
| 198 | {
|
|---|
| 199 | tv.tv_sec--;
|
|---|
| 200 | tv.tv_usec = tv.tv_usec + 1000000 - tvStart.tv_usec;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | printf("%s: %um%u.%06us\n", name(argv[0]),
|
|---|
| 204 | (unsigned)(tv.tv_sec / 60),
|
|---|
| 205 | (unsigned)(tv.tv_sec % 60),
|
|---|
| 206 | (unsigned)tv.tv_usec);
|
|---|
| 207 | }
|
|---|
| 208 | else if (!pid)
|
|---|
| 209 | {
|
|---|
| 210 | execvp(argv[i], &argv[i]);
|
|---|
| 211 | fprintf(stderr, "%s: error: _execvp(\"%s\", ...) failed: %s\n", name(argv[0]), argv[i], strerror(errno));
|
|---|
| 212 | }
|
|---|
| 213 | else
|
|---|
| 214 | fprintf(stderr, "%s: error: fork() failed: %s\n", name(argv[0]), strerror(errno));
|
|---|
| 215 |
|
|---|
| 216 | return rc;
|
|---|
| 217 | #endif
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|