[3ae31e9] | 1 | /*
|
---|
| 2 | =============================================================================
|
---|
| 3 | dir.c - directory routines similar to unix 4.2 BSD
|
---|
| 4 | Version 1 -- 1988-12-08 -- D.N. Lynx Crowe
|
---|
| 5 |
|
---|
| 6 | Modified versions of Usenet public domain source.
|
---|
| 7 | Original author unknown.
|
---|
| 8 | =============================================================================
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 | #include "errno.h"
|
---|
| 12 | #include "dir.h"
|
---|
| 13 |
|
---|
| 14 | extern int errno;
|
---|
| 15 | extern char *calloc();
|
---|
| 16 |
|
---|
| 17 | /* |
---|
| 18 |
|
---|
| 19 | */
|
---|
| 20 |
|
---|
| 21 | /*
|
---|
| 22 | =============================================================================
|
---|
| 23 | opendir() - opens given directory and reads its contents into memory.
|
---|
| 24 | Other functions use this data or the DIR structure to do their work.
|
---|
| 25 | =============================================================================
|
---|
| 26 | */
|
---|
| 27 |
|
---|
| 28 | DIR *
|
---|
| 29 | opendir(dirname)
|
---|
| 30 | char *dirname;
|
---|
| 31 | {
|
---|
| 32 | struct direct **namelist;
|
---|
| 33 | DIR *dirptr;
|
---|
| 34 |
|
---|
| 35 | dirptr = (DIR *)calloc(1, sizeof(DIR));
|
---|
| 36 |
|
---|
| 37 | if(dirptr == (DIR *)0) {
|
---|
| 38 |
|
---|
| 39 | errno = ENOMEM;
|
---|
| 40 | return((DIR *)0);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | dirptr->d_magic = DMAGIC;
|
---|
| 44 | dirptr->d_pos = 0;
|
---|
| 45 | dirptr->d_length = scandir(dirname, &(dirptr->namelist),
|
---|
| 46 | (int (*)())0, (int (*)())0);
|
---|
| 47 |
|
---|
| 48 | if(dirptr->d_length < 0) {
|
---|
| 49 |
|
---|
| 50 | free((char *)dirptr);
|
---|
| 51 | return((DIR *)0);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | return(dirptr);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /* |
---|
| 58 |
|
---|
| 59 | */
|
---|
| 60 |
|
---|
| 61 | /*
|
---|
| 62 | =============================================================================
|
---|
| 63 | readdir - returns the next directory structure from the list and
|
---|
| 64 | updates values in the DIR structure.
|
---|
| 65 | =============================================================================
|
---|
| 66 | */
|
---|
| 67 |
|
---|
| 68 | struct direct *
|
---|
| 69 | readdir(dirptr)
|
---|
| 70 | DIR *dirptr;
|
---|
| 71 | {
|
---|
| 72 | if(dirptr->d_magic != DMAGIC) {
|
---|
| 73 |
|
---|
| 74 | errno = ENOTDIR;
|
---|
| 75 | return((struct direct *)0);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | if(dirptr->d_pos >= dirptr->d_length) {
|
---|
| 79 |
|
---|
| 80 | errno = ENFILE;
|
---|
| 81 | return((struct direct *)0);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | return(dirptr->namelist[dirptr->d_pos++]);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /* |
---|
| 88 |
|
---|
| 89 | */
|
---|
| 90 |
|
---|
| 91 | /*
|
---|
| 92 | =============================================================================
|
---|
| 93 | telldir - return the current position of the directory.
|
---|
| 94 | =============================================================================
|
---|
| 95 | */
|
---|
| 96 |
|
---|
| 97 | long
|
---|
| 98 | telldir(dirptr)
|
---|
| 99 | DIR *dirptr;
|
---|
| 100 | {
|
---|
| 101 | if(dirptr->d_magic != DMAGIC) {
|
---|
| 102 |
|
---|
| 103 | errno = ENOTDIR;
|
---|
| 104 | return(-1L);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | return((long)dirptr->d_pos);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /* |
---|
| 111 |
|
---|
| 112 | */
|
---|
| 113 |
|
---|
| 114 | /*
|
---|
| 115 | =============================================================================
|
---|
| 116 | seekdir - position the given DIR stream to position given.
|
---|
| 117 | =============================================================================
|
---|
| 118 | */
|
---|
| 119 |
|
---|
| 120 | seekdir(dirptr, loc)
|
---|
| 121 | DIR *dirptr;
|
---|
| 122 | long loc;
|
---|
| 123 | {
|
---|
| 124 | if(dirptr->d_magic != DMAGIC) {
|
---|
| 125 |
|
---|
| 126 | errno = ENOTDIR;
|
---|
| 127 | return(-1);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | if(loc > (long)dirptr->d_length) {
|
---|
| 131 |
|
---|
| 132 | errno = EINVAL;
|
---|
| 133 | return(-1);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | dirptr->d_pos = (int)loc;
|
---|
| 137 | return(0);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /* |
---|
| 141 |
|
---|
| 142 | */
|
---|
| 143 |
|
---|
| 144 | /*
|
---|
| 145 | =============================================================================
|
---|
| 146 | rewinddir - rewind given DIR to beginning
|
---|
| 147 | =============================================================================
|
---|
| 148 | */
|
---|
| 149 |
|
---|
| 150 | rewinddir(dirptr)
|
---|
| 151 | DIR *dirptr;
|
---|
| 152 | {
|
---|
| 153 | if(dirptr->d_magic != DMAGIC) {
|
---|
| 154 |
|
---|
| 155 | errno = ENOTDIR;
|
---|
| 156 | return(-1);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | dirptr->d_pos = 0;
|
---|
| 160 | return(0);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /* |
---|
| 164 |
|
---|
| 165 | */
|
---|
| 166 |
|
---|
| 167 | /*
|
---|
| 168 | =============================================================================
|
---|
| 169 | closedir - close given directory. destroy given DIR struct so we
|
---|
| 170 | know it is closed.
|
---|
| 171 | =============================================================================
|
---|
| 172 | */
|
---|
| 173 |
|
---|
| 174 | closedir(dirptr)
|
---|
| 175 | DIR *dirptr;
|
---|
| 176 | {
|
---|
| 177 | if(dirptr->d_magic != DMAGIC) {
|
---|
| 178 |
|
---|
| 179 | errno = ENOTDIR;
|
---|
| 180 | return(-1);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | dirptr->d_magic = ~DMAGIC; /* mess it up a little */
|
---|
| 184 | freedir(dirptr->namelist);
|
---|
| 185 | free(dirptr);
|
---|
| 186 | return(0);
|
---|
| 187 | }
|
---|