1/* Checking macros for unistd functions.
2   Copyright (C) 2005, 2006, 2007, 2008 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 _UNISTD_H
21# error "Never include <bits/unistd.h> directly; use <unistd.h> instead."
22#endif
23
24extern ssize_t __read_chk (int __fd, void *__buf, size_t __nbytes,
25			   size_t __buflen) __wur;
26extern ssize_t __REDIRECT (__read_alias, (int __fd, void *__buf,
27					  size_t __nbytes), read) __wur;
28extern ssize_t __REDIRECT (__read_chk_warn,
29			   (int __fd, void *__buf, size_t __nbytes,
30			    size_t __buflen), __read_chk)
31     __wur __warnattr ("read called with bigger length than size of "
32		       "the destination buffer");
33
34__extern_always_inline __wur ssize_t
35read (int __fd, void *__buf, size_t __nbytes)
36{
37  if (__bos0 (__buf) != (size_t) -1)
38    {
39      if (!__builtin_constant_p (__nbytes))
40	return __read_chk (__fd, __buf, __nbytes, __bos0 (__buf));
41
42      if (__nbytes > __bos0 (__buf))
43	return __read_chk_warn (__fd, __buf, __nbytes, __bos0 (__buf));
44    }
45  return __read_alias (__fd, __buf, __nbytes);
46}
47
48#ifdef __USE_UNIX98
49extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
50			    __off_t __offset, size_t __bufsize) __wur;
51extern ssize_t __pread64_chk (int __fd, void *__buf, size_t __nbytes,
52			      __off64_t __offset, size_t __bufsize) __wur;
53extern ssize_t __REDIRECT (__pread_alias,
54			   (int __fd, void *__buf, size_t __nbytes,
55			    __off_t __offset), pread) __wur;
56extern ssize_t __REDIRECT (__pread64_alias,
57			   (int __fd, void *__buf, size_t __nbytes,
58			    __off64_t __offset), pread64) __wur;
59extern ssize_t __REDIRECT (__pread_chk_warn,
60			   (int __fd, void *__buf, size_t __nbytes,
61			    __off_t __offset, size_t __bufsize), __pread_chk)
62     __wur __warnattr ("pread called with bigger length than size of "
63		       "the destination buffer");
64extern ssize_t __REDIRECT (__pread64_chk_warn,
65			   (int __fd, void *__buf, size_t __nbytes,
66			    __off64_t __offset, size_t __bufsize),
67			    __pread64_chk)
68     __wur __warnattr ("pread64 called with bigger length than size of "
69		       "the destination buffer");
70
71# ifndef __USE_FILE_OFFSET64
72__extern_always_inline __wur ssize_t
73pread (int __fd, void *__buf, size_t __nbytes, __off_t __offset)
74{
75  if (__bos0 (__buf) != (size_t) -1)
76    {
77      if (!__builtin_constant_p (__nbytes))
78	return __pread_chk (__fd, __buf, __nbytes, __offset, __bos0 (__buf));
79
80      if ( __nbytes > __bos0 (__buf))
81	return __pread_chk_warn (__fd, __buf, __nbytes, __offset,
82				 __bos0 (__buf));
83    }
84  return __pread_alias (__fd, __buf, __nbytes, __offset);
85}
86# else
87__extern_always_inline __wur ssize_t
88pread (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
89{
90  if (__bos0 (__buf) != (size_t) -1)
91    {
92      if (!__builtin_constant_p (__nbytes))
93	return __pread64_chk (__fd, __buf, __nbytes, __offset, __bos0 (__buf));
94
95      if ( __nbytes > __bos0 (__buf))
96	return __pread64_chk_warn (__fd, __buf, __nbytes, __offset,
97				   __bos0 (__buf));
98    }
99
100  return __pread64_alias (__fd, __buf, __nbytes, __offset);
101}
102# endif
103
104# ifdef __USE_LARGEFILE64
105__extern_always_inline __wur ssize_t
106pread64 (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
107{
108  if (__bos0 (__buf) != (size_t) -1)
109    {
110      if (!__builtin_constant_p (__nbytes))
111	return __pread64_chk (__fd, __buf, __nbytes, __offset, __bos0 (__buf));
112
113      if ( __nbytes > __bos0 (__buf))
114	return __pread64_chk_warn (__fd, __buf, __nbytes, __offset,
115				   __bos0 (__buf));
116    }
117
118  return __pread64_alias (__fd, __buf, __nbytes, __offset);
119}
120# endif
121#endif
122
123#if defined __USE_BSD || defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K
124extern ssize_t __readlink_chk (__const char *__restrict __path,
125			       char *__restrict __buf, size_t __len,
126			       size_t __buflen)
127     __THROW __nonnull ((1, 2)) __wur;
128extern ssize_t __REDIRECT_NTH (__readlink_alias,
129			       (__const char *__restrict __path,
130				char *__restrict __buf, size_t __len), readlink)
131     __nonnull ((1, 2)) __wur;
132extern ssize_t __REDIRECT_NTH (__readlink_chk_warn,
133			       (__const char *__restrict __path,
134				char *__restrict __buf, size_t __len,
135				size_t __buflen), __readlink_chk)
136     __nonnull ((1, 2)) __wur __warnattr ("readlink called with bigger length "
137					  "than size of destination buffer");
138
139__extern_always_inline __nonnull ((1, 2)) __wur ssize_t
140__NTH (readlink (__const char *__restrict __path, char *__restrict __buf,
141		 size_t __len))
142{
143  if (__bos (__buf) != (size_t) -1)
144    {
145      if (!__builtin_constant_p (__len))
146	return __readlink_chk (__path, __buf, __len, __bos (__buf));
147
148      if ( __len > __bos (__buf))
149	return __readlink_chk_warn (__path, __buf, __len, __bos (__buf));
150    }
151  return __readlink_alias (__path, __buf, __len);
152}
153#endif
154
155#ifdef __USE_ATFILE
156extern ssize_t __readlinkat_chk (int __fd, __const char *__restrict __path,
157				 char *__restrict __buf, size_t __len,
158				 size_t __buflen)
159     __THROW __nonnull ((2, 3)) __wur;
160extern ssize_t __REDIRECT_NTH (__readlinkat_alias,
161			       (int __fd, __const char *__restrict __path,
162				char *__restrict __buf, size_t __len),
163			       readlinkat)
164     __nonnull ((2, 3)) __wur;
165extern ssize_t __REDIRECT_NTH (__readlinkat_chk_warn,
166			       (int __fd, __const char *__restrict __path,
167				char *__restrict __buf, size_t __len,
168				size_t __buflen), __readlinkat_chk)
169     __nonnull ((2, 3)) __wur __warnattr ("readlinkat called with bigger "
170					  "length than size of destination "
171					  "buffer");
172
173__extern_always_inline __nonnull ((2, 3)) __wur ssize_t
174__NTH (readlinkat (int __fd, __const char *__restrict __path,
175		   char *__restrict __buf, size_t __len))
176{
177  if (__bos (__buf) != (size_t) -1)
178    {
179      if (!__builtin_constant_p (__len))
180	return __readlinkat_chk (__fd, __path, __buf, __len, __bos (__buf));
181
182      if (__len > __bos (__buf))
183	return __readlinkat_chk_warn (__fd, __path, __buf, __len,
184				      __bos (__buf));
185    }
186  return __readlinkat_alias (__fd, __path, __buf, __len);
187}
188#endif
189
190extern char *__getcwd_chk (char *__buf, size_t __size, size_t __buflen)
191     __THROW __wur;
192extern char *__REDIRECT_NTH (__getcwd_alias,
193			     (char *__buf, size_t __size), getcwd) __wur;
194extern char *__REDIRECT_NTH (__getcwd_chk_warn,
195			     (char *__buf, size_t __size, size_t __buflen),
196			     __getcwd_chk)
197     __wur __warnattr ("getcwd caller with bigger length than size of "
198		       "destination buffer");
199
200__extern_always_inline __wur char *
201__NTH (getcwd (char *__buf, size_t __size))
202{
203  if (__bos (__buf) != (size_t) -1)
204    {
205      if (!__builtin_constant_p (__size))
206	return __getcwd_chk (__buf, __size, __bos (__buf));
207
208      if (__size > __bos (__buf))
209	return __getcwd_chk_warn (__buf, __size, __bos (__buf));
210    }
211  return __getcwd_alias (__buf, __size);
212}
213
214#if defined __USE_BSD || defined __USE_XOPEN_EXTENDED
215extern char *__getwd_chk (char *__buf, size_t buflen)
216     __THROW __nonnull ((1)) __wur;
217extern char *__REDIRECT_NTH (__getwd_warn, (char *__buf), getwd)
218     __nonnull ((1)) __wur __warnattr ("please use getcwd instead, as getwd "
219				       "doesn't specify buffer size");
220
221__extern_always_inline __nonnull ((1)) __attribute_deprecated__ __wur char *
222__NTH (getwd (char *__buf))
223{
224  if (__bos (__buf) != (size_t) -1)
225    return __getwd_chk (__buf, __bos (__buf));
226  return __getwd_warn (__buf);
227}
228#endif
229
230extern size_t __confstr_chk (int __name, char *__buf, size_t __len,
231			     size_t __buflen) __THROW;
232extern size_t __REDIRECT_NTH (__confstr_alias, (int __name, char *__buf,
233						size_t __len), confstr);
234extern size_t __REDIRECT_NTH (__confstr_chk_warn,
235			      (int __name, char *__buf, size_t __len,
236			       size_t __buflen), __confstr_chk)
237     __warnattr ("confstr called with bigger length than size of destination "
238		 "buffer");
239
240__extern_always_inline size_t
241__NTH (confstr (int __name, char *__buf, size_t __len))
242{
243  if (__bos (__buf) != (size_t) -1)
244    {
245      if (!__builtin_constant_p (__len))
246	return __confstr_chk (__name, __buf, __len, __bos (__buf));
247
248      if (__bos (__buf) < __len)
249	return __confstr_chk_warn (__name, __buf, __len, __bos (__buf));
250    }
251  return __confstr_alias (__name, __buf, __len);
252}
253
254
255extern int __getgroups_chk (int __size, __gid_t __list[], size_t __listlen)
256     __THROW __wur;
257extern int __REDIRECT_NTH (__getgroups_alias, (int __size, __gid_t __list[]),
258			   getgroups) __wur;
259extern int __REDIRECT_NTH (__getgroups_chk_warn,
260			   (int __size, __gid_t __list[], size_t __listlen),
261			   __getgroups_chk)
262     __wur __warnattr ("getgroups called with bigger group count than what "
263		       "can fit into destination buffer");
264
265__extern_always_inline int
266__NTH (getgroups (int __size, __gid_t __list[]))
267{
268  if (__bos (__list) != (size_t) -1)
269    {
270      if (!__builtin_constant_p (__size) || __size < 0)
271	return __getgroups_chk (__size, __list, __bos (__list));
272
273      if (__size * sizeof (__gid_t) > __bos (__list))
274	return __getgroups_chk_warn (__size, __list, __bos (__list));
275    }
276  return __getgroups_alias (__size, __list);
277}
278
279
280extern int __ttyname_r_chk (int __fd, char *__buf, size_t __buflen,
281			    size_t __nreal) __THROW __nonnull ((2));
282extern int __REDIRECT_NTH (__ttyname_r_alias, (int __fd, char *__buf,
283					       size_t __buflen), ttyname_r)
284     __nonnull ((2));
285extern int __REDIRECT_NTH (__ttyname_r_chk_warn,
286			   (int __fd, char *__buf, size_t __buflen,
287			    size_t __nreal), __ttyname_r_chk)
288     __nonnull ((2)) __warnattr ("ttyname_r called with bigger buflen than "
289				 "size of destination buffer");
290
291__extern_always_inline int
292__NTH (ttyname_r (int __fd, char *__buf, size_t __buflen))
293{
294  if (__bos (__buf) != (size_t) -1)
295    {
296      if (!__builtin_constant_p (__buflen))
297	return __ttyname_r_chk (__fd, __buf, __buflen, __bos (__buf));
298
299      if (__buflen > __bos (__buf))
300	return __ttyname_r_chk_warn (__fd, __buf, __buflen, __bos (__buf));
301    }
302  return __ttyname_r_alias (__fd, __buf, __buflen);
303}
304
305
306#if defined __USE_REENTRANT || defined __USE_POSIX199506
307extern int __getlogin_r_chk (char *__buf, size_t __buflen, size_t __nreal)
308     __nonnull ((1));
309extern int __REDIRECT (__getlogin_r_alias, (char *__buf, size_t __buflen),
310		       getlogin_r) __nonnull ((1));
311extern int __REDIRECT (__getlogin_r_chk_warn,
312		       (char *__buf, size_t __buflen, size_t __nreal),
313		       __getlogin_r_chk)
314     __nonnull ((1)) __warnattr ("getlogin_r called with bigger buflen than "
315				 "size of destination buffer");
316
317__extern_always_inline int
318getlogin_r (char *__buf, size_t __buflen)
319{
320  if (__bos (__buf) != (size_t) -1)
321    {
322      if (!__builtin_constant_p (__buflen))
323	return __getlogin_r_chk (__buf, __buflen, __bos (__buf));
324
325      if (__buflen > __bos (__buf))
326	return __getlogin_r_chk_warn (__buf, __buflen, __bos (__buf));
327    }
328  return __getlogin_r_alias (__buf, __buflen);
329}
330#endif
331
332
333#if defined __USE_BSD || defined __USE_UNIX98
334extern int __gethostname_chk (char *__buf, size_t __buflen, size_t __nreal)
335     __THROW __nonnull ((1));
336extern int __REDIRECT_NTH (__gethostname_alias, (char *__buf, size_t __buflen),
337			   gethostname) __nonnull ((1));
338extern int __REDIRECT_NTH (__gethostname_chk_warn,
339			   (char *__buf, size_t __buflen, size_t __nreal),
340			   __gethostname_chk)
341     __nonnull ((1)) __warnattr ("gethostname called with bigger buflen than "
342				 "size of destination buffer");
343
344__extern_always_inline int
345__NTH (gethostname (char *__buf, size_t __buflen))
346{
347  if (__bos (__buf) != (size_t) -1)
348    {
349      if (!__builtin_constant_p (__buflen))
350	return __gethostname_chk (__buf, __buflen, __bos (__buf));
351
352      if (__buflen > __bos (__buf))
353	return __gethostname_chk_warn (__buf, __buflen, __bos (__buf));
354    }
355  return __gethostname_alias (__buf, __buflen);
356}
357#endif
358
359
360#if defined __USE_BSD || (defined __USE_XOPEN && !defined __USE_UNIX98)
361extern int __getdomainname_chk (char *__buf, size_t __buflen, size_t __nreal)
362     __THROW __nonnull ((1)) __wur;
363extern int __REDIRECT_NTH (__getdomainname_alias, (char *__buf,
364						   size_t __buflen),
365			   getdomainname) __nonnull ((1)) __wur;
366extern int __REDIRECT_NTH (__getdomainname_chk_warn,
367			   (char *__buf, size_t __buflen, size_t __nreal),
368			   __getdomainname_chk)
369     __nonnull ((1)) __wur __warnattr ("getdomainname called with bigger "
370				       "buflen than size of destination "
371				       "buffer");
372
373__extern_always_inline int
374__NTH (getdomainname (char *__buf, size_t __buflen))
375{
376  if (__bos (__buf) != (size_t) -1)
377    {
378      if (!__builtin_constant_p (__buflen))
379	return __getdomainname_chk (__buf, __buflen, __bos (__buf));
380
381      if (__buflen > __bos (__buf))
382	return __getdomainname_chk_warn (__buf, __buflen, __bos (__buf));
383    }
384  return __getdomainname_alias (__buf, __buflen);
385}
386#endif
387