Re: [hackers] [ubase][PATCH] Add pwdx(1)

From: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Sat, 26 Mar 2016 12:34:52 +0100

On Sat, Mar 26, 2016 at 12:30:50AM +0100, Mattias Andr??e wrote:
> Signed-off-by: Mattias Andr??e <maandree_AT_kth.se>
> diff --git a/pwdx.c b/pwdx.c
> new file mode 100644
> index 0000000..b9836b7
> --- /dev/null
> +++ b/pwdx.c
> _AT_@ -0,0 +1,53 @@
> +/* See LICENSE file for copyright and license details. */
> +#include <limits.h>
> +#include <stdio.h>
> +#include <string.h>
> +#include <errno.h>
> +#include <unistd.h>
> +

Sort includes.

> +#include "util.h"
> +
> +static void
> +usage(void)
> +{
> + eprintf("usage: %s pid...\n", argv0);
> +}
> +
> +int
> +main(int argc, char *argv[])
> +{
> + int ret = 0;
> + char path[sizeof("/proc//cwd") + 3 * sizeof(pid_t)];

Just use PATH_MAX here.

> + char target[PATH_MAX + sizeof(" (deleted)")];

Why + sizeof(" (deleted)") ?

> +
> + for (; argc > 0; argc--, argv++) {
> + if (strlen(*argv) > 3 * sizeof(pid_t)) {
> + fprintf(stderr, "%s: No such process\n", *argv);
> + ret = 1;
> + continue;
> + }
> + sprintf(path, "/proc/%s/cwd", *argv);

Please use snprintf here and check for -1 and truncation.

> + n = readlink(path, target, sizeof(target) - 1);
> + if (n >= 0) {
> + target[n] = '\0';
> + printf("%s: %s\n", *argv, target);
> + } else if (errno == ENOENT) {
> + fprintf(stderr, "%s: No such process\n", *argv);
> + ret = 1;

Here you can use eprintf().

> + } else {
> + perror(*argv);
> + ret = 1;
> + }
> + }
> +
> + return ret;
> +}
> --
> 2.7.3
>
>

Kind regards,
Hiltjo
Received on Sat Mar 26 2016 - 12:34:52 CET

This archive was generated by hypermail 2.3.0 : Sat Mar 26 2016 - 12:36:14 CET