Submitted By: Robert Connolly <robert at linuxfromscratch dot org> (ashes)
Date: 2005-04-29
Initial Package Version: 21.6
Upstream Status: Submitted
ORIGIN: http://www.gentoo.org/cgi-bin/viewcvs.cgi/sys-process/psmisc/files/\
	psmisc-21.6-scanf.patch
Description: This fixes pstree.

Fix sscanf() invocation to be C98/C99 compliant.  Most people don't
notice because glibc isn't C98/C99 compliant in this respect, but
some of us use libc's which are :).

to be more specific, mjn3 of uClibc writes:
Both C99 and C89 state that the %c conversion for scanf reads the
exact number of bytes specified by the optional field width (or 1
if not specified).  There is an argument that perhaps the specified
width should be treated as an upper bound, based on some historical
use.  Glibc does not obey this specification nor mention it in any
compliance documentation.

Also, the first hunk is to fix segfaults that may arise from
running pstree with an user argument but find_proc(1) is NULL.
This normally doesn't occur, but it is possible:
vapier@uclibc-nemo 0 src $ pstree vapier
Segmentation fault

Mike Frysinger <vapier@gentoo.org>

diff -Naur psmisc-21.6.orig/src/pstree.c psmisc-21.6/src/pstree.c
--- psmisc-21.6.orig/src/pstree.c	2004-12-15 11:15:52.000000000 +0000
+++ psmisc-21.6/src/pstree.c	2005-04-30 00:41:05.000000000 +0000
@@ -544,6 +544,9 @@
 {
   const CHILD *walk;
 
+  if (!current)
+    return;
+
   if (current->uid == uid)
     {
       if (dumped)
@@ -616,11 +619,10 @@
             {
               memset(comm, '\0', COMM_LEN+1);
               tmpptr = strrchr(readbuf, ')'); /* find last ) */
-              *tmpptr = '\0';
               /* We now have readbuf with pid and cmd, and tmpptr+2
                * with the rest */
               /*printf("readbuf: %s\n", readbuf);*/
-              if (sscanf(readbuf, "%*d (%15c", comm) == 1)
+              if (sscanf(readbuf, "%*d (%[^)]", comm) == 1)
               {
                 /*printf("tmpptr: %s\n", tmpptr+2);*/
                 if (sscanf(tmpptr+2, "%*c %d", &ppid) == 1)
