0

As far as I know, "Documents" and all other directories under home are localization related, my question is: is it possible to get full path of "Documents" in C/C++ on Linux (Ubuntu)? Didn't find it in PATH nor other env.

I'm not looking for $HOME, I'm looking for ${HOME}"Documents". For instance, it's "Documents" for English locale, but "文档" for Chinese, etc.

Thanks.

pipipi
  • 501
  • 1
  • 3
  • 11
  • Possible duplicate of [Get path to My Documents](https://stackoverflow.com/questions/2414828/get-path-to-my-documents) – Lorence Hernandez Feb 26 '18 at 07:23
  • 1
    `Documents` is not required to exist at all, so you need to check whether it is provided before attempting to get the path. In Linux the `$HOME` environment variable holds the user home directory which is where `Documents` would be located if it exists. – David C. Rankin Feb 26 '18 at 07:24
  • 3
    @LorenceHernandez It's not its duplicate because it's not the same OS. –  Feb 26 '18 at 07:25
  • 1
    @LorenceHernandez I'm asking with Linux, thanks. – pipipi Feb 26 '18 at 07:27
  • The `xdg-user-dir` command could be useful to know. Read its manual page with `man xdg-user-dir`. And [this link](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/) might be useful as well. – Some programmer dude Feb 26 '18 at 07:28
  • @DavidC.Rankin I updated the description, I understand it's not required to exist, so is it possible to retrieve it if it exists? – pipipi Feb 26 '18 at 07:29
  • @paladin_t It will be distribution dependent but I expect most popular distros will use free-desktop-org settings as per Mike's answer. – Galik Feb 26 '18 at 07:33
  • Thank you all for the info, it's helpful – pipipi Feb 26 '18 at 07:36
  • @paladin_t, yes, see the `open` function to check whether the directory exists, e.g. `int fd = open (dirname, flags, mode);` if the return is `-1` it doesn't exist, otherwise just close the fd, and you know it does. – David C. Rankin Feb 26 '18 at 07:45

1 Answers1

2

It looks like, if this is based on the XDG FreeDesktop standard, then there are two files that contain the info:

~/.config/user-dirs.locale

and

~/.config/user-dirs.dirs 

The former contains the locale itself, while the second contains the mapping to the actual folder paths.

References:

[1] https://blogs.gnome.org/simos/2007/11/11/localisation-issues-in-home-directory-folders-xdg-user-dirs/
[2] https://www.freedesktop.org/wiki/Software/xdg-user-dirs/

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151