Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The shell can expand ~ to your home directory. $HOME usually has the same deal, but often you want to refer to the current users home directory from a context that may not support such expansion.

I have had config files where $HOME works but ~ doesn't and vice versa.

I would guess that fuse could provide something along these lines, something like /var/myself -> $HOME

With that I could place values in config files to point to things like /var/myself/backdrops/pornography/wtf/yarly.jpg

Is there something like this already? If not, are there good reasons for there not being something like this?

share|improve this question
2  
This actually seems like a really good idea, and fuse could do this pretty trivially if I remember the interface correctly –  Michael Mrozek Sep 9 '10 at 6:54
add comment

4 Answers 4

up vote 5 down vote accepted

I understand your concern but the answer is "no" there is not such thing.
The usual method is to ask the OS the user's home path, or get the $HOME variable.

All these options needs always some coding from the application. A lot of applications, like bash, offer the "alias" ~ (open(2) does not translate that).

Of course a vfs or a fuse module could be implemented to do this. Probably there is something to do that, I am going to ask that!

But is it really needed? You can use a workaround like:

share|improve this answer
    
I ask for the vfs tool: unix.stackexchange.com/questions/1811/… –  Keymon Sep 9 '10 at 8:43
add comment

One trick (on Linux, at least) would be to change directory to $HOME before running the app and then use /proc/self/cwd/... in the config file.

share|improve this answer
    
I like the hackishness of this but presumably it would run into problems it the process changes the current directory. I'd like a /proc/PID/iwd for initial working directory. –  Lerc Sep 12 '10 at 11:26
    
...and for that matter a /proc/PID/home –  Lerc Sep 12 '10 at 11:32
    
@Lerc, if it's going in a config file it needs to be /proc/self not /proc/PID, since you can't know in advance what the PID is going to be. –  Neil Mayhew Sep 12 '10 at 13:53
    
@Lerc, yes, if the process changes directory then it would fail, but typically programs read the config file once when they start up, and then cache the values in memory. It all depends on the program you are working with, and if the program doesn't offer enough flexibility, either on the command line or in the config file syntax, then you are stuck. –  Neil Mayhew Sep 12 '10 at 13:56
    
Yeah, I meant /proc/PID/home to mean that all proc/PID dirs should support this and thus include /proc/self –  Lerc Sep 13 '10 at 11:09
add comment

Most programs allow you to specify a path to the configuration file on the command line. So you could write a wrapper that takes a standard configuration file, filters it to substitute things like $HOME for the current user, and then passes the modified, temporary configuration file to the program.

share|improve this answer
    
If you do that then you've effectively taken the ability to specify a path to the config file away from the user. Might be doable if you get rally fancy with a wrapper exe. –  Lerc Sep 13 '10 at 11:08
add comment

You can get the pid of requestin user and ask system of his/her home directory. So it is possible.

However I'm not sure if there is no SUID programs that would assume FS as static.

Edit:

struct fuse_context *ctx = fuse_get_context ();
struct passwd pwd, *ppwd;
char buffer[1024];
int status = getpwuid_r (ctx->ui, &pwd, buffer, sizeof(buffer), &ppwd);
if (status == 0) {
  if (ppwd == NULL) {
    // No record found
  } else {
    // Handle record
  }
} else {
    // Handle error
} 

The above code is not ideal but should do work for common cases.

share|improve this answer
    
How would you find his home directory after you know what user it is? –  Hugo Sep 12 '10 at 18:47
    
Using getpwuid or getpwuid_r. –  Maciej Piechotka Sep 12 '10 at 20:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.