home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / libio / iotempname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-10  |  6.3 KB  |  239 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. #ifdef __STDC__
  22. #include <stddef.h>
  23. #endif
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h>
  29. #include <unistd.h>
  30. #include "libioP.h"
  31. #include "stdio.h"
  32.  
  33. #ifndef _IO_open
  34. #define _IO_open open
  35. #endif
  36.  
  37. #ifndef _IO_close
  38. #define _IO_close close
  39. #endif
  40.  
  41. #ifndef _IO_stat
  42. #define _IO_stat stat
  43. #endif
  44.  
  45. #ifndef _IO_getpid
  46. #define _IO_getpid getpid
  47. #endif
  48.  
  49. #ifndef _IO_geteuid
  50. #define _IO_geteuid geteuid
  51. #endif
  52.  
  53. /* Return nonzero if DIR is an existent directory.  */
  54. static int
  55. DEFUN(diraccess, (dir), CONST char *dir)
  56. {
  57.   struct stat buf;
  58.   _IO_uid_t euid;
  59.  
  60.   if (_IO_stat(dir, &buf) != 0 || !S_ISDIR(buf.st_mode)) return 0;
  61.  
  62.   /* That is going to be tough. */
  63.  
  64.   euid = _IO_geteuid ();
  65.   
  66.   /* super user */
  67.   if (!euid) return 1;
  68.  
  69.   if (euid == buf.st_uid)
  70.     return ((buf.st_mode & S_IWUSR) && (buf.st_mode & S_IXUSR));
  71.  
  72.   if (_IO_getegid () == buf.st_gid)
  73.     return ((buf.st_mode & S_IWGRP) && (buf.st_mode & S_IXGRP));
  74.  
  75.   return ((buf.st_mode & S_IWOTH) && (buf.st_mode & S_IXOTH));
  76. }
  77.  
  78.  
  79. /* Return nonzero if FILE exists.  */
  80. static int
  81. DEFUN(exists, (file), CONST char *file)
  82. {
  83.   /* We can stat the file even if we can't read its data.  */
  84.   struct stat st;
  85.   int save = errno;
  86.   if (_IO_stat (file, &st) == 0)
  87.     return 1;
  88.   else
  89.     {
  90.       /* We report that the file exists if stat failed for a reason other
  91.      than nonexistence.  In this case, it may or may not exist, and we
  92.      don't know; but reporting that it does exist will never cause any
  93.      trouble, while reporting that it doesn't exist when it does would
  94.      violate the interface of __stdio_gen_tempname.  */
  95.       int exists = errno != ENOENT;
  96.       errno = save;
  97.       return exists;
  98.     }
  99. }
  100.  
  101.  
  102. /* These are the characters used in temporary filenames.  */
  103. static CONST char letters[] =
  104.   "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  105.  
  106. /* Generate a temporary filename and return it (in a static buffer).  If
  107.    STREAMPTR is not NULL, open a stream "w+b" on the file and set
  108.    *STREAMPTR to it.  If DIR_SEARCH is nonzero, DIR and PFX are used as
  109.    described for tempnam.  If not, a temporary filename in P_tmpdir with no
  110.    special prefix is generated.  If LENPTR is not NULL, *LENPTR is set the
  111.    to length (including the terminating '\0') of the resultant filename,
  112.    which is returned.  This goes through a cyclic pattern of all possible
  113.    filenames consisting of five decimal digits of the current pid and three
  114.    of the characters in `letters'.  Data for tempnam and tmpnam is kept
  115.    separate, but when tempnam is using P_tmpdir and no prefix (i.e, it is
  116.    identical to tmpnam), the same data is used.  Each potential filename is
  117.    tested for an already-existing file of the same name, and no name of an
  118.    existing file will be returned.  When the cycle reaches its end
  119.    (12345ZZZ), NULL is returned.  */
  120. char *
  121. DEFUN(_IO_gen_tempname, (dir, pfx, dir_search, lenptr, streamptr),
  122.       CONST char *dir AND CONST char *pfx AND
  123.       int dir_search AND _IO_size_t *lenptr AND
  124.       _IO_FILE **streamptr)
  125. {
  126.   int saverrno = errno;
  127.   static CONST char tmpdir[] = P_tmpdir;
  128.   static struct
  129.     {
  130.       unsigned char digits [4];
  131.     } infos[2], *info;
  132.   static char buf[FILENAME_MAX];
  133.   static _IO_pid_t oldpid = (pid_t) 0;
  134.   _IO_pid_t pid = _IO_getpid();
  135.   register _IO_size_t len, plen, dlen;
  136.   int i, carry;
  137.  
  138.   if (dir_search)
  139.     {
  140.       register CONST char *d = getenv("TMPDIR");
  141.       if (d != NULL && !diraccess(d))
  142.     d = NULL;
  143.       if (d == NULL && dir != NULL && diraccess(dir))
  144.     d = dir;
  145.       if (d == NULL && diraccess(tmpdir))
  146.     d = tmpdir;
  147.       if (d == NULL && diraccess("/tmp"))
  148.     d = "/tmp";
  149.       if (d == NULL)
  150.     {
  151.       errno = ENOENT;
  152.       return NULL;
  153.     }
  154.       dir = d;
  155.     }
  156.   else
  157.     dir = tmpdir;
  158.  
  159.   dlen = strlen (dir);
  160.  
  161.   /* Remove trailing slashes from the directory name.  */
  162.   while (dlen > 1 && dir[dlen - 1] == '/')
  163.     --dlen;
  164.  
  165.   if (pfx != NULL && *pfx != '\0')
  166.     {
  167.       plen = strlen(pfx);
  168.       if (plen > 5)
  169.     plen = 5;
  170.     }
  171.   else
  172.     plen = 0;
  173.  
  174.   if (dir != tmpdir && !strcmp(dir, tmpdir))
  175.     dir = tmpdir;
  176.   info = &infos[(plen == 0 && dir == tmpdir) ? 1 : 0];
  177.  
  178.   if (pid != oldpid)
  179.     {
  180.       oldpid = pid;
  181.       for (i = 0; i < sizeof (info->digits); i++)
  182.     infos[0].digits[i] = infos[1].digits[i] = 0;
  183.     }
  184.  
  185.   len = dlen + 1 + plen + 5;
  186.   for (;;)
  187.     {
  188.       if (info->digits [sizeof (info->digits) - 1])
  189.     {
  190.       errno = EEXIST;
  191.       return NULL;
  192.     }
  193.  
  194.       if ((sizeof (buf) - sizeof (info->digits)) < len ||
  195.         sprintf(buf, "%.*s/%.*s%.5d", (int) dlen, dir,
  196.         (int) plen, pfx, pid % 100000) != (int) len)
  197.     return NULL;
  198.  
  199.       /* Get the last part of string */
  200.       for (i = 0; i < sizeof (info->digits) - 1; i++)
  201.         buf [len++] = letters [info->digits [i]];
  202.       buf [len] = '\0';
  203.  
  204.       /* Always return a unique string.  */
  205.       carry = ++info->digits [0] / (sizeof (letters) - 1);
  206.       info->digits [0] %= (sizeof (letters) - 1);
  207.       for (i = 1; i < sizeof (info->digits); i++) {
  208.       info->digits [i] += carry;
  209.       carry = info->digits [i] / (sizeof (letters) - 1);
  210.       info->digits [i] %= (sizeof (letters) - 1);
  211.       }
  212.  
  213.       if (streamptr != NULL)
  214.     {
  215.       int fd = _IO_open (buf, O_RDWR|O_CREAT|O_EXCL, 0666);
  216.       if (fd >= 0)
  217.         {
  218.           *streamptr = _IO_fdopen (fd, "w+b");
  219.           if (*streamptr == NULL)
  220.         {
  221.           int save = errno;
  222.           (void) _IO_close (fd);
  223.           errno = save;
  224.           return NULL;
  225.         }
  226.           break;
  227.         }
  228.     }
  229.       else if (!exists (buf))
  230.     break;
  231.     }
  232.  
  233.   errno = saverrno;
  234.  
  235.   if (lenptr != NULL)
  236.     *lenptr = len + 1;
  237.   return buf;
  238. }
  239.