3 * dostat.c - *stat() function common part
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
10 #include <proto/dos.h>
11 #include <proto/utility.h>
12 #include <libraries/usergroup.h>
14 #include <sys/types.h>
21 /* DOS 3.0 and MuFS extensions to file info block */
25 * Conversion table from Amiga filetypes to Unix filetypes
27 const static mode_t ftype
[ST_LINKDIR
- ST_PIPEFILE
+ 1] = {
41 * Conversion table from Amiga protections to Unix protections
44 const static UBYTE fbits
[16] =
46 00, 02, 01, 03, 02, 02, 03, 03,
47 04, 06, 05, 07, 06, 06, 07, 07,
50 void __dostat(struct FileInfoBlock
*fib
,
53 ULONG pbits
= fib
->fib_Protection
^ 0xf;
54 short fibtype
= fib
->fib_DirEntryType
- ST_PIPEFILE
;
59 else if (fibtype
> ST_LINKDIR
- ST_PIPEFILE
)
60 fibtype
= ST_LINKDIR
- ST_PIPEFILE
;
62 bzero(st
, sizeof(*st
));
64 mode
= ftype
[fibtype
] | (fbits
[pbits
& 0xf] << 6)
65 | (fbits
[(pbits
>> FIBB_GRP_DELETE
) & 0xf] << 3)
66 | fbits
[(pbits
>> FIBB_OTR_DELETE
) & 0xf];
68 if ((pbits
& FIBF_PURE
) != 0)
70 if ((pbits
& FIBF_SUID
) != 0)
72 if ((pbits
& FIBF_SGID
) != 0)
75 st
->st_ino
= fib
->fib_DiskKey
;
78 st
->st_uid
= MU2UG(fib
->fib_OwnerUID
);
79 st
->st_gid
= MU2UG(fib
->fib_OwnerGID
);
81 st
->st_size
= fib
->fib_Size
;
84 * Calculatory time since Jan 1 1970, UCT
85 * (in reality there are an odd number of leap seconds,
86 * which are not included)
88 st
->st_atime
= st
->st_ctime
= st
->st_mtime
=
89 60 * ((fib
->fib_Date
.ds_Days
+ (8*365+2)) * 24 * 60
90 + fib
->fib_Date
.ds_Minute
)
91 + fib
->fib_Date
.ds_Tick
/ 50;
94 st
->st_blocks
= fib
->fib_NumBlocks
;
95 st
->st_dosmode
= fib
->fib_Protection
;
96 st
->st_type
= fib
->fib_DirEntryType
;
97 st
->st_comment
= fib
->fib_Comment
;
100 /****** net.lib/stat *********************************************************
103 stat, lstat, fstat - get file status
106 #include <sys/types.h>
107 #include <sys/stat.h>
109 success = stat(path, buf)
111 int stat(const char *, struct stat *);
113 success = lstat(path, buf);
115 int lstat(const char *, struct stat *);
117 success = fstat(fd, buf);
119 int fstat(int, struct stat *);
122 The stat() function obtains information about the file pointed to by
123 path. Read, write or execute permission of the named file is not
124 required, but all directories listed in the path name leading to the
125 file must be seachable.
127 Lstat() is like stat() except in the case where the named file is a
128 symbolic link, in which case lstat() returns information about the
129 link, while stat() returns information about the file the link
132 The fstat() obtains the same information about an open file known by
133 the file descriptor fd, such as would be obtained by an open call.
135 Buf is a pointer to a stat() structure as defined by <sys/stat.h>
136 (shown below) and into which information is placed concerning the
141 dev_t st_dev; \* unique device id *\
142 ino_t st_ino; \* inode of file (key block) *\
143 mode_t st_mode; \* Unix style mode *\
144 ushort st_nlink; \* number of links (unimplemented) *\
145 uid_t st_uid; \* owner's user ID *\
146 gid_t st_gid; \* owner's group ID *\
147 dev_t st_rdev; \* special file ID (unimplemented) *\
148 off_t st_size; \* file size *\
149 time_t st_atime; \* Time of last access *\
150 time_t st_mtime; \* Last modification time *\
151 time_t st_ctime; \* Last file status change time *\
152 long st_blksize; \* Size of disk block *\
153 long st_blocks; \* Size in blocks *\
154 long st_dosmode; \* DOS protection bits *\
155 short st_type; \* DOS file type *\
156 char *st_comment; \* DOS file comment *\
159 The time-related fields of struct stat have same contents, time when
160 file data last modified.
162 The status information word st_mode has bits as follows:
164 #define S_ISUID 0004000 \* set user id on execution *\
165 #define S_ISGID 0002000 \* set group id on execution *\
166 #define S_ISVTX 0001000 \* save swapped text even after use *\
167 #define S_IRUSR 0000400 \* read permission for owner *\
168 #define S_IWUSR 0000200 \* write permission for owner *\
169 #define S_IXUSR 0000100 \* execute permission for owner *\
170 #define S_IRGRP 0000040 \* read permission for group *\
171 #define S_IWGRP 0000020 \* write permission for group *\
172 #define S_IXGRP 0000010 \* execute permission for group *\
173 #define S_IROTH 0000004 \* read permission for other *\
174 #define S_IWOTH 0000002 \* write permission for other *\
175 #define S_IXOTH 0000001 \* execute permission for other *\
176 #define S_IFCHR 0020000 \* character special *\
177 #define S_IFDIR 0040000 \* directory *\
178 #define S_IFBLK 0060000 \* block special *\
179 #define S_IFREG 0100000 \* regular *\
180 #define S_IFLNK 0120000 \* symbolic link *\
181 #define S_IFSOCK 0140000 \* socket *\
182 #define S_IFIFO 0010000 \* named pipe (fifo) *\
184 For a list of access modes, see <sys/stat.h>, access(2) and chmod(2).
187 Upon successful completion a value of 0 is returned. Otherwise, a
188 value of -1 is returned and errno is set to indicate the error.
191 The functions stat() and lstat() will fail if:
193 [ENOTDIR] A component of the path prefix is not a directory.
195 [ENAMETOOLONG] A component of a pathname exceeded 255 characters,
196 or an entire path name exceeded 1023 characters.
198 [ENOENT] The named file does not exist.
200 [ELOOP] Too many symbolic links were encountered in
201 translating the pathname.
203 [EACCES] Search permission is denied for a component of the
206 [EFAULT] Buf or name points to an invalid address.
208 [EIO] An I/O error occurred while reading from or writing
211 The function fstat() will fail if:
213 [EBADF] fd is not a valid open file descriptor.
215 [EFAULT] Buf points to an invalid address.
217 [EIO] An I/O error occurred while reading from or writing to the
224 Applying fstat to a socket returns a zero'd buffer.
226 *******************************************************************************
229 /****** net.lib/fstat *********************************************************
234 *******************************************************************************
237 /****** net.lib/lstat *********************************************************
242 *******************************************************************************
250 void printstat(char *what
, struct stat
*st
)
281 void main(int argc
, char *argv
[])
286 if (stat(argv
[1], st
) == 0) {
287 printstat(argv
[1], st
);
295 if (lstat(argv
[1], st
) == 0) {
296 printstat(argv
[1], st
);
304 int fd
= open(argv
[1], O_RDONLY
, 0);
307 if (fstat(fd
, st
) == 0) {
308 printstat(argv
[1], st
);