doc: improve various BLOCKSIZE and SIZE help
[coreutils.git] / src / getlimits.c
blob82cd34531e7b3084a5403401ce00a58d14c5d659
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. */
20 #include <stdio.h>
21 #include <sys/types.h>
23 #include "system.h"
24 #include "c-ctype.h"
25 #include "long-options.h"
27 #define PROGRAM_NAME "getlimits"
29 #define AUTHORS proper_name_utf8 ("Padraig Brady", "P\303\241draig Brady")
31 #ifndef TIME_T_MAX
32 # define TIME_T_MAX TYPE_MAXIMUM (time_t)
33 #endif
35 #ifndef TIME_T_MIN
36 # define TIME_T_MIN TYPE_MINIMUM (time_t)
37 #endif
39 #ifndef SSIZE_MIN
40 # define SSIZE_MIN TYPE_MINIMUM (ssize_t)
41 #endif
43 #ifndef PID_T_MIN
44 # define PID_T_MIN TYPE_MINIMUM (pid_t)
45 #endif
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. */
50 #define SIZE_MIN 0
51 #define UCHAR_MIN 0
52 #define UINT_MIN 0
53 #define ULONG_MIN 0
54 #define UINTMAX_MIN 0
55 #define UID_T_MIN 0
56 #define GID_T_MIN 0
58 void
59 usage (int status)
61 if (status != EXIT_SUCCESS)
62 fprintf (stderr, _("Try `%s --help' for more information.\n"),
63 program_name);
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_bug_reporting_address ();
78 exit (status);
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:
84 * I.E.
85 * 1 + 1 -> 2
86 * -1 + -1 -> -2
87 * -1 + 1 -> -2
88 * 1 + -1 -> -2
90 static char *
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;
100 int carry = 0;
101 *rp = '\0';
103 while (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)
109 break;
110 carry = t1 > '0' + '9' || t1 == ' ' + '9' + 1;
111 t1 += 6 * carry;
112 *--rp = (t1 & 0x0F) | 0x30; /* top nibble to ASCII */
114 if ((d1 >= str1 && *d1 == '-') || (d2 >= str2 && (*d2 == '-')))
115 *--rp = '-';
117 if (rp != result)
118 memmove (result, rp, rlen - (rp - result));
120 return result;
124 main (int argc, char **argv)
126 char limit[64]; /* big enough for 128 bit at least */
127 char *oflow;
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); \
146 free (oflow); \
147 if (TYPE##_MIN) \
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); \
153 free (oflow); \
156 /* Variable sized ints */
157 print_int (CHAR);
158 print_int (SCHAR);
159 print_int (UCHAR);
160 print_int (SHRT);
161 print_int (INT);
162 print_int (UINT);
163 print_int (LONG);
164 print_int (ULONG);
165 print_int (SIZE);
166 print_int (SSIZE);
167 print_int (TIME_T);
168 print_int (UID_T);
169 print_int (GID_T);
170 print_int (PID_T);
171 print_int (OFF_T);
172 print_int (INTMAX);
173 print_int (UINTMAX);