What is different symbolic link with hard link after unlik?
Solution 1:
First of all, are you sure you've got the output of the programs around the right way? I get a failure for the symlink version and a success for the hard link version.
Second, passing numeric constants as the flags argument for open
is unportable and makes your code difficult to read. The relevant flags are defined in <fcntl.h>
, and the constant you are using is O_WRONLY
.
If you pass a symlink as the first argument to open
, it is equivalent to passing the name the symlink points to. In your example, the symlink points to a non-existent file, causing the system call to fail. If you would like the call to create the file, you will need to pass O_WRONLY|O_CREAT
as flags. This will result in the file foo
being created.
For the hard link case, the name bar
links to foo
's file contents rather than the name. Unlinking foo
doesn't change this fact, so bar
continues to exist and can be opened without O_CREAT
.