Use Linux 6.6 in build-many-glibcs.py
[glibc.git] / stdio-common / tst-sprintf-errno.c
blob79764d7a5f62b619c49e913890548020eb73187b
1 /* Test the %m, %#m printf specifiers via asprintf.
2 Copyright (C) 2021-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <errno.h>
20 #include <libc-diag.h>
21 #include <stdio.h>
22 #include <support/check.h>
23 #include <support/support.h>
25 /* GCC does not yet know about the %#m specifier. */
26 DIAG_PUSH_NEEDS_COMMENT;
27 DIAG_IGNORE_NEEDS_COMMENT (11, "-Wformat=");
29 static int
30 do_test (void)
32 char buf[64];
34 errno = EINVAL;
35 TEST_COMPARE (sprintf (buf, "%m"), 16);
36 TEST_COMPARE_STRING (buf, "Invalid argument");
38 errno = EINVAL;
39 TEST_COMPARE (sprintf (buf, "%#m"), 6);
40 TEST_COMPARE_STRING (buf, "EINVAL");
42 errno = 0;
43 TEST_COMPARE (sprintf (buf, "%m"), 7);
44 TEST_COMPARE_STRING (buf, "Success");
46 errno = 0;
47 TEST_COMPARE (sprintf (buf, "%#m"), 1);
48 TEST_COMPARE_STRING (buf, "0");
50 errno = -1;
51 #ifdef __GNU__
52 TEST_COMPARE (sprintf (buf, "%m"), 39);
53 TEST_COMPARE_STRING (buf, "Error in unknown error system: FFFFFFFF");
54 #else
55 TEST_COMPARE (sprintf (buf, "%m"), 16);
56 TEST_COMPARE_STRING (buf, "Unknown error -1");
57 #endif
59 errno = -1;
60 TEST_COMPARE (sprintf (buf, "%#m"), 2);
61 TEST_COMPARE_STRING (buf, "-1");
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
72 errno = 1002003;
73 TEST_COMPARE (sprintf (buf, "%#m"), 7);
74 TEST_COMPARE_STRING (buf, "1002003");
76 errno = EINVAL;
77 TEST_COMPARE (sprintf (buf, "%20m"), 20);
78 TEST_COMPARE_STRING (buf, " Invalid argument");
80 errno = EINVAL;
81 TEST_COMPARE (sprintf (buf, "%#20m"), 20);
82 TEST_COMPARE_STRING (buf, " EINVAL");
84 errno = EINVAL;
85 TEST_COMPARE (sprintf (buf, "%-20m"), 20);
86 TEST_COMPARE_STRING (buf, "Invalid argument ");
88 errno = EINVAL;
89 TEST_COMPARE (sprintf (buf, "%-#20m"), 20);
90 TEST_COMPARE_STRING (buf, "EINVAL ");
92 errno = 0;
93 TEST_COMPARE (sprintf (buf, "%-20m"), 20);
94 TEST_COMPARE_STRING (buf, "Success ");
96 errno = 0;
97 TEST_COMPARE (sprintf (buf, "%-#20m"), 20);
98 TEST_COMPARE_STRING (buf, "0 ");
100 return 0;
103 #include <support/test-driver.c>