home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / useful / dist / util / arc / zoo / portable.c < prev    next >
C/C++ Source or Header  |  1991-07-28  |  21KB  |  726 lines

  1. #ifndef LINT
  2. /* @(#) portable.c 2.24 88/08/24 01:22:06 */
  3. static char sccsid[]="@(#) portable.c 2.24 88/08/24 01:22:06";
  4. #endif /* LINT */
  5.  
  6. #include "options.h"
  7. /*
  8. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  9. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  10. */
  11. /**********************
  12. portable.c contains functions needed to make Zoo portable to various
  13. implementations of C.
  14.  
  15. Note:  Provided a 2's complement machine is used, all functions in
  16. this file are themselves machine-independent and need not be changed
  17. when implementing Zoo on a different machine.  Some code will choke
  18. on 1's complement machines--I think.
  19.  
  20. For machine-dependent declarations see files "machine.h" and "options.h".
  21.  
  22. For machine-dependent functions see file "machine.c"
  23. */
  24.  
  25. #include "zoo.h"
  26. #include "zooio.h"
  27.  
  28. #include "various.h"
  29. #include "zoofns.h"
  30.  
  31. #include "machine.h"
  32. #include "debug.h"
  33. #include "assert.h"
  34.  
  35. #ifdef NEEDCTYP
  36. #include <ctype.h>                  /* for tolower() */
  37. #endif
  38.  
  39. #include "portable.h"
  40.  
  41. #ifdef TRACE_IO
  42. extern int verbose;
  43. #endif
  44.  
  45. /* Functions defined for use within this file only.  */
  46. long to_long PARMS((BYTE[]));
  47. int to_int PARMS((BYTE[]));
  48. void b_to_zooh PARMS((struct zoo_header *, BYTE[]));
  49. void b_to_dir PARMS((struct direntry *, BYTE[]));
  50. int dir_to_b PARMS((BYTE[], struct direntry *));
  51. void zooh_to_b PARMS((BYTE[], struct zoo_header *));
  52. void splitlong PARMS((BYTE[], long));
  53. void splitint PARMS((BYTE[], int));
  54.  
  55. #ifdef TRACE_IO
  56. void show_h PARMS ((struct zoo_header *));
  57. void show_dir PARMS ((struct direntry *));
  58. #endif /* TRACE_IO */
  59.  
  60. extern unsigned int crccode;
  61.  
  62. /************************************************************************/
  63. /* I/O functions */
  64. /************************************************************************/
  65.  
  66. /* some functions get defined only if they aren't already macros */
  67.  
  68. #ifndef zooread
  69. int zooread (file, buffer, count)
  70. ZOOFILE file; char *buffer; int count;
  71. { return (fread (buffer, 1, count, file)); }
  72. #endif /* zooread */
  73.  
  74. #ifndef FIZ
  75. #ifndef zoowrite
  76. int zoowrite (file, buffer, count)
  77. ZOOFILE file; char *buffer; int count;
  78. {
  79.     if (file == NULLFILE)
  80.         return (count);
  81.     else
  82.         return (fwrite (buffer, 1, count, file));
  83. }
  84. #endif /* zoowrite */
  85.  
  86. ZOOFILE zoocreate (fname)
  87. char *fname;
  88. { return ((ZOOFILE) fopen (fname, Z_NEW)); }
  89.  
  90. #endif /* FIZ */
  91.  
  92. #ifndef zooseek
  93. long zooseek (file, offset, whence)
  94. ZOOFILE file; long offset; int whence;
  95. { return (fseek (file, offset, whence)); }
  96. #endif /* zooseek */
  97.  
  98. ZOOFILE zooopen (fname, option)
  99. char *fname; char *option;
  100. { return ((ZOOFILE) fopen (fname, option)); }
  101.  
  102. #ifndef zootell
  103. long zootell (file)
  104. ZOOFILE file;
  105. { return ftell (file); }
  106. #endif /* zootell */
  107.  
  108. int zooclose (file)
  109. ZOOFILE file;
  110. { return fclose (file); }
  111.  
  112. /**********************
  113. low_ch() is a macro that returns a lowercased char; it may be
  114. used with any char, whether or not it is uppercase.    It will
  115. be used below by one or two functions.
  116. */
  117.  
  118. #define low_ch(c)    (isupper(c) ? tolower(c) : c)
  119. #ifndef toascii
  120. #define toascii(c)   (c)
  121. #endif
  122.  
  123. /************************************************************************/
  124. /*** Following are functions that make up for various implementations ***/
  125. /*** of C not having certain library routines.                                 ***/
  126. /************************************************************************/
  127.  
  128. #ifndef FIZ
  129. /**********************
  130. str_lwr() converts a string to lowercase and returns a pointer to the string
  131. */
  132. char *str_lwr (str)
  133. char *str;
  134. {
  135.     register char *s;
  136.     s = str;
  137.     while (*s != '\0') {
  138.         *s = toascii(*s);
  139.         *s = low_ch(*s);
  140.         s++;
  141.     }
  142.     return (str);
  143. }
  144.  
  145. /**********************
  146. str_icmp() compares strings just like strcmp() but it does it without regard to
  147. case.
  148. */
  149. int str_icmp (s1, s2)
  150. register char *s1, *s2;
  151. {
  152.     for ( ; low_ch(*s1) == low_ch(*s2);  s1++, s2++)
  153.         if (*s1 == '\0')
  154.             return(0);
  155.     return(low_ch(*s1) - low_ch(*s2));
  156. }
  157.  
  158. #ifdef NEED_MEMSET
  159. /**********************
  160. memset() it sets the first "count" bytes of "dest" to the character
  161. "c" and returns a pointer to "dest".
  162. */
  163. char *memset (dest, c, count)
  164. register char *dest;
  165. int c;
  166. unsigned count;
  167. {
  168.     register unsigned i;
  169.     for (i = 0; i < count; i++) {
  170.         *((char *) (dest + i)) = c;
  171.     }
  172.     return dest;
  173. }
  174. #endif /* NEED_MEMSET */
  175.  
  176. #ifdef NEED_MEMCPY
  177. /**********************
  178. memcpy() copies "count" bytes from "src" to "dest" and returns
  179. a pointer to "dest".  Not necessarily safe for overlapping moves. */
  180.  
  181. char *memcpy(dest, src, count)
  182. register char *dest;
  183. register char *src;
  184. unsigned count;
  185. {
  186.     VOIDPTR savedest = dest;
  187.     while (count > 0) {
  188.         *((char *) dest++) = *((char *) src++);
  189.         count--;
  190.     }
  191. }
  192. #endif /* NEED_MEMCPY */
  193.  
  194. #ifndef FPUTCHAR
  195. /**********************
  196. fputchar() writes a character to stdout.  It is identical to putchar
  197. but is a function, not a macro.
  198. */
  199. int fputchar (c)
  200. int c;
  201. {
  202.     return (fputc(c, stdout));
  203. }
  204. #endif /* FPUTCHAR */
  205. #endif /* FIZ */
  206.  
  207. /***********************************************************************/
  208. /*** Following are declarations and functions that are written in a    ***/
  209. /*** machine-independent way but they implement machine-dependent     ***/
  210. /*** activities                                                                        ***/
  211. /***********************************************************************/
  212.  
  213. #ifndef DIRECT_CONVERT
  214. /**********************
  215. to_long() converts four consecutive bytes, in order of increasing
  216. significance, to a long integer.  It is used to make Zoo independent of the
  217. byte order of the system.
  218. */
  219. long to_long(data)
  220. BYTE data[];
  221. {
  222.     return (long) ((unsigned long) data[0] | ((unsigned long) data[1] << 8) |
  223.             ((unsigned long) data[2] << 16) | ((unsigned long) data[3] << 24));
  224. }
  225.  
  226. #ifndef FIZ
  227. /********************
  228. splitlong() converts a long integer to four consecutive BYTEs in order
  229. of increasing significance.
  230. */
  231. void splitlong(bytes, bigword)
  232. BYTE bytes[];
  233. long bigword;
  234. {
  235.     int i;
  236.     for (i = 0; i < 4; i++) {
  237.         bytes[i] = bigword & 0xff;
  238.         bigword = (unsigned long) bigword >> 8;
  239.     }
  240. }
  241. #endif /* FIZ */
  242.  
  243. /*******************
  244. splitint() converts an integer to two consecutive BYTEs in order
  245. of increasing significance.
  246. */
  247. void splitint(bytes, word)
  248. BYTE bytes[];
  249. int word;
  250. {
  251.     bytes[0] = word & 0xff;
  252.     word = (unsigned int) word >> 8;
  253.     bytes[1] = word & 0xff;
  254. }
  255.  
  256. /**********************
  257. to_int() converts two consecutive bytes, in order of increasing
  258. significance, to an integer, in a machine-independent manner
  259. */
  260. int to_int(data)
  261. BYTE data[];
  262. {
  263.     return (int) ((unsigned int) data[0] | ((unsigned int) data[1] << 8));
  264. }
  265.  
  266. #else /* else of ifndef DIRECT_CONVERT */
  267.  
  268. long to_long(data)
  269. BYTE data[];
  270. {
  271.     return ( * (long *) data );
  272. }
  273.  
  274. #ifndef FIZ
  275. /********************
  276. splitlong() converts a long integer to four consecutive BYTEs in order
  277. of increasing significance.
  278. */
  279. void splitlong(bytes, bigword)
  280. BYTE bytes[];
  281. long bigword;
  282. {
  283.     * (long *) bytes = bigword;
  284. }
  285. #endif /* FIZ */
  286.  
  287. /*******************
  288. splitint() converts an integer to two consecutive BYTEs in order
  289. of increasing significance.
  290. */
  291. void splitint(bytes, word)
  292. BYTE bytes[];
  293. int word;
  294. {
  295.     * (int *) bytes = word;
  296. }
  297.  
  298. /**********************
  299. to_int() converts two consecutive bytes, in order of increasing
  300. significance, to an integer.
  301. */
  302. int to_int(data)
  303. BYTE data[];
  304. {
  305.     return (*(int *) data);
  306. }
  307.  
  308. #endif /* ifndef DIRECT_CONVERT .. else ... */
  309.  
  310. #ifndef FIZ
  311. /**********************
  312. Function frd_zooh() reads the header of a Zoo archive in a machine-
  313. independent manner, from a ZOOFILE.
  314. */
  315. int frd_zooh(zoo_header, zoo_file)
  316. struct zoo_header *zoo_header;
  317. ZOOFILE zoo_file;
  318. {
  319.     int status;
  320.     BYTE bytes[SIZ_ZOOH];            /* canonical header representation */
  321. #ifdef TRACE_IO
  322.     if (verbose) {
  323.         printf("At file position [%8lx] ", ftell(zoo_file));
  324.     }
  325. #endif
  326.     status = zooread (zoo_file, (char *) bytes, SIZ_ZOOH);
  327.     b_to_zooh (zoo_header, bytes);   /* convert array to structure */
  328. #ifdef TRACE_IO
  329.     if (verbose) {
  330.         printf("frd_zooh: reading\n");
  331.         show_h(zoo_header);
  332.     }
  333. #endif
  334.     if (status < MINZOOHSIZ)
  335.         return (-1);
  336.     else
  337.         return (0);
  338. }
  339. #endif /* FIZ */
  340.  
  341. /**********************
  342. Function frd_dir() reads a directory entry in a machine-independent manner,
  343. from a ZOOFILE.
  344. */
  345. int frd_dir(direntry, zoo_file)
  346. struct direntry *direntry;
  347. ZOOFILE zoo_file;
  348. {
  349.     int status;
  350.     BYTE bytes[MAXDIRSIZE];     /* big enough to hold variable part too */
  351.