UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » how to get the linux home path using c language

#1 2007-04-20 08:30 AM

linuxhero
Guest

Re: how to get the linux home path using c language

hai guys

i am new to linux with c language.

can anybody tell abt how to get the home path using c language.

bye
Manju

#2 2007-04-20 10:58 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: how to get the linux home path using c language

You can find it in the $HOME environment variable, which you can get by calling getenv("HOME").

Offline

#3 2007-04-20 01:32 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: how to get the linux home path using c language

Yes, that's the easy way...  But, not necessarily reliable...  Depending on the shell
you use and how your login scripts are configured, $HOME may not always be set...
(Though, it really should be on any sane system...)  Plus, of course, users can
always override $HOME to anything they want...  If you want to be sure to get their
REAL home directory, the best way is to lookup their "/etc/passwd" entry and get
it from that, which you can do by calling getpwuid() or getpwnam(), and using the
"pw_dir" entry in the returned structure...

Offline

#4 2007-04-20 01:55 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: how to get the linux home path using c language

If it's just used to find the user's own home, getenv() should be fine. If users want to fool themselves, that's their problem. But if it is used to look up other's homedirs, or it's a security sensitive application, then using getpwuid() or getpwnam() is better.

If $HOME isn't set then that's a bug that should be fixed on your system. ;-)

Offline

  • Index
  • » C
  • » how to get the linux home path using c language

Board footer

Powered by FluxBB