Problem:
It would be useful if there were a way for Micro Focus COBOL to retrieve the user-id from the UNIX operating system -- the same user-id that the UNIX "id" command would show.
Resolution:
Micro Focus COBOL can call C routines directly, if the C routines have been linked with the COBOL run-time system. A programmer would use the CALL statement, and put the name of the C routine in quotes.
The UNIX system library "libc.a" (or "libc.so") has already been linked with the run-time system, so COBOL can directly call any of the UNIX system calls contained in "libc". On most platforms, the intro(2) man page shows a complete list of these calls.
getuid(2) is a system call contained in "libc", so COBOL can call it directly. Consider the following program:
01 uid-ws pic 9(8) comp-5.
call "getuid" returning uid-ws.
display "user-id is ", uid-ws.
This is a complete COBOL program. Put these three lines by themselves in a file named (for example) "mytest.cbl", then compile and run. This shows a way to get the user-id into a data item in working-storage.
This example is set up expecting getuid(2) to return a C int, which is analogous to a COBOL comp-5. In most cases getuid(2) will return an int, but this may vary depending on the platform and bit-mode (32 versus 64 bit). Be sure to check the getuid(2) man page and the system header file "types.h" to determine the data type that getuid(2) will return.
This has been tested with Server Express v2.2 and v4.0, as well as Object COBOL Developer's Suite v4.1 and v4.2, on HP/UX, Sun Solaris, IBM AIX, Linux, SCO, and HP/Tru64.