linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* HOME directory
@ 2005-04-25 16:27 HIToC
       [not found] ` <426D1DC8.2080900@hq.ntsp.nec.co.jp>
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: HIToC @ 2005-04-25 16:27 UTC (permalink / raw)
  To: linux-c-programming

Hello all!
	I am writing a piece of code that makes files, open directories in my
HOME directory. All worked well until I changed the name of my HOME dir.
To make the code portable I tried with use of '~' character:

	const char*	filename = "~/file.txt";
	ofstream	 my_file(filename, ios::out);

but this solution does not work.
Have you any suggestion to find the path of the current home directory or
the current user name?

Thanks for any suggestion.
-- 
With regards,


					HIToC
					hitoc_mail@yahoo.it

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: HOME directory
       [not found] ` <426D1DC8.2080900@hq.ntsp.nec.co.jp>
@ 2005-04-25 16:42   ` Ron Michael Khu
  2005-04-25 16:46     ` Ron Michael Khu
  0 siblings, 1 reply; 10+ messages in thread
From: Ron Michael Khu @ 2005-04-25 16:42 UTC (permalink / raw)
  To: linux-c-programming

I dont know what specific system calls are used for retreiving the 
current user or home dir(if there are any).
But perhaps u can make use of the getenv() function to retrieve the 
value of $HOME and $USER
(it may not work on some shells, though, didnt test it)




HIToC wrote:

>> Hello all!
>>     I am writing a piece of code that makes files, open directories 
>> in my
>> HOME directory. All worked well until I changed the name of my HOME dir.
>> To make the code portable I tried with use of '~' character:
>>
>>     const char*    filename = "~/file.txt";
>>     ofstream     my_file(filename, ios::out);
>>
>> but this solution does not work.
>> Have you any suggestion to find the path of the current home 
>> directory or
>> the current user name?
>>
>> Thanks for any suggestion.
>>  
>>
>
>



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: HOME directory
  2005-04-25 16:27 HOME directory HIToC
       [not found] ` <426D1DC8.2080900@hq.ntsp.nec.co.jp>
@ 2005-04-25 16:43 ` Benjamin Machuletz
  2005-04-25 16:59 ` Richard Nairn
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 10+ messages in thread
From: Benjamin Machuletz @ 2005-04-25 16:43 UTC (permalink / raw)
  To: linux-c-programming

[-- Attachment #1: Type: text/plain, Size: 685 bytes --]

hi,

if you rename your home-directory, you have to change your /etc/passwd to 
point your user entry to the new home-directory.

benjamin,


On Monday 25 April 2005 18:27, HIToC wrote:
> Hello all!
>  I am writing a piece of code that makes files, open directories in my
> HOME directory. All worked well until I changed the name of my HOME dir.
> To make the code portable I tried with use of '~' character:
>
>  const char* filename = "~/file.txt";
>  ofstream  my_file(filename, ios::out);
>
> but this solution does not work.
> Have you any suggestion to find the path of the current home directory or
> the current user name?
>
> Thanks for any suggestion.

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: HOME directory
  2005-04-25 16:42   ` Ron Michael Khu
