home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / zoo_src / z201src1 / misc2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-25  |  7.3 KB  |  307 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 exists.  It would be better to
  117. use open() with the O_EXCL flag but that will not work for many
  118. systems.
  119. */
  120.  
  121. int exists (fname)
  122. char *fname;
  123. {
  124. #ifdef EXISTS
  125.     return EXISTS(fname);
  126. #else
  127.    ZOOFILE f;
  128.  
  129.    if ( (f = zooopen (fname, Z_READ )) != NOFILE ||
  130.           (f = zooopen (fname, Z_WRITE)) != NOFILE ) {
  131.       zooclose (f);
  132.       return (1);
  133.    } else
  134.       return (0);
  135. #endif /* ifdef EXISTS */
  136. }
  137.  
  138. /****************
  139. newcat() allocates enough space to concatenate two strings then returns
  140. a pointer to the concatenated result */
  141.  
  142. char *newcat (r, s)
  143. char *r, *s;
  144. {
  145.    char *temp = emalloc (strlen (r) + strlen (s) + 2); /* 1 spare */
  146.    strcpy (temp, r);
  147.    strcat (temp, s);
  148.    return (temp);
  149. }
  150.  
  151.  
  152. /* Creates a path */
  153. int makepath(path)
  154. char *path;
  155. {
  156.    char tmppath[PATHSIZE];
  157.    char *slashpos;
  158.    if (path == NULL)
  159.       return;
  160.    while (*lastptr(path) == *PATH_CH)     /* remove trailing slashes */
  161.       *lastptr(path) = '\0';
  162.    if (*path == '\0')
  163.       return;
  164.  
  165.    slashpos = findlast(path, PATH_CH);    /* find last slash */
  166.    if (slashpos == NULL) {                /* if not, just create dir. */
  167.       MKDIR(path);
  168.       return;
  169.    } else {                               /* otherwise...         */
  170.       if (slashpos == path) {             /* if leading slash */
  171.          MKDIR(slashpos);                 /* make that directory */
  172.          return;                          /* and done */
  173.       } else {
  174.          strcpy(tmppath,path);            /* save path */
  175.          *slashpos = '\0';                /* split into prefix & suffix */
  176. #ifdef DEBUG
  177.          printf("making path from [%s]\n", path);
  178. #endif
  179.          makepath(path);                     /* make path from prefix */
  180. #ifdef DEBUG
  181.          printf("making dir from [%s]\n", tmppath);
  182. #endif
  183.          MKDIR(tmppath);                  /* make dir from suffix */
  184.       }
  185.    }
  186. } /* makepath() */
  187.  
  188. /*
  189. If no extension in filename add supplied extension
  190. */
  191. char *addext (fname, ext)
  192. char *fname;
  193. char *ext;
  194. {
  195.    if (strchr (nameptr (fname), EXT_CH) == NULL)
  196.       return (newcat (fname, ext));
  197.    else
  198.       return (fname);
  199. }
  200.  
  201. #ifdef VER_CH       /* remove any trailing extension field */
  202. char *strip_ver (fname)
  203. char *fname;
  204. {
  205.    char *p = strchr (fname, VER_CH);
  206.    if (p != NULL)
  207.       *p = '\0';
  208. }
  209. #endif
  210.  
  211. /*
  212. Function samefile() compares two filenames to see if they are the
  213. same file.  Just strcmp() or strcmpi() could have been used, except
  214. that if the filenames have trailing version fields, we want to
  215. compare those always equal.  samefile() is called by routines
  216. that want to avoid adding an archive to itself.
  217. */
  218. int samefile (f1, f2)
  219. char *f1;
  220. char *f2;
  221. {
  222. #ifdef IGNORECASE
  223. #define COMPARE strcmpi
  224. #else
  225. #define COMPARE strcmp
  226. #endif
  227.  
  228. #ifdef VER_CH
  229.    char tf1[LFNAMESIZE];
  230.    char tf2[LFNAMESIZE];
  231.    strcpy (tf1, f1);
  232.    strcpy (tf2, f2);
  233.    strip_ver (tf1);   /* strip version fields */
  234.    strip_ver (tf2);
  235.    return (COMPARE (tf1, tf2) == 0);
  236. #else
  237. /* if no version fields, just use strcmp(i) */
  238.    return (COMPARE (f1, f2) == 0);
  239. #endif
  240. }
  241.  
  242. #ifdef USE_ASCII
  243. int isdigit (c)
  244. char c;
  245. {
  246.     return (c >= '0' && c <= '9');
  247. }
  248. int isupper (c)
  249. char c;
  250. {
  251.     return (c >= 'A' && c <= 'Z');
  252. }
  253.  
  254. int toascii (c)
  255. char c;
  256. {
  257.     return (c & 0x7f);
  258. }
  259.  
  260. int tolower (c)
  261. char c;
  262. {
  263.     return (isupper(c) ? (c | 0x20) : c);
  264. }
  265. #endif
  266.  
  267. #ifdef GETTZ
  268. /****************
  269. Function tzadj() accepts a directory entry and adjusts its timestamp
  270. to reflect its timezone.  Uses function mstime() from mstime.i
  271. and mstonix() from nixtime.i.
  272. */
  273.  
  274. long mstonix();
  275. long gettz();
  276. #include "mstime.i"    /* get mstime() */
  277.  
  278. void tzadj (direntry)
  279. struct direntry *direntry;
  280. {
  281.     long diff_tz;
  282.     long longtime;
  283.     if (direntry->tz == NO_TZ)        /* none stored */
  284.         return;
  285.     diff_tz = (long) direntry->tz * (3600/4) - gettz(); /* diff. in seconds */
  286.     longtime = mstonix (direntry->date, direntry->time) + diff_tz; /* adj tz */
  287.     mstime (longtime, &direntry->date, &direntry->time);
  288. }
  289. #endif /* GETTZ */
  290.  
  291. /* how long an int can be in text form -- allow 64-bit ints */
  292. #define INT_TEXT 21
  293.  
  294. /* Function add_version adds a version suffix to a filename, given
  295. the directory entry corresponding to the file */
  296. void add_version (fname, direntry)
  297. char *fname;
  298. struct direntry *direntry;
  299. {
  300.     char verstr[INT_TEXT];    /* string buffer for conversion to text */
  301.     if (direntry->vflag & VFL_ON) {
  302.         sprintf (verstr, "%u", direntry->version_no);
  303.         strcat (fname, VER_DISPLAY);
  304.         strcat (fname, verstr);
  305.     }
  306. }
  307.