process-util: rename char *r to ans and add comment

Add a comment about the return value and rename r to ans. r is
nowadays reserved for the integer return value, and char *r is confusing.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2017-01-15 12:41:34 -05:00
parent e50412ef19
commit c05347807f

View file

@ -104,7 +104,7 @@ int get_process_comm(pid_t pid, char **name) {
int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) { int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
_cleanup_fclose_ FILE *f = NULL; _cleanup_fclose_ FILE *f = NULL;
bool space = false; bool space = false;
char *r = NULL, *k; char *k, *ans = NULL;
const char *p; const char *p;
int c; int c;
@ -118,7 +118,7 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
* command line that resolves to the empty string will return the "comm" name of the process instead. * command line that resolves to the empty string will return the "comm" name of the process instead.
* *
* Returns -ESRCH if the process doesn't exist, and -ENOENT if the process has no command line (and * Returns -ESRCH if the process doesn't exist, and -ENOENT if the process has no command line (and
* comm_fallback is false). */ * comm_fallback is false). Returns 0 and sets *line otherwise. */
p = procfs_file_alloca(pid, "cmdline"); p = procfs_file_alloca(pid, "cmdline");
@ -132,11 +132,11 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
if (max_length == 1) { if (max_length == 1) {
/* If there's only room for one byte, return the empty string */ /* If there's only room for one byte, return the empty string */
r = new0(char, 1); ans = new0(char, 1);
if (!r) if (!ans)
return -ENOMEM; return -ENOMEM;
*line = r; *line = ans;
return 0; return 0;
} else if (max_length == 0) { } else if (max_length == 0) {
@ -144,36 +144,36 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
while ((c = getc(f)) != EOF) { while ((c = getc(f)) != EOF) {
if (!GREEDY_REALLOC(r, allocated, len+3)) { if (!GREEDY_REALLOC(ans, allocated, len+3)) {
free(r); free(ans);
return -ENOMEM; return -ENOMEM;
} }
if (isprint(c)) { if (isprint(c)) {
if (space) { if (space) {
r[len++] = ' '; ans[len++] = ' ';
space = false; space = false;
} }
r[len++] = c; ans[len++] = c;
} else if (len > 0) } else if (len > 0)
space = true; space = true;
} }
if (len > 0) if (len > 0)
r[len] = 0; ans[len] = '\0';
else else
r = mfree(r); ans = mfree(ans);
} else { } else {
bool dotdotdot = false; bool dotdotdot = false;
size_t left; size_t left;
r = new(char, max_length); ans = new(char, max_length);
if (!r) if (!ans)
return -ENOMEM; return -ENOMEM;
k = r; k = ans;
left = max_length; left = max_length;
while ((c = getc(f)) != EOF) { while ((c = getc(f)) != EOF) {
@ -197,20 +197,20 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
*(k++) = (char) c; *(k++) = (char) c;
left--; left--;
} else if (k > r) } else if (k > ans)
space = true; space = true;
} }
if (dotdotdot) { if (dotdotdot) {
if (max_length <= 4) { if (max_length <= 4) {
k = r; k = ans;
left = max_length; left = max_length;
} else { } else {
k = r + max_length - 4; k = ans + max_length - 4;
left = 4; left = 4;
/* Eat up final spaces */ /* Eat up final spaces */
while (k > r && isspace(k[-1])) { while (k > ans && isspace(k[-1])) {
k--; k--;
left++; left++;
} }
@ -223,11 +223,11 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
} }
/* Kernel threads have no argv[] */ /* Kernel threads have no argv[] */
if (isempty(r)) { if (isempty(ans)) {
_cleanup_free_ char *t = NULL; _cleanup_free_ char *t = NULL;
int h; int h;
free(r); free(ans);
if (!comm_fallback) if (!comm_fallback)
return -ENOENT; return -ENOENT;
@ -237,22 +237,22 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
return h; return h;
if (max_length == 0) if (max_length == 0)
r = strjoin("[", t, "]"); ans = strjoin("[", t, "]");
else { else {
size_t l; size_t l;
l = strlen(t); l = strlen(t);
if (l + 3 <= max_length) if (l + 3 <= max_length)
r = strjoin("[", t, "]"); ans = strjoin("[", t, "]");
else if (max_length <= 6) { else if (max_length <= 6) {
r = new(char, max_length); ans = new(char, max_length);
if (!r) if (!ans)
return -ENOMEM; return -ENOMEM;
memcpy(r, "[...]", max_length-1); memcpy(ans, "[...]", max_length-1);
r[max_length-1] = 0; ans[max_length-1] = 0;
} else { } else {
char *e; char *e;
@ -264,14 +264,14 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
e--; e--;
*e = 0; *e = 0;
r = strjoin("[", t, "...]"); ans = strjoin("[", t, "...]");
} }
} }
if (!r) if (!ans)
return -ENOMEM; return -ENOMEM;
} }
*line = r; *line = ans;
return 0; return 0;
} }