home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / compress / zoosrc20.zoo / misc2.c < prev    next >
C/C++ Source or Header  |  1989-07-25  |  8KB  |  312 lines

  1. #ifndef LINT
  2. /* @(#) misc2.c 2.7 88/01/24 12:47:36 */
  3. static char sccsid[]="@(#) misc2.c 2.7 88/01/24 12:47:36";
  4. #endif /* LINT */
  5.  
  6. /*
  7. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  8. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  9. */
  10. #include "options.h"
  11. /* Miscellaneous routines */
  12. #include "portable.h"
  13. #include "zooio.h"
  14. #include "various.h"
  15. #include "zoofns.h"     /* only for malloc */
  16. #include "errors.i"
  17. #include "zoomem.h"
  18. #include "zoo.h"
  19.  
  20. #ifdef LINT_ARGS
  21. int makepath (char *);
  22. #else
  23. int makepath ();
  24. #endif
  25.  
  26.  
  27. /**********************/
  28. /* memerr() */
  29. /* Give error message on memory error and abort */
  30. void memerr()
  31. {
  32. #ifdef OOZ
  33.    prterror ('f', no_memory, "", "");
  34. #else
  35.    prterror ('f', no_memory);
  36. #endif
  37. }
  38.  
  39. /**********************/
  40. /*
  41. emalloc() allocates memory like malloc() does, except that it automatically
  42. calls the error function memerr() if memory couldn't be allocated.  It also
  43. assumes (unless small memory allocation is being done) that memory will
  44. never be freed and conserves it by allocating memory in large chunks
  45. and then partitioning it out with no administrative overhead.
  46. */
  47.  
  48. char *emalloc (size)
  49. unsigned int size;
  50. {
  51. #define  BLOCK_SIZE  512      /* memory allocation granularity */
  52.  
  53. #ifdef USE_MALLOC
  54. /* Pass on memory requests to malloc() */
  55.    char *ptr;
  56.    if ((ptr = malloc (size)) == NULL)
  57.       memerr();
  58.    return (ptr);
  59. #else
  60.    static char *memptr;
  61.    static unsigned avail = 0;
  62.    unsigned malloc_incr;
  63.    char *retval;
  64.  
  65.    if (size == 0)
  66.       return (NULL);
  67.  
  68.    /* if not enough space avail get some more */
  69.    if (avail < size) {
  70.       malloc_incr = BLOCK_SIZE;
  71.       if (malloc_incr < size)
  72.          malloc_incr = size;
  73.       while (malloc_incr >= size && (memptr = malloc (malloc_incr)) == NULL)
  74.          malloc_incr = (malloc_incr / 6) * 5;
  75.       avail = malloc_incr;
  76.    }
  77.  
  78.    if (avail < size)
  79.       memerr();                              /* no return from this */
  80.    retval = memptr;
  81.    memptr += size;
  82.    avail -= size;
  83.    return (retval);
  84. #endif  /* end of not USE_MALLOC */
  85. }
  86.  
  87. /**********************/
  88. /* putstr()
  89. This function prints a string to standard output.  If the received
  90. string pointer is NULL, it is handled safely.  This function is here
  91. for historical reasons:  Ooz was once coded to not use printf under
  92. MSDOS to save space, and at that time putstr() printed a string
  93. without using printf.  It should eventually be eliminated and all
  94. calls to it replaced with calls to printf directly.
  95. */
  96. putstr (str)
  97. register char *str;
  98. {
  99.    if (str == NULL)
  100.       return;
  101.     printf ("%s", str);
  102. }
  103.  
  104. /**********************/
  105. /* exists()
  106. This function checks the existence of a file.  
  107.  
  108. If the symbol EXISTS is defined, that is called as a macro and
  109. supplied the filename.  It must return 1 if the file exists and
  110. 0 if it does not.
  111.  
  112. If EXISTS is not defined, exists() tests to see if the file can be 
  113. opened for reading or writing;  if so, it returns 1 else it returns 0. 
  114.  
  115. Because of the delay between the time existence is checked and the time Zoo
  116. creates a files, a race condition exist
  117. s.  It would be better to
  118. use open() with the O_EXCL flag but that will not work for many
  119. systems.
  120. */
  121.  
  122. int exists (fname)
  123. char *fname;
  124. {
  125. #ifdef EXISTS
  126.     return EXISTS(fname);
  127. #else
  128.    ZOOFILE f;
  129.  
  130.    if ( (f = zooopen (fname, Z_READ )) != NOFILE ||
  131.           (f = zooopen (fname, Z_WRITE)) != NOFILE ) {
  132.       zooclose (f);
  133.       return (1);
  134.    } else
  135.       return (0);
  136. #endif /* ifdef EXISTS */
  137. }
  138.  
  139. /****************
  140. newcat() allocates enough space to concatenate two strings then returns
  141. a pointer to the concatenated
  142.  result */
  143.  
  144. char *newcat (r, s)
  145. char *r, *s;
  146. {
  147.    char *temp = emalloc (strlen (r) + strlen (s) + 2); /* 1 spare */
  148.    strcpy (temp, r);
  149.    strcat (temp, s);
  150.    return (temp);
  151. }
  152.  
  153.  
  154. /* Creates a path */
  155. int makepath(path)
  156. char *path;
  157. {
  158.    char tmppath[PATHSIZE];
  159.    char *slashpos;
  160.    if (path == NULL)
  161.       return;
  162.    while (*lastptr(path) == *PATH_CH)     /* remove trailing slashes */
  163.       *lastptr(path) = '\0';
  164.    if (*path == '\0')
  165.       return;
  166.  
  167.    slashpos = findlast(path, PATH_CH);    /* find last sl
  168. ash */
  169.    if (slashpos == NULL) {                /* if not, just create dir. */
  170.       MKDIR(path);
  171.       return;
  172.    } else {                               /* otherwise...         */
  173.       if (slashpos == path) {             /* if leading slash */
  174.          MKDIR(slashpos);                 /* make that directory */
  175.          return;                          /* and done */
  176.       } else {
  177.          strcpy(tmppath,path);            /* save path */
  178.          *slashpos = '\0';                /* split into prefix & 
  179. suffix */
  180. #ifdef DEBUG
  181.          printf("making path from [%s]\n", path);
  182. #endif
  183.          makepath(path);                     /* make path from prefix */
  184. #ifdef DEBUG
  185.          printf("making dir from [%s]\n", tmppath);
  186. #endif
  187.          MKDIR(tmppath);                  /* make dir from suffix */
  188.       }
  189.    }
  190. } /* makepath() */
  191.  
  192. /*
  193. If no extension in filename add supplied extension
  194. */
  195. char *addext (fname, ext)
  196. char *fname;
  197. char *ext;
  198. {
  199.    if (strchr (nameptr (fname), EXT_CH) == NULL)
  200.       return (newcat (fname, ext));
  201.    else
  202.       return (fname);
  203. }
  204.  
  205. #ifdef VER_CH       /* remove any trailing extension field */
  206. char *strip_ver (fname)
  207. char *fname;
  208. {
  209.    char *p = strchr (fname, VER_CH);
  210.    if (p != NULL)
  211.       *p = '\0';
  212. }
  213. #endif
  214.  
  215. /*
  216. Function samefile() compares two filenames to see if they are the
  217. same file.  Just strcmp() or strcmpi() could have been used, except
  218. that if the filenames have trailing version fields, we want to
  219. compare those always equal.  samefile() is called by routines
  220. that want to avoid adding an archive to itself.
  221. */
  222. int samefile (f1, f2)
  223. char *f1;
  224. char *f2;
  225. {
  226. #ifdef IGNORECASE
  227. #define COMPARE strcmpi
  228. #else
  229. #define COMPARE strcmp
  230. #endif
  231.  
  232. #ifdef VER_CH
  233.    char tf1[LFNAMESIZE];
  234.    char tf2[LFNAMESIZE];
  235.    strcpy (tf1, f1);
  236.    strcpy (tf2, f2);
  237.    strip_ver (tf1);   /* strip version fields */
  238.    strip_ver (tf2);
  239.    return (COMPARE (tf1, tf2) == 0);
  240. #else
  241. /* if no version fields, just use strcmp(i) */
  242.    return (COMPARE (f1, f2) == 0);
  243. #endif
  244. }
  245.  
  246. #ifdef USE_ASCII
  247. int isdigit (c)
  248. char c;
  249. {
  250.     return (c >= '0' && c <= '9');
  251. }
  252. int isupper (c)
  253. char c;
  254. {
  255.     return (c >= 'A' && c <= 'Z');
  256. }
  257.  
  258. int toascii (c)
  259. char c;
  260. {
  261.     return (c & 0x7f);
  262. }
  263.  
  264. int tolower (c)
  265. char c;
  266. {
  267.     return (isupper(c) ? (c | 0x20) : c);
  268. }
  269. #endif
  270.  
  271. #ifdef GETTZ
  272. /****************
  273. Function tzadj() accepts a directory entry and adjusts its timestamp
  274. to reflect its timezone.  Uses function mstime() from mstime.i
  275. and mstonix() from nixtime.i.
  276. */
  277.  
  278. long mstonix();
  279. long gettz();
  280. #include "mstime.i"    /* get mstime() */
  281.  
  282. void tzadj (direntry)
  283. struct direntry *direntry;
  284. {
  285.     long diff_tz;
  286.     long longtime;
  287.     if (direntry->tz == NO_TZ)        /* none stored */
  288.         return;
  289.     diff_tz = (long) direntry->tz * (3600/4) - gettz(); /* diff. in seconds */
  290.     longtime = mstonix (direntry->date, direntry->time) + diff_tz; /* adj tz */
  291.     mstime (longtime, &direntry->date, &direntry->time);
  292. }
  293. #endif /* GETTZ */
  294.  
  295. /* how long an int can be in text form -- allow 64-bit ints */
  296. #define INT_TEXT 21
  297.  
  298. /* Function add_version adds a version suffix to a filename, given
  299. the directory entry corresponding to the file */
  300. void add_version (fname, direntry)
  301. char *fname;
  302. struct direntry *direntry;
  303. {
  304.     char verstr[INT_TEXT];    /* string buffer for conversion to text */
  305.     if (direntry->vflag & VFL_ON) {
  306.         sprintf (verstr, "%u", direntry->version_no);
  307.         strcat (fname, VER_DISPLAY);
  308.         strcat (fname, verstr);
  309.     }
  310. }
  311.  
  312.