Changeset 2392


Ignore:
Timestamp:
Sep 11, 2009, 3:22:01 AM (10 years ago)
Author:
bruno
Message:
  • Solve issues around call_program_and_get_last_line_of_output
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.10/mondo/src/common/libmondo-fork.c

    r2383 r2392  
    3232 * @param call The program to run.
    3333 * @return The last line of its output.
    34  * @note The returned value points to static storage that will be overwritten with each call.
     34 * @note The returned value points to an allocated string that the caller needs to free
    3535 */
    3636char *call_program_and_get_last_line_of_output(char *call)
     
    3838    /*@ buffers ***************************************************** */
    3939    char *result = NULL;
     40    char *p = NULL;
    4041
    4142    /*@ pointers **************************************************** */
     
    4546
    4647    assert_string_is_neither_NULL_nor_zerolength(call);
     48
     49    log_msg(4, "Calling command: %s", call);
     50    /* By default return an empty string in any case */
     51    mr_asprintf(result, "");
     52
     53    /* popen seems to always return an empty line after the interesting last line result */
    4754    if ((fin = popen(call, "r"))) {
    48         for (mr_getline(result, fin); !feof(fin); mr_getline(result, fin));
     55        while (!feof(fin)) {
     56            mr_getline(p, fin);
     57            log_msg(9, "p: %s", p);
     58            if ((p != NULL) && (strlen(p) > 1)) {
     59                mr_free(result);
     60                mr_asprintf(result, "%s", p);
     61                log_msg(9, "Result: %s", result);
     62            }
     63            mr_free(p);
     64        }
     65        log_msg(4, "Result: %s", result);
     66        mr_strip_spaces(result);
    4967        paranoid_pclose(fin);
    5068    } else {
    5169        log_OS_error("Unable to popen call");
    5270    }
    53     mr_strip_spaces(result);
     71    log_msg(4, "Returns: %s", result);
    5472    return(result);
    5573}
Note: See TracChangeset for help on using the changeset viewer.