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

rename.c

解説を見る。
00001 /* rename.c -- BSD compatible directory function for System V
00002    Copyright (C) 1988, 1990 Free Software Foundation, Inc.
00003 
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 2, or (at your option)
00007    any later version.
00008 
00009    This program 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
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program; if not, write to the Free Software
00016    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.  */
00017 
00018 #ifdef HAVE_CONFIG_H
00019 #include <config.h>
00020 #endif
00021 
00022 #ifndef HAVE_RENAME
00023 
00024 #ifdef HAVE_SYS_TYPES_H
00025 #include <sys/types.h>
00026 #endif
00027 #include <sys/stat.h>
00028 #include <errno.h>
00029 #ifndef errno
00030 extern int errno;
00031 #endif
00032 
00033 #ifdef STAT_MACROS_BROKEN
00034 #undef S_ISDIR
00035 #endif /* STAT_MACROS_BROKEN.  */
00036 
00037 #if !defined(S_ISDIR) && defined(S_IFDIR)
00038 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
00039 #endif
00040 
00041 #include "safe-stat.h"
00042 
00043 /* Rename file FROM to file TO.
00044    Return 0 if successful, -1 if not. */
00045 
00046 int
00047 rename (from, to)
00048      char *from;
00049      char *to;
00050 {
00051   struct stat from_stats, to_stats;
00052   int pid, status;
00053 
00054   if (SAFE_STAT (from, &from_stats))
00055     return -1;
00056 
00057   /* Be careful not to unlink `from' if it happens to be equal to `to' or
00058      (on filesystems that silently truncate filenames after 14 characters)
00059      if `from' and `to' share the significant characters. */
00060   if (SAFE_STAT (to, &to_stats))
00061     {
00062       if (errno != ENOENT)
00063         return -1;
00064     }
00065   else
00066     {
00067       if ((from_stats.st_dev == to_stats.st_dev)
00068           && (from_stats.st_ino == to_stats.st_dev))
00069         /* `from' and `to' designate the same file on that filesystem. */
00070         return 0;
00071 
00072       if (unlink (to) && errno != ENOENT)
00073         return -1;
00074     }
00075 
00076   if (S_ISDIR (from_stats.st_mode))
00077     {
00078       /* Need a setuid root process to link and unlink directories. */
00079       pid = fork ();
00080       switch (pid)
00081         {
00082         case -1:                /* Error. */
00083           error (1, errno, "cannot fork");
00084 
00085         case 0:                 /* Child. */
00086           execl (MVDIR, "mvdir", from, to, (char *) 0);
00087           error (255, errno, "cannot run `%s'", MVDIR);
00088 
00089         default:                /* Parent. */
00090           while (wait (&status) != pid)
00091             /* Do nothing. */ ;
00092 
00093           errno = 0;            /* mvdir printed the system error message. */
00094           if (status)
00095             return -1;
00096         }
00097     }
00098   else
00099     {
00100       if (link (from, to))
00101         return -1;
00102       if (unlink (from) && errno != ENOENT)
00103         {
00104           unlink (to);
00105           return -1;
00106         }
00107     }
00108   return 0;
00109 }
00110 
00111 #endif

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