1 /*
2  * k5-platform.h
3  *
4  * Copyright 2003, 2004, 2005, 2007, 2008, 2009 Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.	Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  *
26  *
27  * Some platform-dependent definitions to sync up the C support level.
28  * Some to a C99-ish level, some related utility code.
29  *
30  * Currently:
31  * + strlcpy, strlcat
32  * + [v]asprintf
33  * + detecting snprintf overflows
34  */
35 
36 #ifndef K5_PLATFORM_H
37 #define K5_PLATFORM_H
38 
39 #include "autoconf.h"
40 #include <string.h>
41 #include <stdarg.h>
42 #include <limits.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <fcntl.h>
46 #include <errno.h>
47 
48 /* Prototypes for system functions possibly defined in libmissing. */
49 
50 #ifndef HAVE_DAEMON
51 int daemon(int nochdir, int noclose);
52 #endif
53 
54 #ifndef HAVE_GETDTABLESIZE
55 int getdtablesize(void);
56 #endif
57 
58 #ifndef HAVE_GETOPT
59 int getopt(int nargc, char *const *nargv, const char *ostr);
60 #endif
61 
62 #ifndef HAVE_HERROR
63 void herror(const char *s);
64 #endif
65 
66 #ifndef HAVE_PARSETOS
67 int parsetos(char *name, char *proto);
68 #endif
69 
70 #ifndef HAVE_SETENV
71 int setenv(char *name, char *value, int rewrite);
72 void unsetenv(char *name);
73 #endif
74 
75 #ifndef HAVE_SETSID
76 int setsid(void);
77 #endif
78 
79 #ifndef HAVE_STRCASECMP
80 int strcasecmp(const char *s1, const char *s2);
81 int strncasecmp(const char *s1, const char *s2, size_t n);
82 #endif
83 
84 #ifndef HAVE_STRDUP
85 char *strdup(const char *str);
86 #endif
87 
88 #ifndef HAVE_STRERROR
89 char *strerror(int num);
90 #endif
91 
92 #ifndef HAVE_STRFTIME
93 size_t strftime(char *s, size_t maxsize, const char *format,
94 		const struct tm *t);
95 #endif
96 
97 #ifndef HAVE_STRLCPY
98 size_t strlcpy(char *dst, const char *src, size_t siz);
99 size_t strlcat(char *dst, const char *src, size_t siz);
100 #endif
101 
102 #ifndef HAVE_VASPRINTF
103 int vasprintf(char **, const char *, va_list)
104 #if !defined(__cplusplus) && (__GNUC__ > 2)
105     __attribute__((__format__(__printf__, 2, 0)))
106 #endif
107     ;
108 int asprintf(char **, const char *, ...)
109 #if !defined(__cplusplus) && (__GNUC__ > 2)
110     __attribute__((__format__(__printf__, 2, 3)))
111 #endif
112     ;
113 #endif /* HAVE_VASPRINTF */
114 
115 /* Return true if the snprintf return value RESULT reflects a buffer
116    overflow for the buffer size SIZE.
117 
118    We cast the result to unsigned int for two reasons.  First, old
119    implementations of snprintf (such as the one in Solaris 9 and
120    prior) return -1 on a buffer overflow.  Casting the result to -1
121    will convert that value to UINT_MAX, which should compare larger
122    than any reasonable buffer size.  Second, comparing signed and
123    unsigned integers will generate warnings with some compilers, and
124    can have unpredictable results, particularly when the relative
125    widths of the types is not known (size_t may be the same width as
126    int or larger).
127 */
128 #define SNPRINTF_OVERFLOW(result, size) \
129     ((unsigned int)(result) >= (size_t)(size))
130 
131 #endif /* K5_PLATFORM_H */
132