2 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
3 * Copyright (c) 2016-2018 The strace developers.
6 * SPDX-License-Identifier: GPL-2.0-or-later
18 * Provides pointer to static string buffer with printed return code in format
19 * used by strace - with errno and error message.
21 * @param rc Return code.
22 * @param fmt Output format. Currently, raw (used for diff matching) and grep
23 * (for extended POSIX regex-based pattern matching) formats are
25 * @return Pointer to (statically allocated) buffer containing decimal
26 * representation of return code and errno/error message in case @rc
29 static inline const char *
30 sprintrc_ex(long rc, enum sprintrc_fmt fmt)
32 static const char *formats[] = {
33 [SPRINTRC_FMT_RAW] = "-1 %s (%m)",
34 [SPRINTRC_FMT_GREP] = "-1 %s \\(%m\\)",
36 static char buf[4096];
42 ? snprintf(buf, sizeof(buf), formats[fmt], errno2name())
43 : snprintf(buf, sizeof(buf), "%ld", rc);
46 perror_msg_and_fail("snprintf");
47 if ((size_t) ret >= sizeof(buf))
48 error_msg_and_fail("snprintf overflow: got %d, expected"
49 " no more than %zu", ret, sizeof(buf));
57 return sprintrc_ex(rc, SPRINTRC_FMT_RAW);
61 sprintrc_grep(long rc)
63 return sprintrc_ex(rc, SPRINTRC_FMT_GREP);