chiark / gitweb /
process-util: rename char *r to ans and add comment
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 15 Jan 2017 17:41:34 +0000 (12:41 -0500)
committerSven Eden <yamakuzure@gmx.net>
Mon, 17 Jul 2017 15:58:36 +0000 (17:58 +0200)
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.

src/basic/process-util.c

index 74fac0835710e45465cd4e66065708af1a74d130..a5f355c10d567e87de29d9208e4c6e30f94e3481 100644 (file)
@@ -102,7 +102,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) {
         _cleanup_fclose_ FILE *f = NULL;
         bool space = false;
-        char *r = NULL, *k;
+        char *k, *ans = NULL;
         const char *p;
         int c;
 
@@ -116,7 +116,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.
          *
          * 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");
 
@@ -130,11 +130,11 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
         if (max_length == 1) {
 
                 /* If there's only room for one byte, return the empty string */
-                r = new0(char, 1);
-                if (!r)
+                ans = new0(char, 1);
+                if (!ans)
                         return -ENOMEM;
 
-                *line = r;
+                *line = ans;
                 return 0;
 
         } else if (max_length == 0) {
@@ -142,36 +142,36 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
 
                 while ((c = getc(f)) != EOF) {
 
-                        if (!GREEDY_REALLOC(r, allocated, len+3)) {
-                                free(r);
+                        if (!GREEDY_REALLOC(ans, allocated, len+3)) {
+                                free(ans);
                                 return -ENOMEM;
                         }
 
                         if (isprint(c)) {
                                 if (space) {
-                                        r[len++] = ' ';
+                                        ans[len++] = ' ';
                                         space = false;
                                 }
 
-                                r[len++] = c;
+                                ans[len++] = c;
                         } else if (len > 0)
                                 space = true;
                }
 
                 if (len > 0)
-                        r[len] = 0;
+                        ans[len] = '\0';
                 else
-                        r = mfree(r);
+                        ans = mfree(ans);
 
         } else {
                 bool dotdotdot = false;
                 size_t left;
 
-                r = new(char, max_length);
-                if (!r)
+                ans = new(char, max_length);
+                if (!ans)
                         return -ENOMEM;
 
-                k = r;
+                k = ans;
                 left = max_length;
                 while ((c = getc(f)) != EOF) {
 
@@ -195,20 +195,20 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
 
                                 *(k++) = (char) c;
                                 left--;
-                        } else if (k > r)
+                        } else if (k > ans)
                                 space = true;
                 }
 
                 if (dotdotdot) {
                         if (max_length <= 4) {
-                                k = r;
+                                k = ans;
                                 left = max_length;
                         } else {
-                                k = r + max_length - 4;
+                                k = ans + max_length - 4;
                                 left = 4;
 
                                 /* Eat up final spaces */
-                                while (k > r && isspace(k[-1])) {
+                                while (k > ans && isspace(k[-1])) {
                                         k--;
                                         left++;
                                 }
@@ -221,11 +221,11 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
         }
 
         /* Kernel threads have no argv[] */
-        if (isempty(r)) {
+        if (isempty(ans)) {
                 _cleanup_free_ char *t = NULL;
                 int h;
 
-                free(r);
+                free(ans);
 
                 if (!comm_fallback)
                         return -ENOENT;
@@ -235,22 +235,22 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
                         return h;
 
                 if (max_length == 0)
-                        r = strjoin("[", t, "]");
+                        ans = strjoin("[", t, "]");
                 else {
                         size_t l;
 
                         l = strlen(t);
 
                         if (l + 3 <= max_length)
-                                r = strjoin("[", t, "]");
+                                ans = strjoin("[", t, "]");
                         else if (max_length <= 6) {
 
-                                r = new(char, max_length);
-                                if (!r)
+                                ans = new(char, max_length);
+                                if (!ans)
                                         return -ENOMEM;
 
-                                memcpy(r, "[...]", max_length-1);
-                                r[max_length-1] = 0;
+                                memcpy(ans, "[...]", max_length-1);
+                                ans[max_length-1] = 0;
                         } else {
                                 char *e;
 
@@ -262,14 +262,14 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
                                         e--;
                                 *e = 0;
 
-                                r = strjoin("[", t, "...]");
+                                ans = strjoin("[", t, "...]");
                         }
                 }
-                if (!r)
+                if (!ans)
                         return -ENOMEM;
         }
 
-        *line = r;
+        *line = ans;
         return 0;
 }