comparison src/fileio.c @ 1997:1f53050b3868 v7.2.294

updated for version 7.2-294
author vimboss
date Tue, 17 Nov 2009 11:08:52 +0000
parents ba2ac6b5bfb9
children 98cdf5c477ec
comparison
equal deleted inserted replaced
1996:077c15286de8 1997:1f53050b3868
144 # ifdef MACOS_X 144 # ifdef MACOS_X
145 static int get_mac_fio_flags __ARGS((char_u *ptr)); 145 static int get_mac_fio_flags __ARGS((char_u *ptr));
146 # endif 146 # endif
147 #endif 147 #endif
148 static int move_lines __ARGS((buf_T *frombuf, buf_T *tobuf)); 148 static int move_lines __ARGS((buf_T *frombuf, buf_T *tobuf));
149 static void vim_settempdir __ARGS((char_u *tempdir));
149 #ifdef FEAT_AUTOCMD 150 #ifdef FEAT_AUTOCMD
150 static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name"); 151 static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
151 #endif 152 #endif
152 153
153 void 154 void
6985 } 6986 }
6986 } 6987 }
6987 #endif 6988 #endif
6988 6989
6989 /* 6990 /*
6991 * Directory "tempdir" was created. Expand this name to a full path and put
6992 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
6993 * "tempdir" must be no longer than MAXPATHL.
6994 */
6995 static void
6996 vim_settempdir(tempdir)
6997 char_u *tempdir;
6998 {
6999 char_u *buf;
7000
7001 buf = alloc((unsigned)MAXPATHL + 2);
7002 if (buf != NULL)
7003 {
7004 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
7005 STRCPY(buf, tempdir);
7006 # ifdef __EMX__
7007 if (vim_strchr(buf, '/') != NULL)
7008 STRCAT(buf, "/");
7009 else
7010 # endif
7011 add_pathsep(buf);
7012 vim_tempdir = vim_strsave(buf);
7013 vim_free(buf);
7014 }
7015 }
7016
7017 /*
6990 * vim_tempname(): Return a unique name that can be used for a temp file. 7018 * vim_tempname(): Return a unique name that can be used for a temp file.
6991 * 7019 *
6992 * The temp file is NOT created. 7020 * The temp file is NOT created.
6993 * 7021 *
6994 * The returned pointer is to allocated memory. 7022 * The returned pointer is to allocated memory.
7005 #endif 7033 #endif
7006 7034
7007 #ifdef TEMPDIRNAMES 7035 #ifdef TEMPDIRNAMES
7008 static char *(tempdirs[]) = {TEMPDIRNAMES}; 7036 static char *(tempdirs[]) = {TEMPDIRNAMES};
7009 int i; 7037 int i;
7010 long nr;
7011 long off;
7012 # ifndef EEXIST 7038 # ifndef EEXIST
7013 struct stat st; 7039 struct stat st;
7014 # endif 7040 # endif
7015 7041
7016 /* 7042 /*
7025 /* 7051 /*
7026 * Try the entries in TEMPDIRNAMES to create the temp directory. 7052 * Try the entries in TEMPDIRNAMES to create the temp directory.
7027 */ 7053 */
7028 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) 7054 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i)
7029 { 7055 {
7056 size_t itmplen;
7057 # ifndef HAVE_MKDTEMP
7058 long nr;
7059 long off;
7060 # endif
7061
7030 /* expand $TMP, leave room for "/v1100000/999999999" */ 7062 /* expand $TMP, leave room for "/v1100000/999999999" */
7031 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20); 7063 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
7032 if (mch_isdir(itmp)) /* directory exists */ 7064 if (mch_isdir(itmp)) /* directory exists */
7033 { 7065 {
7034 # ifdef __EMX__ 7066 # ifdef __EMX__
7038 if (vim_strchr(itmp, '/') != NULL) 7070 if (vim_strchr(itmp, '/') != NULL)
7039 STRCAT(itmp, "/"); 7071 STRCAT(itmp, "/");
7040 else 7072 else
7041 # endif 7073 # endif
7042 add_pathsep(itmp); 7074 add_pathsep(itmp);
7043 7075 itmplen = STRLEN(itmp);
7076
7077 # ifdef HAVE_MKDTEMP
7078 /* Leave room for filename */
7079 STRCAT(itmp, "vXXXXXX");
7080 if (mkdtemp((char *)itmp) != NULL)
7081 vim_settempdir(itmp);
7082 # else
7044 /* Get an arbitrary number of up to 6 digits. When it's 7083 /* Get an arbitrary number of up to 6 digits. When it's
7045 * unlikely that it already exists it will be faster, 7084 * unlikely that it already exists it will be faster,
7046 * otherwise it doesn't matter. The use of mkdir() avoids any 7085 * otherwise it doesn't matter. The use of mkdir() avoids any
7047 * security problems because of the predictable number. */ 7086 * security problems because of the predictable number. */
7048 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L; 7087 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
7050 /* Try up to 10000 different values until we find a name that 7089 /* Try up to 10000 different values until we find a name that
7051 * doesn't exist. */ 7090 * doesn't exist. */
7052 for (off = 0; off < 10000L; ++off) 7091 for (off = 0; off < 10000L; ++off)
7053 { 7092 {
7054 int r; 7093 int r;
7055 #if defined(UNIX) || defined(VMS) 7094 # if defined(UNIX) || defined(VMS)
7056 mode_t umask_save; 7095 mode_t umask_save;
7057 #endif 7096 # endif
7058 7097
7059 sprintf((char *)itmp + STRLEN(itmp), "v%ld", nr + off); 7098 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
7060 # ifndef EEXIST 7099 # ifndef EEXIST
7061 /* If mkdir() does not set errno to EEXIST, check for 7100 /* If mkdir() does not set errno to EEXIST, check for
7062 * existing file here. There is a race condition then, 7101 * existing file here. There is a race condition then,
7063 * although it's fail-safe. */ 7102 * although it's fail-safe. */
7064 if (mch_stat((char *)itmp, &st) >= 0) 7103 if (mch_stat((char *)itmp, &st) >= 0)
7065 continue; 7104 continue;
7066 # endif 7105 # endif
7067 #if defined(UNIX) || defined(VMS) 7106 # if defined(UNIX) || defined(VMS)
7068 /* Make sure the umask doesn't remove the executable bit. 7107 /* Make sure the umask doesn't remove the executable bit.
7069 * "repl" has been reported to use "177". */ 7108 * "repl" has been reported to use "177". */
7070 umask_save = umask(077); 7109 umask_save = umask(077);
7071 #endif 7110 # endif
7072 r = vim_mkdir(itmp, 0700); 7111 r = vim_mkdir(itmp, 0700);
7073 #if defined(UNIX) || defined(VMS) 7112 # if defined(UNIX) || defined(VMS)
7074 (void)umask(umask_save); 7113 (void)umask(umask_save);
7075 #endif 7114 # endif
7076 if (r == 0) 7115 if (r == 0)
7077 { 7116 {
7078 char_u *buf; 7117 vim_settempdir(itmp);
7079
7080 /* Directory was created, use this name.
7081 * Expand to full path; When using the current
7082 * directory a ":cd" would confuse us. */
7083 buf = alloc((unsigned)MAXPATHL + 1);
7084 if (buf != NULL)
7085 {
7086 if (vim_FullName(itmp, buf, MAXPATHL, FALSE)
7087 == FAIL)
7088 STRCPY(buf, itmp);
7089 # ifdef __EMX__
7090 if (vim_strchr(buf, '/') != NULL)
7091 STRCAT(buf, "/");
7092 else
7093 # endif
7094 add_pathsep(buf);
7095 vim_tempdir = vim_strsave(buf);
7096 vim_free(buf);
7097 }
7098 break; 7118 break;
7099 } 7119 }
7100 # ifdef EEXIST 7120 # ifdef EEXIST
7101 /* If the mkdir() didn't fail because the file/dir exists, 7121 /* If the mkdir() didn't fail because the file/dir exists,
7102 * we probably can't create any dir here, try another 7122 * we probably can't create any dir here, try another
7103 * place. */ 7123 * place. */
7104 if (errno != EEXIST) 7124 if (errno != EEXIST)
7105 # endif 7125 # endif
7106 break; 7126 break;
7107 } 7127 }
7128 # endif /* HAVE_MKDTEMP */
7108 if (vim_tempdir != NULL) 7129 if (vim_tempdir != NULL)
7109 break; 7130 break;
7110 } 7131 }
7111 } 7132 }
7112 } 7133 }