Merge pull request #7 from ccawley2011/msvc
[debian-nspark.git] / main.c
blobbbe1191f02fd04883a1e9c7166f7c2da080b2c32
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 to_stdout = 0; /* -c */
98 unsigned char retry = 0; /* -R */
99 unsigned char apptype = 0; /* -T */
100 unsigned char singlecase = 0; /* -C */
102 #ifdef DEBUGGING
103 unsigned char debugging = 0; /* -D */
105 #endif /* DEBUGGING */
106 void usage(void);
107 int do_unarc(void);
108 int do_arc(void);
111 main(int argc, char *argv[])
113 register int i;
115 /* BB extra switch indicator for command line parsing
116 DOS allows switches like /a/b/c which enters as one
117 `word' in argv. */
118 #ifdef __MSDOS__
119 int nextmaybeaswitch;
121 #endif /* */
123 #ifdef DEBUGGING
125 * check types are defined correctly for this machine (or compiler ???)
127 if (sizeof(Word) != 4)
129 error("Word size != 4");
130 exit(1);
132 if (sizeof(Halfword) != 2)
134 error("Halfword size != 2");
135 exit(1);
137 if (sizeof(Byte) != 1)
139 error("Byte size != 1");
140 exit(1);
143 #endif /* DEBUGGING */
144 ourname = basename(*argv++);
146 /* BB cosmetics for DOS: Strip extention .exe (or .com if
147 somebody would like to compile nspark to a .com) from
148 the ourname string. And convert it to lowercase. That
149 way it looks better than ``NSPARK.EXE: error ...''. */
150 #ifdef __MSDOS__
151 /* For DOS prior to 3.0, argv[0] contains the NULL pointer.
152 So substitute a `default'. */
153 if (!ourname)
154 ourname = "nspark";
156 /* NB: stricmp == strcmpi == strcasecmp */
157 if (stricmp(&ourname[strlen(ourname) - 4], ".com") == 0
158 || stricmp(&ourname[strlen(ourname) - 4], ".exe") == 0)
161 /* We cannot write a '\0' into ourname because it points to
162 argv[0]. And that may be in a read-only data segment. */
163 char *newname, *cp;
164 if ((newname = (char *) malloc(strlen(ourname)) - 3) != NULL)
166 strncpy(newname, ourname, strlen(ourname) - 4);
167 newname[strlen(ourname) - 4] = '\0';
168 ourname = newname; /* Allocated space will be released
169 automatically at exit */
170 for (cp = ourname; *cp; cp++)
171 if (isascii(*cp) && isupper(*cp))
172 *cp = tolower(*cp);
176 #endif /* __MSDOS__ */
177 argc--;
180 * parse args (can't use getopt() 'cos not all C libraries have it)
182 while (argc)
184 int donext = 0;
185 char *arg = *argv;
187 /* BB changed next line */
188 /* if (*arg == '-') { */
189 #ifdef __MSDOS__
190 if (*arg == '-' || *arg == '/')
193 #else /* */
194 if (*arg == '-')
197 #endif /* __MSDOS__ */
198 char c;
200 #ifdef __MSDOS__
201 /* BB first char following a switch may not be another switch */
202 nextmaybeaswitch = 0;
204 #endif /* __MSDOS__ */
205 while (!donext && !isspace(c = *++arg) && c)
207 switch (c)
209 case 'u':
210 case 'x':
211 unarc = 1;
212 break;
213 case 't':
214 testing++;
215 unarc = 1; /* implied */
216 break;
217 case 'l':
218 listing++;
219 unarc = 1; /* implied */
220 break;
221 case 'q':
222 quiet = 1;
223 break;
224 case 'v':
225 verbose = 1;
226 break;
227 case 'c':
228 to_stdout = 1;
229 case 'f':
230 force = 1;
231 break;
232 case 's':
233 stamp = 0;
234 break;
235 case 'R':
236 retry = 1;
237 break;
238 case 'V':
239 fprintf(stderr, "%s v%s - maintained by %s - PUBLIC DOMAIN\n",
240 ourname, VERSION, MAINTAINER);
241 break;
242 case 'T':
243 apptype = 1;
244 logfile = NULL;
245 break;
246 case 'C':
247 singlecase = 1;
248 break;
249 case 'L':
250 if (!apptype)
252 if (*++arg)
253 logfile = arg;
255 else
256 if (--argc)
257 logfile = *++argv;
259 else
260 usage();
262 donext++;
263 break;
265 #ifdef DEBUGGING
266 case 'D':
267 debugging = 1;
268 break;
270 #endif /* DEBUGGING */
271 #ifdef __MSDOS__
272 /* BB DOS allows switches like /a/b/c */
273 case '/':
274 if (nextmaybeaswitch && arg[1] != '/')
275 break;
277 else /* fall through to error message */
280 #endif /* __MSDOS__ */
281 case 'I':
282 unarc = 1;
283 inffiles = 1;
284 break;
285 case 'p':
286 if (*++arg)
287 set_password(arg);
288 else
289 if (--argc)
290 set_password(*++argv);
291 else
292 usage();
293 donext++;
294 break;
295 default:
296 error("unknown option '%c'", c);
297 exit(1);
300 #ifdef __MSDOS__
301 /* BB We've had a valid switch, next char may be
302 a / again */
303 nextmaybeaswitch = 1;
305 #endif /* __MSDOS__ */
307 argv++;
308 argc--;
310 else
311 break;
313 if (!argc)
314 usage();
315 archive = *argv++;
316 files = argv;
317 if (unarc)
318 i = do_unarc();
320 else
321 i = do_arc();
322 exit(i);
324 /* BB added next line */
325 return 0; /* Keep compiler happy. */
331 * display program usage and exit
333 void
334 usage()
336 fprintf(stderr, "usage: %s [options] archive [file ... file]\n",
337 ourname);
338 fprintf(stderr, " where options are:\n");
339 fprintf(stderr,
340 " -u or -x unarchive -t test archive integrity\n");
341 fprintf(stderr, " -l list archive contents -q quiet\n");
342 fprintf(stderr,
343 " -f force file overwrite -s no filestamp\n");
344 fprintf(stderr,
345 " -v verbose -V display version number\n");
346 fprintf(stderr,
347 " -R retry if archive corrupt -L<name> set logfile name\n");
348 fprintf(stderr,
349 " -T append filetype to name -C create lowercase filenames\n");
351 /* MU added instuctions for the -I option */
352 fprintf(stderr, " -I unarchive with .inf files -p<password> set password\n");
353 fprintf(stderr, " -c extract files to stdout\n");
354 exit(1);