@ 2005-04-25 16:46     ` Ron Michael Khu
  0 siblings, 0 replies; 10+ messages in thread
From: Ron Michael Khu @ 2005-04-25 16:46 UTC (permalink / raw)
  To: linux-c-programming

Ooops... inapplicable solution...
scratch it...


Ron Michael Khu wrote:

> I dont know what specific system calls are used for retreiving the 
> current user or home dir(if there are any).
> But perhaps u can make use of the getenv() function to retrieve the 
> value of $HOME and $USER
> (it may not work on some shells, though, didnt test it)
>
>
>
>
> HIToC wrote:
>
>>> Hello all!
>>>     I am writing a piece of code that makes files, open directories 
>>> in my
>>> HOME directory. All worked well until I changed the name of my HOME 
>>> dir.
>>> To make the code portable I tried with use of '~' character:
>>>
>>>     const char*    filename = "~/file.txt";
>>>     ofstream     my_file(filename, ios::out);
>>>
>>> but this solution does not work.
>>> Have you any suggestion to find the path of the current home 
>>> directory or
>>> the current user name?
>>>
>>> Thanks for any suggestion.
>>>  
>>>
>>
>>
>
>



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: HOME directory
@ 2005-04-25 16:50 Luciano Moreira - ht
  0 siblings, 0 replies; 10+ messages in thread
From: Luciano Moreira - ht @ 2005-04-25 16:50 UTC (permalink / raw)
  To: linux-c-programming

Yeah ! You need to read the desired environment variable (like HOME), 
using a API like "|char *getenv(const char */name/);".

After read, you shall concatenate the 2 strings:

char *pHome;
char *pMyDir="mydir/mysubdir";
char sFullPath[_MAX_PATH];

pHome = getenv("HOME");
sprintf(sFullPath, "%s/%s", pHome, pMyDir);

WARNING: "sprintf()" isn't the faster way to build a concatenated 
string. If you'll need to repeat this in a long loop, try another way 
using only a single call to "strcat()".

Luciano
|

HIToC escreveu:

>Hello all!
>	I am writing a piece of code that makes files, open directories in my
>HOME directory. All worked well until I changed the name of my HOME dir.
>To make the code portable I tried with use of '~' character:
>
>	const char*	filename = "~/file.txt";
>	ofstream	 my_file(filename, ios::out);
>
>but this solution does not work.
>Have you any suggestion to find the path of the current home directory or
>the current user name?
>
>Thanks for any suggestion.
>  
>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: HOME directory
  2005-04-25 16:27 HOME directory HIToC
       [not found] ` <426D1DC8.2080900@hq.ntsp.nec.co.jp>
  2005-04-25 16:43 ` Benjamin Machuletz
@ 2005-04-25 16:59 ` Richard Nairn
  2005-04-25 17:05 ` Steve Graegert
  2005-04-25 17:06 ` Glynn Clements
  4 siblings, 0 replies; 10+ messages in thread
From: Richard Nairn @ 2005-04-25 16:59 UTC (permalink / raw)
  To: HIToC, linux-c-programming

You can use the environment variables to determine what the home directory  
is. Look at the manpage for getenv(3) and environ(5)

On Mon, 25 Apr 2005 10:27:59 -0600, HIToC <hitoc_mail@yahoo.it> wrote:

> Hello all!
> 	I am writing a piece of code that makes files, open directories in my
> HOME directory. All worked well until I changed the name of my HOME dir.
> To make the code portable I tried with use of '~' character:
>
> 	const char*	filename = "~/file.txt";
> 	ofstream	 my_file(filename, ios::out);
>
> but this solution does not work.
> Have you any suggestion to find the path of the current home directory or
> the current user name?
>
> Thanks for any suggestion.



-- 
  |       Richard Nairn          Specializing in Linux
  |     Nairn Consulting         Web / Database Solutions
  |        Calgary, AB
  | Richard@NairnConsulting.ca

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: HOME directory
  2005-04-25 16:27 HOME directory HIToC
                   ` (2 preceding siblings ...)
  2005-04-25 16:59 ` Richard Nairn
@ 2005-04-25 17:05 ` Steve Graegert
       [not found]   ` <426D38D3.5060901@tlen.pl>
  2005-04-25 17:06 ` Glynn Clements
  4 siblings, 1 reply; 10+ messages in thread
From: Steve Graegert @ 2005-04-25 17:05 UTC (permalink / raw)
  To: HIToC; +Cc: linux-c-programming

On 4/25/05, HIToC <hitoc_mail@yahoo.it> wrote:
> Hello all!
>         I am writing a piece of code that makes files, open directories in my
> HOME directory. All worked well until I changed the name of my HOME dir.
> To make the code portable I tried with use of '~' character:
> 
>         const char*     filename = "~/file.txt";
>         ofstream         my_file(filename, ios::out);
> 
> but this solution does not work.
> Have you any suggestion to find the path of the current home directory or
> the current user name?

All SUSv2/3 compliant systems know the system call getpwnam() to
obtain information about a given user by querying the passwd database.
 The following (untested) code snippet  demonstrates its usage.  Hope
this is what you're lookin' for.

----- BEGIN -----

#include <stdio.h>
#include <errno.h>
#include <pwd.h>

int main(int argc, char **argv)
{
    char *login_name;
    struct passwd *p;

    if (argc == 1) {
        if ((login_name = getlogin()) == NULL) {
            perror("getlogin() failed.\n");
            exit(errno);
        }
    } else if (argc == 2) {
        login_name = argv[1];
    }

    if ((p = getpwnam(login_name)) == NULL) {
        perror("getpwnam() failed.\n");
        exit(errno);
    }

    printf("HOME dir for %s: %s\n", login_name, p->pw_dir);

    return (0);
}

----- END -----

If you have questions or suggestions, feel free to drop me a line.

Kind Regards

    \Steve

--

Steve Graegert <graegerts@gmail.com>
Independent Software Consultant {C/C++ && Java && .NET}
Mobile: +49 (176)  21 24 88 69
Office: +49 (9131) 71 26 40 9

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: HOME directory
  2005-04-25 16:27 HOME directory HIToC
                   ` (3 preceding siblings ...)
  2005-04-25 17:05 ` Steve Graegert
@ 2005-04-25 17:06 ` Glynn Clements
  4 siblings, 0 replies; 10+ messages in thread
