]> git.sur5r.net Git - cc65/blob - libsrc/apple2/readdir.c
Update from Oliver Schmidt
[cc65] / libsrc / apple2 / readdir.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                readdir.c                                  */
4 /*                                                                           */
5 /*                           Read directory entry                            */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2005  Oliver Schmidt, <ol.sc@web.de>                                  */
10 /*                                                                           */
11 /*                                                                           */
12 /* This software is provided 'as-is', without any expressed or implied       */
13 /* warranty.  In no event will the authors be held liable for any damages    */
14 /* arising from the use of this software.                                    */
15 /*                                                                           */
16 /* Permission is granted to anyone to use this software for any purpose,     */
17 /* including commercial applications, and to alter it and redistribute it    */
18 /* freely, subject to the following restrictions:                            */
19 /*                                                                           */
20 /* 1. The origin of this software must not be misrepresented; you must not   */
21 /*    claim that you wrote the original software. If you use this software   */
22 /*    in a product, an acknowledgment in the product documentation would be  */
23 /*    appreciated but is not required.                                       */
24 /* 2. Altered source versions must be plainly marked as such, and must not   */
25 /*    be misrepresented as being the original software.                      */
26 /* 3. This notice may not be removed or altered from any source              */
27 /*    distribution.                                                          */
28 /*                                                                           */
29 /*****************************************************************************/
30
31
32
33 #include <stddef.h>
34 #include <unistd.h>
35 #include <dirent.h>
36 #include <dir.h>
37
38
39
40 /*****************************************************************************/
41 /*                                   Code                                    */
42 /*****************************************************************************/
43
44
45
46 struct dirent* __fastcall__ readdir (DIR* dir)
47 {
48     unsigned char* entry;
49
50     /* Search for the next active directory entry */
51     do {
52
53         /* Read next directory block if necessary */
54         if (dir->current_entry == dir->entries_per_block) {
55             if (read (dir->fd,
56                       dir->block.bytes,
57                       sizeof (dir->block)) != sizeof (dir->block)) {
58
59                 /* Just return failure as read() has */
60                 /* set errno if (and only if) no EOF */
61                 return NULL;
62             }
63
64             /* Start with first entry in next block */
65             dir->current_entry = 0;
66         }
67
68         /* Compute pointer to current entry */
69         entry = dir->block.content.entries +
70                 dir->current_entry * dir->entry_length;
71
72         /* Switch to next entry */
73         ++dir->current_entry;
74     } while (entry[0] == 0);
75
76     /* Move creation date/time to allow for next step below */
77     *(unsigned long*)&entry[0x1A] = *(unsigned long*)&entry[0x18];
78
79     /* Feature unsigned long access to EOF by extending from 3 to 4 bytes */
80     entry[0x18] = 0;
81
82     /* Move file type to allow for next step below */
83     entry[0x19] = entry[0x10];
84
85     /* Zero-terminate file name */
86     entry[0x01 + (entry[0x00] & 0x0F)] = 0;
87
88     /* Return success */
89     return (struct dirent*)&entry[0x01];
90 }