Merge pull request #12 from davel/davel/sqsh
[debian-nspark.git] / main.c
blobad133f467daba832ee3f0d076d759b0d24d38d0c
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 <string.h>
50 #include <ctype.h>
51 #include "spark.h"
53 /* BB changed next line */
55 /* #include "io.h" */
56 #include "nsparkio.h"
57 #include "error.h"
58 #include "misc.h"
59 #include "arc.h"
60 #include "unarc.h"
61 #include "garble.h"
62 #include "version.h"
64 char *ourname; /* program name */
65 char *archive; /* name of archive file */
68 /* MU changed the default log filename for MSDOS */
69 #ifdef __MSDOS__
70 char *logfile = "settypes.inf"; /* default name for log file */
72 #else /* */
73 char *logfile = "settypes"; /* default name for log file */
75 #endif /* */
76 char **files; /* optional file arguments */
77 unsigned char unarc = 0; /* -u or -x or I */
78 unsigned char inffiles = 0; /* I */
79 unsigned char quiet = 0; /* -q */
80 unsigned char verbose = 0; /* -v */
81 unsigned char testing = 0; /* -t */
82 unsigned char listing = 0; /* -l */
83 unsigned char force = 0; /* -f */
84 unsigned char stamp = 1; /* -s */
85 unsigned char to_stdout = 0; /* -c */
86 unsigned char retry = 0; /* -R */
87 unsigned char apptype = 0; /* -T */
88 unsigned char singlecase = 0; /* -C */
90 #ifdef DEBUGGING
91 unsigned char debugging = 0; /* -D */
93 #endif /* DEBUGGING */
94 void usage(void);
95 int do_unarc(void);
96 int do_arc(void);
98 int
99 main(int argc, char *argv[])
101 register int i;
103 /* BB extra switch indicator for command line parsing
104 DOS allows switches like /a/b/c which enters as one
105 `word' in argv. */
106 #ifdef __MSDOS__
107 int nextmaybeaswitch;
109 #endif /* */
111 #ifdef DEBUGGING
113 * check types are defined correctly for this machine (or compiler ???)
115 if (sizeof(Word) != 4)
117 error("Word size != 4");
118 exit(1);
120 if (sizeof(Halfword) != 2)
122 error("Halfword size != 2");
123 exit(1);
125 if (sizeof(Byte) != 1)
127 error("Byte size != 1");
128 exit(1);
131 #endif /* DEBUGGING */
132 ourname = basename(*argv++);
134 /* BB cosmetics for DOS: Strip extention .exe (or .com if
135 somebody would like to compile nspark to a .com) from
136 the ourname string. And convert it to lowercase. That
137 way it looks better than ``NSPARK.EXE: error ...''. */
138 #ifdef __MSDOS__
139 /* For DOS prior to 3.0, argv[0] contains the NULL pointer.
140 So substitute a `default'. */
141 if (!ourname)
142 ourname = "nspark";
144 /* NB: stricmp == strcmpi == strcasecmp */
145 if (stricmp(&ourname[strlen(ourname) - 4], ".com") == 0
146 || stricmp(&ourname[strlen(ourname) - 4], ".exe") == 0)
149 /* We cannot write a '\0' into ourname because it points to
150 argv[0]. And that may be in a read-only data segment. */
151 char *newname, *cp;
152 if ((newname = (char *) malloc(strlen(ourname)) - 3) != NULL)
154 strncpy(newname, ourname, strlen(ourname) - 4);
155 newname[strlen(ourname) - 4] = '\0';
156 ourname = newname; /* Allocated space will be released
157 automatically at exit */
158 for (cp = ourname; *cp; cp++)
159 if (isascii(*cp) && isupper(*cp))
160 *cp = tolower(*cp);
164 #endif /* __MSDOS__ */
165 argc--;
168 * parse args (can't use getopt() 'cos not all C libraries have it)
170 while (argc)
172 int donext = 0;
173 char *arg = *argv;
175 /* BB changed next line */
176 /* if (*arg == '-') { */
177 #ifdef __MSDOS__
178 if (*arg == '-' || *arg == '/')
181 #else /* */
182 if (*arg == '-')
185 #endif /* __MSDOS__ */
186 char c;
188 #ifdef __MSDOS__
189 /* BB first char following a switch may not be another switch */
190 nextmaybeaswitch = 0;
192 #endif /* __MSDOS__ */
193 while (!donext && !isspace(c = *++arg) && c)
195 switch (c)
197 case 'u':
198 case 'x':
199 unarc = 1;
200 break;
201 case 't':
202 testing++;
203 unarc = 1; /* implied */
204 break;
205 case 'l':
206 listing++;
207 unarc = 1; /* implied */
208 break;
209 case 'q':
210 quiet = 1;
211 break;
212 case 'v':
213 verbose = 1;
214 break;
215 case 'c':
216 to_stdout = 1;
217 break;
218 case 'f':
219 force = 1;
220 break;
221 case 's':
222 stamp = 0;
223 break;
224 case 'R':
225 retry = 1;
226 break;
227 case 'V':
228 fprintf(stderr, "%s v%s - maintained by %s - PUBLIC DOMAIN\n",
229 ourname, VERSION, MAINTAINER);
230 break;
231 case 'T':
232 apptype = 1;
233 logfile = NULL;
234 break;
235 case 'C':
236 singlecase = 1;
237 break;
238 case 'L':
239 if (!apptype)
241 if (*++arg)
242 logfile = arg;
244 else
245 if (--argc)
246 logfile = *++argv;
248 else
249 usage();
251 donext++;
252 break;
254 #ifdef DEBUGGING
255 case 'D':
256 debugging = 1;
257 break;
259 #endif /* DEBUGGING */
260 #ifdef __MSDOS__
261 /* BB DOS allows switches like /a/b/c */
262 case '/':
263 if (nextmaybeaswitch && arg[1] != '/')
264 break;
266 else /* fall through to error message */
269 #endif /* __MSDOS__ */
270 case 'I':
271 unarc = 1;
272 inffiles = 1;
273 break;
274 case 'p':
275 if (*++arg)
276 set_password(arg);
277 else
278 if (--argc)
279 set_password(*++argv);
280 else
281 usage();
282 donext++;
283 break;
284 default:
285 error("unknown option '%c'", c);
286 exit(1);
289 #ifdef __MSDOS__
290 /* BB We've had a valid switch, next char may be
291 a / again */
292 nextmaybeaswitch = 1;
294 #endif /* __MSDOS__ */
296 argv++;
297 argc--;
299 else
300 break;
302 if (!argc)
303 usage();
304 archive = *argv++;
305 files = argv;
306 if (unarc)
307 i = do_unarc();
309 else
310 i = do_arc();
311 exit(i);
313 /* BB added next line */
314 return 0; /* Keep compiler happy. */
320 * display program usage and exit
322 void
323 usage()
325 fprintf(stderr, "usage: %s [options] archive [file ... file]\n",
326 ourname);
327 fprintf(stderr, " where options are:\n");
328 fprintf(stderr,
329 " -u or -x unarchive -t test archive integrity\n");
330 fprintf(stderr, " -l list archive contents -q quiet\n");
331 fprintf(stderr,
332 " -f force file overwrite -s no filestamp\n");
333 fprintf(stderr,
334 " -v verbose -V display version number\n");
335 fprintf(stderr,
336 " -R retry if archive corrupt -L<name> set logfile name\n");
337 fprintf(stderr,
338 " -T append filetype to name -C create lowercase filenames\n");
340 /* MU added instuctions for the -I option */
341 fprintf(stderr, " -I unarchive with .inf files -p<password> set password\n");
342 fprintf(stderr, " -c extract files to stdout\n");
343 exit(1);