2 * Copyright (c) 2018 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 * Non-standard, Darwin-specific additions for the stdio(3) family of APIs.
28 #ifndef __DARWIN_STDIO_H
29 #define __DARWIN_STDIO_H
33 #include <sys/cdefs.h>
38 #include <sys/types.h>
40 #if __has_include(<sys/guarded.h>)
41 #include <sys/guarded.h>
43 typedef uint64_t guardid_t
;
54 * A type alias for a file descriptor.
59 * @function os_fd_valid
60 * Returns whether the given integer is a valid file descriptor number.
63 * The integer to check.
66 * A Boolean indicating whether the integer is a valid file descriptor number,
67 * that is, greater than or equal to zero.
69 DARWIN_API_AVAILABLE_20180727
70 OS_ALWAYS_INLINE OS_WARN_RESULT
72 os_fd_valid(os_fd_t fd
)
74 return (fd
>= STDIN_FILENO
);
78 * @function os_guardid_from_ptr
79 * Converts the given pointer to a guardid_t.
82 * The pointer to convert.
85 * The pointer as a guardid_t.
87 DARWIN_API_AVAILABLE_20190830
88 OS_ALWAYS_INLINE OS_WARN_RESULT
89 static inline guardid_t
90 os_guardid_from_ptr(const void *p
)
92 return (guardid_t
)(uintptr_t)p
;
97 * Checks the status of an fread(3) or fwrite(3) operation to a FILE.
100 * The file on which the operation was performed.
103 * The return value of the operation.
106 * The expected return value of the operation.
109 * One of the following integers:
111 * 0 The operation succeeded
112 * EOF The operation encountered the end of the FILE stream before it
114 * 1 There was an error
116 DARWIN_API_AVAILABLE_20180727
117 OS_EXPORT OS_WARN_RESULT OS_NONNULL1
119 fcheck_np(FILE *f
, size_t n
, size_t expected
);
123 * Variant of dup(2) that guarantees the dup(2) operation will either succeed or
127 * The descriptor to dup(2).
130 * A new file descriptor number that is functionally equivalent to what the
134 * The implementation will retry if the operation was interrupted by a signal.
135 * If the operation failed for any other reason, the implementation will
136 * terminate the caller.
138 DARWIN_API_AVAILABLE_20180727
139 OS_EXPORT OS_WARN_RESULT
144 * @function claimfd_np
145 * Claims the given file descriptor for the caller's exclusive use by applying a
146 * guard and invalidating the given storage.
149 * A pointer to the storage for the descriptor to claim. Upon return, a known-
150 * invalid value is written into this memory.
153 * The optional guard value to enforce the caller's claim on the descriptor.
156 * The guard flags to enforce the caller's claim on the descriptor.
159 * The given descriptor with the guard applied.
161 DARWIN_API_AVAILABLE_20190830
162 OS_EXPORT OS_WARN_RESULT OS_NONNULL1
164 claimfd_np(os_fd_t
*fdp
, const guardid_t
*gdid
, u_int gdflags
);
167 * @function xferfd_np
168 * Transfers ownership from the given file descriptor back to the general public
169 * by clearing the guard associated with it.
172 * A pointer to the storage for the descriptor to claim. Upon return, a known-
173 * invalid value is written into this memory.
176 * The optional guard value to reliquish ownership on the descriptor.
179 * The guard flags to relinquish.
182 * The given descriptor with the guard cleared. This descriptor is suitable for
183 * claiming with {@link claimfd_np}.
185 DARWIN_API_AVAILABLE_20190830
186 OS_EXPORT OS_WARN_RESULT OS_NONNULL1
188 xferfd_np(os_fd_t
*fdp
, const guardid_t
*gdid
, u_int gdflags
);
191 * @function close_drop_np
192 * Variant of close(2) which transfers ownership from the caller and performs
193 * the close(2) operation. These semantics are useful for ensuring that a
194 * descriptor is not erroneously re-used after it has been closed. To achieve
195 * these semantics, this variant will clear the memory in which the descriptor
196 * resides and replace it with a known-invalid value before returning.
199 * A pointer to the storage for the descriptor to close. Upon return, a known-
200 * invalid value is written into this memory.
203 * The optional guard. If the descriptor is not guarded, pass NULL.
206 * If the implementation encounters a failure to close a valid descriptor
207 * number, the caller will be terminated.
209 OS_EXPORT OS_NONNULL1
211 close_drop_np(os_fd_t
*fdp
, const guardid_t
*gdid
);
214 * @function close_drop_optional_np
215 * Variant of {@link close_drop} which will not attempt to close an invalid
216 * descriptor. Otherwise all semantics are the same.
219 * A pointer to the storage for the descriptor to close. Upon return, a known-
220 * invalid value is written into this memory.
223 * The optional guard. If the descriptor is not guarded, pass NULL.
226 * If the implementation encounters a failure to close a valid descriptor
227 * number, the caller will be terminated. The implementation will not attempt to
228 * close the descriptor if its value is -1.
230 OS_EXPORT OS_NONNULL1
232 close_drop_optional_np(os_fd_t
*fdp
, const guardid_t
*gdid
);
235 * @function zsnprintf_np
236 * snprintf(3) variant which returns the numnber of bytes written less the null
240 * The buffer in which to write the string.
243 * The length of the buffer.
246 * The printf(3)-like format string.
249 * The arguments corresponding to the format string.
252 * The number of bytes written into the buffer, less the null terminator. This
253 * routine is useful for successive string printing that may be lossy, as it
254 * will simply return zero when there is no space left in the destination
255 * buffer, i.e. enables the following pattern:
258 * size_t left = sizeof(buff);
259 * for (i = 0; i < n_strings; i++) {
260 * size_t n_written = zsnprintf_np(buff, left, "%s", strings[i]);
265 * This loop will safely terminate without any special care since, as soon as
266 * the buffer's space is exhausted, all further calls to zsnprintf_np() will
267 * write nothing and return zero.
269 DARWIN_API_AVAILABLE_20170407
270 OS_EXPORT OS_WARN_RESULT OS_NONNULL1 OS_NONNULL3
OS_FORMAT_PRINTF(3, 4)
272 zsnprintf_np(char *buff
, size_t len
, const char *fmt
, ...);
275 * @function crfprintf_np
276 * fprintf(3) variant that appends a new line character to the output.
279 * The file to which the output should be written.
282 * The printf(3)-like format string.
285 * The arguments corresponding to the format string.
287 DARWIN_API_AVAILABLE_20181020
288 OS_EXPORT OS_NONNULL1 OS_NONNULL2
OS_FORMAT_PRINTF(2, 3)
290 crfprintf_np(FILE *f
, const char *fmt
, ...);
293 * @function vcrfprintf_np
294 * vfprintf(3) variant that appends a new line character to the output.
297 * The file to which the output should be written.
300 * The printf(3)-like format string.
303 * The argument list corresponding to the format string.
305 DARWIN_API_AVAILABLE_20181020
306 OS_EXPORT OS_NONNULL1 OS_NONNULL2 OS_NONNULL3
308 vcrfprintf_np(FILE *f
, const char *fmt
, va_list ap
);
311 * @function wfprintf_np
312 * fprintf(3) variant which wraps the output to the specified column width,
313 * inserting new lines as necessary. Output will be word-wrapped with a trivial
317 * The file to which the output should be written.
320 * The number of spaces that should be inserted prior to the first line of
321 * output. If a negative value is given, the implementation will assume that an
322 * amount of spaces equal to the absolute value of the parameter has already
323 * been written, and therefore it will only use the parameter to compute line-
324 * wrapping information and not insert any additional spaces on the first line
328 * The number of spaces that should be inserted prior to every line of output
329 * except the first line.
332 * The maximum number of columns of each line of output. Pass zero to indicate
333 * that there is no maximum.
336 * The printf(3)-like format string.
339 * The arguments corresponding to the format string.
342 * This routine will silently fail to print to the desired output stream if
343 * there was a failure to allocate heap memory.
345 DARWIN_API_AVAILABLE_20181020
346 OS_EXPORT OS_NONNULL1 OS_NONNULL5 OS_NONNULL6
348 wfprintf_np(FILE *f
, ssize_t initpad
, size_t pad
, size_t width
,
349 const char *fmt
, ...);
352 * @function vwfprintf_np
353 * vfprintf(3) variant which wraps the output to the specified column width,
354 * inserting new lines as necessary. Output will be word-wrapped with a trivial
358 * The file to which the output should be written.
361 * The number of spaces that should be inserted prior to the first line of
362 * output. If a negative value is given, the implementation will assume that an
363 * amount of spaces equal to the absolute value of the parameter has already
364 * been written, and therefore it will only use the parameter to compute line-
365 * wrapping information and not insert any additional spaces on the first line
369 * The number of spaces that should be inserted prior to every line of output
370 * except the first line.
373 * The maximum number of columns of each line of output. Pass zero to indicate
374 * that there is no maximum.
377 * The printf(3)-like format string.
380 * The argument list corresponding to the format string.
383 * This routine will silently fail to print to the desired output stream if
384 * there was a failure to allocate heap memory.
386 DARWIN_API_AVAILABLE_20181020
387 OS_EXPORT OS_NONNULL1 OS_NONNULL5 OS_NONNULL6
389 vwfprintf_np(FILE *f
, ssize_t initpad
, size_t pad
, size_t width
,
390 const char *fmt
, va_list ap
);
394 #endif // __DARWIN_STDIO_H