fopen("~/.prefs.rc", "r"); can't find file

fopen("~/.prefs.rc", "r"); can't find file

Post by Erik van Zijs » Thu, 07 May 1998 04:00:00



Hi, I'm writing my first "big" GUI program.
In this program, I store some preferences in a rc file. Somehow, I just
can't place it in the user's homedir.

When I use:

FILE *fp;
fp = fopen(".prefs.rc", "r");

it works fine. Although when I use
fp = fopen("~/.prefs.rc", "r"); it somehow cannot find the file.
What's wrong? Can't I use the tilde (~) in a filename? Or should it be
preceded by some escape character? (In case it matters: I use Linux.)

Any help would be greatly appreciated.

Thanks in advance,
Erik van Zijst
--


http://www.xs4all.nl/~icehawk      (MegaLAN, Internet and Music Party!)
ICQ UIN number: 4545632

 
 
 

fopen("~/.prefs.rc", "r"); can't find file

Post by Andrew Giert » Thu, 07 May 1998 04:00:00


 Erik> Hi, I'm writing my first "big" GUI program.
 Erik> In this program, I store some preferences in a rc
 Erik> file. Somehow, I just can't place it in the user's homedir.

 Erik> When I use:

 Erik> FILE *fp;
 Erik> fp = fopen(".prefs.rc", "r");

 Erik> it works fine. Although when I use
 Erik> fp = fopen("~/.prefs.rc", "r"); it somehow cannot find the file.
 Erik> What's wrong? Can't I use the tilde (~) in a filename?

Expansion of ~ is done by the shell, not by the system. See the FAQ for
examples of how to do it in your own code.

(Expansion of ~ on its own is trivial; just replace it with the value of
the $HOME environment variable.)

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

fopen("~/.prefs.rc", "r"); can't find file

Post by Robert Hyat » Thu, 07 May 1998 04:00:00



: Hi, I'm writing my first "big" GUI program.
: In this program, I store some preferences in a rc file. Somehow, I just
: can't place it in the user's homedir.

: When I use:

: FILE *fp;
: fp = fopen(".prefs.rc", "r");

: it works fine. Although when I use
: fp = fopen("~/.prefs.rc", "r"); it somehow cannot find the file.
: What's wrong? Can't I use the tilde (~) in a filename? Or should it be
: preceded by some escape character? (In case it matters: I use Linux.)

So far as I know, the ~ notation is handled within the various shells
and other programs that accept filenames and do something with them.  But
*not* the fopen() system call.  We wouldn't want to see that have to run
over to the /etc/passwd file to figure out where ~user's home directory
really is.

You can expand the ~ yourself, of course, by getting your passwd entry
and parsing the home directory piece, and adding that to the front of
the filename before you open it...

--
Robert Hyatt                    Computer and Information Sciences

(205) 934-2213                  115A Campbell Hall, UAB Station
(205) 934-5473 FAX              Birmingham, AL 35294-1170

 
 
 

fopen("~/.prefs.rc", "r"); can't find file

Post by Peter William » Thu, 07 May 1998 04:00:00



> Hi, I'm writing my first "big" GUI program.
> In this program, I store some preferences in a rc file. Somehow, I just
> can't place it in the user's homedir.

> When I use:

> FILE *fp;
> fp = fopen(".prefs.rc", "r");

> it works fine. Although when I use
> fp = fopen("~/.prefs.rc", "r"); it somehow cannot find the file.
> What's wrong? Can't I use the tilde (~) in a filename? Or should it be
> preceded by some escape character? (In case it matters: I use Linux.)

No, this is expanded by the shell you are using.
Use the getenv function to determine the path of the home directory.

For example:

s_home_dir = getenv("HOME");

 
 
 

fopen("~/.prefs.rc", "r"); can't find file

Post by Andrew Giert » Thu, 07 May 1998 04:00:00


 Robert> You can expand the ~ yourself, of course, by getting your
 Robert> passwd entry and parsing the home directory piece, and adding
 Robert> that to the front of the filename before you open it...

That is incorrect behaviour for a bare ~ (i.e. not ~username).

~username should be expanded by consulting the passwd file, but just ~
on its own should be simply replaced by $HOME.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

