I have a question regarding the link()
function in C.
I am using it to create a hard link to a file on a Unix system.
First, in my home directory (the same problem happens in other directories)
I create a test directory and go into it (cd
).
Now inside it, I execute a program that creates a hard link to a file in the ..
directory (the parent directory).
But for some reason that fails with the errno set on 2 (ENOENT).
The file exists -> its name is ../Exercise3.c
(the name is Exercise3.c
but it resides in the ..
directory).
And for some reason creating links to files like ../scr.c
and others works, but I cannot create a link to that file. It has all the same privileges as other text files in that directory.
I have tried debugging with deleting the file and recreating it (it did not work), printing the name of the file in the program to see if the code failed somehow, but it does not.
The strange thing is that if I stay in the parent directory, I can create the link to that file, but as soon as I go to the test directory I created and try to create a link to it (the file in question) with the path name ../Exercise3.c
it fails. Again ../scr.c
creates a link.
int status = link(dest, name);
if (status == 0)
{
exitStatus = 0;
}
else
{
exitStatus = errno;
printf("Failed: %s\n", dest);
perror("Creating hard link: ");
}
This is the function that creates the hard link. char *dest
is the ../Exercise.c
file and char *name
is the hard link name. And the hard link name or the dest doesn't point to an empty string.