]> arthur.barton.de Git - netatalk.git/blob - include/atalk/util.h
Cleanup header checks in configure.in
[netatalk.git] / include / atalk / util.h
1 /*!
2  * @file
3  * Netatalk utility functions
4  *
5  * Utility functions for these areas: \n
6  * * sockets \n
7  * * locking \n
8  * * misc UNIX function wrappers, eg for getcwd
9  */
10
11 #ifndef _ATALK_UTIL_H
12 #define _ATALK_UTIL_H 1
13
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <unistd.h>
17
18 #ifndef NO_DDP
19 #include <netatalk/at.h>
20 #endif
21 #include <atalk/unicode.h>
22
23 /* exit error codes */
24 #define EXITERR_CLNT 1  /* client related error */
25 #define EXITERR_CONF 2  /* error in config files/cmd line parameters */
26 #define EXITERR_SYS  3  /* local system error */
27
28 /* Print a SBT and exit */
29 #define AFP_PANIC(why) \
30     do {                                            \
31         netatalk_panic(why);                        \
32         abort();                                    \
33     } while(0);
34
35 /* LOG assert errors */
36 #ifndef NDEBUG
37 #define AFP_ASSERT(b) \
38     do {                                                                \
39         if (!(b)) {                                                     \
40             AFP_PANIC(#b);                                              \
41         } \
42     } while(0);
43 #else
44 #define AFP_ASSERT(b)
45 #endif /* NDEBUG */
46
47 #ifndef MIN
48 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
49 #endif
50
51 #ifndef MAX
52 #define MAX(a, b)  ((a) > (b) ? (a) : (b))
53 #endif
54
55 #define STRCMP(a,b,c) (strcmp(a,c) b 0)
56 #define ZERO_STRUCT(a) memset(&(a), 0, sizeof(a))
57
58 #ifdef WITH_SENDFILE
59 extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count);
60 #endif
61
62 extern const int _diacasemap[], _dialowermap[];
63
64 extern char **getifacelist(void);
65 extern void freeifacelist(char **);
66
67 #define diatolower(x)     _dialowermap[(unsigned char) (x)]
68 #define diatoupper(x)     _diacasemap[(unsigned char) (x)]
69 #ifndef NO_DDP
70 extern int atalk_aton     (char *, struct at_addr *);
71 #endif
72 extern void bprint        (char *, int);
73 extern int strdiacasecmp  (const char *, const char *);
74 extern int strndiacasecmp (const char *, const char *, size_t);
75 extern pid_t server_lock  (char * /*program*/, char * /*file*/, int /*debug*/);
76 extern void fault_setup   (void (*fn)(void *));
77 extern void netatalk_panic(const char *why);
78 #define server_unlock(x)  (unlink(x))
79
80 #ifndef HAVE_DLFCN_H
81 extern void *mod_open    (const char *);
82 extern void *mod_symbol  (void *, const char *);
83 extern void mod_close    (void *);
84 #define mod_error()      ""
85 #else /* ! HAVE_DLFCN_H */
86 #include <dlfcn.h>
87
88 #ifndef RTLD_NOW
89 #define RTLD_NOW 1
90 #endif /* ! RTLD_NOW */
91
92 /* NetBSD doesn't like RTLD_NOW for dlopen (it fails). Use RTLD_LAZY.
93  * OpenBSD currently does not use the second arg for dlopen(). For
94  * future compatibility we define DL_LAZY */
95 #ifdef __NetBSD__
96 #define mod_open(a)      dlopen(a, RTLD_LAZY)
97 #elif defined(__OpenBSD__)
98 #define mod_open(a)      dlopen(a, DL_LAZY)
99 #else /* ! __NetBSD__ && ! __OpenBSD__ */
100 #define mod_open(a)      dlopen(a, RTLD_NOW)
101 #endif /* __NetBSD__ */
102
103 #ifndef DLSYM_PREPEND_UNDERSCORE
104 #define mod_symbol(a, b) dlsym(a, b)
105 #else /* ! DLSYM_PREPEND_UNDERSCORE */
106 extern void *mod_symbol  (void *, const char *);
107 #endif /* ! DLSYM_PREPEND_UNDERSCORE */
108 #define mod_error()      dlerror()
109 #define mod_close(a)     dlclose(a)
110 #endif /* ! HAVE_DLFCN_H */
111
112 /******************************************************************
113  * locking.c
114  ******************************************************************/
115
116 extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
117 #define read_lock(fd, offset, whence, len) \
118     lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
119 #define write_lock(fd, offset, whence, len) \
120     lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
121 #define unlock(fd, offset, whence, len) \
122     lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
123
124 /******************************************************************
125  * socket.c
126  ******************************************************************/
127
128 extern int setnonblock(int fd, int cmd);
129 extern ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout);
130 extern const char *getip_string(const struct sockaddr *sa);
131 extern unsigned int getip_port(const struct sockaddr *sa);
132 extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
133 extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
134
135 /******************************************************************
136  * unix.c
137  *****************************************************************/
138
139 extern const char *abspath(const char *name);
140 extern const char *getcwdpath(void);
141 extern char *stripped_slashes_basename(char *p);
142 extern int lchdir(const char *dir);
143 extern void randombytes(void *buf, int n);
144 #endif  /* _ATALK_UTIL_H */