home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / EMXLIB8F.ZIP / EMX / LIB / IO / TMPFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  3.5 KB  |  160 lines

  1. /* tmpfile.c (emx+gcc) -- Copyright (c) 1990-1993 by Eberhard Mattes */
  2. /*                        Copyright (c) 1991-1993 by Kolja Elsaesser */
  3.  
  4. #include <sys/emx.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <io.h>
  9. #include <errno.h>
  10. #include <sys/param.h>
  11.  
  12. #define IDX_LO 10000000
  13. #define IDX_HI 99999999
  14.  
  15. static int _tmpidx = IDX_LO;
  16.  
  17. static int _isdir (char *dst, const char *src);
  18.  
  19.  
  20. FILE *tmpfile (void)
  21. {
  22.   char name[L_tmpnam];
  23.   FILE *f;
  24.  
  25.   if (tmpnam (name) == NULL)
  26.     return (NULL);
  27.   f = fopen (name, "w+b");
  28.   if (f == NULL)
  29.     return (NULL);
  30.   f->tmpidx = _tmpidx;
  31.   f->flags |= _IOTMP;
  32.   return (f);
  33. }
  34.  
  35.  
  36. char *tmpnam (char *string)
  37. {
  38.   char *p;
  39.   int saved_errno, idx_start;
  40. #if defined (__MT__)
  41.   struct _thread *tp = _thread ();
  42. #define tnbuf (tp->_th_tmpnam_buf)
  43. #else
  44.   static char tnbuf[L_tmpnam];
  45. #endif
  46.  
  47.   if (string == NULL) string = tnbuf;
  48.   strcpy (string, P_tmpdir);
  49.   p = strchr (string, 0);
  50.   if (string[0] != 0 && p[-1] != '\\' && p[-1] != '/' && p[-1] != ':')
  51.     *p++ = '/';
  52.   saved_errno = errno;
  53.   idx_start = _tmpidx;
  54.   for (;;)
  55.     {
  56.       if (_tmpidx >= IDX_HI)
  57.         _tmpidx = IDX_LO;
  58.       else
  59.         ++_tmpidx;
  60.       if (_tmpidx == idx_start)
  61.         {
  62.           errno = EINVAL;
  63.           return (NULL);
  64.         }
  65.       _itoa (_tmpidx, p, 10);
  66.       strcat (p, ".tmp");
  67.       errno = 0;
  68.       if (access (string, 0) != 0)
  69.         {
  70.           if (errno == ENOENT)
  71.             break;
  72.           return (NULL);
  73.         }
  74.     }
  75.   errno = saved_errno;
  76.   return (string);
  77. }
  78.  
  79. /* Create absolute path name from src, copy it to dst and return 1 if
  80.    it's a directory. Note that this works also with a trailing backslash! */
  81.  
  82. static int _isdir (char *dst, const char *src)
  83. {
  84.   int attr;
  85.  
  86.   if (_fullpath (dst, src, MAXPATHLEN) != 0)
  87.     return (0);
  88.   attr = __chmod (dst, 0, 0);
  89.   return (attr >= 0 && (attr & _A_SUBDIR));
  90. }
  91.  
  92.  
  93. char *tempnam (const char *dir, const char *prefix)
  94. {
  95.   const char *tmpdir;
  96.   char *tmpname, *p, buf[MAXPATHLEN];
  97.   int saved_errno, idx_start;
  98.  
  99.   saved_errno = errno;
  100.   if (strlen (prefix) > 5)
  101.     {
  102.       errno = EINVAL;
  103.       return (NULL);
  104.     }
  105.   tmpdir = NULL;
  106.   p = getenv ("TMP");
  107.   if (tmpdir == NULL && p != NULL && _isdir (buf, p))
  108.     tmpdir = buf;
  109.   if (tmpdir == NULL && dir != NULL && _isdir (buf, dir))
  110.     tmpdir = buf;
  111.   if (tmpdir == NULL)
  112.     tmpdir = P_tmpdir;
  113.   tmpname = malloc (strlen (tmpdir) + 1 + L_tmpnam);
  114.   if (tmpname == NULL)
  115.     {
  116.       errno = ENOMEM;
  117.       return (NULL);
  118.     }
  119.   strcpy (tmpname, tmpdir);
  120.   p = strchr (tmpname, 0);
  121.   if (tmpname[0] != 0 && p[-1] != '\\' && p[-1] != '/' && p[-1] != ':')
  122.     *p++ = '/';
  123.   idx_start = _tmpidx;
  124.   for (;;)
  125.     {
  126.       if (_tmpidx == IDX_HI)
  127.         _tmpidx = IDX_LO;
  128.       else
  129.         ++_tmpidx;
  130.       if (_tmpidx == idx_start)
  131.         {
  132.           free (tmpname);
  133.           errno = EINVAL;
  134.           return (NULL);
  135.         }
  136.       _itoa (_tmpidx, p, 10);
  137.       strcat (p, ".tmp");
  138.       memmove (p, prefix, strlen (prefix));
  139.       errno = 0;
  140.       if (access (tmpname, 0) != 0)
  141.         {
  142.           if (errno == ENOENT)
  143.             break;
  144.           return (NULL);
  145.         }
  146.     }
  147.   errno = saved_errno;
  148.   if (_fullpath (buf, tmpname, sizeof (buf)) != 0)
  149.     {
  150.       free (tmpname);
  151.       errno = ENOENT;
  152.       return (NULL);
  153.     }
  154.   free (tmpname);
  155.   p = strdup (buf);
  156.   if (p == NULL)
  157.     errno = ENOMEM;
  158.   return (p);
  159. }
  160.