OpenVMS Source Code Demos

neil_cmd_line.c

KAWC99::Neil>
KAWC99::Neil> type NEIL_CMD_LINE.C
        //================================================================================
        // title  : NEIL_CMD_LINE.C
        // author : Neil Rieck
        // created: 99.12.15
        // purpose: display command line args and environmentals
        // notes  : On OpenVMS, this program is run from DCL as a foreign command like so:
        //              $ yada :== $path:name.exe
        //              $ yada param1 param2 param3 ...
        //================================================================================
        #include <stdio.h>
        #include <string.h>
        #include <stdlib.h>
        //
        //      global variables
        //
        int     my_count;
        char    *tmpPtr;
        char    host_addr[128];
        int     junk;
        //
        //      note: "*argv[]" is the same as "**argv"
        //
        int main ( int argc, char *argv[], char *env[] )
        {
                printf("-i-program: %s\n",argv[0]);
                //
                //      display cmd line args
                //
                printf("-i-argc: %ld\n", argc);
                argc--;                         /* adjust for zero based array */
                my_count = 0;
                while (my_count <= argc)
                {
                        printf("-i-arg: %ld %s\n", my_count, argv[my_count]);
                        my_count++;
                }
                //
                //      display cmd line environmentals
                //
                my_count = 0;
                while (env[my_count] != NULL)
                {
                        printf("-i-env: %ld %s\n", my_count, env[my_count]);
                        my_count++;
                }
                //
                //      display a specified environmental (shell symbol or logical name)
                //
                tmpPtr = getenv("TCPIP$INET_HOSTADDR");                                         //
                if (tmpPtr==NULL) {                                                             //
                    fprintf(stderr,"-e-fatal error, couldn't determine TCPIP$INET_HOSTADDR\n"); //
                } else {                                                                        //
                    fprintf(stderr,"-i-Host Addr: %s\n",tmpPtr);                                //
                    junk = strlen(tmpPtr);                                                      //
                    if (junk<sizeof(host_addr)){
                        sprintf(host_addr,"%s",tmpPtr);
                    }else{
                        fprintf(stderr,"-e-no room to store TCPIP$INET_HOSTADDR\n");            //
                    }
                }                                                                               //

                return 1;       /* return -s- to vms */
        }

KAWC99::Neil> cc NEIL_CMD_LINE.C
KAWC99::Neil> link NEIL_CMD_LINE
KAWC99::Neil> show default
  CSMIS$ROOT3:[DVLP._C]
KAWC99::Neil> yada :== $CSMIS$ROOT3:[DVLP._C]NEIL_CMD_LINE
KAWC99::Neil> yada 1 2 3
-i-program: kawc99$dra1:[csmis.][dvlp._c]neil_cmd_line.exe;9
-i-argc: 4
-i-arg: 0 kawc99$dra1:[csmis.][dvlp._c]neil_cmd_line.exe;9
-i-arg: 1 1
-i-arg: 2 2
-i-arg: 3 3
-i-env: 0 PATH=csmis$root3:[dvlp._c]
-i-env: 1 HOME=csmis$user3:[admcsm.neil]
-i-env: 2 TERM=vt300-132
-i-env: 3 USER=NEIL
-i-Host Addr: 207.35.137.66
KAWC99::Neil>