fix lintian issues
[debian/mtx] / scsieject.c
1 /* Copyright 2007-2008, Robert Nelson <robertn@the-nelsons.org>\r
2  *   Released under terms of the GNU General Public License as\r
3  * required by the license on 'mtxl.c'.\r
4  * $Date: 2007-01-28 19:23:33 -0800 (Sun, 28 Jan 2007) $\r
5  * $Revision: 125 $\r
6  */\r
7 \r
8 /* This is a generic SCSI device control program. It operates by\r
9  * directly sending commands to the device.\r
10  */\r
11 \r
12 /* \r
13  *      Commands:\r
14  *              load -- Load medium\r
15  *              unload -- Unload medium\r
16  *              start -- Start device\r
17  *              stop -- Stop device\r
18  *              lock -- Lock medium\r
19  *              unlock -- Unlock medium\r
20  */\r
21 \r
22 #include <stdio.h>\r
23 #include <string.h>\r
24 \r
25 #include "mtx.h"\r
26 #include "mtxl.h"\r
27 \r
28 #if HAVE_UNISTD_H\r
29 #include <unistd.h>\r
30 #endif\r
31 \r
32 #if HAVE_SYS_TYPES_H\r
33 #include <sys/types.h>\r
34 #endif\r
35 \r
36 #ifdef _MSC_VER\r
37 #include <io.h>\r
38 #endif\r
39 \r
40 char *argv0;\r
41 \r
42 /* the device handle we're operating upon. */\r
43 static char *device;  /* the device name. */\r
44 static DEVICE_TYPE DeviceFD = (DEVICE_TYPE) -1;\r
45 \r
46 static int S_load(void);\r
47 static int S_unload(void);\r
48 static int S_start(void);\r
49 static int S_stop(void);\r
50 static int S_lock(void);\r
51 static int S_unlock(void);\r
52 \r
53 struct command_table_struct\r
54 {\r
55         char *name;\r
56         int (*command)(void);\r
57 }\r
58         command_table[] =\r
59 {\r
60         { "load", S_load },\r
61         { "unload", S_unload },\r
62         { "start", S_start },\r
63         { "stop", S_stop },\r
64         { "lock", S_lock },\r
65         { "unlock", S_unlock },\r
66         { NULL, NULL } /* terminate list */\r
67 };\r
68 \r
69 void Usage(void)\r
70 {\r
71         FatalError("Usage: scsieject -f <generic-device> <command> where <command> is:\n load | unload | start | stop | lock | unlock\n");\r
72 }\r
73 \r
74 /* open_device() -- set the 'DeviceFD' variable.... */\r
75 void open_device(void)\r
76 {\r
77         if (DeviceFD != -1)\r
78         {\r
79                 SCSI_CloseDevice("Unknown", DeviceFD);\r
80         }\r
81 \r
82         DeviceFD = SCSI_OpenDevice(device);\r
83 }\r
84 \r
85 /* we see if we've got a file open. If not, we open one :-(. Then\r
86  * we execute the actual command. Or not :-(. \r
87  */ \r
88 int execute_command(struct command_table_struct *command)\r
89 {\r
90         /*\r
91          * If the device is not already open, then open it from the \r
92          * environment.\r
93          */\r
94         if (DeviceFD == -1)\r
95         {\r
96                 /* try to get it from STAPE or TAPE environment variable... */\r
97                 if ((device = getenv("STAPE")) == NULL &&\r
98                         (device = getenv("TAPE")) == NULL)\r
99                 {\r
100                         Usage();        /* Doesn't return */\r
101                 }\r
102 \r
103                 open_device();\r
104         }\r
105 \r
106         /* okay, now to execute the command... */\r
107         return command->command();\r
108 }\r
109 \r
110 \r
111 /* parse_args():\r
112  * Basically, we are parsing argv/argc. We can have multiple commands\r
113  * on a line, such as "load start" to load a tape and start the device.\r
114  * We execute these commands one at a time as we come to them. If we don't \r
115  * have a -f at the start and the default device isn't defined in a TAPE or \r
116  * STAPE environment variable, we exit.\r
117  */ \r
118 \r
119 int parse_args(int argc, char **argv)\r
120 {\r
121         int index, retval;\r
122         struct command_table_struct *command;\r
123 \r
124         argv0 = argv[0];\r
125 \r
126         for (index = 1; index < argc; index++)\r
127         {\r
128                 if (strcmp(argv[index], "-f") == 0)\r
129                 {\r
130                         index++;\r
131                         if (index >= argc)\r
132                         {\r
133                                 Usage();        /* Doesn't return */\r
134                         }\r
135                         device = argv[index];\r
136                         open_device();\r
137                 }\r
138                 else\r
139                 {\r
140                         for (command = &command_table[0]; command->name != NULL; command++)\r
141                         {\r
142                                 if (strcmp(command->name, argv[index]) == 0)\r
143                                 {\r
144                                         break;\r
145                                 }\r
146                         }\r
147 \r
148                         if (command->name == NULL)\r
149                         {\r
150                                 Usage();        /* Doesn't return */\r
151                         }\r
152 \r
153                         retval = execute_command(command);\r
154 \r
155                         if (retval < 0)\r
156                         {\r
157                                 /* Command failed, we probably shouldn't continue */\r
158                                 return retval;\r
159                         }\r
160                 }\r
161         }\r
162 \r
163         return 0;\r
164 }\r
165 \r
166 int S_load(void)\r
167 {\r
168         int result = LoadUnload(DeviceFD, 1);\r
169 \r
170         if (result < 0)\r
171         {\r
172                 fputs("scsieject: load failed\n", stderr);\r
173                 fflush(stderr);\r
174         }\r
175 \r
176         return result;\r
177 }\r
178 \r
179 int S_unload(void)\r
180 {\r
181         int result = LoadUnload(DeviceFD, 0);\r
182 \r
183         if (result < 0)\r
184         {\r
185                 fputs("scsieject: unload failed\n", stderr);\r
186                 fflush(stderr);\r
187         }\r
188 \r
189         return result;\r
190 }\r
191 \r
192 int S_start(void)\r
193 {\r
194         int result = StartStop(DeviceFD, 1);\r
195 \r
196         if (result < 0)\r
197         {\r
198                 fputs("scsieject: start failed\n", stderr);\r
199                 fflush(stderr);\r
200         }\r
201 \r
202         return result;\r
203 }\r
204 \r
205 int S_stop(void)\r
206 {\r
207         int result = StartStop(DeviceFD, 0);\r
208 \r
209         if (result < 0)\r
210         {\r
211                 fputs("scsieject: stop failed\n", stderr);\r
212                 fflush(stderr);\r
213         }\r
214 \r
215         return result;\r
216 }\r
217 \r
218 int S_lock(void)\r
219 {\r
220         int result = LockUnlock(DeviceFD, 1);\r
221 \r
222         if (result < 0)\r
223         {\r
224                 fputs("scsieject: lock failed\n", stderr);\r
225                 fflush(stderr);\r
226         }\r
227 \r
228         return result;\r
229 }\r
230 \r
231 int S_unlock(void)\r
232 {\r
233         int result = LockUnlock(DeviceFD, 0);\r
234 \r
235         if (result < 0)\r
236         {\r
237                 fputs("scsieject: unlock failed\n", stderr);\r
238                 fflush(stderr);\r
239         }\r
240 \r
241         return result;\r
242 }\r
243 \r
244 /* See parse_args for the scoop. parse_args does all. */\r
245 int main(int argc, char **argv)\r
246 {\r
247         parse_args(argc, argv);\r
248 \r
249         if (device)\r
250         {\r
251                 SCSI_CloseDevice(device, DeviceFD);\r
252         }\r
253 \r
254         exit(0);\r
255 }\r