views:

177

answers:

3

Calling system() to run an external .exe and checking error code upon errors:

#include <errno.h>       
#include <stdlib.h>

function()
{
    errno_t err;


      if( system(tailCmd) == -1) //if there is an error get errno
      {
          //Error calling tail.exe 
          _get_errno( &err );  

      }
}

First two compile errors:

error C2065: 'err' : undeclared identifier
error C2065: 'errno_t' : undeclared identifier

Not sure why as I am including the required and optional header files?
Any help is appreciated. Thank You.

+2  A: 

Just use 'errno' without any declaration. It is a macro that expands to an int value.

abc
awesome, thanks
Tommy
+2  A: 

A typical usage is like:

if (somecall() == -1) {
    int errsv = errno;
    printf("somecall() failed\n");
    if (errsv == ...) { ... }
}

which is taken from here.

shinkou
Works, thanks. I'm curious, do you have any idea why the compile using the windows functions didn't work?
Tommy
I'm not 100% sure, but my best guess is because of the Windows and .NET versions. btw, since the function name starts with an underscore, it's not supposed to be used externally (3rd party development). Take a look of the function's info here: http://msdn.microsoft.com/en-us/library/wwfcfxas%28VS.80%29.aspxA comment at the bottom points out the example shown there is wrong too.
shinkou
+2  A: 

In the world of Standard C, the type 'errno_t' is defined by TR24731-1 (see http://stackoverflow.com/questions/372980/ for more information) and you have to 'activate it' by defining '__STDC_WANT_LIB_EXT1__'.

However, you appear to be working on Windows (judging from 'tail.exe', and also the non-standard '_get_errno()'). The rules there may depend on the C compiler you are using. You should be able to chase down the information from this MSDN article on 'Security Enhancements in the CRT'. My impression was that it should be defined unless you actively suppress the feature, so check out whether you are actively suppressing it in your compilations.

Be aware that the MSVC definition of functions such as vsnprintf_s() do not match the TR24731-1 definitions:

MSDN:

int vsnprintf_s(
   char *buffer,
   size_t sizeOfBuffer,
   size_t count,
   const char *format,
   va_list argptr 
);

TR 24731-1:

int vsnprintf_s(
    char * restrict s,
    rsize_t n,
    const char * restrict format,
    va_list arg
);

The difference is not just a question of type aliases or qualifiers (rsize_t, restrict) - there are two sizes in the MS version and one in the Standard version. So much for standardization!

Jonathan Leffler
Great info, thank you.
Tommy