comparison src/process.c @ 98246:06b2f516c2fb

(procfs_system_process_attributes): Fix cmdline in case /proc/PID/cmdline is empty.
author Eli Zaretskii <eliz@gnu.org>
date Fri, 19 Sep 2008 17:19:28 +0000
parents cdd80926d2ea
children 215a88f14455
comparison
equal deleted inserted replaced
98245:dd5392c63164 98246:06b2f516c2fb
7417 /* args */ 7417 /* args */
7418 strcpy (procfn_end, "/cmdline"); 7418 strcpy (procfn_end, "/cmdline");
7419 fd = emacs_open (fn, O_RDONLY, 0); 7419 fd = emacs_open (fn, O_RDONLY, 0);
7420 if (fd >= 0) 7420 if (fd >= 0)
7421 { 7421 {
7422 for (cmdsize = 0; emacs_read (fd, &c, 1) == 1; cmdsize++) 7422 for (cmdline_size = 0; emacs_read (fd, &c, 1) == 1; cmdline_size++)
7423 { 7423 {
7424 if (isspace (c) || c == '\\') 7424 if (isspace (c) || c == '\\')
7425 cmdsize++; /* for later quoting, see below */ 7425 cmdline_size++; /* for later quoting, see below */
7426 } 7426 }
7427 if (cmdsize) 7427 if (cmdline_size)
7428 { 7428 {
7429 cmdline = xmalloc (cmdsize + 1); 7429 cmdline = xmalloc (cmdline_size + 1);
7430 lseek (fd, 0L, SEEK_SET); 7430 lseek (fd, 0L, SEEK_SET);
7431 cmdline[0] = '\0'; 7431 cmdline[0] = '\0';
7432 if ((nread = read (fd, cmdline, cmdsize)) >= 0) 7432 if ((nread = read (fd, cmdline, cmdline_size)) >= 0)
7433 cmdline[nread++] = '\0'; 7433 cmdline[nread++] = '\0';
7434 /* We don't want trailing null characters. */ 7434 /* We don't want trailing null characters. */
7435 for (p = cmdline + nread - 1; p > cmdline && !*p; p--) 7435 for (p = cmdline + nread - 1; p > cmdline && !*p; p--)
7436 nread--; 7436 nread--;
7437 for (p = cmdline; p < cmdline + nread; p++) 7437 for (p = cmdline; p < cmdline + nread; p++)
7444 *p++ = '\\'; 7444 *p++ = '\\';
7445 } 7445 }
7446 else if (*p == '\0') 7446 else if (*p == '\0')
7447 *p = ' '; 7447 *p = ' ';
7448 } 7448 }
7449 cmdsize = nread; 7449 cmdline_size = nread;
7450 } 7450 }
7451 else 7451 else
7452 { 7452 {
7453 cmdsize = strlen (cmd) + 2; 7453 cmdline_size = cmdsize + 2;
7454 cmdline = xmalloc (cmdsize + 1); 7454 cmdline = xmalloc (cmdline_size + 1);
7455 strcpy (cmdline, "["); 7455 strcpy (cmdline, "[");
7456 strcat (strcat (cmdline, cmd), "]"); 7456 strcat (strncat (cmdline, cmd, cmdsize), "]");
7457 } 7457 }
7458 emacs_close (fd); 7458 emacs_close (fd);
7459 /* Command line is encoded in locale-coding-system; decode it. */ 7459 /* Command line is encoded in locale-coding-system; decode it. */
7460 cmd_str = make_unibyte_string (cmdline, cmdsize); 7460 cmd_str = make_unibyte_string (cmdline, cmdline_size);
7461 decoded_cmd = code_convert_string_norecord (cmd_str, 7461 decoded_cmd = code_convert_string_norecord (cmd_str,
7462 Vlocale_coding_system, 0); 7462 Vlocale_coding_system, 0);
7463 xfree (cmdline); 7463 xfree (cmdline);
7464 attrs = Fcons (Fcons (Qargs, decoded_cmd), attrs); 7464 attrs = Fcons (Fcons (Qargs, decoded_cmd), attrs);
7465 } 7465 }