1 /* getlimits - print various platform dependent limits.
2 Copyright (C) 2008-2010 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 <http://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_utf8 ("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
)
63 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
72 Output platform dependent limits in a format useful for shell scripts.\n\
75 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
76 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
77 emit_ancillary_info ();
82 /* Add absolute values of ascii decimal strings.
83 * Strings can have leading spaces.
84 * If any string has a '-' it's preserved in the output:
92 decimal_ascii_add (const char *str1
, const char *str2
)
94 int len1
= strlen (str1
);
95 int len2
= strlen (str2
);
96 int rlen
= MAX (len1
, len2
) + 3; /* space for extra digit or sign + NUL */
97 char *result
= xmalloc (rlen
);
98 char *rp
= result
+ rlen
- 1;
99 const char *d1
= str1
+ len1
- 1;
100 const char *d2
= str2
+ len2
- 1;
106 char c1
= (d1
< str1
? ' ' : (*d1
== '-' ? ' ' : *d1
--));
107 char c2
= (d2
< str2
? ' ' : (*d2
== '-' ? ' ' : *d2
--));
108 char t1
= c1
+ c2
+ carry
; /* ASCII digits are BCD */
109 if (!c_isdigit (c1
) && !c_isdigit (c2
) && !carry
)
111 carry
= t1
> '0' + '9' || t1
== ' ' + '9' + 1;
113 *--rp
= (t1
& 0x0F) | 0x30; /* top nibble to ASCII */
115 if ((d1
>= str1
&& *d1
== '-') || (d2
>= str2
&& (*d2
== '-')))
119 memmove (result
, rp
, rlen
- (rp
- result
));
125 main (int argc
, char **argv
)
127 char limit
[64]; /* big enough for 128 bit integers at least */
130 initialize_main (&argc
, &argv
);
131 set_program_name (argv
[0]);
132 setlocale (LC_ALL
, "");
133 bindtextdomain (PACKAGE
, LOCALEDIR
);
134 textdomain (PACKAGE
);
136 initialize_exit_failure (EXIT_FAILURE
);
137 atexit (close_stdout
);
139 parse_long_options (argc
, argv
, PROGRAM_NAME
, PACKAGE_NAME
, VERSION
,
140 usage
, AUTHORS
, (char const *) NULL
);
142 #define print_int(TYPE) \
143 snprintf (limit, sizeof limit, "%"PRIuMAX, (uintmax_t)TYPE##_MAX); \
144 printf (#TYPE"_MAX=%s\n", limit); \
145 oflow = decimal_ascii_add (limit, "1"); \
146 printf (#TYPE"_OFLOW=%s\n", oflow); \
150 snprintf (limit, sizeof limit, "%"PRIdMAX, (intmax_t)TYPE##_MIN); \
151 printf (#TYPE"_MIN=%s\n", limit); \
152 oflow = decimal_ascii_add (limit, "-1"); \
153 printf (#TYPE"_UFLOW=%s\n", oflow); \
157 #define print_float(TYPE) \
158 printf (#TYPE"_MIN=%Le\n", (long double)TYPE##_MIN); \
159 printf (#TYPE"_MAX=%Le\n", (long double)TYPE##_MAX);
161 /* Variable sized ints */
180 /* Variable sized floats */