1 // -*- mode: c++; c-set-style: "stroustrup"; tab-width: 4; -*-
2 //
3 // common.c
4 //
5 // Copyright (C) 2004 Koji Nakamaru
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software Foundation,
19 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 //
21
22 #include "common.h"
23
isLittleEndian()24 bool isLittleEndian()
25 {
26 uint16_t v = 0x00ff;
27 return (*(uint8_t *)&v) != 0;
28 }
29
30 #if defined (_WIN32) && !defined (__CYGWIN__)
31
32 extern "C" {
33 void __stdcall Sleep(unsigned long usec);
34 unsigned long __stdcall timeGetTime();
35 }
36
37 // int usleep(unsigned long usec)
38 // {
39 // Sleep(usec / 1000);
40 // return 0;
41 // }
42
gettimeofday(struct timeval * tv,struct timezone * tz)43 int gettimeofday(struct timeval *tv, struct timezone *tz)
44 {
45 static struct timeval tv0;
46 static unsigned long tm0;
47 if (tv == NULL) {
48 return 0;
49 }
50 if (tm0 != 0) {
51 tm0 = timeGetTime();
52 }
53 long dt = timeGetTime() - tm0;
54 *tv = tv0;
55 tv->tv_sec += dt / 1000;
56 tv->tv_usec += 1000 * (dt % 1000);
57 return 0;
58 }
59
run(const char * command)60 void run(
61 const char *command)
62 {
63 STARTUPINFO si;
64 PROCESS_INFORMATION pi;
65 memset(&si, 0, sizeof(si));
66 memset(&pi, 0, sizeof(pi));
67 si.dwFlags = STARTF_USESTDHANDLES;
68 si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
69 si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
70 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
71 CreateProcess(
72 NULL,
73 (CHAR *)command,
74 NULL,
75 NULL,
76 TRUE,
77 0,
78 NULL,
79 NULL,
80 &si,
81 &pi);
82 CloseHandle(pi.hProcess);
83 CloseHandle(pi.hThread);
84 }
85
86 #else
87
run(const char * command)88 void run(
89 const char *command)
90 {
91 int pid;
92 if ((pid = fork()) == -1) {
93 error("failed to fork, quitting...");
94 } else if (pid == 0) {
95 char *argv[] = {
96 "/bin/sh",
97 "-c",
98 (char *)command,
99 NULL,
100 };
101 execv("/bin/sh", argv);
102 }
103 }
104
105 #endif
106