diff options
author | Burt P <pburt0@gmail.com> | 2017-08-16 11:28:49 -0500 |
---|---|---|
committer | Leandro A. F. Pereira <leandro@hardinfo.org> | 2017-08-25 07:03:23 -0700 |
commit | cb7fe9ba9bbb87cc9a0e876d873a2cd2a61a46e2 (patch) | |
tree | e0c462d13fa4ee122c8af5a76f0fa874d0591e6d /modules/computer | |
parent | 96be5741228fed60dcd584656ac03f14ec0155fa (diff) |
boots.c: capture stderr to prevent a leak
The only error would be inability to access /var/log/wtmp.
Signed-off-by: Burt P <pburt0@gmail.com>
Diffstat (limited to 'modules/computer')
-rw-r--r-- | modules/computer/boots.c | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/modules/computer/boots.c b/modules/computer/boots.c index d8a6d32a..ba458242 100644 --- a/modules/computer/boots.c +++ b/modules/computer/boots.c @@ -24,43 +24,43 @@ void scan_boots_real(void) { - FILE *last; - char buffer[256]; + gchar **tmp; + gboolean spawned; + gchar *out, *err, *p, *s, *next_nl; scan_os(FALSE); if (!computer->os->boots) - computer->os->boots = g_strdup_printf("[%s]\n", _("Boots")); + computer->os->boots = strdup(""); else return; - last = popen("last", "r"); - if (last) { - while (fgets(buffer, 256, last)) { - if (strstr(buffer, "system boot")) { - gchar **tmp, *buf = buffer; - - strend(buffer, '\n'); - - while (*buf) { - if (*buf == ' ' && *(buf + 1) == ' ') { - memmove(buf, buf + 1, strlen(buf) + 1); - - buf--; - } else { - buf++; + spawned = g_spawn_command_line_sync("last", + &out, &err, NULL, NULL); + if (spawned && out != NULL) { + p = out; + while(next_nl = strchr(p, '\n')) { + strend(p, '\n'); + if (strstr(p, "system boot")) { + s = p; + while (*s) { + if (*s == ' ' && *(s + 1) == ' ') { + memmove(s, s + 1, strlen(s) + 1); + s--; + } else { + s++; + } + } + tmp = g_strsplit(p, " ", 0); + computer->os->boots = + h_strdup_cprintf("\n%s %s %s %s=%s", + computer->os->boots, + tmp[4], tmp[5], tmp[6], tmp[7], tmp[3]); + g_strfreev(tmp); } - } - - tmp = g_strsplit(buffer, " ", 0); - computer->os->boots = - h_strdup_cprintf("\n%s %s %s %s=%s|%s", - computer->os->boots, - tmp[4], tmp[5], tmp[6], tmp[7], tmp[3], tmp[8]); - g_strfreev(tmp); + p = next_nl + 1; } - } - - pclose(last); + g_free(out); + g_free(err); } } |