]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/init.c
Sync user and kernel code, make xfs_io -f option useful.
[thirdparty/xfsprogs-dev.git] / io / init.c
1 /*
2 * Copyright (c) 2003 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33 #include <libxfs.h>
34 #include "input.h"
35 #include "command.h"
36
37 int fdesc;
38 char *fname;
39 char *progname;
40 int exitcode;
41
42 int readonly;
43 int directio;
44 int realtime;
45 int append;
46 int osync;
47 int trunc;
48
49 static int ncmdline;
50 static char **cmdline;
51
52 void
53 usage(void)
54 {
55 fprintf(stderr,
56 _("Usage: %s [-r] [-p prog] [-c cmd]... file\n"), progname);
57 exit(1);
58 }
59
60 void
61 init(
62 int argc,
63 char **argv)
64 {
65 int fflag = 0;
66 int c;
67
68 progname = basename(argv[0]);
69 setlocale(LC_ALL, "");
70 bindtextdomain(PACKAGE, LOCALEDIR);
71 textdomain(PACKAGE);
72
73 while ((c = getopt(argc, argv, "ac:dfp:rstVx")) != EOF) {
74 switch (c) {
75 case 'a': /* append */
76 append = 1;
77 break;
78 case 'c': /* commands */
79 ncmdline++;
80 cmdline = realloc(cmdline, sizeof(char*) * (ncmdline));
81 if (!cmdline) {
82 perror("realloc");
83 exit(1);
84 }
85 cmdline[ncmdline-1] = optarg;
86 break;
87 case 'd': /* directIO */
88 directio = 1;
89 break;
90 case 'f': /* create */
91 fflag = 1;
92 break;
93 case 'p': /* progname */
94 progname = optarg;
95 break;
96 case 'r': /* readonly */
97 readonly = 1;
98 break;
99 case 's': /* sync */
100 osync = 1;
101 break;
102 case 't': /* truncate */
103 trunc = 1;
104 break;
105 case 'x': /* realtime */
106 realtime = 1;
107 break;
108 case 'V':
109 printf(_("%s version %s\n"), progname, VERSION);
110 exit(0);
111 default:
112 usage();
113 }
114 }
115
116 if (optind != argc - 1)
117 usage();
118
119 fname = strdup(argv[optind]);
120 if ((fdesc = openfile(fname, append, fflag, directio,
121 readonly, osync, trunc, realtime)) < 0)
122 exit(1);
123
124 init_commands();
125 }
126
127 int
128 main(
129 int argc,
130 char **argv)
131 {
132 int c, i, done = 0;
133 char *input;
134 char **v;
135
136 init(argc, argv);
137
138 for (i = 0; !done && i < ncmdline; i++) {
139 v = breakline(cmdline[i], &c);
140 if (c)
141 done = command(c, v);
142 free(v);
143 }
144 if (cmdline) {
145 free(cmdline);
146 return exitcode;
147 }
148 while (!done) {
149 if ((input = fetchline()) == NULL)
150 break;
151 v = breakline(input, &c);
152 if (c)
153 done = command(c, v);
154 doneline(input, v);
155 }
156 return exitcode;
157 }