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