DIRECTORY(3) DIRECTORY(3) NAME opendir, readdir, telldir, seekdir, rewinddir, closedir - directory operations SYNOPSIS #include #include DIR *opendir(filename) char *filename; struct direct *readdir(dirp) DIR *dirp; long telldir(dirp) DIR *dirp; seekdir(dirp, loc) DIR *dirp; long loc; rewinddir(dirp) DIR *dirp; closedir(dirp) DIR *dirp; DESCRIPTION _O_p_e_n_d_i_r opens the directory named by _f_i_l_e_n_a_m_e and associates a _d_i_r_e_c_‐ _t_o_r_y _s_t_r_e_a_m with it. _O_p_e_n_d_i_r returns a pointer to be used to identify the _d_i_r_e_c_t_o_r_y _s_t_r_e_a_m in subsequent operations. The pointer NULL is returned if _f_i_l_e_n_a_m_e cannot be accessed, or if it cannot _m_a_l_l_o_c(3) enough memory to hold the whole thing. _R_e_a_d_d_i_r returns a pointer to the next directory entry. It returns NULL upon reaching the end of the directory or detecting an invalid _s_e_e_k_d_i_r operation. _T_e_l_l_d_i_r returns the current location associated with the named _d_i_r_e_c_‐ _t_o_r_y _s_t_r_e_a_m_. _S_e_e_k_d_i_r sets the position of the next _r_e_a_d_d_i_r operation on the _d_i_r_e_c_‐ _t_o_r_y _s_t_r_e_a_m_. The new position reverts to the one associated with the _d_i_r_e_c_t_o_r_y _s_t_r_e_a_m when the _t_e_l_l_d_i_r operation was performed. Values returned by _t_e_l_l_d_i_r are good only for the lifetime of the DIR pointer from which they are derived. If the directory is closed and then reopened, the _t_e_l_l_d_i_r value may be invalidated due to undetected direc‐ tory compaction. It is safe to use a previous _t_e_l_l_d_i_r value immedi‐ ately after a call to _o_p_e_n_d_i_r and before any calls to _r_e_a_d_d_i_r_. _R_e_w_i_n_d_d_i_r resets the position of the named _d_i_r_e_c_t_o_r_y _s_t_r_e_a_m to the beginning of the directory. _C_l_o_s_e_d_i_r closes the named _d_i_r_e_c_t_o_r_y _s_t_r_e_a_m and frees the structure associated with the DIR pointer. Sample code which searchs a directory for entry ‘‘name’’ is: len = strlen(name); dirp = opendir("."); for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) if (dp->d_namlen == len && !strcmp(dp->d_name, name)) { closedir(dirp); return FOUND; } closedir(dirp); return NOT_FOUND; SEE ALSO open(2), close(2), read(2), lseek(2), dir(5) 4.2 Berkeley Distribution September 24, 1985 DIRECTORY(3)