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 $
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
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
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
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
67 * return last element in pathname
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); */
91 #else /* not RISCOS */
97 * append ".arc" ("_arc" in RISCOS) to a string, if an extension doesn't
98 * already exist, and return the new string.
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
)
112 * free previous allocation (hope it's finished with :-/)
116 newname
= malloc(strlen(s
) + sizeof(DOTARC
));
118 return (s
); /* don't complain */
120 strcat(newname
, DOTARC
);
125 * turn a local-host pathname into a RISC OS path
130 riscos_path(register char *s
)
132 static char *riscosname
= NULL
;
138 riscosname
= malloc(strlen(s
) + 1);
141 for (cptr
= riscosname
; *s
; s
++, cptr
++)
151 static char *pathname
= NULL
;
154 * uplevel() and downlevel() maintain the pathname as directories are found
161 register int olen
, nlen
;
166 olen
= strlen(pathname
);
167 cptr
= pathname
+ olen
- 1;
168 while (cptr
> pathname
)
169 if (*cptr
== PATHSEP
)
177 if (cptr
== pathname
)
184 nlen
= strlen(pathname
);
186 pathname
= realloc(pathname
, nlen
+ 1);
192 downlevel(char *filename
)
194 register int len
, flen
;
199 len
= strlen(pathname
);
201 flen
= strlen(filename
);
204 pathname
= malloc(flen
+ 1);
206 strcpy(pathname
, filename
);
210 pathname
= realloc(pathname
, len
+ flen
+ 2);
213 strcat(pathname
, PATHSEPSTR
);
214 strcat(pathname
, filename
);
221 get_comp_desc(Byte comptype
)
223 switch (comptype
& ~ARCHPACK
)
244 * print archive file details (size, data and time)
247 print_details(Header
*header
)
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
));
274 /* BB added long to first format in next line.
275 Header.origlen is a Word (i.e. a long) */
277 printf("%8ld &%08lX &%08lX ---- %s", header
->origlen
,
279 printf("%8d &%08lX &%08lX ---- %s", header
->origlen
,
280 #endif /* __MSDOS__ */
281 (long)header
->load
, (long)header
->exec
,
282 get_comp_desc(header
->comptype
));
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
;
310 return (1); /* no "files list" */
314 if (strnicmp(filename
, *filelist
++, len
) == 0)
316 if (strncmp(filename
, *filelist
++, len
) == 0)
317 #endif /* __MSDOS__ */
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 */
339 sprintf(append
, ".%03X", (header
->load
>> 8) & 0xfff);
341 sprintf(append
, ",%03X", (header
->load
>> 8) & 0xfff);
342 #endif /* __MSDOS__ */
343 strcat(filename
, append
);
352 * print archive file header info
355 print_header(Header
*header
)
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
,
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
,
368 #endif /* DEBUGGING */