aboutsummaryrefslogtreecommitdiff
path: root/toys/other/pwdx.c
blob: b2acc411ad333dc4eed221939c200250c3c1dd08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* pwdx.c - report current directory of a process. 
 *
 * Copyright 2013 Lukasz Skalski <l.skalski@partner.samsung.com>

USE_PWDX(NEWTOY(pwdx, "<1a", TOYFLAG_USR|TOYFLAG_BIN))

config PWDX
  bool "pwdx"
  default y
  help
    usage: pwdx PID...

    Print working directory of processes listed on command line.
*/

#include "toys.h"

void pwdx_main(void)
{
  char **optargs;

  for (optargs = toys.optargs; *optargs; optargs++) {
    char *path;
    int num_bytes;

    path = xmsprintf("/proc/%s/cwd", *optargs);
    num_bytes = readlink(path, toybuf, sizeof(toybuf)-1);
    free(path);

    if (num_bytes==-1) {
      path = strerror(errno);
      toys.exitval = 1;
    } else {
      path = toybuf;
      toybuf[num_bytes] = 0;
    }
    xprintf("%s: %s\n", *optargs, path);
  }
}