getting home directory

getting home directory

Post by MrZammle » Fri, 12 Apr 2002 22:55:55



Hi,

How can I get the home directory of the user executing a program?

I.e.  want to fopen a file in /home/user but this fails:
fp=fopen("~/testfile", "a");

Thanks,
MrZammler

 
 
 

getting home directory

Post by MrZammle » Fri, 12 Apr 2002 23:03:57


Please ignore it, discovered getenv :)

MrZammler

 
 
 

getting home directory

Post by Bill Medlan » Fri, 12 Apr 2002 23:50:47



Quote:> Please ignore it, discovered getenv :)

> MrZammler

See comp.unix.programmer Unix FAQ
2.9 How do I expand `~' in a filename like the shell does?
which even shows what to do if HOME isn't set

(The FAQ is worth having nearby)

Bill

 
 
 

getting home directory

Post by Norm Dresne » Sat, 13 Apr 2002 00:16:08



Quote:> Hi,

> How can I get the home directory of the user executing a program?

> I.e.  want to fopen a file in /home/user but this fails:
> fp=fopen("~/testfile", "a");

    I know you've discovered the getenv() function, but you should be aware
that it can be made to lie to you: a user can set the environment variable
HOME to anything she wants!  See the FAQ's.

        Norm

 
 
 

getting home directory

Post by MrZammle » Sat, 13 Apr 2002 00:24:10


Thanks Bill, I will do that.

MrZammler




> > Please ignore it, discovered getenv :)

> > MrZammler

> See comp.unix.programmer Unix FAQ
> 2.9 How do I expand `~' in a filename like the shell does?
> which even shows what to do if HOME isn't set

> (The FAQ is worth having nearby)

> Bill

 
 
 

getting home directory

Post by Stephen Youn » Sat, 13 Apr 2002 10:32:45


Quote:> > How can I get the home directory of the user executing a program?

> > I.e.  want to fopen a file in /home/user but this fails:
> > fp=fopen("~/testfile", "a");

>     I know you've discovered the getenv() function, but you should be aware
> that it can be made to lie to you: a user can set the environment variable
> HOME to anything she wants!  See the FAQ's.

Very true. As added caution, i'm saying this to you, "BE very, very careful with
environment variables. Avoid using environment variables like a you'd avoid a
plague."

Not to say that you shouldn't use EV's but know what you are doing.

Laters,
Stephen Young

 
 
 

getting home directory

Post by Kasper Dupon » Sat, 13 Apr 2002 17:52:35





> > Hi,

> > How can I get the home directory of the user executing a program?

> > I.e.  want to fopen a file in /home/user but this fails:
> > fp=fopen("~/testfile", "a");

>     I know you've discovered the getenv() function, but you should be aware
> that it can be made to lie to you: a user can set the environment variable
> HOME to anything she wants!  See the FAQ's.

It is important to know when to use HOME and when not
to. Usually you should rely on HOME if it is set and
only use alternatives as a fallback. Users might want
your program to use another directory as ~, that could
be to have an alternate set of configuration files.

When you write suid programs you should not rely on
HOME, actually you should normally discard the entire
environment.

--
Kasper Dupont -- der bruger for meget tid p? usenet.

 
 
 

getting home directory

Post by Eric G. Mille » Mon, 22 Apr 2002 11:14:37



> Hi,

> How can I get the home directory of the user executing a program?

> I.e.  want to fopen a file in /home/user but this fails:
> fp=fopen("~/testfile", "a");

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>

#define TESTFILE "/testfile"

{
    uid_t me;
    struct passwd *my_passwd;
    size_t len;
    char *buff = NULL;
    char testfile[] = TESTFILE;
    FILE *fp = NULL;

    me = getuid();
    my_passwd = getpwuid (me);

    if (my_passwd != NULL)
    {
         len = strlen (my_passwd->pw_dir) + sizeof (testfile) + 1;
         if ((buff = calloc (1, len)) != NULL)
         {
              snprintf (buff, len, "%s%s", my_passwd->pw_dir, testfile);
              if ((fp = fopen (buff, "a")) != NULL)
              {
                  /* stuff... */
                  fclose (fp);
              }
              else
              {
                  /* error handler */
              }
              free (buff);
         }
         else
         {
              /* error handler */
         }
    }
    else
    {
         /* error handler */
    }  

Quote:}

 
 
 

getting home directory

Post by Eric P. McC » Mon, 22 Apr 2002 11:46:22



Quote:> #define TESTFILE "/testfile"

