ls: omit unnecessary test
[coreutils.git] / src / extract-magic
blob571a9eb850ae83598c9fcf598299adf0f22cb26b
1 #!/usr/bin/perl -w
2 # Derive #define directives from specially formatted 'case ...:' statements.
4 # Copyright (C) 2003-2024 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 use strict;
21 use Getopt::Long;
23 (my $VERSION = '$Revision: 1.5 $ ') =~ tr/[0-9].//cd;
24 (my $ME = $0) =~ s|.*/||;
26 END
28 # Nobody ever checks the status of print()s. That's okay, because
29 # if any do fail, we're guaranteed to get an indicator when we close()
30 # the filehandle.
32 # Close stdout now, and if there were no errors, return happy status.
33 # If stdout has already been closed by the script, though, do nothing.
34 defined fileno STDOUT
35 or return;
36 close STDOUT
37 and return;
39 # Errors closing stdout. Indicate that, and hope stderr is OK.
40 warn "$ME: closing standard output: $!\n";
42 # Don't be so arrogant as to assume that we're the first END handler
43 # defined, and thus the last one invoked. There may be others yet
44 # to come. $? will be passed on to them, and to the final _exit().
46 # If it isn't already an error, make it one (and if it _is_ an error,
47 # preserve the value: it might be important).
48 $? ||= 1;
51 sub usage ($)
53 my ($exit_code) = @_;
54 my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
55 if ($exit_code != 0)
57 print $STREAM "Try '$ME --help' for more information.\n";
59 else
61 print $STREAM <<EOF;
62 Usage: $ME [OPTIONS] FILE
64 FIXME: describe
66 OPTIONS:
68 There are two modes of operation, the default, which is to emit
69 #define directives derived from specially formatted 'case' statements,
70 and that with --local, which is to emit a static inline function
71 mapping S_MAGIC_* values to 1, 0, -1, corresponding to known-local,
72 known-remote/distributed/network and unknown, respectively.
74 --local emit an is_local_fs_type function
75 --help display this help and exit
76 --version output version information and exit
78 EOF
80 exit $exit_code;
84 # The default is to print S_MAGIC_* definitions.
85 my $emit_magic = 1;
87 GetOptions
89 local => sub { $emit_magic = 0 },
90 help => sub { usage 0 },
91 version => sub { print "$ME version $VERSION\n"; exit },
92 ) or usage 1;
94 my $fail = 0;
96 @ARGV < 1
97 and (warn "$ME: missing FILE arguments\n"), $fail = 1;
98 1 < @ARGV
99 and (warn "$ME: too many arguments\n"), $fail = 1;
100 $fail
101 and usage 1;
103 my $file = $ARGV[0];
105 open FH, $file
106 or die "$ME: can't open '$file' for reading: $!\n";
108 # For each line like this:
109 # case S_MAGIC_ROMFS: /* 0x7275 */
110 # emit one like this:
111 # # define S_MAGIC_ROMFS 0x7275
112 # Fail if there is a 'case S_MAGIC_.*' line without
113 # a properly formed comment.
115 my $map_comment = <<EOF;
116 /* Map each S_MAGIC_* value to 1, 0 or -1.
117 1 if it is known to be a remote file system type,
118 0 if it is known to be a local file system type, or -1 otherwise. */
120 my $magic_comment = <<EOF;
121 /* Define the magic numbers as given by statfs(2).
122 Please send additions to bug-coreutils\@gnu.org and meskes\@debian.org.
123 This file is generated automatically from $file. */
125 print $emit_magic ? $magic_comment : $map_comment;
127 $emit_magic
128 and print "\n#if defined __linux__ || defined __ANDROID__\n";
129 $emit_magic
130 or print "static inline int\n"
131 . "is_local_fs_type (unsigned long int magic)\n"
132 . "{\n switch (magic)\n {\n";
134 while (defined (my $line = <FH>))
136 $line =~ /^[ \t]+case S_MAGIC_/
137 or next;
138 $line =~
139 m!^[ \t]+case (S_MAGIC_\w+): /\* (0x[0-9A-Fa-f]+) (local|remote) \*/!
140 or (warn "$ME:$file:$.: malformed case S_MAGIC_... line"),
141 $fail = 1, next;
142 my $name = $1;
143 my $magic = $2;
144 my $local = $3 eq 'local' ? 1 : 0;
145 print $emit_magic
146 ? "# define $name $magic\n"
147 : " case $name: return $local;\n";
150 $emit_magic
151 and print <<\EOF;
152 #elif defined __GNU__
153 # include <hurd/hurd_types.h>
154 #endif
156 $emit_magic
157 or printf " default: return -1;\n }\n}\n";
159 close FH;
161 exit $fail;