maint: use new emit_try_help in place of equivalent fprintf
[coreutils/ericb.git] / src / getlimits.c
blob1403ee58f506a662c7577f31cf44f90c5e166903
1 /* getlimits - print various platform dependent limits.
2 Copyright (C) 2008-2012 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 "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 emit_try_help ();
63 else
65 printf (_("\
66 Usage: %s\n\
67 "), program_name);
69 fputs (_("\
70 Output platform dependent limits in a format useful for shell scripts.\n\
71 \n\
72 "), stdout);
73 fputs (HELP_OPTION_DESCRIPTION, stdout);
74 fputs (VERSION_OPTION_DESCRIPTION, stdout);
75 emit_ancillary_info ();
77 exit (status);
80 /* Add one to the absolute value of the number whose textual
81 representation is BUF + 1. Do this in-place, in the buffer.
82 Return a pointer to the result, which is normally BUF + 1, but is
83 BUF if the representation grew in size. */
84 static char const *
85 decimal_absval_add_one (char *buf)
87 bool negative = (buf[1] == '-');
88 char *absnum = buf + 1 + negative;
89 char *p = absnum + strlen (absnum);
90 absnum[-1] = '0';
91 while (*--p == '9')
92 *p = '0';
93 ++*p;
94 char *result = MIN (absnum, p);
95 if (negative)
96 *--result = '-';
97 return result;
101 main (int argc, char **argv)
103 char limit[1 + MAX (INT_BUFSIZE_BOUND (intmax_t),
104 INT_BUFSIZE_BOUND (uintmax_t))];
106 initialize_main (&argc, &argv);
107 set_program_name (argv[0]);
108 setlocale (LC_ALL, "");
109 bindtextdomain (PACKAGE, LOCALEDIR);
110 textdomain (PACKAGE);
112 initialize_exit_failure (EXIT_FAILURE);
113 atexit (close_stdout);
115 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, VERSION,
116 usage, AUTHORS, (char const *) NULL);
118 #define print_int(TYPE) \
119 sprintf (limit + 1, "%"PRIuMAX, (uintmax_t) TYPE##_MAX); \
120 printf (#TYPE"_MAX=%s\n", limit + 1); \
121 printf (#TYPE"_OFLOW=%s\n", decimal_absval_add_one (limit)); \
122 if (TYPE##_MIN) \
124 sprintf (limit + 1, "%"PRIdMAX, (intmax_t) TYPE##_MIN); \
125 printf (#TYPE"_MIN=%s\n", limit + 1); \
126 printf (#TYPE"_UFLOW=%s\n", decimal_absval_add_one (limit)); \
129 #define print_float(TYPE) \
130 printf (#TYPE"_MIN=%Le\n", (long double)TYPE##_MIN); \
131 printf (#TYPE"_MAX=%Le\n", (long double)TYPE##_MAX);
133 /* Variable sized ints */
134 print_int (CHAR);
135 print_int (SCHAR);
136 print_int (UCHAR);
137 print_int (SHRT);
138 print_int (INT);
139 print_int (UINT);
140 print_int (LONG);
141 print_int (ULONG);
142 print_int (SIZE);
143 print_int (SSIZE);
144 print_int (TIME_T);
145 print_int (UID_T);
146 print_int (GID_T);
147 print_int (PID_T);
148 print_int (OFF_T);
149 print_int (INTMAX);
150 print_int (UINTMAX);
152 /* Variable sized floats */
153 print_float (FLT);
154 print_float (DBL);
155 print_float (LDBL);