Revision: 76624
          http://sourceforge.net/p/brlcad/code/76624
Author:   brlcad
Date:     2020-08-01 04:18:09 +0000 (Sat, 01 Aug 2020)
Log Message:
-----------
not safe to assume 0/1/2 actually correspond with stdin/stdout/stderr

go through the std vars as they are more likley to remain semantically
aligned to a corresponding in/out/err descriptor even if
closed/reopened.  this API still assumes too much, but there's not a
good alternative that doesn't entail significant rework at this point.

Modified Paths:
--------------
    brlcad/trunk/src/libbu/process.c

Modified: brlcad/trunk/src/libbu/process.c
===================================================================
--- brlcad/trunk/src/libbu/process.c    2020-08-01 04:03:46 UTC (rev 76623)
+++ brlcad/trunk/src/libbu/process.c    2020-08-01 04:18:09 UTC (rev 76624)
@@ -79,19 +79,19 @@
 bu_process_close(struct bu_process *pinfo, int fd)
 {
     if (!pinfo) return;
-    if (fd == 0) {
+    if (fd == fileno(stdin)) {
        if (!pinfo->fp_in) return;
        (void)fclose(pinfo->fp_in);
        pinfo->fp_in = NULL;
        return;
     }
-    if (fd == 1) {
+    if (fd == fileno(stdout)) {
        if (!pinfo->fp_out) return;
        (void)fclose(pinfo->fp_out);
        pinfo->fp_out = NULL;
        return;
     }
-    if (fd == 2) {
+    if (fd == fileno(stderr)) {
        if (!pinfo->fp_err) return;
        (void)fclose(pinfo->fp_err);
        pinfo->fp_err = NULL;
@@ -107,7 +107,7 @@
 
     bu_process_close(pinfo, fd);
 
-    if (fd == 0) {
+    if (fd == fileno(stdin)) {
 #ifndef _WIN32
        pinfo->fp_in = fdopen(pinfo->fd_in, "wb");
 #else
@@ -115,7 +115,7 @@
 #endif
        return pinfo->fp_in;
     }
-    if (fd == 1) {
+    if (fd == fileno(stdout)) {
 #ifndef _WIN32
        pinfo->fp_out = fdopen(pinfo->fd_out, "rb");
 #else
@@ -123,7 +123,7 @@
 #endif
        return pinfo->fp_out;
     }
-    if (fd == 2) {
+    if (fd == fileno(stderr)) {
 #ifndef _WIN32
        pinfo->fp_err = fdopen(pinfo->fd_err, "rb");
 #else
@@ -139,14 +139,14 @@
 void *
 bu_process_fd(struct bu_process *pinfo, int fd)
 {
-    if (!pinfo || (fd < 0 || fd > 2)) return NULL;
-    if (fd == 0) {
+    if (!pinfo || fd < 0) return NULL;
+    if (fd == fileno(stdin)) {
        return (void *)(&(pinfo->fd_in));
     }
-    if (fd == 1) {
+    if (fd == fileno(stdout)) {
        return (void *)(&(pinfo->fd_out));
     }
-    if (fd == 2) {
+    if (fd == fileno(stderr)) {
        return (void *)(&(pinfo->fd_err));
     }
     return NULL;
@@ -179,8 +179,8 @@
 bu_process_read(char *buff, int *count, struct bu_process *pinfo, int fd, int 
n)
 {
     int ret = 1;
-    if (!pinfo || !buff || !n || !count || (fd < 1 || fd > 2)) return -1;
-    if (fd == 1) {
+    if (!pinfo || !buff || !n || !count || fd < 1) return -1;
+    if (fd == fileno(stdout)) {
 #ifndef _WIN32
        (*count) = read((int)pinfo->fd_out, buff, n);
        if ((*count) <= 0) {
@@ -195,7 +195,7 @@
        (*count) = (int)dcount;
 #endif
     }
-    if (fd == 2) {
+    if (fd == fileno(stderr)) {
 #ifndef _WIN32
        (*count) = read((int)pinfo->fd_err, buff, n);
        if ((*count) <= 0) {
@@ -297,17 +297,17 @@
        setpgid(0, 0);
 
        /* Redirect stdin and stderr */
-       (void)close(0);
+       (void)close(fileno(stdin));
        pret = dup(pipe_in[0]);
        if (pret < 0) {
            perror("dup");
        }
-       (void)close(1);
+       (void)close(fileno(stdout));
        pret = dup(pipe_out[1]);
        if (pret < 0) {
            perror("dup");
        }
-       (void)close(2);
+       (void)close(fileno(stderr));
        pret = dup(pipe_err[1]);
        if (pret < 0) {
            perror("dup");
@@ -505,8 +505,8 @@
     }
 
     /* Clean up */
-    bu_process_close(pinfo, 1);
-    bu_process_close(pinfo, 2);
+    bu_process_close(pinfo, fileno(stdout));
+    bu_process_close(pinfo, fileno(stderr));
 
     /* Free copy of exec args */
     if (pinfo->cmd) {

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to