X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=common-src%2Futil.c;h=b9172ca41950fa716235833f7e2b0c2853fbd957;hb=c6f0a88c567f8536c498f554285aed1f8150da18;hp=c6368a2b3546b22227039ddfdbe38103579c7936;hpb=cd0b924f27312d57bd42f6c4fae2b795139e2d0b;p=debian%2Famanda diff --git a/common-src/util.c b/common-src/util.c index c6368a2..b9172ca 100644 --- a/common-src/util.c +++ b/common-src/util.c @@ -1605,3 +1605,73 @@ debug_executing( amfree(cmdline); } +char * +get_first_line( + GPtrArray *argv_ptr) +{ + char *output_string = NULL; + int inpipe[2], outpipe[2], errpipe[2]; + int pid; + FILE *out, *err; + + assert(argv_ptr != NULL); + assert(argv_ptr->pdata != NULL); + assert(argv_ptr->len >= 1); + + if (pipe(inpipe) == -1) { + error(_("error [open pipe: %s]"), strerror(errno)); + /*NOTREACHED*/ + } + if (pipe(outpipe) == -1) { + error(_("error [open pipe: %s]"), strerror(errno)); + /*NOTREACHED*/ + } + if (pipe(errpipe) == -1) { + error(_("error [open pipe: %s]"), strerror(errno)); + /*NOTREACHED*/ + } + + fflush(stdout); + switch(pid = fork()) { + case -1: + error(_("error [fork: %s]"), strerror(errno)); + /*NOTREACHED*/ + + default: /* parent process */ + aclose(inpipe[0]); + aclose(outpipe[1]); + aclose(errpipe[1]); + break; + + case 0: /* child process */ + aclose(inpipe[1]); + aclose(outpipe[0]); + aclose(errpipe[0]); + + dup2(inpipe[0], 0); + dup2(outpipe[1], 1); + dup2(errpipe[1], 2); + + debug_executing(argv_ptr); + g_fprintf(stdout, "unknown\n"); + execv((char *)*argv_ptr->pdata, (char **)argv_ptr->pdata); + error(_("error [exec %s: %s]"), (char *)*argv_ptr->pdata, strerror(errno)); + } + + aclose(inpipe[1]); + + out = fdopen(outpipe[0],"r"); + err = fdopen(errpipe[0],"r"); + + output_string = agets(out); + if (!output_string) + output_string = agets(err); + + fclose(out); + fclose(err); + + waitpid(pid, NULL, 0); + + return output_string; +} +