This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
92c6923ac33676283d0a2158bf50f52ff01cf942
[perl5.git] / os2 / dir.h
1 /*
2  * @(#) dir.h 1.4 87/11/06   Public Domain.
3  *
4  *  A public domain implementation of BSD directory routines for
5  *  MS-DOS.  Written by Michael Rendell ({uunet,utai}michael@garfield),
6  *  August 1987
7  *
8  *  Enhanced and ported to OS/2 by Kai Uwe Rommel; added scandir() prototype
9  *  December 1989, February 1990
10  */
11
12
13 #define MAXNAMLEN  12
14 #define MAXPATHLEN 128
15
16 #define A_RONLY    0x01
17 #define A_HIDDEN   0x02
18 #define A_SYSTEM   0x04
19 #define A_LABEL    0x08
20 #define A_DIR      0x10
21 #define A_ARCHIVE  0x20
22
23
24 struct direct
25 {
26   ino_t d_ino;                   /* a bit of a farce */
27   int   d_reclen;                /* more farce */
28   int   d_namlen;                /* length of d_name */
29   char  d_name[MAXNAMLEN + 1];   /* null terminated */
30   long  d_size;                  /* size in bytes */
31   int   d_mode;                  /* DOS or OS/2 file attributes */
32 };
33
34 /* The fields d_size and d_mode are extensions by me (Kai Uwe Rommel).
35  * The find_first and find_next calls deliver this data without any extra cost.
36  * If this data is needed, these fields save a lot of extra calls to stat()
37  * (each stat() again performs a find_first call !).
38  */
39
40 struct _dircontents
41 {
42   char *_d_entry;
43   long _d_size;
44   int _d_mode;
45   struct _dircontents *_d_next;
46 };
47
48 typedef struct _dirdesc
49 {
50   int  dd_id;                   /* uniquely identify each open directory */
51   long dd_loc;                  /* where we are in directory entry is this */
52   struct _dircontents *dd_contents;   /* pointer to contents of dir */
53   struct _dircontents *dd_cp;         /* pointer to current position */
54 }
55 DIR;
56
57
58 extern DIR *opendir(char *);
59 extern struct direct *readdir(DIR *);
60 extern void seekdir(DIR *, long);
61 extern long telldir(DIR *);
62 extern void closedir(DIR *);
63 #define rewinddir(dirp) seekdir(dirp, 0L)
64
65 extern int scandir(char *, struct direct ***,
66                    int (*)(struct direct *),
67                    int (*)(struct direct *, struct direct *));
68
69 extern int getfmode(char *);
70 extern int setfmode(char *, unsigned);
71
72 /*
73 NAME
74      opendir, readdir, telldir, seekdir, rewinddir, closedir -
75      directory operations
76
77 SYNTAX
78      #include <sys/types.h>
79      #include <sys/dir.h>
80
81      DIR *opendir(filename)
82      char *filename;
83
84      struct direct *readdir(dirp)
85      DIR *dirp;
86
87      long telldir(dirp)
88      DIR *dirp;
89
90      seekdir(dirp, loc)
91      DIR *dirp;
92      long loc;
93
94      rewinddir(dirp)
95      DIR *dirp;
96
97      int closedir(dirp)
98      DIR *dirp;
99
100 DESCRIPTION
101      The opendir library routine opens the directory named by
102      filename and associates a directory stream with it.  A
103      pointer is returned to identify the directory stream in sub-
104      sequent operations.  The pointer NULL is returned if the
105      specified filename can not be accessed, or if insufficient
106      memory is available to open the directory file.
107
108      The readdir routine returns a pointer to the next directory
109      entry.  It returns NULL upon reaching the end of the direc-
110      tory or on detecting an invalid seekdir operation.  The
111      readdir routine uses the getdirentries system call to read
112      directories. Since the readdir routine returns NULL upon
113      reaching the end of the directory or on detecting an error,
114      an application which wishes to detect the difference must
115      set errno to 0 prior to calling readdir.
116
117      The telldir routine returns the current location associated
118      with the named directory stream. Values returned by telldir
119      are good only for the lifetime of the DIR pointer from which
120      they are derived.  If the directory is closed and then reo-
121      pened, the telldir value may be invalidated due to
122      undetected directory compaction.
123
124      The seekdir routine sets the position of the next readdir
125      operation on the directory stream. Only values returned by
126      telldir should be used with seekdir.
127
128      The rewinddir routine resets the position of the named
129      directory stream to the beginning of the directory.
130
131      The closedir routine closes the named directory stream and
132      returns a value of 0 if successful. Otherwise, a value of -1
133      is returned and errno is set to indicate the error.  All
134      resources associated with this directory stream are
135      released.
136
137 EXAMPLE
138      The following sample code searches a directory for the entry
139      name.
140
141      len = strlen(name);
142
143      dirp = opendir(".");
144
145      for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
146
147      if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
148
149                closedir(dirp);
150
151                return FOUND;
152
153           }
154
155      closedir(dirp);
156
157      return NOT_FOUND;
158
159
160 SEE ALSO
161      close(2), getdirentries(2), lseek(2), open(2), read(2),
162      dir(5)
163 */