udftools

- Main Page
- Classes
- Files

- File List
- File Members

All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
options.c
Go to the documentation of this file.
1 /*
2 * options.c
3 *
4 * Copyright (c) 2016 Vojtech Vladyka <vojtech.vladyka@gmail.com>
5 * All rights reserved.
6 *
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 2 of the License, or
10 * (at your option) any later version.
11 *
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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <malloc.h>
27
28 #include "libudffs.h"
29 #include "options.h"
30 #include "utils.h"
31
32 verbosity_e verbose = NONE;
33 int interactive = 0;
34 int autofix = 0;
35
39 static struct option long_options[] =
40 {
41 /* These options set a flag. */
42 {"verbose", no_argument, 0, 'v'},
43 {"blocksize", required_argument, 0, 'B'},
44 {"interactive", no_argument, 0, 'i'},
45 {"autofix", no_argument, 0, 'p'},
46 {"check", no_argument, 0, 'c'},
47 {"help", no_argument, 0, 'h'},
48 {0, 0, 0, 0}
49 };
50
54 static char * help[] = {
55 "Increase verbosity. Without it are printed only error messages, -v prints warnings, -vv is for humans, -vvv is for developers and curious people.",
56 "Medium block size. Mandatory parameter, can be 512, 1024, 2048 or 4096.",
57 "Medium is will be fixed interactivelly and all fixings must be authorized by user.",
58 "Medium is will be fixed automatically. All found errors will be fixed if possible.",
59 "Medium will be only checked. This is default behavior, but this flag override -p.",
60 "This help message.",
61 ""
62 };
63
67 void usage(void)
68 {
69 int i;
70
71 printf("udffsck " UDFFSCK_VERSION " from " PACKAGE_NAME " " PACKAGE_VERSION ".");
72 printf("\nUsage:\n\tudffsck [-icpvvvh] [-B blocksize] medium\n");
73 printf("Options:\n");
74 for (i = 0; long_options[i].name != NULL; i++) {
75 if (long_options[i].flag != 0)
76 printf(" --%s\t", long_options[i].name);
77 else
78 printf(" -%c\t", long_options[i].val);
79 printf(" %s\n", help[i]);
80 }
81 printf("Return codes:\n");
82 printf(" 0 - No error\n"
83 " 1 - Filesystem errors were fixed\n"
84 /* " 2 - Filesystem errors were fixed, reboot is recomended\n"*/
85 " 4 - Filesystem errors remained unfixed\n"
86 " 8 - Program error\n"
87 " 16 - Wrong input parameters\n"
88 " 32 - Check was interrupted by user request\n"
89 /* " 128 - Shared library error"*/
90 "\n");
91 exit(32);
92 }
93
99 void parse_args(int argc, char *argv[], char **path, int *blocksize)
100 {
101 int c;
102
103 while (1)
104 {
105 /* getopt_long stores the option index here. */
106 int option_index = 0;
107
108 c = getopt_long (argc, argv, "vB:ipch", long_options, &option_index);
109
110 /* Detect the end of the options. */
111 if (c == -1)
112 break;
113
114 switch (c)
115 {
116 case 0:
117 /* If this option set a flag, do nothing else now. */
118 if (long_options[option_index].flag != 0)
119 break;
120 printf ("option %s", long_options[option_index].name);
121 if (optarg)
122 printf (" with arg %s", optarg);
123 printf ("\n");
124 break;
125
126 case 'B':
127 *blocksize = strtol(optarg, NULL, 10);
128 printf("Device block size: %d\n", *blocksize);
129 break;
130
131 case 'i':
132 printf ("Medium will be fixed interactively. Expect questions.\n");
133 interactive = 1;
134 break;
135
136 case 'p':
137 printf ("We try to fix medium automaticaly.\n");
138 autofix = 1;
139 break;
140
141 case 'c':
142 printf ("Medium will be only checked. No corrections.\n");
143 autofix = 0;
144 break;
145
146 case 'v':
147 verbosity ++;
148 if(verbosity > DBG)
149 verbosity = DBG;
150 printf("Verbosity increased to %s.\n", verbosity_level_str(verbosity));
151 break;
152
153 case 'h':
154 usage();
155 break;
156
157 default:
158 printf("Unrecognized option -%c.\n", c);
159 usage();
160 break;
161 }
162 }
163
164 /* Print any remaining command line arguments (not options). */
165 if (optind < argc)
166 {
167 dbg("Optind: %d\n", optind);
168 dbg("non-option ARGV-elements: ");
169 while (optind < argc) {
170 *path = (char*)malloc(strlen(argv[optind])+1);
171 strcpy(*path, argv[optind]);
172 dbg("%s ", *path);
173 optind++;
174 if(optind > 2) //We accept one medium at a time.
175 break;
176 }
177 dbg("\n");
178 }
179 dbg("Param parse done.\n");
180 }
181
verbosity_e
verbosity_e
Definition: utils.h:38
NONE
Definition: utils.h:39
autofix
int autofix
Definition: options.c:34
verbosity
verbosity_e verbosity
Definition: utils.c:49
utils.h
dbg
void dbg(const char *format,...)
Debug output.
Definition: utils.c:296
options.h
long_options
struct option long_options[]
Definition: options.c:33
DBG
Definition: utils.h:42
parse_args
void parse_args(int argc, char *argv[], struct cdrw_disc *disc, char *device)
Definition: options.c:69
verbosity_level_str
char * verbosity_level_str(verbosity_e lvl)
Verbosity level to string.
Definition: utils.c:420
verbose
verbosity_e verbose
Definition: options.c:32
help
static char * help[]
Definition: options.c:54
usage
void usage(void)
Definition: options.c:56
libudffs.h
UDFFSCK_VERSION
#define UDFFSCK_VERSION
udffsck version number
Definition: udffsck.h:34
interactive
int interactive
Definition: options.c:33