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 to the string(3) family of APIs.
28 #ifndef __DARWIN_STRING_H
29 #define __DARWIN_STRING_H
33 #include <sys/cdefs.h>
39 * A type describing a flag's human-readable name.
45 * The human-readable, string representation of the flag value.
47 DARWIN_API_AVAILABLE_20170407
48 typedef struct _os_flag
{
49 const uint64_t ohf_flag
;
50 const char *const ohf_human_flag
;
54 * @define OS_FLAGSET_COUNT
55 * The maximum number of flags that a flagset can represent.
57 #define OS_FLAGSET_COUNT (sizeof(uint64_t) * BYTE_SIZE)
60 * @typedef os_flagset_t
61 * A type describing an array of human flags. Can accommodate up to 64 flags.
63 DARWIN_API_AVAILABLE_20170407
64 typedef os_flag_t os_flagset_t
[OS_FLAGSET_COUNT
];
68 * Initializer for a {@link os_flag_t} structure which stringifies the
72 * The name of the flag to initialize.
74 #define os_flag_init(__flag) { \
76 .ohf_human_flag = #__flag, \
80 * @function strerror_np
81 * Returns a human-readable string for the given {@link errno_t} or
85 * The error code for which to obtain the string.
88 * A human-readable string describing the error condition. If a POSIX error code
89 * is given, this is equivalent to a call to strerror(3).
91 DARWIN_API_AVAILABLE_20170407
92 OS_EXPORT OS_WARN_RESULT OS_PURE
94 strerror_np(int code
);
97 * @function symerror_np
98 * Returns the token name of the given {@link errno_t} or POSIX error
102 * The error code for which to obtain the token string.
105 * The string describing the error token. For example, if code 2 is passed, the
106 * string "EPERM" is returned.
108 DARWIN_API_AVAILABLE_20170407
109 OS_EXPORT OS_WARN_RESULT OS_PURE
111 symerror_np(int code
);
114 * @function symexit_np
115 * Returns the token name of the given sysexits(3) code.
118 * The sysexits(3) code for which to obtain the token string.
121 * The string describing the exit code. For example, if 64 is passed, the string
122 * "EX_USAGE" is returned. If the code is unrecognized, "EX_UNAVAILABLE" is
123 * returned, which is more or less documented in sysexits(3) as the ¯\_(ツ)_/¯
126 DARWIN_API_AVAILABLE_20170407
127 OS_EXPORT OS_WARN_RESULT OS_PURE
129 symexit_np(int code
);
132 * @function os_flagset_copy_string
133 * Returns a human-readable representation of the flags set for a given word.
136 * The human flagset which describes how to interpret the {@link flags}
140 * The flags to interpret.
143 * The human-readable representation of all flags set in the {@link flags}
144 * parameter, separated by the pipe character. The caller is responsible for
145 * calling free(3) on this object when it is no longer needed.
148 * This API is to be used in combination with {@link os_flag_init} to make
149 * printing the contents of flags fields simple. For example, this code can
150 * easily print a human-readable representation of the bits set in a Mach
153 * static const os_flagset_t _mach_msgh_bits = {
154 * os_flag_init(MACH_MSGH_BITS_COMPLEX),
155 * os_flag_init(MACH_MSGH_BITS_RAISEIMP),
156 * os_flag_init(MACH_MSGH_BITS_IMPHOLDASRT),
159 * char *flags_string = os_flagset_copy_string(&_mach_msgh_bits,
162 * For a message header with the MACH_MSGH_BITS_COMPLEX and
163 * MACH_MSGH_BITS_RAISEIMP bits set, this will return the string
165 * MACH_MSGH_BITS_COMPLEX|MACH_MSGH_BITS_RAISEIMP
167 DARWIN_API_AVAILABLE_20170407
168 OS_EXPORT OS_WARN_RESULT OS_MALLOC
170 os_flagset_copy_string(const os_flagset_t flagset
, uint64_t flags
);
174 #endif // __DARWIN_STRING_H