メインページ   クラス階層   構成   ファイル一覧   構成メンバ   ファイルメンバ  

tempname.c

解説を見る。
00001 /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
00002 This file is part of the GNU C Library.
00003 
00004 The GNU C Library is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License as
00006 published by the Free Software Foundation; either version 2 of the
00007 License, or (at your option) any later version.
00008 
00009 The GNU C Library is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public
00015 License along with the GNU C Library; see the file COPYING.  If
00016 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017 Boston, MA  02111-1307, USA.  */
00018 
00019 #ifdef HAVE_CONFIG_H
00020 #include <config.h>
00021 #endif
00022 
00023 #ifndef HAVE_TEMPNAM
00024 
00025 #include <errno.h>
00026 #include <stddef.h>
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 
00031 #ifdef HAVE_UNISTD_H
00032 #ifdef HAVE_SYS_TYPES_H
00033 #include <sys/types.h>
00034 #endif
00035 #include <unistd.h>
00036 #endif
00037 
00038 #include <fcntl.h>
00039 
00040 #include "statdefs.h"
00041 
00042 #ifndef FILENAME_MAX
00043 #ifdef MAXPATHLEN
00044 #define FILENAME_MAX MAXPATHLEN
00045 #else
00046 #define FILENAME_MAX 1024
00047 #endif
00048 #endif
00049 
00050 #ifndef P_tmpdir
00051 #define P_tmpdir "/usr/tmp/"
00052 #endif
00053 
00054 /* Return nonzero if DIR is an existent directory.  */
00055 static int
00056 diraccess (const char *dir)
00057 {
00058   struct stat buf;
00059   return stat (dir, &buf) == 0 && S_ISDIR (buf.st_mode);
00060 }
00061 
00062 /* Return nonzero if FILE exists.  */
00063 static int
00064 exists (const char *file)
00065 {
00066   /* We can stat the file even if we can't read its data.  */
00067   struct stat st;
00068   int save = errno;
00069   if (stat (file, &st) == 0)
00070     return 1;
00071   else
00072     {
00073       /* We report that the file exists if stat failed for a reason other
00074          than nonexistence.  In this case, it may or may not exist, and we
00075          don't know; but reporting that it does exist will never cause any
00076          trouble, while reporting that it doesn't exist when it does would
00077          violate the interface of __stdio_gen_tempname.  */
00078       int exists = errno != ENOENT;
00079       errno = save;
00080       return exists;
00081     }
00082 }
00083 
00084 
00085 /* These are the characters used in temporary filenames.  */
00086 static const char letters[] =
00087   "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
00088 
00089 /* Generate a temporary filename and return it (in a static buffer).  If
00090    STREAMPTR is not NULL, open a stream "w+b" on the file and set
00091    *STREAMPTR to it.  If DIR_SEARCH is nonzero, DIR and PFX are used as
00092    described for tempnam.  If not, a temporary filename in P_tmpdir with no
00093    special prefix is generated.  If LENPTR is not NULL, *LENPTR is set the
00094    to length (including the terminating '\0') of the resultant filename,
00095    which is returned.  This goes through a cyclic pattern of all possible
00096    filenames consisting of five decimal digits of the current pid and three
00097    of the characters in `letters'.  Data for tempnam and tmpnam is kept
00098    separate, but when tempnam is using P_tmpdir and no prefix (i.e, it is
00099    identical to tmpnam), the same data is used.  Each potential filename is
00100    tested for an already-existing file of the same name, and no name of an
00101    existing file will be returned.  When the cycle reaches its end
00102    (12345ZZZ), NULL is returned.  */
00103 char *
00104 __stdio_gen_tempname (const char *dir, const char *pfx,
00105                       int dir_search, size_t *lenptr,
00106                       FILE **streamptr)
00107 {
00108   int saverrno = errno;
00109   static const char tmpdir[] = P_tmpdir;
00110   static size_t indices[2];
00111   size_t *idx;
00112   static char buf[FILENAME_MAX];
00113   static pid_t oldpid = (pid_t) 0;
00114   pid_t pid = getpid();
00115   register size_t len, plen, dlen;
00116 
00117   if (dir_search)
00118     {
00119       register const char *d = getenv("TMPDIR");
00120       if (d != NULL && !diraccess(d))
00121         d = NULL;
00122       if (d == NULL && dir != NULL && diraccess(dir))
00123         d = dir;
00124       if (d == NULL && diraccess(tmpdir))
00125         d = tmpdir;
00126       if (d == NULL && diraccess("/tmp"))
00127         d = "/tmp";
00128       if (d == NULL)
00129         {
00130           errno = ENOENT;
00131           return NULL;
00132         }
00133       dir = d;
00134     }
00135   else
00136     dir = tmpdir;
00137 
00138   dlen = strlen (dir);
00139 
00140   /* Remove trailing slashes from the directory name.  */
00141   while (dlen > 1 && dir[dlen - 1] == '/')
00142     --dlen;
00143 
00144   if (pfx != NULL && *pfx != '\0')
00145     {
00146       plen = strlen(pfx);
00147       if (plen > 5)
00148         plen = 5;
00149     }
00150   else
00151     plen = 0;
00152 
00153   if (dir != tmpdir && !strcmp(dir, tmpdir))
00154     dir = tmpdir;
00155   idx = &indices[(plen == 0 && dir == tmpdir) ? 1 : 0];
00156 
00157   if (pid != oldpid)
00158     {
00159       oldpid = pid;
00160       indices[0] = indices[1] = 0;
00161     }
00162 
00163   len = dlen + 1 + plen + 5 + 3;
00164   for (; *idx < ((sizeof (letters) - 1) * (sizeof (letters) - 1) *
00165                  (sizeof (letters) - 1));
00166        ++*idx)
00167     {
00168       /* Construct a file name and see if it already exists.
00169 
00170          We use a single counter in *IDX to cycle each of three
00171          character positions through each of 62 possible letters.  */
00172 
00173       if (sizeof (buf) < len)
00174         return NULL;
00175 
00176       sprintf (buf, "%.*s/%.*s%.5d%c%c%c",
00177                (int) dlen, dir, (int) plen,
00178                pfx, pid % 100000,
00179                letters[*idx
00180                        % (sizeof (letters) - 1)],
00181                letters[(*idx / (sizeof (letters) - 1))
00182                        % (sizeof (letters) - 1)],
00183                letters[(*idx / ((sizeof (letters) - 1) *
00184                                 (sizeof (letters) - 1)))
00185                        % (sizeof (letters) - 1)]
00186                );
00187 
00188       if (! buf || strlen (buf) != (int) len)
00189         return NULL;
00190   
00191       if (streamptr != NULL)
00192         abort ();
00193       else if (exists (buf))
00194         continue;
00195 
00196       /* If the file already existed we have continued the loop above,
00197          so we only get here when we have a winning name to return.  */
00198 
00199       errno = saverrno;
00200 
00201       if (lenptr != NULL)
00202         *lenptr = len + 1;
00203       return buf;
00204     }
00205 
00206   /* We got out of the loop because we ran out of combinations to try.  */
00207   errno = EEXIST;               /* ? */
00208   return NULL;
00209 }
00210 
00211 #endif

Wed Dec 29 11:52:20 2004に生成されました。 doxygen1.2.18
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送