| 1 | DIRECTORY(3) BSD Programmer's Manual DIRECTORY(3) |
| 2 | |
| 3 | N\bNA\bAM\bME\bE |
| 4 | o\bop\bpe\ben\bnd\bdi\bir\br, r\bre\bea\bad\bdd\bdi\bir\br, t\bte\bel\bll\bld\bdi\bir\br, s\bse\bee\bek\bkd\bdi\bir\br, r\bre\bew\bwi\bin\bnd\bdd\bdi\bir\br, c\bcl\blo\bos\bse\bed\bdi\bir\br, d\bdi\bir\brf\bfd\bd - directo- |
| 5 | ry operations |
| 6 | |
| 7 | S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS |
| 8 | #\b#i\bin\bnc\bcl\blu\bud\bde\be <\b<s\bsy\bys\bs/\b/t\bty\byp\bpe\bes\bs.\b.h\bh>\b> |
| 9 | #\b#i\bin\bnc\bcl\blu\bud\bde\be <\b<d\bdi\bir\bre\ben\bnt\bt.\b.h\bh>\b> |
| 10 | |
| 11 | _\bD_\bI_\bR _\b* |
| 12 | o\bop\bpe\ben\bnd\bdi\bir\br(_\bc_\bo_\bn_\bs_\bt _\bc_\bh_\ba_\br _\b*_\bf_\bi_\bl_\be_\bn_\ba_\bm_\be); |
| 13 | |
| 14 | _\bs_\bt_\br_\bu_\bc_\bt _\bd_\bi_\br_\be_\bn_\bt _\b* |
| 15 | r\bre\bea\bad\bdd\bdi\bir\br(_\bD_\bI_\bR _\b*_\bd_\bi_\br_\bp); |
| 16 | |
| 17 | _\bl_\bo_\bn_\bg |
| 18 | t\bte\bel\bll\bld\bdi\bir\br(_\bc_\bo_\bn_\bs_\bt _\bD_\bI_\bR _\b*_\bd_\bi_\br_\bp); |
| 19 | |
| 20 | _\bv_\bo_\bi_\bd |
| 21 | s\bse\bee\bek\bkd\bdi\bir\br(_\bD_\bI_\bR _\b*_\bd_\bi_\br_\bp, _\bl_\bo_\bn_\bg _\bl_\bo_\bc); |
| 22 | |
| 23 | _\bv_\bo_\bi_\bd |
| 24 | r\bre\bew\bwi\bin\bnd\bdd\bdi\bir\br(_\bD_\bI_\bR _\b*_\bd_\bi_\br_\bp); |
| 25 | |
| 26 | _\bi_\bn_\bt |
| 27 | c\bcl\blo\bos\bse\bed\bdi\bir\br(_\bD_\bI_\bR _\b*_\bd_\bi_\br_\bp); |
| 28 | |
| 29 | _\bi_\bn_\bt |
| 30 | d\bdi\bir\brf\bfd\bd(_\bD_\bI_\bR _\b*_\bd_\bi_\br_\bp); |
| 31 | |
| 32 | D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN |
| 33 | The o\bop\bpe\ben\bnd\bdi\bir\br() function opens the directory named by _\bf_\bi_\bl_\be_\bn_\ba_\bm_\be, associates |
| 34 | a _\bd_\bi_\br_\be_\bc_\bt_\bo_\br_\by _\bs_\bt_\br_\be_\ba_\bm with it and returns a pointer to be used to identify |
| 35 | the _\bd_\bi_\br_\be_\bc_\bt_\bo_\br_\by _\bs_\bt_\br_\be_\ba_\bm in subsequent operations. The pointer NULL is re- |
| 36 | turned if _\bf_\bi_\bl_\be_\bn_\ba_\bm_\be cannot be accessed, or if it cannot malloc(3) enough |
| 37 | memory to hold the whole thing. |
| 38 | |
| 39 | The r\bre\bea\bad\bdd\bdi\bir\br() function returns a pointer to the next directory entry. It |
| 40 | returns NULL upon reaching the end of the directory or detecting an in- |
| 41 | valid s\bse\bee\bek\bkd\bdi\bir\br() operation. |
| 42 | |
| 43 | The t\bte\bel\bll\bld\bdi\bir\br() function returns the current location associated with the |
| 44 | named _\bd_\bi_\br_\be_\bc_\bt_\bo_\br_\by _\bs_\bt_\br_\be_\ba_\bm. |
| 45 | |
| 46 | The s\bse\bee\bek\bkd\bdi\bir\br() function sets the position of the next r\bre\bea\bad\bdd\bdi\bir\br() operation |
| 47 | on the _\bd_\bi_\br_\be_\bc_\bt_\bo_\br_\by _\bs_\bt_\br_\be_\ba_\bm. The new position reverts to the one associated |
| 48 | with the _\bd_\bi_\br_\be_\bc_\bt_\bo_\br_\by _\bs_\bt_\br_\be_\ba_\bm when the t\bte\bel\bll\bld\bdi\bir\br() operation was performed. |
| 49 | Values returned by t\bte\bel\bll\bld\bdi\bir\br() are good only for the lifetime of the DIR |
| 50 | pointer, _\bd_\bi_\br_\bp, from which they are derived. If the directory is closed |
| 51 | and then reopened, the t\bte\bel\bll\bld\bdi\bir\br() value may be invalidated due to unde- |
| 52 | tected directory compaction. It is safe to use a previous t\bte\bel\bll\bld\bdi\bir\br() val- |
| 53 | ue immediately after a call to o\bop\bpe\ben\bnd\bdi\bir\br() and before any calls to |
| 54 | r\bre\bea\bad\bdd\bdi\bir\br(). |
| 55 | |
| 56 | The r\bre\bew\bwi\bin\bnd\bdd\bdi\bir\br() function resets the position of the named _\bd_\bi_\br_\be_\bc_\bt_\bo_\br_\by |
| 57 | _\bs_\bt_\br_\be_\ba_\bm to the beginning of the directory. |
| 58 | |
| 59 | The c\bcl\blo\bos\bse\bed\bdi\bir\br() function closes the named _\bd_\bi_\br_\be_\bc_\bt_\bo_\br_\by _\bs_\bt_\br_\be_\ba_\bm and frees the |
| 60 | structure associated with the _\bd_\bi_\br_\bp pointer, returning 0 on success. On |
| 61 | failure, -1 is returned and the global variable _\be_\br_\br_\bn_\bo is set to indicate |
| 62 | the error. |
| 63 | |
| 64 | The d\bdi\bir\brf\bfd\bd() function returns the integer file descriptor associated with |
| 65 | the named _\bd_\bi_\br_\be_\bc_\bt_\bo_\br_\by _\bs_\bt_\br_\be_\ba_\bm, see open(2). |
| 66 | |
| 67 | Sample code which searchs a directory for entry ``name'' is: |
| 68 | |
| 69 | len = strlen(name); |
| 70 | dirp = opendir("."); |
| 71 | while ((dp = readdir(dirp)) != NULL) |
| 72 | if (dp->d_namlen == len && !strcmp(dp->d_name, name)) { |
| 73 | (void)closedir(dirp); |
| 74 | return FOUND; |
| 75 | } |
| 76 | (void)closedir(dirp); |
| 77 | return NOT_FOUND; |
| 78 | |
| 79 | S\bSE\bEE\bE A\bAL\bLS\bSO\bO |
| 80 | open(2), close(2), read(2), lseek(2), dir(5) |
| 81 | |
| 82 | H\bHI\bIS\bST\bTO\bOR\bRY\bY |
| 83 | The o\bop\bpe\ben\bnd\bdi\bir\br(), r\bre\bea\bad\bdd\bdi\bir\br(), t\bte\bel\bll\bld\bdi\bir\br(), s\bse\bee\bek\bkd\bdi\bir\br(), r\bre\bew\bwi\bin\bnd\bdd\bdi\bir\br(), c\bcl\blo\bos\bse\bed\bdi\bir\br(), |
| 84 | and d\bdi\bir\brf\bfd\bd() functions appeared in 4.2BSD. |
| 85 | |
| 86 | 4.2 Berkeley Distribution June 4, 1993 2 |