fopen("~/.prefs.rc", "r"); can't find file

Post by Erik van Zijs » Fri, 08 May 1998 04:00:00




> > Hi, I'm writing my first "big" GUI program.
> > In this program, I store some preferences in a rc file. Somehow, I just
> > can't place it in the user's homedir.

> > When I use:

> > FILE *fp;
> > fp = fopen(".prefs.rc", "r");

> > it works fine. Although when I use
> > fp = fopen("~/.prefs.rc", "r"); it somehow cannot find the file.
> > What's wrong? Can't I use the tilde (~) in a filename? Or should it be
> > preceded by some escape character? (In case it matters: I use Linux.)

> No, this is expanded by the shell you are using.
> Use the getenv function to determine the path of the home directory.

> For example:

> s_home_dir = getenv("HOME");

Thanks a lot (to all who replied), this works. You guys helped me out
...again... :)

Erik van Zijst
--


http://www.xs4all.nl/~icehawk      (MegaLAN, Internet and Music Party!)
ICQ UIN number: 4545632

 
 
 

fopen("~/.prefs.rc", "r"); can't find file

Post by Matthew Hannig » Tue, 12 May 1998 04:00:00





> Robert> You can expand the ~ yourself, of course, by getting your
> Robert> passwd entry and parsing the home directory piece, and adding
> Robert> that to the front of the filename before you open it...

>That is incorrect behaviour for a bare ~ (i.e. not ~username).

>~username should be expanded by consulting the passwd file, but just ~
>on its own should be simply replaced by $HOME.

I know most would think of this but just for completeness...

If you're writing a set[ug]id or a network program, or anything that
runs with different authority that of the invoker, you should not trust
the value of $HOME.

--
        -Matt

 
 
 

fopen("~/.prefs.rc", "r"); can't find file

Post by Dan A. Merc » Tue, 12 May 1998 04:00:00



: Hi, I'm writing my first "big" GUI program.
: In this program, I store some preferences in a rc file. Somehow, I just
: can't place it in the user's homedir.

: When I use:

: FILE *fp;
: fp = fopen(".prefs.rc", "r");

: it works fine. Although when I use
: fp = fopen("~/.prefs.rc", "r"); it somehow cannot find the file.
: What's wrong? Can't I use the tilde (~) in a filename? Or should it be
: preceded by some escape character? (In case it matters: I use Linux.)

~ notation is expanded by the shell and is supported by the Csh and
POSIX compliant shells (POSIX sh, ksh,  bash,  etc).
Well,  since you are using Linux,  bash should be the shell used by
system() and popen() and bash will expand ~ notation,  so you could
rewrite the fopen as a popen:

if (NULL == (pp = popen("/path/to/cat ~/.prefs.rc", "r")))
   {
   perror("popen ~/.prefs.rc");
   exit(1);
   }

or some such. Be sure you don't forget the pclose or you'll leave a zombie
process* around.  I've used this trick for years on HP-UX 9/10
which uses the POSIX sh for system and popen.  I would fully qualify
the path to cat,  however,  to discourage mischief.

: Any help would be greatly appreciated.

: Thanks in advance,
: Erik van Zijst
: --


: http://www.veryComputer.com/~icehawk      (MegaLAN, Internet and Music Party!)
: ICQ UIN number: 4545632

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.

 
 
 

fopen("~/.prefs.rc", "r"); can't find file

Post by Josh Yelo » Tue, 12 May 1998 04:00:00



> : FILE *fp;
> : fp = fopen(".prefs.rc", "r");

> : it works fine. Although when I use
> : fp = fopen("~/.prefs.rc", "r"); it somehow cannot find the file.

The shell replaces the ~ with your home directory.  Try this instead:

char buffer[MAXPATHLEN];
sprintf(buffer, "%s/.prefs.rc", getenv("HOME"));
fp = fopen(buffer, "r");

Actually, come to think of it, you should probably use something other
than sprintf to avoid buffer overflows.

- Josh

 
 
 

fopen("~/.prefs.rc", "r"); can't find file

Post by Nathan Dorfm » Tue, 12 May 1998 04:00:00




