home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / zoo141_c.lzh / MISC2.C < prev    next >
C/C++ Source or Header  |  1987-02-07  |  6KB  |  247 lines

  1. /* misc2.c */
  2. /*
  3. Copyright (C) 1986 Rahul Dhesi -- All rights reserved
  4. */
  5. #include "options.h"
  6. /* Miscellaneous routines needed by both Zoo and Ooz */
  7. #ifdef NOFCNTL
  8. #include <file.h>
  9. #else
  10. #include <fcntl.h>
  11. #endif
  12.  
  13. #ifdef FLAT
  14. #include <types.h>
  15. #include <stat.h>
  16. #else
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #endif
  20.  
  21. #include "portable.h"
  22. #include <stdio.h>
  23. #include "various.h"
  24. #include "zoofns.h"     /* only for malloc */
  25. #include "errors.i"
  26. #include "zoomem.h"
  27. #include "zoo.h"
  28.  
  29. /**********************/
  30. /* memerr() */
  31. /* Give error message on memory error and abort */
  32. void memerr()
  33. {
  34. #ifdef OOZ
  35.    prterror ('f', no_memory, "", "");
  36. #else
  37.    prterror ('f', no_memory);
  38. #endif
  39. }
  40.  
  41. /**********************/
  42. /* 
  43. emalloc() allocates memory like malloc() does, except that it automatically
  44. calls the error function memerr() if memory couldn't be allocated.  It also
  45. assumes (unless small memory allocation is being done) that memory will
  46. never be freed and conserves it by allocating memory in large chunks
  47. and then partitioning it out with no administrative overhead.
  48. */
  49.  
  50. char *emalloc (size)
  51. unsigned int size;
  52. {
  53. #define  BLOCK_SIZE  512      /* memory allocation granularity */
  54.  
  55. #ifdef USE_MALLOC
  56. /* Pass on memory requests to malloc() */
  57.    char *ptr;
  58.    if ((ptr = malloc (size)) == NULL)
  59.       memerr();
  60.    return (ptr);
  61. #else
  62.    static char *memptr;
  63.    static unsigned avail = 0;
  64.    unsigned malloc_incr;
  65.    char *retval;
  66.  
  67.    if (size == 0)
  68.       return (NULL);
  69.  
  70.    /* if not enough space avail get some more */
  71.    if (avail < size) {
  72.       malloc_incr = BLOCK_SIZE;
  73.       if (malloc_incr < size)
  74.          malloc_incr = size;
  75.       while (malloc_incr >= size && (memptr = malloc (malloc_incr)) == NULL)
  76.          malloc_incr = (malloc_incr / 6) * 5;
  77.       avail = malloc_incr;
  78.    }
  79.  
  80.    if (avail < size)
  81.       memerr();
  82.    else {
  83.       retval = memptr;
  84.       memptr += size;
  85.       avail -= size;
  86.       return (retval);
  87.    }
  88. #endif  /* end of not USE_MALLOC */
  89. }     
  90.  
  91. /**********************/
  92. /* putstr()
  93. This function prints a string to standard output without using printf().
  94. If a null string, nothing is printed (not even the null character).
  95. */
  96. putstr (str)
  97. register char *str;
  98. {
  99.    if (str == NULL)
  100.       return;
  101.    while (*str) {
  102.       fputchar (*str++);
  103.    }
  104. }
  105.  
  106. /**********************/
  107. /* exists()
  108. This function checks the existence of a file.  It tests to see if the
  109. file can be opened for either reading or writing;  if so, it returns
  110. 1 else it returns 0.  If portable, we test for the write too because
  111. under operating systems other than MS-DOS, write-enabled files may
  112. not always be read-enabled.  
  113.  
  114. Because of the delay between the time existence is checked and the time Zoo
  115. creates a files, a race condition exists.  It would be preferable to use the
  116. O_EXCL flag of Microsoft C and System V to open() but that may not be
  117. portable.  The access() function could be used too but that may not be
  118. available on some systems.
  119. */
  120.  
  121. int exists (fname)
  122. char *fname;
  123. {
  124.    int han;
  125.  
  126. #ifdef PORTABLE
  127.    if ((han = OPEN(fname,F_READ)) != -1 || (han = OPEN(fname,F_WRITE)) != -1){
  128. #else
  129.    /* Under MS-DOS, all files are readable */
  130.    if ((han = OPEN(fname,F_READ)) != -1) {
  131. #endif
  132.  
  133.       close (han);
  134.       return (1);
  135.    } else
  136.       return (0);
  137. }
  138.  
  139. /************************************************************************
  140. The following MS-DOS-specific functions read directory entries and zoo 
  141. headers.  Portable versions are elsewhere.
  142.  
  143. Note:  At some future time, it will be better to make the MS-DOS versions
  144. of these into macros.
  145. */
  146.  
  147. /* Currently using portable I/O for MSC too */
  148. #ifdef COMMENT
  149. #ifndef PORTABLE
  150. /**********************
  151. Function frd_zooh() reads the header of a Zoo archive from a FILE.
  152. */
  153. int frd_zooh(zoo_header, zoo_file)
  154. struct zoo_header *zoo_header;
  155. FILE *zoo_file;
  156. {
  157.    if (fread ((char *) zoo_header, SIZ_ZOOH, 1, zoo_file) != 1)
  158.       return (-1);
  159.    else
  160.       return (0);
  161. }
  162.  
  163. /**********************
  164. Function frd_dir() reads a directory entry from a FILE.
  165. */
  166. int frd_dir(direntry, zoo_file)
  167. struct direntry *direntry;
  168. FILE *zoo_file;
  169. {
  170.    if (fread ((char *) direntry, SIZ_DIR, 1, zoo_file) != 1)
  171.       return (-1);
  172.    else
  173.       return (0);
  174. }
  175.  
  176. /**********************
  177. Function rd_zooh() reads a Zoo archive header from a file handle.
  178. */
  179. int rd_zooh (header, zoo_han)
  180. struct zoo_header *header;
  181. int zoo_han;
  182. {
  183.    return (read (zoo_han, (char *) header, SIZ_ZOOH));
  184. }
  185.  
  186. /**********************
  187. Function rd_dir() reads a directory entry from a file handle */
  188. int rd_dir(direntry, zoo_han)
  189. struct direntry *direntry;
  190. int zoo_han;
  191. {
  192.    return (read (zoo_han, (char *) direntry, SIZ_DIR));
  193. }
  194.  
  195. #endif /* end of not PORTABLE */
  196. #endif /* COMMENT */
  197.  
  198. /****************
  199. newcat() allocates enough space to concatenate two strings then returns
  200. a pointer to the concatenated result */
  201.  
  202. char *newcat (r, s)
  203. char *r, *s;
  204. {
  205.    char *temp = emalloc (strlen (r) + strlen (s) + 2); /* 1 spare */
  206.    strcpy (temp, r);
  207.    strcat (temp, s);
  208.    return (temp);
  209. }
  210.  
  211.  
  212. /* Creates a path */
  213. int makepath(path)
  214. char *path;
  215. {
  216.    char tmppath[PATHSIZE];
  217.    char *slashpos;
  218.    if (path == NULL)
  219.       return;
  220.    while (*lastptr(path) == *PATH_CH)     /* remove trailing slashes */
  221.       *lastptr(path) = '\0';
  222.    if (*path == '\0')
  223.       return;
  224.  
  225.    slashpos = findlast(path, PATH_CH);    /* find last slash */
  226.    if (slashpos == NULL) {                /* if not, just create dir. */
  227.       MKDIR(path);
  228.       return;
  229.    } else {                               /* otherwise...         */
  230.       if (slashpos == path) {             /* if leading slash */
  231.          MKDIR(slashpos);                 /* make that directory */
  232.          return;                          /* and done */
  233.       } else {
  234.          strcpy(tmppath,path);            /* save path */
  235.          *slashpos = '\0';                /* split into prefix & suffix */
  236. #ifdef DEBUG
  237.          printf("making path from [%s]\n", path);
  238. #endif
  239.          makepath(path);                     /* make path from prefix */
  240. #ifdef DEBUG
  241.          printf("making dir from [%s]\n", tmppath);
  242. #endif
  243.          MKDIR(tmppath);                  /* make dir from suffix */          
  244.       }
  245.    }
  246. } /* makepath() */
  247.