DIRECTORY(3X) UNIX Programmer's Manual DIRECTORY(3X) NAME opendir, readdir, telldir, seekdir, rewinddir, closedir - directory operations SYNOPSIS #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; cc ... -lndir HP-UX COMPATIBILITY Level: HP-UX/STANDARD Origin: 4.2 BSD DESCRIPTION The purpose of this library package is to provide functions which allow programs to read directory entries without hav- ing to know the actual directory format associated with the file system. This allows programs to be ported from one file system to another. Therefore, this is the recommended way to read directory entries. _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 opera- tions. The pointer NULL is returned if _f_i_l_e_n_a_m_e cannot be accessed, if _f_i_l_e_n_a_m_e is not a directory, or if sufficient memory cannot be allocated for a buffer of size DIRBLKSIZ blocks (see HARDWARE DEPENDENCIES). _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, in bytes, 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. _L_o_c is a byte offset within the direc- tory file. 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 re-opened, the _t_e_l_l_d_i_r value may be invalidated due to undetected directory compac- tion. 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 causes the named _d_i_r_e_c_t_o_r_y _s_t_r_e_a_m to be closed, and the structure associated with the DIR pointer to be freed. See /_u_s_r/_i_n_c_l_u_d_e/_n_d_i_r._h for a description of the fields available in a directory entry. The preferred way to search the current 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; When compiling using _c_c(1), this library is accessed by specifying -lndir as the last argument to the compile com- mand line, as in cc -o prog prog.c -lndir HARDWARE DEPENDENCIES Series 200: _M_a_l_l_o_c(3C) is used to allocate memory. Series 500: _M_e_m_a_l_l_c(2) is used to allocate memory. These libraries currently cannot be used to read direc- tories while deleting entries at the same time. This is because when a directory entry is deleted, the last entry in the directory is moved to the newly-created hole. Thus, you will not see the moved directory entry with these libraries. SEE ALSO /usr/include/ndir.h, close(2), lseek(2), open(2), read(2).