00001 /* Copyright (C) 2000 MySQL AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 #include "mysys_priv.h" 00018 #include <m_string.h> 00019 00020 #if defined( __WIN__) || defined(__NETWARE__) 00021 #define DELIM ';' 00022 #else 00023 #define DELIM ':' 00024 #endif 00025 00026 my_bool init_tmpdir(MY_TMPDIR *tmpdir, const char *pathlist) 00027 { 00028 char *end, *copy; 00029 char buff[FN_REFLEN]; 00030 DYNAMIC_ARRAY t_arr; 00031 DBUG_ENTER("init_tmpdir"); 00032 DBUG_PRINT("enter", ("pathlist: %s", pathlist ? pathlist : "NULL")); 00033 00034 pthread_mutex_init(&tmpdir->mutex, MY_MUTEX_INIT_FAST); 00035 if (my_init_dynamic_array(&t_arr, sizeof(char*), 1, 5)) 00036 goto err; 00037 if (!pathlist || !pathlist[0]) 00038 { 00039 /* Get default temporary directory */ 00040 pathlist=getenv("TMPDIR"); /* Use this if possible */ 00041 #if defined( __WIN__) || defined(__NETWARE__) 00042 if (!pathlist) 00043 pathlist=getenv("TEMP"); 00044 if (!pathlist) 00045 pathlist=getenv("TMP"); 00046 #endif 00047 if (!pathlist || !pathlist[0]) 00048 pathlist=(char*) P_tmpdir; 00049 } 00050 do 00051 { 00052 uint length; 00053 end=strcend(pathlist, DELIM); 00054 strmake(buff, pathlist, (uint) (end-pathlist)); 00055 length= cleanup_dirname(buff, buff); 00056 if (!(copy= my_strndup(buff, length, MYF(MY_WME))) || 00057 insert_dynamic(&t_arr, (gptr) ©)) 00058 DBUG_RETURN(TRUE); 00059 pathlist=end+1; 00060 } 00061 while (*end); 00062 freeze_size(&t_arr); 00063 tmpdir->list=(char **)t_arr.buffer; 00064 tmpdir->max=t_arr.elements-1; 00065 tmpdir->cur=0; 00066 DBUG_RETURN(FALSE); 00067 00068 err: 00069 delete_dynamic(&t_arr); /* Safe to free */ 00070 pthread_mutex_destroy(&tmpdir->mutex); 00071 DBUG_RETURN(TRUE); 00072 } 00073 00074 00075 char *my_tmpdir(MY_TMPDIR *tmpdir) 00076 { 00077 char *dir; 00078 if (!tmpdir->max) 00079 return tmpdir->list[0]; 00080 pthread_mutex_lock(&tmpdir->mutex); 00081 dir=tmpdir->list[tmpdir->cur]; 00082 tmpdir->cur= (tmpdir->cur == tmpdir->max) ? 0 : tmpdir->cur+1; 00083 pthread_mutex_unlock(&tmpdir->mutex); 00084 return dir; 00085 } 00086 00087 void free_tmpdir(MY_TMPDIR *tmpdir) 00088 { 00089 uint i; 00090 for (i=0; i<=tmpdir->max; i++) 00091 my_free(tmpdir->list[i], MYF(0)); 00092 my_free((gptr)tmpdir->list, MYF(0)); 00093 pthread_mutex_destroy(&tmpdir->mutex); 00094 } 00095
1.4.7

