doc: printf %b: clarify octal processing
[coreutils.git] / src / getlimits.c
blob3138455ed3b20caa5cfef29cacb8b68fb58a4272
1 /* getlimits - print various platform dependent limits.
2 Copyright (C) 2008-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Pádraig Brady */
19 #include <config.h> /* sets _FILE_OFFSET_BITS=64 etc. */
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <float.h>
24 #include "ftoastr.h"
25 #include "system.h"
26 #include "long-options.h"
28 #define PROGRAM_NAME "getlimits"
30 #define AUTHORS proper_name_lite ("Padraig Brady", "P\303\241draig Brady")
32 #ifndef TIME_T_MAX
33 # define TIME_T_MAX TYPE_MAXIMUM (time_t)
34 #endif
36 #ifndef TIME_T_MIN
37 # define TIME_T_MIN TYPE_MINIMUM (time_t)
38 #endif
40 #ifndef SSIZE_MIN
41 # define SSIZE_MIN TYPE_MINIMUM (ssize_t)
42 #endif
44 #ifndef PID_T_MIN
45 # define PID_T_MIN TYPE_MINIMUM (pid_t)
46 #endif
48 /* These are not interesting to print.
49 * Instead of these defines it would be nice to be able to do
50 * #ifdef (TYPE##_MIN) in function macro below. */
51 #define SIZE_MIN 0
52 #define UCHAR_MIN 0
53 #define UINT_MIN 0
54 #define ULONG_MIN 0
55 #define UINTMAX_MIN 0
56 #define UID_T_MIN 0
57 #define GID_T_MIN 0
59 void
60 usage (int status)
62 if (status != EXIT_SUCCESS)
63 emit_try_help ();
64 else
66 printf (_("\
67 Usage: %s\n\
68 "), program_name);
70 fputs (_("\
71 Output platform dependent limits in a format useful for shell scripts.\n\
72 \n\
73 "), stdout);
74 fputs (HELP_OPTION_DESCRIPTION, stdout);
75 fputs (VERSION_OPTION_DESCRIPTION, stdout);
76 emit_ancillary_info (PROGRAM_NAME);
78 exit (status);
81 /* Add one to the absolute value of the number whose textual
82 representation is BUF + 1. Do this in-place, in the buffer.
83 Return a pointer to the result, which is normally BUF + 1, but is
84 BUF if the representation grew in size. */
85 static char const *
86 decimal_absval_add_one (char *buf)
88 bool negative = (buf[1] == '-');
89 char *absnum = buf + 1 + negative;
90 char *p = absnum + strlen (absnum);
91 absnum[-1] = '0';
92 while (*--p == '9')
93 *p = '0';
94 ++*p;
95 char *result = MIN (absnum, p);
96 if (negative)
97 *--result = '-';
98 return result;
101 #define PRINT_FLOATTYPE(N, T, FTOASTR, BUFSIZE) \
102 static void \
103 N (T x) \
105 char buf[BUFSIZE]; \
106 FTOASTR (buf, sizeof buf, FTOASTR_LEFT_JUSTIFY, 0, x); \
107 puts (buf); \
110 PRINT_FLOATTYPE (print_FLT, float, ftoastr, FLT_BUFSIZE_BOUND)
111 PRINT_FLOATTYPE (print_DBL, double, dtoastr, DBL_BUFSIZE_BOUND)
112 PRINT_FLOATTYPE (print_LDBL, long double, ldtoastr, LDBL_BUFSIZE_BOUND)
115 main (int argc, char **argv)
117 char limit[1 + MAX (INT_BUFSIZE_BOUND (intmax_t),
118 INT_BUFSIZE_BOUND (uintmax_t))];
120 initialize_main (&argc, &argv);
121 set_program_name (argv[0]);
122 setlocale (LC_ALL, "");
123 bindtextdomain (PACKAGE, LOCALEDIR);
124 textdomain (PACKAGE);
126 atexit (close_stdout);
128 parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
129 VERSION, true, usage, AUTHORS,
130 (char const *) nullptr);
132 #define print_int(TYPE) \
133 sprintf (limit + 1, "%ju", (uintmax_t) TYPE##_MAX); \
134 printf (#TYPE"_MAX=%s\n", limit + 1); \
135 printf (#TYPE"_OFLOW=%s\n", decimal_absval_add_one (limit)); \
136 if (TYPE##_MIN) \
138 sprintf (limit + 1, "%jd", (intmax_t) TYPE##_MIN); \
139 printf (#TYPE"_MIN=%s\n", limit + 1); \
140 printf (#TYPE"_UFLOW=%s\n", decimal_absval_add_one (limit)); \
143 #define print_float(TYPE) \
144 printf (#TYPE"_MIN="); print_##TYPE (TYPE##_MIN); \
145 printf (#TYPE"_MAX="); print_##TYPE (TYPE##_MAX);
147 /* Variable sized ints */
148 print_int (CHAR);
149 print_int (SCHAR);
150 print_int (UCHAR);
151 print_int (SHRT);
152 print_int (INT);
153 print_int (UINT);
154 print_int (LONG);
155 print_int (ULONG);
156 print_int (SIZE);
157 print_int (SSIZE);
158 print_int (TIME_T);
159 print_int (UID_T);
160 print_int (GID_T);
161 print_int (PID_T);
162 print_int (OFF_T);
163 print_int (INTMAX);
164 print_int (UINTMAX);
166 /* Variable sized floats */
167 print_float (FLT);
168 print_float (DBL);
169 print_float (LDBL);
171 return EXIT_SUCCESS;