sl@0
|
1 |
/************************************************************************
|
sl@0
|
2 |
*
|
sl@0
|
3 |
* system.cpp - definitions of testsuite helpers
|
sl@0
|
4 |
*
|
sl@0
|
5 |
* $Id: system.cpp 290012 2005-09-18 23:38:12Z sebor $
|
sl@0
|
6 |
*
|
sl@0
|
7 |
************************************************************************
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Copyright (c) 1994-2005 Quovadx, Inc., acting through its Rogue Wave
|
sl@0
|
10 |
* Software division. Licensed under the Apache License, Version 2.0 (the
|
sl@0
|
11 |
* "License"); you may not use this file except in compliance with the
|
sl@0
|
12 |
* License. You may obtain a copy of the License at
|
sl@0
|
13 |
* http://www.apache.org/licenses/LICENSE-2.0. Unless required by
|
sl@0
|
14 |
* applicable law or agreed to in writing, software distributed under
|
sl@0
|
15 |
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
sl@0
|
16 |
* CONDITIONS OF ANY KIND, either express or implied. See the License
|
sl@0
|
17 |
* for the specific language governing permissions and limitations under
|
sl@0
|
18 |
* the License.
|
sl@0
|
19 |
*
|
sl@0
|
20 |
**************************************************************************/
|
sl@0
|
21 |
|
sl@0
|
22 |
// expand _TEST_EXPORT macros
|
sl@0
|
23 |
#define _RWSTD_TEST_SRC
|
sl@0
|
24 |
|
sl@0
|
25 |
#include <system.h>
|
sl@0
|
26 |
|
sl@0
|
27 |
#include <driver.h>
|
sl@0
|
28 |
#include <printf.h>
|
sl@0
|
29 |
|
sl@0
|
30 |
#include <stdarg.h> // for va_copy, va_list, ...
|
sl@0
|
31 |
#include <stdlib.h> // for system
|
sl@0
|
32 |
|
sl@0
|
33 |
#if !defined (_WIN32) && !defined (_WIN64)
|
sl@0
|
34 |
# include <unistd.h>
|
sl@0
|
35 |
# include <sys/wait.h> // for WIFEXITED(), WIFSIGNALED(), WTERMSIG()
|
sl@0
|
36 |
#endif
|
sl@0
|
37 |
|
sl@0
|
38 |
#ifdef __SYMBIAN32__
|
sl@0
|
39 |
#include <assert.h>
|
sl@0
|
40 |
#endif
|
sl@0
|
41 |
|
sl@0
|
42 |
/**************************************************************************/
|
sl@0
|
43 |
|
sl@0
|
44 |
_TEST_EXPORT int
|
sl@0
|
45 |
rw_vasnprintf (char**, size_t*, const char*, va_list);
|
sl@0
|
46 |
|
sl@0
|
47 |
/**************************************************************************/
|
sl@0
|
48 |
|
sl@0
|
49 |
static int
|
sl@0
|
50 |
_rw_vsystem (const char *cmd, va_list va)
|
sl@0
|
51 |
{
|
sl@0
|
52 |
_RWSTD_ASSERT (0 != cmd);
|
sl@0
|
53 |
|
sl@0
|
54 |
char buffer [256];
|
sl@0
|
55 |
char *buf = buffer;
|
sl@0
|
56 |
|
sl@0
|
57 |
size_t bufsize = sizeof buffer;
|
sl@0
|
58 |
|
sl@0
|
59 |
rw_vasnprintf (&buf, &bufsize, cmd, va);
|
sl@0
|
60 |
|
sl@0
|
61 |
rw_note (0, __FILE__, __LINE__,
|
sl@0
|
62 |
"executing \"%s\"", buf);
|
sl@0
|
63 |
|
sl@0
|
64 |
const int ret = system (buf);
|
sl@0
|
65 |
|
sl@0
|
66 |
if (ret) {
|
sl@0
|
67 |
|
sl@0
|
68 |
#if !defined (_WIN32)
|
sl@0
|
69 |
|
sl@0
|
70 |
if (-1 == ret) {
|
sl@0
|
71 |
// system() failed, e.g., because fork() failed
|
sl@0
|
72 |
rw_error (0, __FILE__, __LINE__,
|
sl@0
|
73 |
"system (\"%s\") failed: errno = %{#m} (%{m})", buf);
|
sl@0
|
74 |
}
|
sl@0
|
75 |
else if (WIFSIGNALED (ret)) {
|
sl@0
|
76 |
// command exited with a signal
|
sl@0
|
77 |
const int signo = WTERMSIG (ret);
|
sl@0
|
78 |
|
sl@0
|
79 |
rw_error (0, __FILE__, __LINE__,
|
sl@0
|
80 |
"the command \"%s\" exited with signal %d (%{K})",
|
sl@0
|
81 |
buf, signo, signo);
|
sl@0
|
82 |
}
|
sl@0
|
83 |
else {
|
sl@0
|
84 |
// command exited with a non-zero status
|
sl@0
|
85 |
const int status = WEXITSTATUS (ret);
|
sl@0
|
86 |
|
sl@0
|
87 |
rw_error (0, __FILE__, __LINE__,
|
sl@0
|
88 |
"the command \"%s\" exited with status %d",
|
sl@0
|
89 |
buf, status);
|
sl@0
|
90 |
}
|
sl@0
|
91 |
#else // if defined (_WIN32)
|
sl@0
|
92 |
|
sl@0
|
93 |
// FIXME: make this more descriptive
|
sl@0
|
94 |
rw_error (0, __FILE__, __LINE__,
|
sl@0
|
95 |
"the command \"%s\" failed with code %d",
|
sl@0
|
96 |
buf, ret);
|
sl@0
|
97 |
|
sl@0
|
98 |
#endif // _WIN32
|
sl@0
|
99 |
|
sl@0
|
100 |
}
|
sl@0
|
101 |
|
sl@0
|
102 |
if (buf != buffer)
|
sl@0
|
103 |
free (buf);
|
sl@0
|
104 |
|
sl@0
|
105 |
return ret;
|
sl@0
|
106 |
}
|
sl@0
|
107 |
|
sl@0
|
108 |
/**************************************************************************/
|
sl@0
|
109 |
|
sl@0
|
110 |
_TEST_EXPORT int
|
sl@0
|
111 |
rw_system (const char *cmd, ...)
|
sl@0
|
112 |
{
|
sl@0
|
113 |
va_list va;
|
sl@0
|
114 |
va_start (va, cmd);
|
sl@0
|
115 |
|
sl@0
|
116 |
const int ret = _rw_vsystem (cmd, va);
|
sl@0
|
117 |
|
sl@0
|
118 |
va_end (va);
|
sl@0
|
119 |
return ret;
|
sl@0
|
120 |
}
|