Fix make depend rule
[debian-nspark.git] / misc.c
blob5ed84a3efaff903cb301cffcc657f49bd9bb698b
2 /*
3 * miscellaneous functions
5 * Revision 1.14 99/03/17 MU
6 * Also capitilised the hexadecimal output strings.
8 * $Header: misc.c 1.12 95/08/01 $
9 * $Log: misc.c,v $
10 * Revision 1.13 95/08/01 xx:xx:xx BB
11 * Fixed for Borland C/C++
12 * Made inlist() case insensitive (DOS only) because DOS filenames are case
13 * insensitive.
14 * Made basename safe for null-strings.
16 * Revision 1.12 92/12/22 09:54:29 duplain
17 * Changed #include <malloc.h> to #include <stdlib.h> .
19 * Revision 1.11 92/12/09 09:42:36 duplain
20 * Simplified append_type(). Changed append_type() to append lowercase types.
22 * Revision 1.10 92/12/08 10:20:07 duplain
23 * Added append_type().
25 * Revision 1.9 92/12/07 17:18:58 duplain
26 * reformatted source.
28 * Revision 1.8 92/11/12 09:03:30 duplain
29 * Fixed bug with realloc() size in uplevel().
31 * Revision 1.7 92/11/06 12:42:28 duplain
32 * Changed print_details() so it supports PC archive headers correctly.
34 * Revision 1.6 92/11/04 16:56:14 duplain
35 * Added check for PC archive header in print_header().
37 * Revision 1.5 92/10/09 18:06:58 duplain
38 * Added "+1" to malloc() call in riscos_path()... SCO UNIX was quiet right
39 * to core dump :-)
41 * Revision 1.4 92/10/07 10:56:39 duplain
42 * Added check for SYSV2 when including <malloc.h>. Made riscos_path() compile
43 * for non-RISCOS systems only.
45 * Revision 1.3 92/10/06 12:12:29 duplain
46 * Removed reference to date->csecond in print_details().
48 * Revision 1.2 92/09/30 10:26:58 duplain
49 * Fixed basename(). Added riscos_path().
51 * Revision 1.1 92/09/29 18:02:21 duplain
52 * Initial revision
56 #include <stdlib.h>
57 #include <string.h>
59 #include <stdio.h>
60 #include "spark.h"
61 #include "main.h"
63 #include "date.h"
64 #include "misc.h"
67 * return last element in pathname
69 char *
70 basename(char *s)
72 /* BB changed next line to cope with null-pointers.
73 (IRIX's strlen() produces a coredump when s == NULL). */
74 /* char *cptr = s + strlen(s); */
75 char *cptr;
76 if (!s)
77 return NULL;
78 cptr = s + strlen(s);
79 while (cptr > s)
81 if (*cptr == PATHSEP)
82 return (++cptr);
83 cptr--;
85 return (s);
88 #if defined(RISCOS)
89 #define DOTARC "_arc"
90 #define DOT '_'
91 #else /* not RISCOS */
92 #define DOTARC ".arc"
93 #define DOT '.'
94 #endif /* RISCOS */
97 * append ".arc" ("_arc" in RISCOS) to a string, if an extension doesn't
98 * already exist, and return the new string.
100 char *
101 name_dot_arc(char *s)
103 static char *newname = NULL;
106 * check that there's room for the extension
108 if (strlen(basename(s)) + sizeof(DOTARC) - 1 > FILENAMELEN)
109 return (s);
112 * free previous allocation (hope it's finished with :-/)
114 if (newname)
115 free(newname);
116 newname = malloc(strlen(s) + sizeof(DOTARC));
117 if (!newname)
118 return (s); /* don't complain */
119 strcpy(newname, s);
120 strcat(newname, DOTARC);
121 return (newname);
125 * turn a local-host pathname into a RISC OS path
128 #ifndef RISCOS
129 char *
130 riscos_path(register char *s)
132 static char *riscosname = NULL;
133 register char *cptr;
135 if (riscosname)
136 free(riscosname);
138 riscosname = malloc(strlen(s) + 1);
139 if (!riscosname)
140 return (NULL);
141 for (cptr = riscosname; *s; s++, cptr++)
142 if (*s == PATHSEP)
143 *cptr = '.';
144 else
145 *cptr = *s;
146 *cptr = '\0';
147 return (riscosname);
149 #endif /* RISC OS */
151 static char *pathname = NULL;
154 * uplevel() and downlevel() maintain the pathname as directories are found
155 * within the archive
157 char *
158 uplevel()
160 register char *cptr;
161 register int olen, nlen;
163 if (!pathname)
164 return (NULL);
166 olen = strlen(pathname);
167 cptr = pathname + olen - 1;
168 while (cptr > pathname)
169 if (*cptr == PATHSEP)
171 *cptr = '\0';
172 break;
174 else
175 cptr--;
177 if (cptr == pathname)
179 free(pathname);
180 pathname = NULL;
182 else
184 nlen = strlen(pathname);
185 if (nlen < olen)
186 pathname = realloc(pathname, nlen + 1);
188 return (pathname);
191 char *
192 downlevel(char *filename)
194 register int len, flen;
196 if (!pathname)
197 len = 0;
198 else
199 len = strlen(pathname);
201 flen = strlen(filename);
202 if (!len)
204 pathname = malloc(flen + 1);
205 if (pathname)
206 strcpy(pathname, filename);
208 else
210 pathname = realloc(pathname, len + flen + 2);
211 if (pathname)
213 strcat(pathname, PATHSEPSTR);
214 strcat(pathname, filename);
217 return (pathname);
220 char *
221 get_comp_desc(Byte comptype)
223 switch (comptype & ~ARCHPACK)
225 case CT_NOTCOMP:
226 case CT_NOTCOMP2:
227 return "Stored";
228 case CT_PACK:
229 return "Packed";
230 case CT_CRUNCH:
231 return "Crunched";
232 case CT_SQUASH:
233 return "Squashed";
234 case CT_COMP:
235 return "Compressed";
236 default:
237 return "Unknown";
240 return "Unknown";
244 * print archive file details (size, data and time)
246 void
247 print_details(Header *header)
249 Date *date;
251 if (!header)
252 return;
254 if (header->comptype & ARCHPACK)
256 /* Archimedes archive header */
258 /* BB changed constants in next line to long */
259 if ((header->load & (Word) 0xfff00000l) == (Word) 0xfff00000l)
261 /* time stamp valid */
262 date = makedate(header);
263 printf("%8ld %02d-%s-%04d %02d:%02d:%02d &%03X %s",
264 (long)header->origlen, date->day,
265 monthname(date->month), date->year + 1900,
266 date->hour, date->minute, date->second,
267 (header->load >> 8) & 0xfff,
268 get_comp_desc(header->comptype));
271 else
273 /* load/exec only */
274 /* BB added long to first format in next line.
275 Header.origlen is a Word (i.e. a long) */
276 #ifdef __MSDOS__
277 printf("%8ld &%08lX &%08lX ---- %s", header->origlen,
278 #else
279 printf("%8d &%08lX &%08lX ---- %s", header->origlen,
280 #endif /* __MSDOS__ */
281 (long)header->load, (long)header->exec,
282 get_comp_desc(header->comptype));
285 else
287 /* PC archive header */
288 date = makedate(header);
289 printf("%8ld %02d-%s-%02d %02d:%02d:%02d ---- %s",
290 (long)header->origlen, date->day, monthname(date->month),
291 date->year + 1900, date->hour, date->minute, date->second,
292 get_comp_desc(header->comptype));
297 * Test if the given filename matches any of the names specified in the
298 * command line "files list". This function is also used to test if a given
299 * pathname is contained in the any of the names given in the "files list".
301 * Returns non-zero if filename matches, or no "files list" exists.
304 inlist(char *filename)
306 register int len = strlen(filename);
307 register char **filelist = files;
309 if (!*filelist)
310 return (1); /* no "files list" */
312 while (*filelist)
313 #ifdef __MSDOS__
314 if (strnicmp(filename, *filelist++, len) == 0)
315 #else
316 if (strncmp(filename, *filelist++, len) == 0)
317 #endif /* __MSDOS__ */
318 return (1);
319 return (0);
323 * append the file's type to the end if it's name
324 * (this function assumes that enough storage is available in the argument
325 * "filename" to store the additional characters ",XXX")
328 /* BB: For DOS, a comma is of no use. So changed that into a dot. */
330 append_type(Header *header, char *filename)
332 char append[sizeof(",xxx")];
334 /* BB changed constants in next line to long. */
335 if ((header->load & (Word) 0xfff00000l) == (Word) 0xfff00000l)
337 /* valid time-stamp */
338 #ifdef __MSDOS__
339 sprintf(append, ".%03X", (header->load >> 8) & 0xfff);
340 #else
341 sprintf(append, ",%03X", (header->load >> 8) & 0xfff);
342 #endif /* __MSDOS__ */
343 strcat(filename, append);
344 return (0);
346 return (-1);
349 #ifdef DEBUGGING
352 * print archive file header info
354 void
355 print_header(Header *header)
357 if (!header)
358 return;
360 printf("comptype=0X%X name=%s complen=%lu date=0X%X time=0X%X\n",
361 header->comptype, header->name, header->complen, header->date,
362 header->time);
363 printf("crc=0X%X origlen=%lu load=0X%lX exec=0X%lX attr=0x%lX\n",
364 header->crc, header->origlen, header->load, header->exec,
365 header->attr);
368 #endif /* DEBUGGING */