I have a program that reads from a file and writes to a file. I'd like to prevent the user from specifying the same file for both (for obvious reasons). Lets say the first path is in char* path1
and the second path is in char* path2
. can I fopen()
both paths, call fileno()
on each and get the same number?
To explain more clearly:
char* path1 = "/asdf"
char* path2 = "/asdf"
FILE* f1 = fopen(path1, "r");
FILE* f2 = fopen(path2, "w");
int fd1 = fileno(f1);
int fd2 = fileno(f2);
if(fd1 == fd2) {
printf("These are the same file, you really shouldn't do this\n");
}
EDIT:
I do not want to compare filenames because one could easily defeat that with paths like /asdf/./asdf
or by using symlinks. Ultimately, I do not want to write my output into the file that I'm reading from (could cause serious issues).