death to trailing whitespace
[openafs.git] / src / util / afsutil.h
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #ifndef _AFSUTIL_H_
11 #define _AFSUTIL_H_
12
13 #include <time.h>
14 /* Include afs installation dir retrieval routines */
15 #include <afs/dirpath.h>
16
17 /* These macros are return values from extractAddr. They do not represent
18  * any valid IP address and so can indicate a failure.
19  */
20 #define AFS_IPINVALID           0xffffffff      /* invalid IP address */
21 #define AFS_IPINVALIDIGNORE     0xfffffffe      /* no input given to extractAddr */
22
23 /* logging defines
24  */
25 #ifndef AFS_NT40_ENV
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h> /* for inet_ntoa() */
30 #endif
31
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <string.h>
35
36 extern int LogLevel;
37 extern int mrafsStyleLogs;
38 #ifndef AFS_NT40_ENV
39 extern int serverLogSyslog;
40 extern int serverLogSyslogFacility;
41 extern char *serverLogSyslogTag;
42 #endif
43 extern void vFSLog(const char *format, va_list args)
44         AFS_ATTRIBUTE_FORMAT(__printf__, 1, 0);
45
46 extern void SetLogThreadNumProgram(int (*func) (void) );
47
48 extern void FSLog(const char *format, ...)
49         AFS_ATTRIBUTE_FORMAT(__printf__, 1, 2);
50
51 #define ViceLog(level, str)  do { if ((level) <= LogLevel) (FSLog str); } while (0)
52 #define vViceLog(level, str) do { if ((level) <= LogLevel) (vFSLog str); } while (0)
53
54 extern int OpenLog(const char *filename);
55 extern int ReOpenLog(const char *fileName);
56 extern void SetupLogSignals(void);
57
58 extern int
59 afs_vsnprintf( /*@out@ */ char *p, size_t avail, const char *fmt,
60               va_list ap)
61     AFS_ATTRIBUTE_FORMAT(__printf__, 3, 0)
62     /*@requires maxSet(p) >= (avail-1)@ */
63     /*@modifies p@ */ ;
64
65 extern /*@printflike@ */ int
66 afs_snprintf( /*@out@ */ char *p, size_t avail, const char *fmt, ...)
67     AFS_ATTRIBUTE_FORMAT(__printf__, 3, 4)
68     /*@requires maxSet(p) >= (avail-1)@ */
69     /*@modifies p@ */ ;
70
71 extern int
72 afs_vasnprintf (char **ret, size_t max_sz, const char *format, va_list args)
73     AFS_ATTRIBUTE_FORMAT(__printf__, 3, 0);
74
75 extern int
76 afs_vasprintf (char **ret, const char *format, va_list args)
77     AFS_ATTRIBUTE_FORMAT(__printf__, 2, 0);
78
79 extern int
80 afs_asprintf (char **ret, const char *format, ...)
81     AFS_ATTRIBUTE_FORMAT(__printf__, 2, 3);
82
83 extern int
84 afs_asnprintf (char **ret, size_t max_sz, const char *format, ...)
85     AFS_ATTRIBUTE_FORMAT(__printf__, 3, 4);
86
87 /* special version of ctime that clobbers a *different static variable, so
88  * that ViceLog can call ctime and not cause buffer confusion.
89  */
90 extern char *vctime(const time_t * atime);
91
92 /* Need a thead safe ctime for pthread builds. Use std ctime for LWP */
93 #if defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
94 #if defined(AFS_SUN5_ENV) && !defined(_POSIX_PTHREAD_SEMANTICS) && (_POSIX_C_SOURCE - 0 < 199506L)
95 #define afs_ctime(C, B, L) ctime_r(C, B, L)
96 #else
97 /* Cast is for platforms which do not prototype ctime_r */
98 #define afs_ctime(C, B, L) (char*)ctime_r(C, B)
99 #endif /* AFS_SUN5_ENV */
100 #else /* AFS_PTHREAD_ENV && !AFS_NT40_ENV */
101 static_inline char *
102 afs_ctime(const time_t *C, char *B, size_t S) {
103 #if !defined(AFS_NT40_ENV) || (_MSC_VER < 1400)
104     strncpy(B, ctime(C), (S-1));
105     B[S-1] = '\0';
106 #else
107     char buf[32];
108     if (ctime_s(buf, sizeof(buf), C) ||
109         strncpy_s(B, S, buf, _TRUNCATE))
110          B[0] = '\0';
111 #endif
112     return B;
113 }
114 #endif /* AFS_PTHREAD_ENV && !AFS_NT40_ENV */
115
116
117 /* abort the current process. */
118 #ifdef AFS_NT40_ENV
119 #define afs_abort() afs_NTAbort()
120 #else
121 #define afs_abort() abort()
122 #endif
123
124
125 #ifdef AFS_NT40_ENV
126 #ifndef _MFC_VER
127 #include <winsock2.h>
128 #endif /* _MFC_VER */
129
130 /* Initialize the windows sockets before calling networking routines. */
131      extern int afs_winsockInit(void);
132      extern void afs_winsockCleanup(void);
133
134      struct timezone {
135          int tz_minuteswest;    /* of Greenwich */
136          int tz_dsttime;        /* type of dst correction to apply */
137      };
138 #define gettimeofday afs_gettimeofday
139      int afs_gettimeofday(struct timeval *tv, struct timezone *tz);
140
141 /* Unbuffer output when Un*x would do line buffering. */
142 #define setlinebuf(S) setvbuf(S, NULL, _IONBF, 0)
143
144 /* Abort on error, possibly trapping to debugger or dumping a trace. */
145      void afs_NTAbort(void);
146 #endif /* AFS_NT40_ENV */
147
148 #ifndef HAVE_POSIX_REGEX
149 extern char *re_comp(const char *sp);
150 extern int re_exec(const char *p1);
151 #endif
152
153      typedef char b32_string_t[8];
154 /* b64_string_t is 8 bytes, in stds.h */
155      typedef char lb64_string_t[12];
156
157 #ifndef HAVE_STRLCAT
158 extern size_t strlcat(char *dst, const char *src, size_t siz);
159 #endif
160
161 #ifndef HAVE_STRLCPY
162 extern size_t strlcpy(char *dst, const char *src, size_t siz);
163 #endif
164
165 #include <afs/ktime.h>
166 #include "afsutil_prototypes.h"
167
168 #endif /* _AFSUTIL_H_ */