diff --git a/src/ggfile.c b/src/ggfile.c index 766c833..31b5f10 100644 --- a/src/ggfile.c +++ b/src/ggfile.c @@ -441,11 +441,13 @@ build_directory_entries(char *pathname, char **error_msg) if (errno) { - *error_msg = xstrcpy(NULL, pathname, "/", dirst->file_name, ": ", strerror(errno), NULL); + *error_msg = xstrcpy(NULL, "'", clean_path(pathname), + dirst->file_name, "' ", strerror(errno), NULL); } else { - *error_msg = xstrcpy(NULL, pathname, "/: is empty", NULL); + *error_msg = xstrcpy(NULL, "'", clean_path(pathname), + "' is empty", NULL); } close_dir (dirst); @@ -457,15 +459,14 @@ build_directory_entries(char *pathname, char **error_msg) /* The parent represents the original directory */ parent = memt_alloc (NULL, sizeof (DIRECTORY_ENTRY_ITEM)); - curpath = strip_file_name (pathname); - parent-> path = xstrcpy (NULL, curpath, "/", NULL); - parent-> name = memt_strdup (NULL, strip_file_path (pathname)); - parent-> dirst = NULL; - parent-> links = 0; - parent-> parent = NULL; - parent-> first_child = NULL; - parent-> sibling = NULL; - parent-> exists = TRUE; + parent-> path = xstrcpy (NULL, pathname, "/", NULL); + parent-> name = memt_strdup (NULL, pathname); + parent-> dirst = NULL; + parent-> links = 0; + parent-> parent = NULL; + parent-> first_child = NULL; + parent-> sibling = NULL; + parent-> exists = TRUE; /* build a list of the directory children now so any file access problems show up here and not during an iteration. */ @@ -480,14 +481,14 @@ build_directory_entries(char *pathname, char **error_msg) #endif { directory = memt_alloc (NULL, sizeof (DIRECTORY_ENTRY_ITEM)); - directory-> path = xstrcpy (NULL, dirst-> dir_name, "/", NULL); - directory-> name = memt_strdup (NULL, dirst-> file_name); - directory-> dirst = NULL; - directory-> links = 0; - directory-> parent = parent; - directory-> first_child = NULL; - directory-> sibling = NULL; - directory-> exists = TRUE; + directory-> path = xstrcpy (NULL, dirst-> dir_name, "/", NULL); + directory-> name = memt_strdup (NULL, dirst-> file_name); + directory-> dirst = NULL; + directory-> links = 0; + directory-> parent = parent; + directory-> first_child = NULL; + directory-> sibling = NULL; + directory-> exists = TRUE; last_value = link_directory_entry (parent, last_value, & directory_entry_class, directory); @@ -507,7 +508,7 @@ build_directory_entries(char *pathname, char **error_msg) file-> dirst = NULL; file-> links = 0; file-> parent = parent; - file-> sibling = NULL; + file-> sibling = NULL; file-> handle = NULL; file-> error_msg = NULL; @@ -658,7 +659,7 @@ open_the_file (FILE_ENTRY_ITEM *file, char mode, file-> path, "/", file-> name, NULL); errno = 0; - file-> handle = file_open (filename, mode); + file-> handle = file_open (clean_path(filename), mode); mem_free (filename); return store_file_error (file, gsl_thread, error, @@ -1264,9 +1265,10 @@ directory_open (int argc, RESULT_NODE **argv, void *item, RESULT_NODE *result, T *context = item; char *curpath, - *pathname, + *fullpath, *lastchar, - *error_msg; + *error_msg, + *relative; DIRECTORY_ENTRY_ITEM *directory; @@ -1275,25 +1277,33 @@ directory_open (int argc, RESULT_NODE **argv, void *item, RESULT_NODE *result, T rc; ASSERT (context); + + relative = path ? string_value (&path-> value) : "."; + + // normalize relative path +#ifdef GATES_FILESYSTEM + strconvch (relative, PATHEND, '/'); +#endif + // make absolute path, with trailing slash curpath = get_curdir (); - pathname = locate_path (curpath, - path ? string_value (&path-> value) : "."); + fullpath = locate_path (curpath, relative); mem_free (curpath); - lastchar = pathname + strlen (pathname) - 1; + + // strip trailing slash + lastchar = fullpath + strlen (fullpath) - 1; if (*lastchar == '/') *lastchar = 0; - curpath = strip_file_name (pathname); error_msg = NULL; - directory = build_directory_entries (pathname, &error_msg); + directory = build_directory_entries (fullpath, &error_msg); if (directory) { assign_pointer (& result-> value, & directory_entry_class, directory); } - mem_free (pathname); + mem_free (fullpath); if (error_msg) { diff --git a/src/sfldir.c b/src/sfldir.c index b57736a..0e1577a 100644 --- a/src/sfldir.c +++ b/src/sfldir.c @@ -96,7 +96,7 @@ open_dir ( ASSERT (dir != NULL); if (! dir) - return FALSE; + return FALSE; memset (dir, 0, sizeof (DIRST)); @@ -105,18 +105,10 @@ open_dir ( if (dir_name == NULL || *dir_name == 0) strcpy (dir_spec, DEFAULT_DIR); else - strcpy (dir_spec, dir_name); + strcpy(dir_spec, clean_path(dir_name)); -#if (defined (GATES_FILESYSTEM)) - strconvch (dir_spec, '/', '\\'); -#endif - /* Remove a trailing slash from the directory name */ + /* Save the unmodified directory name for cleanup later */ dir_spec_end = dir_spec + strlen (dir_spec); - if (dir_spec_end [-1] == PATHEND) - { - dir_spec_end [-1] = '\0'; - dir_spec_end--; - } /* Open directory stream or find first directory entry */ #if (defined (__UNIX__) || defined (__VMS_XOPEN) || defined (__OS2__)) @@ -151,8 +143,15 @@ open_dir ( #if (defined (__MSDOS__) || defined (__OS2__)) *dir_spec_end = '\0'; /* Kill the \*.* again */ #endif + dir-> dir_name = dir_spec; /* Now owned by DIRST structure */ + // normalize the path and filename +#ifdef GATES_FILESYSTEM + strconvch (dir-> dir_name, PATHEND, '/'); + strconvch (dir-> file_name, PATHEND, '/'); +#endif + #if (defined (__UNIX__) || defined (__VMS_XOPEN) || defined (__OS2__)) /* Under UNIX & VMS we still need to fetch the first file entry */ return (read_dir (dir)); @@ -210,6 +209,11 @@ populate_entry (DIRST *dir) dir-> file_name = dir-> _dir_entry.ff_name; #endif + // normalize the filename +#ifdef GATES_FILESYSTEM + strconvch (dir-> file_name, PATHEND, '/'); +#endif + #if (defined (__UNIX__) || defined (GATES_FILESYSTEM)) /* If the filename is . or .., skip this entry and do the next. We */ /* use a little bit of a recursive call to make this code simple. */ @@ -1436,10 +1440,9 @@ remove_dir ( if (!file_is_directory (path)) return (-1); - copy_path = mem_strdup (path); + copy_path = mem_strdup (clean_path (path)); if (copy_path) { - strconvch (copy_path, '/', '\\'); # if (defined (WIN32)) if (RemoveDirectoryA (copy_path)) rc = 0; diff --git a/src/sflfile.c b/src/sflfile.c index 96d3ea2..1321f40 100644 --- a/src/sflfile.c +++ b/src/sflfile.c @@ -198,7 +198,7 @@ system_devicename (const char *supplied_filename) CloseHandle(fh); } else - is_devicefile = FALSE; /* Doesn't exist */ + is_devicefile = FALSE; /* Doesn't exist */ return (is_devicefile); #else @@ -1680,11 +1680,11 @@ char /* ---------------------------------------------------------------------[<]- Function: strip_file_name - Synopsis: Returns the path for a fully-qualified filename. The path is - cleaned-up and resolved. The returned string is held in a static area - that should be copied directly after calling this function. The returned - path does not end in '/' unless that is the entire path. If the supplied - name contains no path, the returned path is ".". + Synopsis: Returns the path for a fully-qualified filename. The returned + string is held in a static area that should be copied directly after + calling this function. The returned path does not end in '/' unless + that is the entire path. If the supplied name contains no path, the + returned path is ".". ---------------------------------------------------------------------[>]-*/ char @@ -1697,18 +1697,16 @@ char ASSERT (strlen (name) <= LINE_MAX); strcpy (work_name, name); - path_end = strrchr (work_name, PATHEND); + path_end = strrchr (work_name, PATHEND); /* Find end of path, if any */ #if (defined (GATES_FILESYSTEM)) if (path_end == NULL) path_end = strrchr (work_name, '/'); #endif if (path_end == NULL) return ("."); - else - { - path_end [1] = '\0'; - return (clean_path (work_name)); - } + + path_end [1] = '\0'; + return work_name; }