/* * NAME * libosint.c - C function to show example usage of * Oracle8 external procedure. * * OVERVIEW * This program contains the function "RunOSCmd" * called from the Oracle stored procedure * "osutil.RunOsCmd". For more Information on * Service Routines see PL/SQL Users Guide Oracle-8 * on Page 10-14 */ #include #include #include #include #include #ifndef OCI_ORACLE #include #endif /* * RunOsCmd - This function runs the OS command * pointed to by "cmd". The output of this command * is returned via the cmdo ptr. */ void RunOsCmd(ctx, cmd, cmd_ind, cmd_len, cmdo, cmdo_ind, cmdo_len) OCIExtProcContext *ctx; /* Oracle ConText Pointer */ char *cmd; /* Pointer to OS command */ short cmd_ind; /* Null Indicator for OS command */ int cmd_len; /* Length indicator variable */ char *cmdo; /* Pointer to OS command output */ short *cmdo_ind; /* Null Indicator for OS command */ int *cmdo_len; /* Length indicator variable */ { char cmdbuf[BUFSIZ]; /* buffer to hold command output */ FILE *fptr; /* pipe pointer for OS command */ int PLSQLBSZ = 32767; /* Max PL/SQL output buffer size */ *cmdo = '\0'; /* initialize output variables */ *cmdo_len = 0; if (cmd_ind == OCI_IND_NULL) { strcpy(cmdo,"Command entered cannot be NULL..."); *cmdo_ind = (short)OCI_IND_NOTNULL; *cmdo_len = 30; } else { if ((fptr = popen(cmd, "r")) != NULL) { /* Execute command */ while (fgets(cmdbuf, BUFSIZ, fptr) != NULL) { strcat(cmdo, cmdbuf); /* Build output string */ *cmdo_len = *cmdo_len + strlen(cmdbuf); } if (*cmdo == NULL) { *cmdo_ind = (short)OCI_IND_NULL; } else { *cmdo_ind = (short)OCI_IND_NOTNULL; if (*cmdo_len > PLSQLBSZ) { /* Trunc output to max buf sz */ *cmdo_len = PLSQLBSZ; cmdo[PLSQLBSZ - 1] = '\0'; } } pclose(fptr); /* Close the pipe */ } else { printf("\nUnable to open FPTR\n"); /* Some OS error occurred */ *cmdo_ind = (short)OCI_IND_NULL; *cmdo_len = 0; } } } /* End of RunOsCmd */