"SfR Fresh" - the SfR Freeware/Shareware Archive 
Member "lha-114i/src/lhdir.c" of archive lha-114i.tar.gz:
As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers.
Alternatively you can here view or download the uninterpreted source code file.
That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
1 /* ------------------------------------------------------------------------ */
2 /* LHa for UNIX */
3 /* lhdir.c -- Directory access routine */
4 /* */
5 /* Copyright (C) MCMLXXXIX Yooichi.Tagawa */
6 /* Modified Nobutaka Watazaki */
7 /* */
8 /* Emulate opendir(),readdir(),closedir() function for LHarc */
9 /* */
10 /* Ver. 0.00 Original 1988.05.31 Y.Tagawa */
11 /* Ver. 0.03 Release #3 for LHarc UNIX 1988.07.02 Y.Tagawa */
12 /* Ver. 1.00 Fixed 1989.09.22 Y.Tagawa */
13 /* Ver. 1.14 Source All chagned 1995.01.14 N.Watazaki */
14 /* ------------------------------------------------------------------------ */
15 #include "lha.h"
16
17 /* Where is O_RDONLY ? (^_^) */
18 #include <sys/file.h>
19
20 #ifndef O_RDONLY
21 #include <fcntl.h>
22 #endif
23
24 #define direct old_direct
25
26 #include <sys/dir.h>
27
28 #undef direct
29
30 #ifndef DIRSIZ
31 /* Warning : Are you sure? (normally defined in <sys/dir.h> */
32 #define DIRSIZ 14
33 #endif
34
35 #include "lhdir.h"
36
37 /* ------------------------------------------------------------------------ */
38 DIR *
39 opendir(name)
40 char *name;
41 {
42 register DIR *dirp;
43 register int fd;
44
45 if ((fd = open(name, O_RDONLY)) >= 0) {
46 if ((dirp = (DIR *) malloc(sizeof(DIR))) != (DIR *) 0) {
47 dirp->dd_fd = fd;
48 dirp->dd_loc = 0;
49 dirp->dd_size = 0;
50 return dirp;
51 }
52
53 close(fd);
54 }
55
56 return (DIR *) 0;
57 }
58
59 /* ------------------------------------------------------------------------ */
60 struct direct *
61 readdir(dirp)
62 register DIR *dirp;
63 {
64 static struct direct lhdir;
65 register struct old_direct *dp;
66
67 do {
68 if (dirp->dd_loc >= dirp->dd_size) {
69 dirp->dd_loc = 0;
70 if ((dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
71 return (struct direct *) 0;
72 }
73
74 dp = (struct old_direct *) (dirp->dd_buf + dirp->dd_loc);
75
76 if (dirp->dd_loc + sizeof(struct old_direct) > dirp->dd_size)
77 return (struct direct *) 0;
78
79 dirp->dd_loc += sizeof(struct old_direct);
80
81 } while (dp->d_ino == 0);
82
83 /* construct new format */
84 lhdir.d_ino = dp->d_ino;
85 strncpy(lhdir.d_name, dp->d_name, DIRSIZ);
86 lhdir.d_name[DIRSIZ] = '\0';
87 lhdir.d_namlen = strlen(lhdir.d_name);
88
89 return &lhdir;
90 }
91
92 /* ------------------------------------------------------------------------ */
93 closedir(dirp)
94 DIR *dirp;
95 {
96 close(dirp->dd_fd);
97 free(dirp);
98 }