Get more help from gcc, add -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2...
[sysvinit.git] / src / fstab-decode.c
CommitLineData
55f242bf
DWF
1/* fstab-decode(8).
2
3Copyright (c) 2006 Red Hat, Inc. All rights reserved.
4
5This copyrighted material is made available to anyone wishing to use, modify,
6copy, or redistribute it subject to the terms and conditions of the GNU General
7Public License v.2.
8
9This program is distributed in the hope that it will be useful, but WITHOUT ANY
10WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License along with
14this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
17Author: Miloslav Trmac <mitr@redhat.com> */
18
19#include <errno.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24
25/* Decode the fstab-encoded string in place. */
26static void
27decode(char *s)
28{
29 const char *src;
30 char *dest;
31
32 src = s;
33 dest = s;
34 while (*src != '\0') {
35 if (*src != '\\')
36 *dest = *src++;
37 else {
38 static const struct repl {
39 char orig[4];
40 size_t len;
41 char new;
42 } repls[] = {
43#define R(X, Y) { X, sizeof(X) - 1, Y }
44 R("\\", '\\'),
45 R("011", '\t'),
46 R("012", '\n'),
47 R("040", ' '),
48 R("134", '\\')
49#undef R
50 };
51
52 size_t i;
53
54 for (i = 0; i < sizeof (repls) / sizeof (repls[0]);
55 i++) {
56 if (memcmp(src + 1, repls[i].orig,
57 repls[i].len) == 0) {
58 *dest = repls[i].new;
59 src += 1 + repls[i].len;
60 goto found;
61 }
62 }
63 *dest = *src++;
64 found:
65 ;
66 }
67 dest++;
68 }
69 *dest = '\0';
70}
71
72int
73main (int argc, char *argv[])
74{
75 size_t i;
76
77 if (argc < 2) {
78 fprintf(stderr, "Usage: fstab-decode command [arguments]\n");
79 return EXIT_FAILURE;
80 }
81 for (i = 2; i < (size_t)argc; i++)
82 decode(argv[i]);
83 execvp(argv[1], argv + 1);
84 fprintf(stderr, "fstab-decode: %s: %s\n", argv[1], strerror(errno));
85 return 127;
86}