1 /* getlimits - print various platform dependent limits.
2 Copyright (C) 2008-2023 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. */
21 #include <sys/types.h>
26 #include "long-options.h"
28 #define PROGRAM_NAME "getlimits"
30 #define AUTHORS proper_name_lite ("Padraig Brady", "P\303\241draig Brady")
33 # define TIME_T_MAX TYPE_MAXIMUM (time_t)
37 # define TIME_T_MIN TYPE_MINIMUM (time_t)
41 # define SSIZE_MIN TYPE_MINIMUM (ssize_t)
45 # define PID_T_MIN TYPE_MINIMUM (pid_t)
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. */
62 if (status
!= EXIT_SUCCESS
)
71 Output platform dependent limits in a format useful for shell scripts.\n\
74 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
75 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
76 emit_ancillary_info (PROGRAM_NAME
);
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. */
86 decimal_absval_add_one (char *buf
)
88 bool negative
= (buf
[1] == '-');
89 char *absnum
= buf
+ 1 + negative
;
90 char *p
= absnum
+ strlen (absnum
);
95 char *result
= MIN (absnum
, p
);
101 #define PRINT_FLOATTYPE(N, T, FTOASTR, BUFSIZE) \
106 FTOASTR (buf, sizeof buf, FTOASTR_LEFT_JUSTIFY, 0, x); \
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)); \
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 */
166 /* Variable sized floats */