1 /* Test the %m, %#m printf specifiers via asprintf.
2 Copyright (C) 2021-2022 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, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <libc-diag.h>
21 #include <stdio.h>
22 #include <support/check.h>
23 #include <support/support.h>
24
25 /* GCC does not yet know about the %#m specifier. */
26 DIAG_PUSH_NEEDS_COMMENT;
27 DIAG_IGNORE_NEEDS_COMMENT (11, "-Wformat=");
28
29 static int
do_test(void)30 do_test (void)
31 {
32 char buf[64];
33
34 errno = EINVAL;
35 TEST_COMPARE (sprintf (buf, "%m"), 16);
36 TEST_COMPARE_STRING (buf, "Invalid argument");
37
38 errno = EINVAL;
39 TEST_COMPARE (sprintf (buf, "%#m"), 6);
40 TEST_COMPARE_STRING (buf, "EINVAL");
41
42 errno = 0;
43 TEST_COMPARE (sprintf (buf, "%m"), 7);
44 TEST_COMPARE_STRING (buf, "Success");
45
46 errno = 0;
47 TEST_COMPARE (sprintf (buf, "%#m"), 1);
48 TEST_COMPARE_STRING (buf, "0");
49
50 errno = -1;
51 #ifdef __GNU__
52 TEST_COMPARE (sprintf (buf, "%m"), 35);
53 TEST_COMPARE_STRING (buf, "Error in unknown error system: : -1");
54 #else
55 TEST_COMPARE (sprintf (buf, "%m"), 16);
56 TEST_COMPARE_STRING (buf, "Unknown error -1");
57 #endif
58
59 errno = -1;
60 TEST_COMPARE (sprintf (buf, "%#m"), 2);
61 TEST_COMPARE_STRING (buf, "-1");
62
63 errno = 1002003;
64 #ifdef __GNU__
65 TEST_COMPARE (sprintf (buf, "%m"), 42);
66 TEST_COMPARE_STRING (buf, "(system kern) error with unknown subsystem");
67 #else
68 TEST_COMPARE (sprintf (buf, "%m"), 21);
69 TEST_COMPARE_STRING (buf, "Unknown error 1002003");
70 #endif
71
72 errno = 1002003;
73 TEST_COMPARE (sprintf (buf, "%#m"), 7);
74 TEST_COMPARE_STRING (buf, "1002003");
75
76 errno = EINVAL;
77 TEST_COMPARE (sprintf (buf, "%20m"), 20);
78 TEST_COMPARE_STRING (buf, " Invalid argument");
79
80 errno = EINVAL;
81 TEST_COMPARE (sprintf (buf, "%#20m"), 20);
82 TEST_COMPARE_STRING (buf, " EINVAL");
83
84 errno = EINVAL;
85 TEST_COMPARE (sprintf (buf, "%-20m"), 20);
86 TEST_COMPARE_STRING (buf, "Invalid argument ");
87
88 errno = EINVAL;
89 TEST_COMPARE (sprintf (buf, "%-#20m"), 20);
90 TEST_COMPARE_STRING (buf, "EINVAL ");
91
92 errno = 0;
93 TEST_COMPARE (sprintf (buf, "%-20m"), 20);
94 TEST_COMPARE_STRING (buf, "Success ");
95
96 errno = 0;
97 TEST_COMPARE (sprintf (buf, "%-#20m"), 20);
98 TEST_COMPARE_STRING (buf, "0 ");
99
100 return 0;
101 }
102
103 #include <support/test-driver.c>
104