summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/posixdir.c
blob: 81f83a2afb6185a2dbf2810e99067bbdc7a8b5a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
**  POSIXDIR.C - POSIX-style directory processing
**
**  Original Copyright 1988-1991 by Bob Stout as part of
**  the MicroFirm Function Library (MFL)
**
**  This subset version is functionally identical to the
**  version originally published by the author in Tech Specialist
**  magazine and is hereby donated to the public domain.
*/

#include <string.h>
#include <stdlib.h>
#include <dos.h>
#include <io.h>
#include <errno.h>
#include "dirent.h"

#define _NDIRS 20
#define SUCCESS 0
#define ERROR  -1

#define LAST_CHAR(s) ((char *)(s))[strlen(s) - 1]

int DFerr;
DOS_DIR _DIRS[_NDIRS];  /* Initilize DOS_DIR array to zeros     */

/*
**  Convert Unix-style pathnames to DOS-style
*/

static char *unix2dos(char *path)
{
      char *p;

      while (NULL != (p = strchr(path, '/')))
            *p = '\\';
      return path;
}

/****************************************************************/
/*                                                              */
/*  opendir()                                                   */
/*                                                              */
/*  Function: Open a directory for reading.                     */
/*                                                              */
/*  Parameters: 1 - Directory name. May include path spec.      */
/*                                                              */
/*  Returns: Pointer to a DOS_DIR typedef'ed structure, similar */
/*           to fopen() returning a FILE pointer.               */
/*                                                              */
/*           NULL if error, DFerr set as follows:               */
/*           SUCCESS - No error                                 */
/*           ENOENT  - Could not locate directory or contents   */
/*           ENOTDIR - Not a directory                          */
/*           ENOMEM  - Too many directories already open        */
/*                                                              */
/*  Side effects: The dd_size element of the DOS_DIR structure  */
/*                will contain a number representing the total  */
/*                number of entries in the directory. The       */
/*                dd_loc element will be set to zero since      */
/*                no elements have, as yet, been read.          */
/*                                                              */
/****************************************************************/

DOS_DIR *opendir(char *fname)
{
      int i;
      unsigned n = 0;
      char nametmp[13], *p;
      struct DSTRUCT dstruct;

      for (i = 0; i < _NDIRS; ++i)
      {
            if (!_DIRS[i].dd_fd)
                  break;
      }
      if (_NDIRS <= i)
      {
            DFerr = ENOMEM;
            return NULL;
      }

      unix2dos(fname);
      if (':' == fname[1] && 1 < strlen(fname))
            p = &fname[2];
      else  p = fname;
      while ('\\' == LAST_CHAR(p) && 1 < strlen(p))
            LAST_CHAR(p) = '\0';
        
      if (strcmp(p, "\\") && strlen(p))
      {
            if (NULL == (rfind_1st(fname, FA_ANY, &_DIRS[i].dd_buf)))
            {
                  DFerr = ENOENT;
                  return NULL;
            }
            if (!(FA_DIREC & _DIRS[i].dd_buf.ATTRIBUTE))
            {
                  DFerr = ENOTDIR;
                  return NULL;
            }
      }
      strcpy(_DIRS[i].dd_dirname, fname);
      if (!strlen(p))
            strcat(_DIRS[i].dd_dirname, ".");
      if ('\\' != LAST_CHAR(_DIRS[i].dd_dirname))
            strcat(_DIRS[i].dd_dirname, "\\");
      strcat(strupr(_DIRS[i].dd_dirname), "*.*");
      if (NULL == rfind_1st(_DIRS[i].dd_dirname, FA_ANY, &_DIRS[i].dd_buf))
      {
            DFerr = ENOENT;
            return NULL;
      }
      memcpy(&dstruct, &_DIRS[i].dd_buf, sizeof(struct DSTRUCT));
      do
      {
            ++n;
      } while (rfind_nxt(&_DIRS[i].dd_buf));
      memcpy(&_DIRS[i].dd_buf, &dstruct, sizeof(struct DSTRUCT));
      _DIRS[i].dd_size = n;
      _DIRS[i].dd_loc  = 0;
      _DIRS[i].dd_fd   = i + 1;
      DFerr = SUCCESS;
      return &_DIRS[i];
}

