• Uncategorized

About c : fstat-works-stat-does-not

Question Detail

I am compiling an Angband variant on Raspbian, and the following fails:

struct stat txt_stat, raw_stat;

/* Build the filename */
path_build(buf, 1024, ANGBAND_DIR_EDIT, template_file);

/* Access stats on text file */
if (stat(buf, &txt_stat))
{
  /* Error */
  msg_format("Oh dear, something went wrong with stat()! %s", strerror(errno));
  msg_format("Could not retrieve stats on the text file at {%s}", buf);
  return (-1);
}

What annoys me to no end is that the file is there at
~/git/hellband/lib/edit/r_info.txt

When I replace the above code with code that opens the file (using the same buf) and then use fstat() on the file descriptor, it works!!

I dont want to open a file (and remember to close that file), I just want to know/fix what is going on with stat().

PostScriptum 2020; it turned that when I used the Angband approach to opening files, it autoexpanded the ~ sign, which is why that worked, and fstat worked.

Question Answer

It’s simple. stat(2) doesn’t expand or understand ~ (tilde).

You could HOME instead. For example, getenv("HOME") and then prefix it with your filename before calling stat(2).

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.