]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdlib/bits/stdlib.h
* debug/mbstowcs_chk.c: New file.
[thirdparty/glibc.git] / stdlib / bits / stdlib.h
1 /* Checking macros for stdlib functions.
2 Copyright (C) 2005 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20 #ifndef _STDLIB_H
21 # error "Never include <bits/stdlib.h> directly; use <stdlib.h> instead."
22 #endif
23
24 extern char *__realpath_chk (__const char *__restrict __name,
25 char *__restrict __resolved,
26 size_t __resolvedlen) __THROW __wur;
27 extern char *__REDIRECT_NTH (__realpath_alias,
28 (__const char *__restrict __name,
29 char *__restrict __resolved), realpath) __wur;
30
31 extern __always_inline __wur char *
32 realpath (const char *__name, char *__resolved)
33 {
34 if (__bos (__resolved) != (size_t) -1)
35 return __realpath_chk (__name, __resolved, __bos (__resolved));
36
37 return __realpath_alias (__name, __resolved);
38 }
39
40
41 extern int __ptsname_r_chk (int __fd, char *__buf, size_t __buflen,
42 size_t __nreal) __THROW __nonnull ((2));
43 extern int __REDIRECT_NTH (__ptsname_r_alias, (int __fd, char *__buf,
44 size_t __buflen), ptsname_r)
45 __nonnull ((2));
46
47 extern __always_inline int
48 ptsname_r (int __fd, char *__buf, size_t __buflen)
49 {
50 if (__bos (__buf) != (size_t) -1
51 && (!__builtin_constant_p (__buflen) || __buflen > __bos (__buf)))
52 return __ptsname_r_chk (__fd, __buf, __buflen, __bos (__buf));
53 return __ptsname_r_alias (__fd, __buf, __buflen);
54 }
55
56
57 extern int __wctomb_chk (char *__s, wchar_t __wchar, size_t __buflen)
58 __THROW __wur;
59 extern int __REDIRECT_NTH (__wctomb_alias, (char *__s, wchar_t __wchar),
60 wctomb) __wur;
61
62 extern __always_inline __wur int
63 wctomb (char *__s, wchar_t __wchar)
64 {
65 /* We would have to include <limits.h> to get a definition of MB_LEN_MAX.
66 But this would only disturb the namespace. So we define our own
67 version here. */
68 #define __STDLIB_MB_LEN_MAX 16
69 #if defined MB_LEN_MAX && MB_LEN_MAX != __STDLIB_MB_LEN_MAX
70 # error "Assumed value of MB_LEN_MAX wrong"
71 #endif
72 if (__bos (__s) != (size_t) -1 && __STDLIB_MB_LEN_MAX > __bos (__s))
73 return __wctomb_chk (__s, __wchar, __bos (__s));
74 return __wctomb_alias (__s, __wchar);
75 }
76
77
78 extern size_t __mbstowcs_chk (wchar_t *__restrict __dst,
79 __const char *__restrict __src,
80 size_t __len, size_t __dstlen) __THROW;
81 extern size_t __REDIRECT_NTH (__mbstowcs_alias,
82 (wchar_t *__restrict __dst,
83 __const char *__restrict __src,
84 size_t __len), mbstowcs);
85
86 extern __always_inline size_t
87 mbstowcs (wchar_t *__restrict __dst, __const char *__restrict __src,
88 size_t __len)
89 {
90 if (__bos (__dst) != (size_t) -1
91 && (!__builtin_constant_p (__len)
92 || __len * sizeof (wchar_t) > __bos (__dst)))
93 return __mbstowcs_chk (__dst, __src, __len, __bos (__dst));
94 return __mbstowcs_alias (__dst, __src, __len);
95 }
96
97
98 extern size_t __wcstombs_chk (char *__restrict __dst,
99 __const wchar_t *__restrict __src,
100 size_t __len, size_t __dstlen) __THROW;
101 extern size_t __REDIRECT_NTH (__wcstombs_alias,
102 (char *__restrict __dst,
103 __const wchar_t *__restrict __src,
104 size_t __len), wcstombs);
105
106 extern __always_inline size_t
107 wcstombs (char *__restrict __dst, __const wchar_t *__restrict __src,
108 size_t __len)
109 {
110 if (__bos (__dst) != (size_t) -1
111 && (!__builtin_constant_p (__len) || __len > __bos (__dst)))
112 return __wcstombs_chk (__dst, __src, __len, __bos (__dst));
113 return __wcstombs_alias (__dst, __src, __len);
114 }