3 # Print values of C constants.
5 # Copyright (C) 2012 Simon Ruderich
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 our $VERSION = '0.01';
31 my $option_version = 0;
32 my @option_include = ();
33 my $option_pathconf = undef;
35 my $option_sysconf = 0;
36 my $option_type = 'd';
37 if (not Getopt::Long::GetOptions(
38 'help|h|?' => \$option_help,
39 'version|v' => \$option_version,
41 'include|i=s' => \@option_include,
42 'pathconf|p=s' => \$option_pathconf,
43 'quiet|q' => \$option_quiet,
44 'sysconf|s' => \$option_sysconf,
45 'type|t=s' => \$option_type,
48 Pod::Usage::pod2usage(2);
52 Pod::Usage::pod2usage(1);
54 if ($option_version) {
56 print-constant $VERSION Copyright (C) 2012 Simon Ruderich
58 This program is free software: you can redistribute it and/or modify
59 it under the terms of the GNU General Public License as published by
60 the Free Software Foundation, either version 3 of the License, or
61 (at your option) any later version.
63 This program is distributed in the hope that it will be useful,
64 but WITHOUT ANY WARRANTY; without even the implied warranty of
65 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
66 GNU General Public License for more details.
68 You should have received a copy of the GNU General Public License
69 along with this program. If not, see <http://www.gnu.org/licenses/>.
74 # We need the constant name as argument.
75 if (scalar @ARGV != 1) {
77 Pod::Usage::pod2usage(2);
81 my $constant = $ARGV[0];
82 # Check for non-constant name inputs - also prevents running arbitrary code on
84 if (not $constant =~ /^[a-zA-Z0-9_]+$/) {
85 print STDERR "Invalid constant name: '$constant'!\n";
90 if (not $option_type =~ /^[a-zA-Z]+$/) {
91 print STDERR "Invalid type: '$option_type'!\n";
94 if ($option_sysconf and defined $option_pathconf) {
95 print STDERR "--sysconf and --pathconf can't be used together!\n";
98 foreach my $include (@option_include) {
99 if (not $include =~ m{^[a-zA-Z0-9./_-]+$}) {
100 print STDERR "Invalid include: '$include'!\n";
105 my $constant_display = $constant;
106 # Pass the constant to sysconf()/pathconf() instead of using it directly.
107 if ($option_sysconf) {
108 $constant = "sysconf($constant)";
109 $constant_display = $constant;
110 } elsif (defined $option_pathconf) {
111 $constant_display = "pathconf(\"$option_pathconf\", $constant)";
112 # Use the current directory to prevent escape problems for the path, see
114 $constant = "pathconf(\".\", $constant)";
118 # Use "limits.h" and "unistd.h" as default include - but only if the user
119 # specified no other includes, in case it conflicts with "limits.h".
120 if (scalar @option_include == 0) {
121 push @option_include, 'limits.h';
122 push @option_include, 'unistd.h';
126 # Temporary directory where the following C program which prints the constant
128 my $tempdir = File::Temp::tempdir(CLEANUP => 1) or die $!;
129 chdir $tempdir or die $!;
131 # Write the C file which prints the constant value.
132 my $c_template = <<'EOF';
137 int main(int argc, char **argv) {
143 open my $fh, '>', 'constant.c' or die $!;
144 printf $fh $c_template,
145 join("\n", map { "#include <$_>" } @option_include),
150 # Compile the constant C file.
151 my @args = ('cc', '-o', 'constant', 'constant.c');
152 if (system(@args) != 0) {
153 print STDERR "Compilation failure (message above).\n";
159 # pathconf() needs a path, we use the current directory - therefore we have to
160 # chdir to the given path. This prevents escape problems with the path.
161 if (defined $option_pathconf) {
162 chdir $option_pathconf;
165 my $value = `$tempdir/constant`;
170 print "$constant_display: $value";
173 # Change the working directory or File::Temp can't remove our temporary
182 print-constant - print values of C constants
186 B<print-constant> [I<options>] I<CONSTANT_NAME>
190 print-constant prints the values of C constants. Useful to find out certain
191 system constants without having to write a small C program to print them.
197 =item B<-i> I<include>, B<--include> I<include>
199 Use I<include> as include file. Adds C<#include E<lt>includeE<gt>> to the
200 beginning of the C file used to print the constant. By default C<limits.h> and
201 C<unistd.h> are used as headers. If this option is specified I<no> default
202 includes are used. Can be specified multiple times.
204 =item B<-p> I<path>, B<--pathconf> I<path>
206 Pass the constant to pathconf(3) and print the result. I<path> is the first
207 argument for pathconf(3). Conflicts with B<--sysconf>.
209 =item B<-q>, B<--quiet>
211 Display only the constant's value.
213 =item B<-s>, B<--sysconf>
215 Pass the constant to sysconf(3) and print the result. Conflicts with
218 =item B<-t> I<type>, B<--type> I<type>
220 Use I<type> as type of the constant, used in printf(3). By default C<d> is
223 =item B<-h>, B<-?>, B<--help>
225 Print available options.
227 =item B<-v>, B<--version>
229 Print version number and license.
235 Simon Ruderich, E<lt>simon@ruderich.orgE<gt>
237 =head1 COPYRIGHT AND LICENSE
239 Copyright (C) 2012 by Simon Ruderich
241 This program is free software: you can redistribute it and/or modify
242 it under the terms of the GNU General Public License as published by
243 the Free Software Foundation, either version 3 of the License, or
244 (at your option) any later version.
246 This program is distributed in the hope that it will be useful,
247 but WITHOUT ANY WARRANTY; without even the implied warranty of
248 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
249 GNU General Public License for more details.
251 You should have received a copy of the GNU General Public License
252 along with this program. If not, see <http://www.gnu.org/licenses/>.
256 L<sysconf(3)>, L<pathconf(3)>