home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / sysdeps / posix / tempname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-19  |  4.9 KB  |  179 lines

  1. /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <errno.h>
  21. #include <stddef.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <unistd.h>
  29.  
  30. /* Return nonzero if DIR is an existent directory.  */
  31. static int
  32. DEFUN(diraccess, (dir), CONST char *dir)
  33. {
  34.   struct stat buf;
  35.   return __stat (dir, &buf) == 0 && S_ISDIR (buf.st_mode);
  36. }
  37.  
  38. /* Return nonzero if FILE exists.  */
  39. static int
  40. DEFUN(exists, (file), CONST char *file)
  41. {
  42.   /* We can stat the file even if we can't read its data.  */
  43.   struct stat st;
  44.   int save = errno;
  45.   if (__stat (file, &st) == 0)
  46.     return 1;
  47.   else
  48.     {
  49.       /* We report that the file exists if stat failed for a reason other
  50.      than nonexistence.  In this case, it may or may not exist, and we
  51.      don't know; but reporting that it does exist will never cause any
  52.      trouble, while reporting that it doesn't exist when it does would
  53.      violate the interface of __stdio_gen_tempname.  */
  54.       int exists = errno != ENOENT;
  55.       errno = save;
  56.       return exists;
  57.     }
  58. }
  59.  
  60.  
  61. /* These are the characters used in temporary filenames.  */
  62. static CONST char letters[] =
  63.   "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  64.  
  65. /* Generate a temporary filename.
  66.    If DIR_SEARCH is nonzero, DIR and PFX are used as
  67.    described for tempnam.  If not, a temporary filename
  68.    in P_tmpdir with no special prefix is generated.  If LENPTR
  69.    is not NULL, *LENPTR is set the to length (including the
  70.    terminating '\0') of the resultant filename, which is returned.
  71.    This goes through a cyclic pattern of all possible filenames
  72.    consisting of five decimal digits of the current pid and three
  73.    of the characters in `letters'.  Data for tempnam and tmpnam
  74.    is kept separate, but when tempnam is using P_tmpdir and no
  75.    prefix (i.e, it is identical to tmpnam), the same data is used.
  76.    Each potential filename is tested for an already-existing file of
  77.    the same name, and no name of an existing file will be returned.
  78.    When the cycle reaches its end (12345ZZZ), NULL is returned.  */
  79. char *
  80. DEFUN(__stdio_gen_tempname, (dir, pfx, dir_search, lenptr),
  81.       CONST char *dir AND CONST char *pfx AND
  82.       int dir_search AND size_t *lenptr)
  83. {
  84.   static CONST char tmpdir[] = P_tmpdir;
  85.   static struct
  86.     {
  87.       char buf[3];
  88.       char *s;
  89.       size_t i;
  90.     } infos[2], *info;
  91.   static char buf[FILENAME_MAX];
  92.   static pid_t oldpid = (pid_t) 0;
  93.   pid_t pid = __getpid();
  94.   register size_t len, plen, dlen;
  95.  
  96.   if (dir_search)
  97.     {
  98.       register CONST char *d = getenv("TMPDIR");
  99.       if (d != NULL && !diraccess(d))
  100.     d = NULL;
  101.       if (d == NULL && dir != NULL && diraccess(dir))
  102.     d = dir;
  103.       if (d == NULL && diraccess(tmpdir))
  104.     d = tmpdir;
  105.       if (d == NULL && diraccess("/tmp"))
  106.     d = "/tmp";
  107.       if (d == NULL)
  108.     {
  109.       errno = ENOENT;
  110.       return NULL;
  111.     }
  112.       dir = d;
  113.     }
  114.   else
  115.     dir = tmpdir;
  116.  
  117.   dlen = strlen (dir);
  118.  
  119.   /* Remove trailing slashes from the directory name.  */
  120.   while (dlen > 1 && dir[dlen - 1] == '/')
  121.     --dlen;
  122.  
  123.   if (pfx != NULL && *pfx != '\0')
  124.     {
  125.       plen = strlen(pfx);
  126.       if (plen > 5)
  127.     plen = 5;
  128.     }
  129.   else
  130.     plen = 0;
  131.  
  132.   if (dir != tmpdir && !strcmp(dir, tmpdir))
  133.     dir = tmpdir;
  134.   info = &infos[(plen == 0 && dir == tmpdir) ? 1 : 0];
  135.  
  136.   if (pid != oldpid)
  137.     {
  138.       oldpid = pid;
  139.       infos[0].buf[0] = infos[0].buf[1] = infos[0].buf[2] = letters[0];
  140.       infos[0].s = infos[0].buf;
  141.       infos[0].i = 0;
  142.       infos[1].buf[0] = infos[1].buf[1] = infos[1].buf[2] = letters[0];
  143.       infos[1].s = infos[1].buf;
  144.       infos[1].i = 0;
  145.     }
  146.  
  147.   len = dlen + 1 + plen + 5 + 3;
  148.   for (;;)
  149.     {
  150.       *info->s = letters[info->i];
  151.       if (sizeof (buf) < len ||
  152.       sprintf (buf, "%.*s/%.*s%.5d%.3s",
  153.            (int) dlen, dir, (int) plen,
  154.            pfx, pid % 100000, info->buf) != (int) len)
  155.     return NULL;
  156.  
  157.       /* Always return a unique string.  */
  158.       ++info->i;
  159.  
  160.       if (!exists (buf))
  161.     break;
  162.  
  163.       if (info->i > sizeof (letters) - 2)
  164.     {
  165.       info->i = 0;
  166.       if (info->s == &info->buf[2])
  167.         {
  168.           errno = EEXIST;
  169.           return NULL;
  170.         }
  171.       ++info->s;
  172.     }
  173.     }
  174.  
  175.   if (lenptr != NULL)
  176.     *lenptr = len + 1;
  177.   return buf;
  178. }
  179.