1/*
2  This file contains some kit-wide typedefs and structs that basically
3  emulate most of a normal posix-y type system.  The purpose of hiding
4  everything behind these typedefs is to avoid inconsistencies between
5  various systems (such as the difference in size between off_t on BeOS
6  and some versions of Unix).  To further avoid complications I've also
7  hidden the stat and dirent structs since those vary even more widely.
8
9  THIS CODE COPYRIGHT DOMINIC GIAMPAOLO.  NO WARRANTY IS EXPRESSED
10  OR IMPLIED.  YOU MAY USE THIS CODE AND FREELY DISTRIBUTE IT FOR
11  NON-COMMERCIAL USE AS LONG AS THIS NOTICE REMAINS ATTACHED.
12
13  FOR COMMERCIAL USE, CONTACT DOMINIC GIAMPAOLO (dbg@be.com).
14
15  Dominic Giampaolo
16  dbg@be.com
17*/
18
19
20#ifndef _COMPAT_H
21#define _COMPAT_H
22
23
24#include <stdlib.h>
25#include <stdarg.h>
26#include <errno.h>
27#include <memory.h>
28#include <string.h>
29#include <fcntl.h>
30#include <time.h>
31
32#if (defined(__BEOS__) || defined(__HAIKU__))
33#include <OS.h>              /* for typedefs and prototypes */
34#include <image.h>           /* for a few typedefs */
35#include <Drivers.h>         /* for various ioctl structs, etc */
36#include <iovec.h>           /* because we're boneheads sometimes */
37#else
38#include <sys/uio.h>
39#endif
40
41
42/*
43  By default (for portability reasons) the size of off_t's and ino_t's
44  is 32-bit.  You can change the file system to be 64-bit if you want
45  by defining OFF_T_SIZE to be 8.
46
47  NOTE: if you change the size of OFF_T_SIZE to be 8 you will have to
48        go through the code and change any calls to printf() to use the
49        appropriate format for 64-bit integers on your OS.  I have seen
50        4 different formats now: %Ld (BeOS and Linux), %qd (FreeBSD),
51        %lld (Irix) and %I64d (NT).
52*/
53#define OFF_T_SIZE 8
54
55#if OFF_T_SIZE == 4
56typedef long fs_off_t;
57typedef long my_ino_t;
58#elif OFF_T_SIZE == 8
59typedef long long fs_off_t;
60typedef long long my_ino_t;
61#else
62#error OFF_T_SIZE must be either 4 or 8.
63#endif
64
65typedef int my_dev_t;
66typedef int my_mode_t;
67typedef int my_uid_t;
68typedef int my_gid_t;
69
70/* This is the maximum length of a file name.  Adjust it as you see fit */
71#define FILE_NAME_LENGTH    256
72
73/* This is maximum name size for naming a volume or semaphore/lock */
74#define IDENT_NAME_LENGTH   32
75
76
77typedef struct my_dirent {
78    my_dev_t        d_dev;
79    my_ino_t        d_ino;
80    unsigned short  d_reclen;
81    char            d_name[1];
82} my_dirent_t;
83
84typedef struct {
85    int                 fd;
86    struct my_dirent    ent;
87} MY_DIR;
88
89
90/*
91  This is a pretty regular stat structure but it's our "internal"
92  version since if we depended on the host version we'd be exposed
93  to all sorts of nasty things (different sized ino_t's, etc).
94  We also can't use the normal naming style of "st_" for each field
95  name because on some systems fields like st_atime are really just
96  define's that expand to all sorts of weird stuff.
97*/
98struct my_stat {
99    my_dev_t        dev;        /* "device" that this file resides on */
100    my_ino_t        ino;        /* this file's inode #, unique per device */
101    my_mode_t       mode;       /* mode bits (rwx for user, group, etc) */
102    int             nlink;      /* number of hard links to this file */
103    my_uid_t        uid;        /* user id of the owner of this file */
104    my_gid_t        gid;        /* group id of the owner of this file */
105    fs_off_t        size;       /* size in bytes of this file */
106    size_t          blksize;    /* preferred block size for i/o */
107    time_t          atime;      /* last access time */
108    time_t          mtime;      /* last modification time */
109    time_t          ctime;      /* last change time, not creation time */
110    time_t          crtime;     /* creation time; not posix but useful */
111};
112
113
114#define     MY_S_IFMT        00000170000 /* type of file */
115#define     MY_S_IFLNK       00000120000 /* symbolic link */
116#define     MY_S_IFREG       00000100000 /* regular */
117#define     MY_S_IFBLK       00000060000 /* block special */
118#define     MY_S_IFDIR       00000040000 /* directory */
119#define     MY_S_IFCHR       00000020000 /* character special */
120#define     MY_S_IFIFO       00000010000 /* fifo */
121
122#define     MY_S_ISREG(m)    (((m) & MY_S_IFMT) == MY_S_IFREG)
123#define     MY_S_ISLNK(m)    (((m) & MY_S_IFMT) == MY_S_IFLNK)
124#define     MY_S_ISBLK(m)    (((m) & MY_S_IFMT) == MY_S_IFBLK)
125#define     MY_S_ISDIR(m)    (((m) & MY_S_IFMT) == MY_S_IFDIR)
126#define     MY_S_ISCHR(m)    (((m) & MY_S_IFMT) == MY_S_IFCHR)
127#define     MY_S_ISFIFO(m)   (((m) & MY_S_IFMT) == MY_S_IFIFO)
128
129#define MY_S_IUMSK 07777     /* user settable bits */
130
131#define MY_S_ISUID 04000     /* set user id on execution */
132#define MY_S_ISGID 02000     /* set group id on execution */
133
134#define MY_S_ISVTX 01000     /* save swapped text even after use */
135
136#define MY_S_IRWXU 00700     /* read, write, execute: owner */
137#define MY_S_IRUSR 00400     /* read permission: owner */
138#define MY_S_IWUSR 00200     /* write permission: owner */
139#define MY_S_IXUSR 00100     /* execute permission: owner */
140#define MY_S_IRWXG 00070     /* read, write, execute: group */
141#define MY_S_IRGRP 00040     /* read permission: group */
142#define MY_S_IWGRP 00020     /* write permission: group */
143#define MY_S_IXGRP 00010     /* execute permission: group */
144#define MY_S_IRWXO 00007     /* read, write, execute: other */
145#define MY_S_IROTH 00004     /* read permission: other */
146#define MY_S_IWOTH 00002     /* write permission: other */
147#define MY_S_IXOTH 00001     /* execute permission: other */
148
149
150
151#ifndef TRUE
152#define TRUE 1
153#endif
154
155#ifndef FALSE
156#define FALSE 0
157#endif
158
159#if (!defined(__BEOS__) && !defined(__HAIKU__))
160typedef long               sem_id;
161typedef unsigned char      uchar;
162typedef short              int16;
163typedef unsigned short     uint16;
164typedef int                int32;
165typedef unsigned int       uint32;
166#define ulong unsigned long         /* make it a #define to avoid conflicts */
167typedef long long          int64;
168typedef unsigned long long uint64;
169typedef unsigned int       port_id;
170typedef int                bool;
171typedef int                image_id;
172typedef long long          bigtime_t;
173typedef long               thread_id;
174typedef long               status_t;
175
176sem_id     create_sem(long count, const char *name);
177long       delete_sem(sem_id sem);
178long       acquire_sem(sem_id sem);
179long       acquire_sem_etc(sem_id sem, int count, int flags,
180                           bigtime_t microsecond_timeout);
181long       release_sem(sem_id sem);
182long       release_sem_etc(sem_id sem, long count, long flags);
183
184long       atomic_add(long *value, long addvalue);
185int        snooze(bigtime_t f);
186bigtime_t  system_time(void);
187ssize_t    read_pos(int fd, fs_off_t _pos, void *data,  size_t nbytes);
188ssize_t    write_pos(int fd, fs_off_t _pos, const void *data,  size_t nbytes);
189ssize_t    readv_pos(int fd, fs_off_t _pos, struct iovec *iov, int count);
190ssize_t    writev_pos(int fd, fs_off_t _pos, struct iovec *iov,  int count);
191
192
193#endif /* __BEOS__ */
194
195void     panic(const char *msg, ...);
196int      device_is_read_only(const char *device);
197int      get_device_block_size(int fd);
198fs_off_t get_num_device_blocks(int fd);
199int      device_is_removeable(int fd);
200int      lock_removeable_device(int fd, bool on_or_off);
201void     hexdump(void *address, int size);
202
203
204#endif /* _COMPAT_H */
205