/* * read one file out of either /etc or the user's home directory. * fileToRead tells which file to read. * * return 1 if it's time to reset the fileToRead (no more files to read). */ int ReadOneFile(int fileToRead) { char *dir = "/etc"; DIR *fd = opendir(dir); int resetCount = 0; #ifdef SOLARIS /* grumble, Solaris does not define struct dirent to be the full length */ typedef union { unsigned char space[sizeof(struct dirent) + MAXNAMELEN]; struct dirent dir; } dirent_hack; dirent_hack entry, firstEntry; #define entry_dir entry.dir #else struct dirent entry, firstEntry; #define entry_dir entry #endif int i, error = -1; if (fd == NULL) { dir = getenv("HOME"); if (dir) { fd = opendir(dir); } } if (fd == NULL) { return 1; } for (i=0; i <= fileToRead; i++) { struct dirent *result = NULL; do { error = readdir_r(fd, &entry_dir, &result); } while (error == 0 && result != NULL && !ReadFileOK(dir,&result->d_name[0])); if (error != 0 || result == NULL) { resetCount = 1; /* read to the end, start again at the beginning */ if (i != 0) { /* ran out of entries in the directory, use the first one */ entry = firstEntry; error = 0; break; } /* if i== 0, there were no readable entries in the directory */ break; } if (i==0) { /* save the first entry in case we run out of entries */ firstEntry = entry; } } if (error == 0) { char filename[PATH_MAX]; int count = snprintf(filename, sizeof filename, "%s/%s",dir, &entry_dir.d_name[0]); if (count >= 1) { ReadSingleFile(filename); } } closedir(fd); return resetCount; }
/* * read one file out of either /etc or the user's home directory. * fileToRead tells which file to read. * * return 1 if it's time to reset the fileToRead (no more files to read). */ static int ReadOneFile(int fileToRead) { char *dir = "/etc"; DIR *fd = opendir(dir); int resetCount = 0; struct dirent *entry; #if defined(__sun) char firstName[256]; #else char firstName[NAME_MAX + 1]; #endif const char *name = NULL; int i; if (fd == NULL) { dir = PR_GetEnvSecure("HOME"); if (dir) { fd = opendir(dir); } } if (fd == NULL) { return 1; } firstName[0] = '\0'; for (i=0; i <= fileToRead; i++) { do { /* readdir() isn't guaranteed to be thread safe on every platform; * this code assumes the same directory isn't read concurrently. * This usage is confirmed safe on Linux, see bug 1254334. */ entry = readdir(fd); } while (entry != NULL && !ReadFileOK(dir, &entry->d_name[0])); if (entry == NULL) { resetCount = 1; /* read to the end, start again at the beginning */ if (firstName[0]) { /* ran out of entries in the directory, use the first one */ name = firstName; } break; } name = entry->d_name; if (i == 0) { /* copy the name of the first in case we run out of entries */ PORT_Assert(PORT_Strlen(name) < sizeof(firstName)); PORT_Strncpy(firstName, name, sizeof(firstName) - 1); firstName[sizeof(firstName) - 1] = '\0'; } } if (name) { char filename[PATH_MAX]; int count = snprintf(filename, sizeof(filename), "%s/%s",dir, name); if (count >= 1) { ReadSingleFile(filename); } } closedir(fd); return resetCount; }