Windows: Use secure ctime and strncpy in afs_ctime
[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 extern void SetLogThreadNumProgram(int (*func) (void) );
45
46 /*@printflike@*/ extern void FSLog(const char *format, ...);
47 #define ViceLog(level, str)  do { if ((level) <= LogLevel) (FSLog str); } while (0)
48 #define vViceLog(level, str) do { if ((level) <= LogLevel) (vFSLog str); } while (0)
49
50 extern int OpenLog(const char *filename);
51 extern int ReOpenLog(const char *fileName);
52 extern void SetupLogSignals(void);
53
54 extern int
55 afs_vsnprintf( /*@out@ */ char *p, size_t avail, const char *fmt,
56               va_list ap)
57     /*@requires maxSet(p) >= (avail-1)@ */
58     /*@modifies p@ */ ;
59
60 extern /*@printflike@ */ int
61 afs_snprintf( /*@out@ */ char *p, size_t avail,
62                     const char *fmt, ...)
63     /*@requires maxSet(p) >= (avail-1)@ */
64     /*@modifies p@ */ ;
65
66 extern int
67 afs_vasnprintf (char **ret, size_t max_sz, const char *format, va_list args);
68
69 extern int
70 afs_vasprintf (char **ret, const char *format, va_list args);
71
72 extern int
73 afs_asprintf (char **ret, const char *format, ...);
74
75 extern int
76 afs_asnprintf (char **ret, size_t max_sz, const char *format, ...);
77
78 /* special version of ctime that clobbers a *different static variable, so
79  * that ViceLog can call ctime and not cause buffer confusion.
80  */
81 extern char *vctime(const time_t * atime);
82
83 /* Need a thead safe ctime for pthread builds. Use std ctime for LWP */
84 #if defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
85 #if defined(AFS_SUN5_ENV) && !defined(_POSIX_PTHREAD_SEMANTICS) && (_POSIX_C_SOURCE - 0 < 199506L)
86 #define afs_ctime(C, B, L) ctime_r(C, B, L)
87 #else
88 /* Cast is for platforms which do not prototype ctime_r */
89 #define afs_ctime(C, B, L) (char*)ctime_r(C, B)
90 #endif /* AFS_SUN5_ENV */
91 #else /* AFS_PTHREAD_ENV && !AFS_NT40_ENV */
92 static_inline char *
93 afs_ctime(const time_t *C, char *B, size_t S) {
94 #if !defined(AFS_NT40_ENV) || (_MSC_VER < 1500)
95     strncpy(B, ctime(C), (S-1));
96     B[S-1] = '\0';
97 #else
98     char buf[32];
99     if (ctime_s(buf, sizeof(buf), C) ||
100         strncpy_s(B, S, buf, _TRUNCATE)
101          B[0] = '\0';
102 #endif
103     return B;
104 }
105 #endif /* AFS_PTHREAD_ENV && !AFS_NT40_ENV */
106
107
108 /* abort the current process. */
109 #ifdef AFS_NT40_ENV
110 #define afs_abort() afs_NTAbort()
111 #else
112 #define afs_abort() abort()
113 #endif
114
115
116 #ifdef AFS_NT40_ENV
117 #ifndef _MFC_VER
118 #include <winsock2.h>
119 #endif /* _MFC_VER */
120
121 /* Initialize the windows sockets before calling networking routines. */
122      extern int afs_winsockInit(void);
123      extern void afs_winsockCleanup(void);
124
125      struct timezone {
126          int tz_minuteswest;    /* of Greenwich */
127          int tz_dsttime;        /* type of dst correction to apply */
128      };
129 #define gettimeofday afs_gettimeofday
130      int afs_gettimeofday(struct timeval *tv, struct timezone *tz);
131
132 /* Unbuffer output when Un*x would do line buffering. */
133 #define setlinebuf(S) setvbuf(S, NULL, _IONBF, 0)
134
135 /* Abort on error, possibly trapping to debugger or dumping a trace. */
136      void afs_NTAbort(void);
137 #endif /* AFS_NT40_ENV */
138
139 #ifndef HAVE_POSIX_REGEX
140 extern char *re_comp(const char *sp);
141 extern int re_exec(const char *p1);
142 #endif
143
144      typedef char b32_string_t[8];
145 /* b64_string_t is 8 bytes, in stds.h */
146      typedef char lb64_string_t[12];
147
148 #ifndef HAVE_STRLCAT
149 extern size_t strlcat(char *dst, const char *src, size_t siz);
150 #endif
151
152 #ifndef HAVE_STRLCPY
153 extern size_t strlcpy(char *dst, const char *src, size_t siz);
154 #endif
155
156 #ifndef UKERNEL
157 #include "afs/ktime.h"
158 #endif
159 #include "afsutil_prototypes.h"
160
161 #endif /* _AFSUTIL_H_ */