From: Glynn Clements @ 2005-04-25 17:06 UTC (permalink / raw)
  To: HIToC; +Cc: linux-c-programming


HIToC wrote:

> 	I am writing a piece of code that makes files, open directories in my
> HOME directory. All worked well until I changed the name of my HOME dir.
> To make the code portable I tried with use of '~' character:
> 
> 	const char*	filename = "~/file.txt";
> 	ofstream	 my_file(filename, ios::out);
> 
> but this solution does not work.
> Have you any suggestion to find the path of the current home directory or
> the current user name?

The "~" syntax is part of the shell; library functions won't expand
it. In the shell, "~/..." is equivalent to "$HOME/...".

If you want to respect $HOME (which is usually the case), use e.g.:

	const char *home = getenv("HOME");
	const char *file = "file.txt";
	char *path = alloca(strlen(home) + strlen(file) + 2);

	sprintf(path, "%s/%s", home, file);

	ofstream my_file(filename, ios::out);

If you want to always use the user's home directory from /etc/passwd
instead of $HOME (e.g. for setuid programs), use getpwuid(), e.g.:

	struct passwd *pw = getpwuid(getuid());
	if (!pw)
		fatal_error("user not listed in /etc/passwd");
	const char *home = pw->pw_dir;

	...

Similarly, the ~user syntax for a specific user's home directory is
implemented using getpwnam().

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: HOME directory
       [not found]     ` <6a00c8d505042511529bd72c8@mail.gmail.com>
@ 2005-04-26 11:26       ` Adam Dyga
  0 siblings, 0 replies; 10+ messages in thread
From: Adam Dyga @ 2005-04-26 11:26 UTC (permalink / raw)
  To: linux-c-programming

Steve Graegert wrote:

>On 4/25/05, Adam Dyga <adeon@tlen.pl> wrote:
>  
>
>>>All SUSv2/3 compliant systems know the system call getpwnam() to
>>>obtain information about a given user by querying the passwd database.
>>>The following (untested) code snippet  demonstrates its usage.  Hope
>>>this is what you're lookin' for.
>>>
>>>      
>>>
>>Note that for security reasons this way much better.
>>    
>>
>Relying on the shell environment is
>usually a bad idea since it can be manipulated and tweaked in many
>different ways.
>
That's exactly what I meant ;)

Cheers
AD

PS: Sorry, my previous reply should have gone to the group....

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: HOME directory
@ 2005-04-28 12:28 HIToC
  0 siblings, 0 replies; 10+ messages in thread
From: HIToC @ 2005-04-28 12:28 UTC (permalink / raw)
  To: linux-c-programming

Thank you for every suggestion!





HIToC


		
___________________________________ 
Nuovo Yahoo! Messenger: E' molto più divertente: Audibles, Avatar, Webcam, Giochi, Rubrica… Scaricalo ora! 
http://it.messenger.yahoo.it
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2005-04-28 12:28 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-04-25 16:27 HOME directory HIToC
     [not found] ` <426D1DC8.2080900@hq.ntsp.nec.co.jp>
2005-04-25 16:42   ` Ron Michael Khu
2005-04-25 16:46     ` Ron Michael Khu
2005-04-25 16:43 ` Benjamin Machuletz
2005-04-25 16:59 ` Richard Nairn
2005-04-25 17:05 ` Steve Graegert
     [not found]   ` <426D38D3.5060901@tlen.pl>
     [not found]     ` <6a00c8d505042511529bd72c8@mail.gmail.com>
2005-04-26 11:26       ` Adam Dyga
2005-04-25 17:06 ` Glynn Clements
  -- strict thread matches above, loose matches on Subject: below --
2005-04-25 16:50 Luciano Moreira - ht
2005-04-28 12:28 HIToC

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).