|
On 13/07/2006, at 2:47 PM, Mike Pantzopoulos - ((H/O)) wrote:
I was lulled into a false sense of security because the other APIS(access, close, write etc.) didn't require that binding directory when Iwas progressively building up the code.
The Unix APIs are found via the QUSAPIBD binding directory which all ILE compilers link to automatically.
So how do I know which APIs require which binding directory, or if they require a binding directory at all?
Experience. All bindable APIs require binding by either specifying the service program containing the API or the binding directory naming the service program. The API documentation generally tells you which service program exports the function if it is bindable. The Unix APIs are a little different and don't provide that information. However, you can find it if you examine the *BNDDIR objects and service program exports.
And why isn't the __errno function mentioned in the manual?
Because __errno is the internal function called to populate the C errno variable. errno is documented in the C run time. When you prototype a C function you are expected to find the C definition in the appropriate H file and map it to the RPGLE equivalent. In this case you would see from the C manual that errno is defined in the errno.h include. Then you'd look in the ERRNO member in QSYSINC/H to find the definition. There you'd see:
#define errno (*__errno())
which tells the C pre-compiler to replace all occurrences of errno in
the source with (*__errno()) which is simply C-speak for the value
returned by the __errno() function. I'll hold my tongue regarding the
obviousness of this syntax. So now that you know it calls a function
you need to find the function definition. Another look though errno.h
will find:
extern int *__errno(void);
which simply means an external function that returns a pointer to an
integer and accepts no parameters is being defined. Converting that to
RPG IV gives:
D errno PI *
extproc('__errno')
C allows you to define the pointer type of the return value on the
definition (the int * bit) but RPG IV doesn't so you need to define a
based integer to access the result.
D errno S 10I 0 based(@errno)
It should be obvious that you can't have a function and a variable both
called errno--at least not until RPG IV gives us separate name
spaces--so we need to change one of them. Because C code simply does:
if (errno == EWOULDBLOCK )
sort of thing it would be nice to maintain similar coding style in RPG
IV so we change the name of the function to something like getErrno
giving:
D errno S 10I 0 based(@errno)
D getErrno PI *
extproc('__errno')
In C you just test errno because the compiler sorts out calling the
function automatically. In RPG IV you have to call getErrno() first to
set the address of the errno variable before you can test it. There is
a rumour to the effect that you need to do this each time you check
errno (which is also what happens in C) but I've never had a problem
with just calling getErrno() in my RPG initialisation routine and then
just reference errno as I need to throughout the program.
Note that the IBM examples in the C programmer's guide are flawed. They do stuff like:
errno = 0;
result = function();
if (errno != 0 )
{
perror("Error occured.");
exit(1);
}
Even though they set errno to 0 before the function call it is better
practice to test the result of the function and only reference errno
once you know the function failed. That is never set it and only test
it, thus:
result = function();
if (result == NULL )
{
switch ( errno )
{
case EWOULDBLOCK:
/* Do something about it */
break;
default:
perror("function()");
exit(1);
}
}
Of course, in both case you need to know that the function sets
errno--not all of them do. You also need to know the correct value for
failure from the function (usually NULL if a pointer or -1 if an
integer). The function documentation should tell you both things.
Regards, Simon Coulter. -------------------------------------------------------------------- FlyByNight Software AS/400 Technical Specialists http://www.flybynight.com.au/ Phone: +61 3 9419 0175 Mobile: +61 0411 091 400 /"\ Fax: +61 3 9419 0175 \ / X ASCII Ribbon campaign against HTML E-Mail / \ --------------------------------------------------------------------
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.