>> : FILE *fp;
>> : fp = fopen(".prefs.rc", "r");

>> : it works fine. Although when I use
>> : fp = fopen("~/.prefs.rc", "r"); it somehow cannot find the file.

>The shell replaces the ~ with your home directory.  Try this instead:

>char buffer[MAXPATHLEN];
>sprintf(buffer, "%s/.prefs.rc", getenv("HOME"));
>fp = fopen(buffer, "r");

>Actually, come to think of it, you should probably use something other
>than sprintf to avoid buffer overflows.

BSD has snprintf(). Really cool :-)

- Show quoted text -

Quote:>- Josh

 
 
 

fopen("~/.prefs.rc", "r"); can't find file

Post by Peter Samuels » Tue, 12 May 1998 04:00:00



Quote:> > char buffer[MAXPATHLEN];
> > sprintf(buffer, "%s/.prefs.rc", getenv("HOME"));
> > fp = fopen(buffer, "r");

> > Actually, come to think of it, you should probably use something
> > other than sprintf to avoid buffer overflows.


Quote:> BSD has snprintf(). Really cool :-)

Why not sprintf(buffer, "%80s/.prefs.rc", getenv("HOME")); ?  How does
snprintf() work and how is it better than the above?

--
Peter Samuelson
<sampo.creighton.edu ! psamuels>

 
 
 

fopen("~/.prefs.rc", "r"); can't find file

Post by Peeter Jo » Wed, 13 May 1998 04:00:00


Quote:>> BSD has snprintf(). Really cool :-)

>Why not sprintf(buffer, "%80s/.prefs.rc", getenv("HOME")); ?  How does
>snprintf() work and how is it better than the above?

snprintf doensn't have the buffer overflow problems of sprintf.  It will be
in the next version of glibc too.  I don't think it has anything to do with the
~ expansion.

--
Peeter Joot

Dept 487         IBM Canada         Tie Line 778-3359        Phone 416-448-3359

 
 
 

fopen("~/.prefs.rc", "r"); can't find file

Post by Aaron Cra » Wed, 13 May 1998 04:00:00


[Newsgroups trimmed.]


> snprintf doensn't have the buffer overflow problems of sprintf.  It will be
> in the next version of glibc too.

Almost.  It's already in glibc, and has been since glibc version 1; this
also applies to vasprintf(), a GNU extension which malloc()s a buffer big
enough for the output.  I think you meant that it's in the draft standard
for ISO C 9x.

Regards.

--

% If I had a ) for every time Windows crashed, what would I have?
Too many )'s

 
 
 

fopen("~/.prefs.rc", "r"); can't find file

Post by Marc Slemk » Wed, 13 May 1998 04:00:00



Quote:>>> BSD has snprintf(). Really cool :-)

>>Why not sprintf(buffer, "%80s/.prefs.rc", getenv("HOME")); ?  How does
>>snprintf() work and how is it better than the above?

You can give it a length for the size of the buffer.   It is far
easier than mucking with format strings all the time, especially
when you have multiple strings in the format.

Quote:>snprintf doensn't have the buffer overflow problems of sprintf.  It will be

I am afraid I don't see the buffer overflow problems in the above code,
as long as your buffer is long enough (ie. 91 chars if I can count right).

Is it a royal pain, especially with multiple %s's?  Yes.

Can it possibly lead to problems with security holes due to
truncation?  Yes, but so can snprintf.

Can it lead to a buffer overflow?  Not unless your sprintf is
very broken.

 
 
 

fopen("~/.prefs.rc", "r"); can't find file

Post by Robert Hyat » Wed, 13 May 1998 04:00:00



: Can it lead to a buffer overflow?  Not unless your sprintf is
: very broken.

The following can overflow a buffer of any size you care to
define:

sprintf(your_buffer,"%s",my_string);

safety depends on the length of my_string, *not* on the length of
your_buffer...  which is a problem...  very similar to the strncmp()
type function that limits the number of bytes, even if the "NULL" is
missing from the end of either string...

--
Robert Hyatt                    Computer and Information Sciences

(205) 934-2213                  115A Campbell Hall, UAB Station
(205) 934-5473 FAX              Birmingham, AL 35294-1170