Merge remote-tracking branch 'upstream/master'
[debian-nspark.git] / main.c
blobcdb8646ddac32ee070a6ef0ce6c335fee995df1e
2 /*
3 * main function
5 * Revision 1.12 99/03/17 MU
6 * added the option -i to extract with .inf files
7 * Also added the .inf extension to the default logfile name for MSDOS
9 * $Header: main.c 1.11 95/08/01 $
10 * $Log: main.c,v $
11 * Revision 1.11 95/08/01 xx:xx:xx BB
12 * Fixed for Borland C/C++
13 * Added / as command line switch option for DOS
15 * Revision 1.10 93/08/20 12:39:28 arb
16 * Added support for ArcFS archive detection
18 * Revision 1.9 93/08/20 10:30:50 arb
19 * Added -C option for "convert filenames to lowercase"
21 * Revision 1.8 93/03/05 15:40:32 arb
22 * Added <stdlib.h> for RISCOS, needed for exit()
24 * Revision 1.7 92/12/09 09:43:03 duplain
25 * Changed "-a" option to "-T".
27 * Revision 1.6 92/12/08 10:19:30 duplain
28 * Added -a option for "append filetype".
30 * Revision 1.5 92/12/07 17:18:42 duplain
31 * reformatted source.
33 * Revision 1.4 92/10/19 09:33:09 duplain
34 * Added -x as an alternative to -u.
36 * Revision 1.3 92/10/01 11:21:39 duplain
37 * Added -R option.
39 * Revision 1.2 92/09/30 10:27:29 duplain
40 * Added logfile option and processing.
42 * Revision 1.1 92/09/29 18:02:20 duplain
43 * Initial revision
45 */
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <ctype.h>
50 #include "spark.h"
52 /* BB changed next line */
54 /* #include "io.h" */
55 #include "nsparkio.h"
56 #include "error.h"
57 #include "misc.h"
58 #include "arc.h"
59 #include "unarc.h"
60 #include "garble.h"
61 #include "version.h"
63 /* BB changed next line */
65 /* #ifdef RISCOS */
66 #if defined(RISCOS) || defined(__MSDOS__)
67 #include <stdlib.h>
68 #endif /* RISCOS || __MSDOS__ */
70 /* BB added next includes */
71 #ifdef __MSDOS__
72 #include <ctype.h>
73 #include <string.h>
74 #endif /* __MSDOS__ */
76 char *ourname; /* program name */
77 char *archive; /* name of archive file */
80 /* MU changed the default log filename for MSDOS */
81 #ifdef __MSDOS__
82 char *logfile = "settypes.inf"; /* default name for log file */
84 #else /* */
85 char *logfile = "settypes"; /* default name for log file */
87 #endif /* */
88 char **files; /* optional file arguments */
89 unsigned char unarc = 0; /* -u or -x or I */
90 unsigned char inffiles = 0; /* I */
91 unsigned char quiet = 0; /* -q */
92 unsigned char verbose = 0; /* -v */
93 unsigned char testing = 0; /* -t */
94 unsigned char listing = 0; /* -l */
95 unsigned char force = 0; /* -f */
96 unsigned char stamp = 1; /* -s */
97 unsigned char retry = 0; /* -R */
98 unsigned char apptype = 0; /* -T */
99 unsigned char singlecase = 0; /* -C */
101 #ifdef DEBUGGING
102 unsigned char debugging = 0; /* -D */
104 #endif /* DEBUGGING */
105 void usage(void);
106 int do_unarc(void);
107 int do_arc(void);
110 main(int argc, char *argv[])
112 register int i;
114 /* BB extra switch indicator for command line parsing
115 DOS allows switches like /a/b/c which enters as one
116 `word' in argv. */
117 #ifdef __MSDOS__
118 int nextmaybeaswitch;
120 #endif /* */
122 #ifdef DEBUGGING
124 * check types are defined correctly for this machine (or compiler ???)
126 if (sizeof(Word) != 4)
128 puts("Word size != 4");
129 exit(1);
131 if (sizeof(Halfword) != 2)
133 puts("Halfword size != 2");
134 exit(1);
136 if (sizeof(Byte) != 1)
138 puts("Byte size != 1");
139 exit(1);
142 #endif /* DEBUGGING */
143 ourname = basename(*argv++);
145 /* BB cosmetics for DOS: Strip extention .exe (or .com if
146 somebody would like to compile nspark to a .com) from
147 the ourname string. And convert it to lowercase. That
148 way it looks better than ``NSPARK.EXE: error ...''. */
149 #ifdef __MSDOS__
150 /* For DOS prior to 3.0, argv[0] contains the NULL pointer.
151 So substitute a `default'. */
152 if (!ourname)
153 ourname = "nspark";
155 /* NB: stricmp == strcmpi == strcasecmp */
156 if (stricmp(&ourname[strlen(ourname) - 4], ".com") == 0
157 || stricmp(&ourname[strlen(ourname) - 4], ".exe") == 0)
160 /* We cannot write a '\0' into ourname because it points to
161 argv[0]. And that may be in a read-only data segment. */
162 char *newname, *cp;
163 if ((newname = (char *) malloc(strlen(ourname)) - 3) != NULL)
165 strncpy(newname, ourname, strlen(ourname) - 4);
166 newname[strlen(ourname) - 4] = '\0';
167 ourname = newname; /* Allocated space will be released
168 automatically at exit */
169 for (cp = ourname; *cp; cp++)
170 if (isascii(*cp) && isupper(*cp))
171 *cp = tolower(*cp);
175 #endif /* __MSDOS__ */
176 argc--;
179 * parse args (can't use getopt() 'cos not all C libraries have it)
181 while (argc)
183 int donext = 0;
184 char *arg = *argv;
186 /* BB changed next line */
187 /* if (*arg == '-') { */
188 #ifdef __MSDOS__
189 if (*arg == '-' || *arg == '/')
192 #else /* */
193 if (*arg == '-')
196 #endif /* __MSDOS__ */
197 char c;
199 #ifdef __MSDOS__
200 /* BB first char following a switch may not be another switch */
201 nextmaybeaswitch = 0;
203 #endif /* __MSDOS__ */
204 while (!donext && !isspace(c = *++arg) && c)
206 switch (c)
208 case 'u':
209 case 'x':
210 unarc = 1;
211 break;
212 case 't':
213 testing++;
214 unarc = 1; /* implied */
215 break;
216 case 'l':
217 listing++;
218 unarc = 1; /* implied */
219 break;
220 case 'q':
221 quiet = 1;
222 break;
223 case 'v':
224 verbose = 1;
225 break;
226 case 'f':
227 force = 1;
228 break;
229 case 's':
230 stamp = 0;
231 break;
232 case 'R':
233 retry = 1;
234 break;
235 case 'V':
236 printf("%s v%s - maintained by %s - PUBLIC DOMAIN\n",
237 ourname, VERSION, MAINTAINER);
238 break;
239 case 'T':
240 apptype = 1;
241 logfile = NULL;
242 break;
243 case 'C':
244 singlecase = 1;
245 break;
246 case 'L':
247 if (!apptype)
249 if (*++arg)
250 logfile = arg;
252 else
253 if (--argc)
254 logfile = *++argv;
256 else
257 usage();
259 donext++;
260 break;
262 #ifdef DEBUGGING
263 case 'D':
264 debugging = 1;
265 break;
267 #endif /* DEBUGGING */
268 #ifdef __MSDOS__
269 /* BB DOS allows switches like /a/b/c */
270 case '/':
271 if (nextmaybeaswitch && arg[1] != '/')
272 break;
274 else /* fall through to error message */
277 #endif /* __MSDOS__ */
278 case 'I':
279 unarc = 1;
280 inffiles = 1;
281 break;
282 case 'p':
283 if (*++arg)
284 set_password(arg);
285 else
286 if (--argc)
287 set_password(*++argv);
288 else
289 usage();
290 donext++;
291 break;
292 default:
293 error("unknown option '%c'", c);
294 exit(1);
297 #ifdef __MSDOS__
298 /* BB We've had a valid switch, next char may be
299 a / again */
300 nextmaybeaswitch = 1;
302 #endif /* __MSDOS__ */
304 argv++;
305 argc--;
307 else
308 break;
310 if (!argc)
311 usage();
312 archive = *argv++;
313 files = argv;
314 if (unarc)
315 i = do_unarc();
317 else
318 i = do_arc();
319 exit(i);
321 /* BB added next line */
322 return 0; /* Keep compiler happy. */
328 * display program usage and exit
330 void
331 usage()
333 fprintf(stderr, "usage: %s [options] archive [file ... file]\n",
334 ourname);
335 fprintf(stderr, " where options are:\n");
336 fprintf(stderr,
337 " -u or -x unarchive -t test archive integrity\n");
338 fprintf(stderr, " -l list archive contents -q quiet\n");
339 fprintf(stderr,
340 " -f force file overwrite -s no filestamp\n");
341 fprintf(stderr,
342 " -v verbose -V display version number\n");
343 fprintf(stderr,
344 " -R retry if archive corrupt -L<name> set logfile name\n");
345 fprintf(stderr,
346 " -T append filetype to name -C create lowercase filenames\n");
348 /* MU added instuctions for the -I option */
349 fprintf(stderr, " -I unarchive with .inf files -p<password> set password\n");
350 exit(1);