copy: factor sparse-copying code into its own function, because
[coreutils.git] / src / getlimits.c
blobac20944142a2885a2e0a3e7ade691583ac59dda8
1 /* getlimits - print various platform dependent limits.
2 Copyright (C) 2008-2011 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 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_ancillary_info ();
78 exit (status);
81 /* Add one to the absolute value of the number whose textual
82 representation is BUF + 1. Do this in-place, in the buffer.
83 Return a pointer to the result, which is normally BUF + 1, but is
84 BUF if the representation grew in size. */
85 static char const *
86 decimal_absval_add_one (char *buf)
88 bool negative = (buf[1] == '-');
89 char *absnum = buf + 1 + negative;
90 char *p = absnum + strlen (absnum);
91 absnum[-1] = '0';
92 while (*--p == '9')
93 *p = '0';
94 ++*p;
95 char *result = MIN (absnum, p);
96 if (negative)
97 *--result = '-';
98 return result;
102 main (int argc, char **argv)
104 char limit[1 + MAX (INT_BUFSIZE_BOUND (intmax_t),
105 INT_BUFSIZE_BOUND (uintmax_t))];
107 initialize_main (&argc, &argv);
108 set_program_name (argv[0]);
109 setlocale (LC_ALL, "");
110 bindtextdomain (PACKAGE, LOCALEDIR);
111 textdomain (PACKAGE);
113 initialize_exit_failure (EXIT_FAILURE);
114 atexit (close_stdout);
116 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, VERSION,
117 usage, AUTHORS, (char const *) NULL);
119 #define print_int(TYPE) \
120 sprintf (limit + 1, "%"PRIuMAX, (uintmax_t) TYPE##_MAX); \
121 printf (#TYPE"_MAX=%s\n", limit + 1); \
122 printf (#TYPE"_OFLOW=%s\n", decimal_absval_add_one (limit)); \
123 if (TYPE##_MIN) \
125 sprintf (limit + 1, "%"PRIdMAX, (intmax_t) TYPE##_MIN); \
126 printf (#TYPE"_MIN=%s\n", limit + 1); \
127 printf (#TYPE"_UFLOW=%s\n", decimal_absval_add_one (limit)); \
130 #define print_float(TYPE) \
131 printf (#TYPE"_MIN=%Le\n", (long double)TYPE##_MIN); \
132 printf (#TYPE"_MAX=%Le\n", (long double)TYPE##_MAX);
134 /* Variable sized ints */
135 print_int (CHAR);
136 print_int (SCHAR);
137 print_int (UCHAR);
138 print_int (SHRT);
139 print_int (INT);
140 print_int (UINT);
141 print_int (LONG);
142 print_int (ULONG);
143 print_int (SIZE);
144 print_int (SSIZE);
145 print_int (TIME_T);
146 print_int (UID_T);
147 print_int (GID_T);
148 print_int (PID_T);
149 print_int (OFF_T);
150 print_int (INTMAX);
151 print_int (UINTMAX);
153 /* Variable sized floats */
154 print_float (FLT);
155 print_float (DBL);
156 print_float (LDBL);