4

How can I get the current user path in Linux? It can be either with the GTK+ framework APIs, or plain C++.

unwind
  • 391,730
  • 64
  • 469
  • 606
boom
  • 5,856
  • 24
  • 61
  • 96
  • 1
    http://stackoverflow.com/questions/143174/c-c-how-to-obtain-the-full-path-of-current-directory – Manu Dec 15 '10 at 10:58
  • 1
    Do you mean the current working directory, the user's home directory, or something else? –  Dec 15 '10 at 10:59

4 Answers4

6

Assuming you mean the current directory of the process:

unwind
  • 391,730
  • 64
  • 469
  • 606
2

If you want to get home directory use getenv("HOME")

DReJ
  • 1,966
  • 15
  • 14
2

g_get_home_dir() from Glib is more cross-platform than getenv("HOME"). It also prefers /etc/passwd entries over the HOME variable for various reasons discussed at the aforementioned link.

ptomato
  • 56,175
  • 13
  • 112
  • 165
1

Not sure whether you're wanting the contents of $PATH or the user's current working directory. However to cover both options...

PATH is an environment variable, so you can access this with getenv(), in this instance getenv("PATH"), and is defined in <stdlib.h>.

The current working directory can be obtained with getcwd(), and is defined in <unistd.h>.

Chris J
  • 30,688
  • 6
  • 69
  • 111