1 /* getlimits - print various platform dependent limits.
2 Copyright (C) 2008-2009 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>
25 #include "long-options.h"
27 #define PROGRAM_NAME "getlimits"
29 #define AUTHORS proper_name_utf8 ("Padraig Brady", "P\303\241draig Brady")
32 # define TIME_T_MAX TYPE_MAXIMUM (time_t)
36 # define TIME_T_MIN TYPE_MINIMUM (time_t)
40 # define SSIZE_MIN TYPE_MINIMUM (ssize_t)
44 # define PID_T_MIN TYPE_MINIMUM (pid_t)
47 /* These are not interesting to print.
48 * Instead of these defines it would be nice to be able to do
49 * #ifdef (TYPE##_MIN) in function macro below. */
61 if (status
!= EXIT_SUCCESS
)
62 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
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_bug_reporting_address ();
81 /* Add absolute values of ascii decimal strings.
82 * Strings can have leading spaces.
83 * If any string has a '-' it's preserved in the output:
91 decimal_ascii_add (const char *str1
, const char *str2
)
93 int len1
= strlen (str1
);
94 int len2
= strlen (str2
);
95 int rlen
= MAX (len1
, len2
) + 3; /* space for extra digit or sign + NUL */
96 char *result
= xmalloc (rlen
);
97 char *rp
= result
+ rlen
- 1;
98 const char *d1
= str1
+ len1
- 1;
99 const char *d2
= str2
+ len2
- 1;
105 char c1
= (d1
< str1
? ' ' : (*d1
== '-' ? ' ' : *d1
--));
106 char c2
= (d2
< str2
? ' ' : (*d2
== '-' ? ' ' : *d2
--));
107 char t1
= c1
+ c2
+ carry
; /* ASCII digits are BCD */
108 if (!c_isdigit (c1
) && !c_isdigit (c2
) && !carry
)
110 carry
= t1
> '0' + '9' || t1
== ' ' + '9' + 1;
112 *--rp
= (t1
& 0x0F) | 0x30; /* top nibble to ASCII */
114 if ((d1
>= str1
&& *d1
== '-') || (d2
>= str2
&& (*d2
== '-')))
118 memmove (result
, rp
, rlen
- (rp
- result
));
124 main (int argc
, char **argv
)
126 char limit
[64]; /* big enough for 128 bit at least */
129 initialize_main (&argc
, &argv
);
130 set_program_name (argv
[0]);
131 setlocale (LC_ALL
, "");
132 bindtextdomain (PACKAGE
, LOCALEDIR
);
133 textdomain (PACKAGE
);
135 initialize_exit_failure (EXIT_FAILURE
);
136 atexit (close_stdout
);
138 parse_long_options (argc
, argv
, PROGRAM_NAME
, PACKAGE_NAME
, VERSION
,
139 usage
, AUTHORS
, (char const *) NULL
);
141 #define print_int(TYPE) \
142 snprintf (limit, sizeof(limit), "%"PRIuMAX, (uintmax_t)TYPE##_MAX); \
143 printf (#TYPE"_MAX=%s\n", limit); \
144 oflow = decimal_ascii_add (limit, "1"); \
145 printf (#TYPE"_OFLOW=%s\n", oflow); \
149 snprintf (limit, sizeof(limit), "%"PRIdMAX, (intmax_t)TYPE##_MIN); \
150 printf (#TYPE"_MIN=%s\n", limit); \
151 oflow = decimal_ascii_add (limit, "-1"); \
152 printf (#TYPE"_UFLOW=%s\n", oflow); \
156 /* Variable sized ints */