home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / rcs57pc3.zip / diff16 / mktemp.c < prev    next >
C/C++ Source or Header  |  1996-03-18  |  772b  |  41 lines

  1. /* mktemp.c (emx+gcc) -- Copyright (c) 1990-1995 by Eberhard Mattes */
  2.  
  3. #include <string.h>
  4. #include <process.h>
  5. #include <io.h>
  6. #include <errno.h>
  7.  
  8. char *mktemp (char *string)
  9. {
  10.   int pid, n, saved_errno;
  11.   char *s;
  12.  
  13.   pid = getpid ();
  14.   s = strchr (string, 0);
  15.   n = 0;
  16.   while (s != string && s[-1] == 'X')
  17.     {
  18.       --s; ++n;
  19.       *s = (char)(pid % 10) + '0';
  20.       pid /= 10;
  21.     }
  22.   if (n < 2)
  23.     return NULL;
  24.   *s = 'a'; saved_errno = errno;
  25.   for (;;)
  26.     {
  27.       errno = 0;
  28.       if (access (string, 0) != 0 && errno == ENOENT)
  29.         {
  30.           errno = saved_errno;
  31.           return string;
  32.         }
  33.       if (*s == 'z')
  34.         {
  35.           errno = saved_errno;
  36.           return NULL;
  37.         }
  38.       ++*s;
  39.     }
  40. }
  41.