Hi Benji,

> I just made it read from /proc/<pid>/cmdline to get the command name. 
> The patch is below. Comments are welcome. Thanks!

Thanks for the patch. I have a couple of improvement suggestions, though:

> +  char buf[50];
> +  char *ret;
> +  int fd;
> +  int pathlen;

Can you try to minimize the scope of local variables? Listing all the
local variables upfront is BSD style and leads to code that is hard to
understand.

> +    {
> +      size_t n = read (fd, buf, 49);
> +      if (n > 0)
> +        {
> +          buf[49] = '\0'; /* Guarantee null-termination */
> +          pathlen = strlen (buf);

If n < 49, this call to strlen may read uninitialized memory, no?
Better put a NUL in buf[n], not buf[49], then.

> +          ret = malloc (pathlen + 1);
> +          if (ret)
> +            {
> +              int i;
> +              int pathsep = 0;
> +              for (i = 0; i < pathlen; i++)
> +                {
> +                  if (buf[i] == '/')
> +                    {
> +                      pathsep = i;
> +                    }
> +                }
> +              strcpy (ret, buf + pathsep + 1);

Can't this code be simplified by calling strrchr (buf, '/') ?

> +              return ret;

Is the size pathlen + 1 really needed for ret? It looks like you need
only strlen (buf + pathsep + 1) + 1 bytes.

> +          }
> +        }
> +      close (fd);
> +    }
> +  return "?";
>   # else

Bruno


Reply via email to