build: support fully excluding stdbuf from the build
[coreutils/ericb.git] / src / getlimits.c
blob93d4035a7845ca508cddac49d6863aef159474cd
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. */
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <float.h>
24 #include "system.h"
25 #include "c-ctype.h"
26 #include "long-options.h"
28 #define PROGRAM_NAME "getlimits"
30 #define AUTHORS proper_name_utf8 ("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 fprintf (stderr, _("Try `%s --help' for more information.\n"),
64 program_name);
65 else
67 printf (_("\
68 Usage: %s\n\
69 "), program_name);
71 fputs (_("\
72 Output platform dependent limits in a format useful for shell scripts.\n\
73 \n\
74 "), stdout);
75 fputs (HELP_OPTION_DESCRIPTION, stdout);
76 fputs (VERSION_OPTION_DESCRIPTION, stdout);
77 emit_ancillary_info ();
79 exit (status);
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:
85 * I.E.
86 * 1 + 1 -> 2
87 * -1 + -1 -> -2
88 * -1 + 1 -> -2
89 * 1 + -1 -> -2
91 static char *
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;
101 int carry = 0;
102 *rp = '\0';
104 while (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)
110 break;
111 carry = t1 > '0' + '9' || t1 == ' ' + '9' + 1;
112 t1 += 6 * carry;
113 *--rp = (t1 & 0x0F) | 0x30; /* top nibble to ASCII */
115 if ((d1 >= str1 && *d1 == '-') || (d2 >= str2 && (*d2 == '-')))
116 *--rp = '-';
118 if (rp != result)
119 memmove (result, rp, rlen - (rp - result));
121 return result;
125 main (int argc, char **argv)
127 char limit[64]; /* big enough for 128 bit integers at least */
128 char *oflow;
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); \
147 free (oflow); \
148 if (TYPE##_MIN) \
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); \
154 free (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 */
162 print_int (CHAR);
163 print_int (SCHAR);
164 print_int (UCHAR);
165 print_int (SHRT);
166 print_int (INT);
167 print_int (UINT);
168 print_int (LONG);
169 print_int (ULONG);
170 print_int (SIZE);
171 print_int (SSIZE);
172 print_int (TIME_T);
173 print_int (UID_T);
174 print_int (GID_T);
175 print_int (PID_T);
176 print_int (OFF_T);
177 print_int (INTMAX);
178 print_int (UINTMAX);
180 /* Variable sized floats */
181 print_float (FLT);
182 print_float (DBL);
183 print_float (LDBL);