How to check whether two file names point to the same physical file

c

Solution

On linux, open both files, and use `fstat` to check if `st_ino` (edit:) and `st_dev` are the same. `open` will follow symbolic links. Don't use `stat` directly, to prevent race conditions.

Problem

I have a program that accepts two file names as arguments: it reads the first file in order to create the second file. How can I ensure that the program won't overwrite the first file? Restrictions: - The method must keep working when the file system supports (soft or hard) links - File permissions are fixed and it is only required that the first file is readable and the second file writeable - It should preferably be platform-neutral (although Linux is the primary target)

Original source