To find out if a named file is already open on linux , you can scan the /proc/self/fd directory to see if the file is associated with a file descriptor. In the program below you can find a solution:
DIR *d = opendir("/proc/self/fd"); if (d) { struct dirent *entry; struct dirent *result; entry = malloc(sizeof(struct dirent) + NAME_MAX + 1); result = 0; while (readdir_r(d, entry, &result) == 0) { if (result == 0) break; if (isdigit(result->d_name[0])) { char path[NAME_MAX+1]; char buf[NAME_MAX+1]; snprintf(path, sizeof(path), "/proc/self/fd/%s", result->d_name); ssize_t bytes = readlink(path, buf, sizeof(buf)); buf[bytes] = '\0'; if (strcmp(file_of_interest, buf) == 0) break; } } free(entry); closedir(d); if (result) return FILE_IS_FOUND; } return FILE_IS_NOT_FOUND;
From your comment, it seems that you want to get the existing FILE * if it was already created by a previous call to fopen() in the file. The mechanism provided by the standard C library is not used to repeat all currently open FILE * . If there was such a mechanism, you could get its file descriptor using fileno() , and then request /proc/self/fd/# using readlink() , as shown above.
This means that you will need to use a data structure to manage public FILE * s. You will probably find a hash table using the file name as the key.
jxh
source share