r6831@lvps87-230-33-50: verhaegs | 2008-02-03 14:08:57 +0100
[tangerine.git] / compiler / clib / readdir.c
blob3f06e92396fc15d5b13ffbff121896b1082fbb30
1 /*
2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX function readdir().
6 */
8 #include "__arosc_privdata.h"
10 #include <dos/dos.h>
11 #include <proto/dos.h>
13 #include <string.h>
14 #include <errno.h>
16 #include "__errno.h"
17 #include "__open.h"
18 #include "__upath.h"
20 /*****************************************************************************
22 NAME */
23 #include <dirent.h>
25 struct dirent *readdir(
27 /* SYNOPSIS */
28 DIR *dir)
30 /* FUNCTION
31 Reads a directory
33 INPUTS
34 dir - the directory stream pointing to the directory being read
36 RESULT
37 The readdir() function returns a pointer to a dirent
38 structure, or NULL if an error occurs or end-of-file is
39 reached.
41 The data returned by readdir() is overwritten by subse­
42 quent calls to readdir() for the same directory stream.
44 According to POSIX, the dirent structure contains a field
45 char d_name[] of unspecified size, with at most NAME_MAX
46 characters preceding the terminating null character. Use
47 of other fields will harm the portability of your pro­
48 grams.
50 NOTES
52 EXAMPLE
54 BUGS
56 SEE ALSO
57 read(), opendir(), closedir(), rewinddir(), seekdir(),
58 telldir(), scandir()
60 INTERNALS
62 ******************************************************************************/
64 int const max = MAXFILENAMELENGTH > NAME_MAX ? NAME_MAX : MAXFILENAMELENGTH;
65 fdesc *desc;
67 if (!dir)
69 errno = EFAULT;
70 return NULL;
73 desc = __getfdesc(dir->fd);
74 if (!desc)
76 errno = EBADF;
77 return NULL;
80 if (__doupath && dir->pos == 0)
82 dir->ent.d_name[0]='.';
83 dir->ent.d_name[1]='\0';
85 else
86 if (__doupath && dir->pos == 1)
88 dir->ent.d_name[0]='.';
89 dir->ent.d_name[1]='.';
90 dir->ent.d_name[2]='\0';
92 else
93 while (TRUE)
95 if (!ExNext(desc->fh, dir->priv))
97 dir->pos--;
98 if (IoErr() != ERROR_NO_MORE_ENTRIES)
99 errno = IoErr2errno(IoErr());
101 return NULL;
103 else
105 CONST_STRPTR name = ((struct FileInfoBlock *)dir->priv)->fib_FileName;
107 if (__doupath && name[0] == '.')
109 if (name[1] == '.')
111 if (name[2] == '\0')
112 continue;
114 else
115 if (name[1] == '\0')
116 continue;
119 strncpy(dir->ent.d_name, name, max);
121 break;
125 dir->pos++;
126 return &(dir->ent);