/****************************************************************/
/*                                                              */
/*  closedir()                                                  */
/*                                                              */
/*  Function: Close a preeviously opened directory.             */
/*                                                              */
/*  Parameters: 1 - DOS_DIR pointer of directory to close.      */
/*                                                              */
/*  Returns: SUCCESS or ERROR.                                  */
/*                                                              */
/****************************************************************/

int closedir(DOS_DIR *dirp)
{
      if (0 == dirp->dd_fd || _NDIRS < dirp->dd_fd)
      {
            DFerr = EBADF;
            return ERROR;
      }
      memset(dirp, 0, sizeof(DOS_DIR));
      return SUCCESS;
}

/****************************************************************/
/*                                                              */
/*  rewinddir()                                                 */
/*                                                              */
/*  Function: Reset an open DOS_DIR to its first entry.         */
/*                                                              */
/*  Parameters: 1 - DOS_DIR pointer of directory to rewind.     */
/*                                                              */
/*  Returns: SUCCESS or ERROR.                                  */
/*                                                              */
/****************************************************************/

int rewinddir(DOS_DIR *dirp)
{
      if (0 == dirp->dd_fd || _NDIRS < dirp->dd_fd)
      {
            DFerr = EBADF;
            return ERROR;
      }
      rfind_1st(dirp->dd_dirname, FA_ANY, &(dirp->dd_buf));
      dirp->dd_loc = 0;
      return SUCCESS;
}

/****************************************************************/
/*                                                              */
/*  seekdir()                                                   */
/*                                                              */
/*  Function: Point to a selected entry in a DOS_DIR.           */
/*                                                              */
/*  Parameters: 1 - DOS_DIR pointer of directory to rewind.     */
/*              2 - Offset of entry to seek                     */
/*              3 - Origin of offset                            */
/*                                                              */
/*  Returns: A DSTRUCT pointer, same as returned by rfind_1st() */
/*           and rfind_nxt().                                   */
/*                                                              */
/*           NULL if error, DFerr set as follows:               */
/*           SUCCESS - No error                                 */
/*           EBADF   - Bad file (DOS_DIR) pointer               */
/*           EACCES  - Illegal origin specification             */
/*           EOF     - Attempt to seek past end of directory    */
/*                                                              */
/****************************************************************/

struct DSTRUCT *seekdir(DOS_DIR *dirp, int offset, int origin)
{
      int i, loc;

      if (0 == dirp->dd_fd || _NDIRS < dirp->dd_fd)
      {
            DFerr = EBADF;
            return NULL;
      }
      switch (origin)
      {
      case SEEK_SET:
            loc = offset + 1;
            break;
      case SEEK_CUR:
            loc = dirp->dd_loc + offset;
            break;
      case SEEK_END:
            loc = dirp->dd_size + offset;
            break;
      default:
            DFerr = EACCES;
            return NULL;
      }

      if (loc > (int)dirp->dd_size || 0 >= loc)
      {
            DFerr = EOF;
            return NULL;
      }

      rewinddir(dirp);
      for (i = 0; i < loc; ++i)
            readdir(dirp);

      DFerr = SUCCESS;
      return (&(dirp->dd_buf));
}

/****************************************************************/
/*                                                              */
/*  readdir()                                                   */
/*                                                              */
/*  Function: Reads entries from an open directory.             */
/*                                                              */
/*  Parameters: 1 - DOS_DIR pointer of directory to read.       */
/*                                                              */
/*  Returns: A DSTRUCT pointer, same as returned by rfind_1st() */
/*           and rfind_nxt().                                   */
/*                                                              */
/*           NULL if error, DFerr set as follows:               */
/*           SUCCESS - No error                                 */
/*           EBADF   - Bad file (DOS_DIR) pointer               */
/*           EOF     - Attempt to read past end of directory    */
/*                                                              */
/*  Side effects: The dd_loc element of the DOS_DIR structure   */
/*                will contain a number representing which      */
/*                element of the directory was returned. It may */
/*                range from 1 to dd_size.                      */
/*                                                              */
/****************************************************************/

struct DSTRUCT *readdir(DOS_DIR *dirp)
{
      if (0 == dirp->dd_fd || _NDIRS < dirp->dd_fd)
      {
            DFerr = EBADF;
            return NULL;
      }
      if (0 == dirp->dd_loc || NULL != rfind_nxt(&(dirp->dd_buf)))
      {
            dirp->dd_loc += 1;
            DFerr = SUCCESS;
            return (&(dirp->dd_buf));
      }
      else
      {
            DFerr = EOF;
            return NULL;
      }
}