summaryrefslogtreecommitdiff
path: root/source3/lib/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'source3/lib/util.c')
-rw-r--r--source3/lib/util.c199
1 files changed, 20 insertions, 179 deletions
diff --git a/source3/lib/util.c b/source3/lib/util.c
index ad5dbcf106..757abc8175 100644
--- a/source3/lib/util.c
+++ b/source3/lib/util.c
@@ -100,30 +100,6 @@ char *tmpdir(void)
return "/tmp";
}
-
-
-/****************************************************************************
-prompte a dptr (to make it recently used)
-****************************************************************************/
-static void array_promote(char *array,int elsize,int element)
-{
- char *p;
- if (element == 0)
- return;
-
- p = (char *)malloc(elsize);
-
- if (!p)
- {
- DEBUG(5,("Ahh! Can't malloc\n"));
- return;
- }
- memcpy(p,array + element * elsize, elsize);
- memmove(array + elsize,array,elsize*element);
- memcpy(array,p,elsize);
- free(p);
-}
-
/****************************************************************************
determine whether we are in the specified group
****************************************************************************/
@@ -189,7 +165,7 @@ char *Atoic(char *p, int *n, char *c)
return p;
}
-int* add_num_to_list(uint32 **num, int *count, int val)
+uint32 *add_num_to_list(uint32 **num, int *count, int val)
{
(*num) = Realloc((*num), ((*count)+1) * sizeof(uint32));
if ((*num) == NULL)
@@ -201,6 +177,7 @@ int* add_num_to_list(uint32 **num, int *count, int val)
return (*num);
}
+
/*************************************************************************
reads a list of numbers
*************************************************************************/
@@ -383,7 +360,7 @@ BOOL file_exist(char *fname,SMB_STRUCT_STAT *sbuf)
SMB_STRUCT_STAT st;
if (!sbuf) sbuf = &st;
- if (dos_stat(fname,sbuf) != 0)
+ if (sys_stat(fname,sbuf) != 0)
return(False);
return(S_ISREG(sbuf->st_mode));
@@ -396,7 +373,7 @@ time_t file_modtime(char *fname)
{
SMB_STRUCT_STAT st;
- if (dos_stat(fname,&st) != 0)
+ if (sys_stat(fname,&st) != 0)
return(0);
return(st.st_mtime);
@@ -412,7 +389,7 @@ BOOL directory_exist(char *dname,SMB_STRUCT_STAT *st)
if (!st) st = &st2;
- if (dos_stat(dname,st) != 0)
+ if (sys_stat(dname,st) != 0)
return(False);
ret = S_ISDIR(st->st_mode);
@@ -428,7 +405,8 @@ SMB_OFF_T file_size(char *file_name)
{
SMB_STRUCT_STAT buf;
buf.st_size = 0;
- dos_stat(file_name,&buf);
+ if(sys_stat(file_name,&buf) != 0)
+ return (SMB_OFF_T)-1;
return(buf.st_size);
}
@@ -451,8 +429,6 @@ char *attrib_string(uint16 mode)
return(attrstr);
}
-
-
/****************************************************************************
make a file into unix format
****************************************************************************/
@@ -667,141 +643,6 @@ void unix_clean_name(char *s)
trim_string(s,NULL,"/..");
}
-
-/*******************************************************************
-a wrapper for the normal chdir() function
-********************************************************************/
-int ChDir(char *path)
-{
- int res;
- static pstring LastDir="";
-
- if (strcsequal(path,".")) return(0);
-
- if (*path == '/' && strcsequal(LastDir,path)) return(0);
- DEBUG(3,("chdir to %s\n",path));
- res = dos_chdir(path);
- if (!res)
- pstrcpy(LastDir,path);
- return(res);
-}
-
-/* number of list structures for a caching GetWd function. */
-#define MAX_GETWDCACHE (50)
-
-struct
-{
- SMB_DEV_T dev; /* These *must* be compatible with the types returned in a stat() call. */
- SMB_INO_T inode; /* These *must* be compatible with the types returned in a stat() call. */
- char *text; /* The pathname in DOS format. */
- BOOL valid;
-} ino_list[MAX_GETWDCACHE];
-
-BOOL use_getwd_cache=True;
-
-/*******************************************************************
- return the absolute current directory path - given a UNIX pathname.
- Note that this path is returned in DOS format, not UNIX
- format.
-********************************************************************/
-char *GetWd(char *str)
-{
- pstring s;
- static BOOL getwd_cache_init = False;
- SMB_STRUCT_STAT st, st2;
- int i;
-
- *s = 0;
-
- if (!use_getwd_cache)
- return(dos_getwd(str));
-
- /* init the cache */
- if (!getwd_cache_init)
- {
- getwd_cache_init = True;
- for (i=0;i<MAX_GETWDCACHE;i++)
- {
- string_init(&ino_list[i].text,"");
- ino_list[i].valid = False;
- }
- }
-
- /* Get the inode of the current directory, if this doesn't work we're
- in trouble :-) */
-
- if (dos_stat(".",&st) == -1)
- {
- DEBUG(0,("Very strange, couldn't stat \".\"\n"));
- return(dos_getwd(str));
- }
-
-
- for (i=0; i<MAX_GETWDCACHE; i++)
- if (ino_list[i].valid)
- {
-
- /* If we have found an entry with a matching inode and dev number
- then find the inode number for the directory in the cached string.
- If this agrees with that returned by the stat for the current
- directory then all is o.k. (but make sure it is a directory all
- the same...) */
-
- if (st.st_ino == ino_list[i].inode &&
- st.st_dev == ino_list[i].dev)
- {
- if (dos_stat(ino_list[i].text,&st2) == 0)
- {
- if (st.st_ino == st2.st_ino &&
- st.st_dev == st2.st_dev &&
- (st2.st_mode & S_IFMT) == S_IFDIR)
- {
- pstrcpy (str, ino_list[i].text);
-
- /* promote it for future use */
- array_promote((char *)&ino_list[0],sizeof(ino_list[0]),i);
- return (str);
- }
- else
- {
- /* If the inode is different then something's changed,
- scrub the entry and start from scratch. */
- ino_list[i].valid = False;
- }
- }
- }
- }
-
-
- /* We don't have the information to hand so rely on traditional methods.
- The very slow getcwd, which spawns a process on some systems, or the
- not quite so bad getwd. */
-
- if (!dos_getwd(s))
- {
- DEBUG(0,("Getwd failed, errno %s\n",strerror(errno)));
- return (NULL);
- }
-
- pstrcpy(str,s);
-
- DEBUG(5,("GetWd %s, inode %d, dev %x\n",s,(int)st.st_ino,(int)st.st_dev));
-
- /* add it to the cache */
- i = MAX_GETWDCACHE - 1;
- string_set(&ino_list[i].text,s);
- ino_list[i].dev = st.st_dev;
- ino_list[i].inode = st.st_ino;
- ino_list[i].valid = True;
-
- /* put it at the top of the list */
- array_promote((char *)&ino_list[0],sizeof(ino_list[0]),i);
-
- return (str);
-}
-
-
-
/*******************************************************************
reduce a file name, removing .. elements and checking that
it is below dir in the heirachy. This uses GetWd() and so must be run
@@ -809,6 +650,7 @@ on the system that has the referenced file system.
widelinks are allowed if widelinks is true
********************************************************************/
+
BOOL reduce_name(char *s,char *dir,BOOL widelinks)
{
#ifndef REDUCE_PATHS
@@ -850,22 +692,22 @@ BOOL reduce_name(char *s,char *dir,BOOL widelinks)
if (!p)
return(True);
- if (!GetWd(wd))
+ if (!dos_GetWd(wd))
{
DEBUG(0,("couldn't getwd for %s %s\n",s,dir));
return(False);
}
- if (ChDir(dir) != 0)
+ if (dos_ChDir(dir) != 0)
{
DEBUG(0,("couldn't chdir to %s\n",dir));
return(False);
}
- if (!GetWd(dir2))
+ if (!dos_GetWd(dir2))
{
DEBUG(0,("couldn't getwd for %s\n",dir));
- ChDir(wd);
+ dos_ChDir(wd);
return(False);
}
@@ -879,16 +721,16 @@ BOOL reduce_name(char *s,char *dir,BOOL widelinks)
*p = '/';
}
- if (ChDir(base_name) != 0)
+ if (dos_ChDir(base_name) != 0)
{
- ChDir(wd);
+ dos_ChDir(wd);
DEBUG(3,("couldn't chdir for %s %s basename=%s\n",s,dir,base_name));
return(False);
}
- if (!GetWd(newname))
+ if (!dos_GetWd(newname))
{
- ChDir(wd);
+ dos_ChDir(wd);
DEBUG(2,("couldn't get wd for %s %s\n",s,dir2));
return(False);
}
@@ -900,13 +742,13 @@ BOOL reduce_name(char *s,char *dir,BOOL widelinks)
}
{
- int l = strlen(dir2);
+ size_t l = strlen(dir2);
if (dir2[l-1] == '/')
l--;
if (strncmp(newname,dir2,l) != 0)
{
- ChDir(wd);
+ dos_ChDir(wd);
DEBUG(2,("Bad access attempt? s=%s dir=%s newname=%s l=%d\n",s,dir2,newname,l));
return(False);
}
@@ -922,7 +764,7 @@ BOOL reduce_name(char *s,char *dir,BOOL widelinks)
pstrcpy(s,newname);
}
- ChDir(wd);
+ dos_ChDir(wd);
if (strlen(s) == 0)
pstrcpy(s,"./");
@@ -2536,7 +2378,7 @@ void smb_panic(char *why)
/*******************************************************************
a readdir wrapper which just returns the file name
********************************************************************/
-char *readdirname(void *p)
+char *readdirname(DIR *p)
{
struct dirent *ptr;
char *dname;
@@ -2560,7 +2402,6 @@ char *readdirname(void *p)
{
static pstring buf;
memcpy(buf, dname, NAMLEN(ptr)+1);
- unix_to_dos(buf, True);
dname = buf;
}