int run_shell_command( char *command, char *input, char *output, int *out_len) /* Or realloc style ? */ { #if HAVE_SYSTEM int result; /* and the return value of the command */ if (!command) return -1; DEBUGMSGTL(("run_shell_command", "running %s\n", command)); DEBUGMSGTL(("run:shell", "running '%s'\n", command)); result = -1; /* * Set up the command and run it. */ if (input) { FILE *file; if (output) { const char *ifname; const char *ofname; /* Filename for output redirection */ char shellline[STRMAX]; /* The full command to run */ ifname = netsnmp_mktemp(); if(NULL == ifname) return -1; file = fopen(ifname, "w"); if(NULL == file) { snmp_log(LOG_ERR,"couldn't open temporary file %s\n", ifname); unlink(ifname); return -1; } fprintf(file, "%s", input); fclose( file ); ofname = netsnmp_mktemp(); if(NULL == ofname) { if(ifname) unlink(ifname); return -1; } snprintf( shellline, sizeof(shellline), "(%s) < \"%s\" > \"%s\"", command, ifname, ofname ); result = system(shellline); /* * If output was requested, then retrieve & return it. * Tidy up, and return the result of the command. */ if (out_len && *out_len != 0) { int fd; /* For processing any output */ int len = 0; fd = open(ofname, O_RDONLY); if(fd >= 0) len = read( fd, output, *out_len-1 ); *out_len = len; if (len >= 0) output[len] = 0; else output[0] = 0; if (fd >= 0) close(fd); } unlink(ofname); unlink(ifname); } else { file = popen(command, "w"); if (file) { fwrite(input, 1, strlen(input), file); result = pclose(file); } } } else { if (output) { FILE* file; file = popen(command, "r"); if (file) { *out_len = fread(output, 1, *out_len - 1, file); if (*out_len >= 0) output[*out_len] = 0; else output[0] = 0; result = pclose(file); } } else result = system(command); } return result; #else return -1; #endif }
/** deprecated, use netsnmp_mktemp instead */ const char *make_tempfile (void) { return netsnmp_mktemp (); }