> {
>     uid_t me;
>     struct passwd *my_passwd;

How thoroughly did you test this code?

--

"Last I checked, it wasn't the power cord for the Clue Generator that
was sticking up your ass." - John Novak, rasfwrj

 
 
 

getting home directory

Post by Floyd Davidso » Mon, 22 Apr 2002 14:30:16




>> #define TESTFILE "/testfile"

>> {
>>     uid_t me;
>>     struct passwd *my_passwd;

>How thoroughly did you test this code?

I'll grant he won't win points on coding style, but it is valid
code, and worked out of the box merely by putting a function
name on it.  What does a thorough test reveal?

--
Floyd L. Davidson         <http://www.ptialaska.net/~floyd>

 
 
 

getting home directory

Post by Eric G. Mille » Tue, 23 Apr 2002 06:48:17




>> #define TESTFILE "/testfile"

>> {
>>     uid_t me;
>>     struct passwd *my_passwd;

> How thoroughly did you test this code?

What's the problem?  It certainly wasn't meant to be complete...
 
 
 

getting home directory

Post by Eric G. Mille » Tue, 23 Apr 2002 06:59:57





>>> #define TESTFILE "/testfile"

>>> {
>>>     uid_t me;
>>>     struct passwd *my_passwd;

>>How thoroughly did you test this code?

> I'll grant he won't win points on coding style, but it is valid
> code, and worked out of the box merely by putting a function
> name on it.  What does a thorough test reveal?

Other than not being complete, maybe you'd like to elaborate?
Granted, in the real world, I'd wrap getting the home directory
into a function of it's own, probably just

char *
get_home_dir (void)
{
   struct passwd *my_passwd = getpwuid (getuid());
   if (my_passwd)
      return strdup(my_passwd->pw_dir);
   else
      return NULL;

Quote:}

 
 
 

getting home directory

Post by Roger Leig » Wed, 24 Apr 2002 02:13:56



Quote:> Other than not being complete, maybe you'd like to elaborate?
> Granted, in the real world, I'd wrap getting the home directory
> into a function of it's own, probably just

> char *
> get_home_dir (void)
> {
>    struct passwd *my_passwd = getpwuid (getuid());
>    if (my_passwd)
>       return strdup(my_passwd->pw_dir);
>    else
>       return NULL;
> }

It would be nice if you used getenv("HOME") before getpwuid() so that
the user may override the default.  If HOME is NULL, then you can fall
back to this.

--
Roger Leigh
                ** Registration Number: 151826, http://counter.li.org **
                Need Epson Stylus Utilities? http://gimp-print.sourceforge.net/
                GPG Public Key: 0x25BFB848 available on public keyservers

 
 
 

getting home directory

Post by Josef M?ller » Wed, 24 Apr 2002 15:48:49




> > Other than not being complete, maybe you'd like to elaborate?
> > Granted, in the real world, I'd wrap getting the home directory
> > into a function of it's own, probably just

> > char *
> > get_home_dir (void)
> > {
> >    struct passwd *my_passwd = getpwuid (getuid());
> >    if (my_passwd)
> >       return strdup(my_passwd->pw_dir);
> >    else
> >       return NULL;
> > }

> It would be nice if you used getenv("HOME") before getpwuid() so that
> the user may override the default.  If HOME is NULL, then you can fall
> back to this.

It depends. If you reallyreally need the user's "real" HOME, then using
getenv("HOME") will not be good, ebacause the user can cheat.
/etc/passwd is more difficult to cheat with.

--
Josef M?llers (Pinguinpfleger bei FSC)
        If failure had no penalty success would not be a prize
                                                -- T.  Pratchett

 
 
 

getting home directory

Post by Kasper Dupon » Wed, 24 Apr 2002 19:47:23




> > It would be nice if you used getenv("HOME") before getpwuid() so that
> > the user may override the default.  If HOME is NULL, then you can fall
> > back to this.

> It depends. If you reallyreally need the user's "real" HOME, then using
> getenv("HOME") will not be good, ebacause the user can cheat.
> /etc/passwd is more difficult to cheat with.

You are absolutely right about that. But OTOH using getpwuid()
will also be a bad idea in some cases. One example is the fact
that in some cases I change the HOME environment variable to
be able to use an atlernate set of configuration files for
netscape. If they had done the wrong thing and used getpwuid()
instead of HOME I would not have been able to do that.

I think the short answer is that for suid/sgid programs you
should use getpwuid(), in all other cases you should use HOME
first and fall back to getpwuid() if HOME is unset. Whenever
you actually use getpwuid() it might be a good idea to set the
HOME environment variable when you are at it. (Only needed if
you are going to exec some other program later on.)

--
Kasper Dupont -- der bruger for meget tid p? usenet.

 
 
 

1. getting home directories

Suppose a C program has to reead the name of a file on another file and then
open it (with "fopen").

Now, suppose the name read has one of the forms:

   -  ~/dir/file
   -  ~user/dir/file

What tools do I have to interpret the ~ and ~user as home directories?
What tools can split path name in single components?

Pierre Gaumond.
--


C.P. 6128, Succursale "A", Montreal,            |
Quebec, Canada.   H3C 3J7                       |

2. 6,4" touchpanel in Europe

3. Getting a user's home directory

4. Sun NetBackup does Sun or Vertias offer a class

5. Really, REALLY stupid question...

6. Getting device from home directory

7. graphics_

8. automountd: trying to mount odd home directories(/home/.htaccess)

9. /home and automount maps with many home directory servers

10. creating user home directories /home/username from /etc/passwd

11. help: won't create home directories in /home

12. home directory=root directory