/[gentoo-x86]/sys-fs/e2fsprogs/files/fsck_ext2fs.c
Gentoo

Contents of /sys-fs/e2fsprogs/files/fsck_ext2fs.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations) (download) (as text)
Sat Sep 15 08:54:26 2007 UTC (7 years, 5 months ago) by uberlord
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/x-csrc
Add the fsck_ext2fs helper and man page for FreeBSD and add a patch for 1.40.2 to work on FreeBSD.
(Portage version: 2.1.3.9)

1 /*
2 * fsck_ext2fs - wrapper for e2fsck on FreeBSD
3 * Copyright (C) 2004,2006 Matthias Andree <matthias.andree@gmx.de>
4 * redistributable in accordance with the
5 * GNU General Public License v2
6 *
7 * $FreeBSD: ports/sysutils/e2fsprogs/files/fsck_ext2fs.c,v 1.5 2006/07/04 15:47:51 leeym Exp $
8 *
9 * Upstream: $Id: fsck_ext2fs.c,v 1.6 2006/07/02 11:37:49 emma Exp $
10 *
11 * format: gindent -kr
12 */
13
14 #include <sys/types.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <sys/wait.h>
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <errno.h>
23
24 __attribute__ ((noreturn))
25 static int die(const char *tag)
26 {
27 perror(tag);
28 exit(EXIT_FAILURE);
29 }
30
31 int main(int argc, char **argv)
32 {
33 int ch, i = 1, force = 0, status, verbose = 0, t;
34 long block = 0;
35 enum { normal, preen, yes, no } mode = normal;
36 char *cmd[256];
37 pid_t pid;
38
39 cmd[0] = "/sbin/e2fsck";
40 while ((ch = getopt(argc, argv, "BFpfnyb:v")) != -1) {
41 switch (ch) {
42 case 'p':
43 mode = preen;
44 break;
45 case 'f':
46 force = 1;
47 break;
48 case 'n':
49 mode = no;
50 break;
51 case 'y':
52 mode = yes;
53 break;
54 case 'b':
55 block = atol(optarg);
56 break;
57 case 'v':
58 verbose++;
59 break;
60 case 'F':
61 /* e2fsck does not support background checking,
62 * hence exit with nonzero status to force
63 * the foreground check. */
64 exit(1);
65 case 'B':
66 default:
67 fprintf(stderr, "%s: unknown option -%c\n",
68 argv[0], optopt);
69 exit(EXIT_FAILURE);
70 }
71 }
72
73 if (force)
74 cmd[i++] = "-f";
75
76 switch (mode) {
77 case normal:
78 /* FreeBSD needs -f to force a check only in context
79 * with -p -- so map normal to force to match
80 * expectations */
81 if (!force)
82 cmd[i++] = "-f";
83 break;
84 case yes:
85 cmd[i++] = "-y";
86 break;
87 case no:
88 cmd[i++] = "-n";
89 break;
90 case preen:
91 cmd[i++] = "-p";
92 break;
93 }
94
95 if (block) {
96 static char b[30];
97
98 sprintf(b, "-b %ld", block);
99 cmd[i++] = b;
100 }
101
102 /* silently limit verbose to 15 so we don't overflow the cmd array */
103 if (verbose > 15)
104 verbose = 15;
105
106 for (t = verbose; t > 1; t--)
107 cmd[i++] = "-v";
108
109 while (optind < argc) {
110 cmd[i++] = argv[optind++];
111 /* sanity check so we don't overflow the cmd buffer */
112 if (i+1 == sizeof(cmd)/sizeof(cmd[0])) {
113 errno = E2BIG;
114 die(argv[0]);
115 }
116 }
117
118 cmd[i++] = 0;
119
120 if (verbose) {
121 for (i=0; cmd[i]; i++)
122 fputs(cmd[i], stderr),
123 fputc(' ', stderr);
124 fputc('\n', stderr);
125 }
126
127 pid = fork();
128 switch (pid) {
129 case -1:
130 /* error */
131 die("fork");
132 break;
133 case 0:
134 /* child */
135 (void) execv(cmd[0], cmd);
136 perror("execve");
137 _exit(127);
138 default:
139 /* parent */
140 if (pid != waitpid(pid, &status, 0))
141 die("waitpid");
142 if (WIFSIGNALED(status)
143 || (WIFEXITED(status) && WEXITSTATUS(status) >= 4))
144 exit(EXIT_FAILURE);
145 }
146 exit(EXIT_SUCCESS);
147 }

  ViewVC Help
Powered by ViewVC 1.1.20