int rpl_rmdir (char const *dir) { /* Work around cygwin 1.5.x bug where rmdir("dir/./") succeeds. */ size_t len = strlen (dir); int result; while (len && ISSLASH (dir[len - 1])) len--; if (len && dir[len - 1] == '.' && (1 == len || ISSLASH (dir[len - 2]))) { errno = EINVAL; return -1; } result = rmdir (dir); /* Work around mingw bug, where rmdir("file/") fails with EINVAL instead of ENOTDIR. We've already filtered out trailing ., the only reason allowed by POSIX for EINVAL. */ if (result == -1 && errno == EINVAL) errno = ENOTDIR; return result; }
/* ** void MakePathName(char ARG_PTR *pszPath, char ARG_PTR *pszFileName); ** ** Append a filename to a path string. ** ** Arguments: pszPath - path string to which pszFileName will be appended ** pszFileName - file name to append ** ** Returns: void ** ** Globals: none */ VOID MakePathName(CHAR ARG_PTR *pszPath, CHAR ARG_PTR *pszFileName) { CHAR chLastPathChar; // Make sure we have an isolated file name. pszFileName = ExtractFileName(pszFileName); // Dont append to a NULL string or a single ".". #ifdef DBCS if (*pszFileName != '\0' && ! (! IsDBCSLeadByte(pszFileName[0]) && pszFileName[0] == PERIOD && ! IsDBCSLeadByte(pszFileName[1]) && pszFileName[1] == '\0')) { CHAR ARG_PTR *psz, *pszPrevious; for (psz = pszPrevious = pszPath; *psz != '\0'; psz = AnsiNext(psz)) pszPrevious = psz; chLastPathChar = *pszPrevious; if (! IsDBCSLeadByte(chLastPathChar) && ! ISSLASH(chLastPathChar) && chLastPathChar != COLON) STRCAT(pszPath, SEP_STR); STRCAT(pszPath, pszFileName); } #else if (*pszFileName != '\0' && ! (*pszFileName == PERIOD && pszFileName[1] == '\0')) { chLastPathChar = pszPath[STRLEN(pszPath) - 1]; if (! ISSLASH(chLastPathChar) && chLastPathChar != COLON) STRCAT(pszPath, SEP_STR); STRCAT(pszPath, pszFileName); } #endif }
const char * relocate (const char *pathname) { #if defined PIC && defined INSTALLDIR static int initialized; if (!initialized) { const char *orig_installprefix = INSTALLPREFIX; const char *orig_installdir = INSTALLDIR; const char *curr_prefix_better; curr_prefix_better = compute_curr_prefix (orig_installprefix, orig_installdir, get_shared_library_fullname ()); if (curr_prefix_better == NULL) curr_prefix_better = curr_prefix; set_relocation_prefix (orig_installprefix, curr_prefix_better); initialized = 1; } #endif if (orig_prefix != NULL && curr_prefix != NULL && strncmp (pathname, orig_prefix, orig_prefix_len) == 0) { if (pathname[orig_prefix_len] == '\0') return curr_prefix; if (ISSLASH (pathname[orig_prefix_len])) { const char *pathname_tail = &pathname[orig_prefix_len]; char *result = (char *) xmalloc (curr_prefix_len + strlen (pathname_tail) + 1); #ifdef NO_XMALLOC if (result != NULL) #endif { memcpy (result, curr_prefix, curr_prefix_len); strcpy (result + curr_prefix_len, pathname_tail); return result; } } } return pathname; }
/* Replace '/' with '\0' in FILENAME if it marks a place that needs testing for the existence of directory. Return the address of the last location replaced, or 0 if none were replaced. */ static char * replace_slashes (char *filename) { char *f; char *last_location_replaced = 0; char const *component_start; for (f = filename + FILE_SYSTEM_PREFIX_LEN (filename); ISSLASH (*f); f++) /* do nothing */ ; component_start = f; for (; *f; f++) if (ISSLASH (*f)) { char *slash = f; /* Treat multiple slashes as if they were one slash. */ while (ISSLASH (f[1])) f++; /* Ignore slashes at the end of the path. */ if (! f[1]) break; /* "." and ".." need not be tested. */ if (! (slash - component_start <= 2 && component_start[0] == '.' && slash[-1] == '.')) { *slash = '\0'; last_location_replaced = slash; } component_start = f + 1; } return last_location_replaced; }
size_t dir_len (char const *file) { size_t prefix_length = FILE_SYSTEM_PREFIX_LEN (file); size_t length; /* Advance prefix_length beyond important leading slashes. */ prefix_length += (prefix_length != 0 ? (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE && ISSLASH (file[prefix_length])) : (ISSLASH (file[0]) ? ((DOUBLE_SLASH_IS_DISTINCT_ROOT && ISSLASH (file[1]) && ! ISSLASH (file[2]) ? 2 : 1)) : 0)); /* Strip the basename and any redundant slashes before it. */ for (length = last_component (file) - file; prefix_length < length; length--) if (! ISSLASH (file[length - 1])) break; return length; }
char * last_component (char const *name) { char const *base = name + FILE_SYSTEM_PREFIX_LEN (name); char const *p; bool saw_slash = false; while (ISSLASH (*base)) base++; for (p = base; *p; p++) { if (ISSLASH (*p)) saw_slash = true; else if (saw_slash) { base = p; saw_slash = false; } } return (char *) base; }
char *basename(char const *name) { char const *base = name += FILESYSTEM_PREFIX_LEN(name); int all_slashes = 1; char const *p; for (p = name; *p; p++) { if (ISSLASH(*p)) base = p + 1; else all_slashes = 0; } /* If NAME is all slashes, arrange to return `/'. */ if (*base == '\0' && ISSLASH(*name) && all_slashes) --base; /* Make sure the last byte is not a slash. */ //assert(all_slashes || !ISSLASH(*(p - 1))); return (char *)base; }
/* Remove empty ancestor directories of FILENAME. Ignore errors, since the path may contain ".."s, and when there is an EEXIST failure the system may return some other error number. */ void removedirs (char const *name) { char *filename = xstrdup (name); size_t i; for (i = strlen (filename); i != 0; i--) if (ISSLASH (filename[i]) && ! (ISSLASH (filename[i - 1]) || (filename[i - 1] == '.' && (i == 1 || ISSLASH (filename[i - 2]) || (filename[i - 2] == '.' && (i == 2 || ISSLASH (filename[i - 3]))))))) { filename[i] = '\0'; if (rmdir (filename) == 0 && verbosity == VERBOSE) say ("Removed empty directory %s\n", quotearg (filename)); filename[i] = '/'; } free (filename); }
/* DIR is the operand of a -C option; add it to vector of chdir targets, and return the index of its location. */ int chdir_arg (char const *dir) { if (wd_count == wd_alloc) { if (wd_alloc == 0) { wd_alloc = 2; wd = xmalloc (sizeof *wd * wd_alloc); } else wd = x2nrealloc (wd, &wd_alloc, sizeof *wd); if (! wd_count) { wd[wd_count].name = "."; wd[wd_count].fd = AT_FDCWD; wd_count++; } } /* Optimize the common special case of the working directory, or the working directory as a prefix. */ if (dir[0]) { while (dir[0] == '.' && ISSLASH (dir[1])) for (dir += 2; ISSLASH (*dir); dir++) continue; if (! dir[dir[0] == '.']) return wd_count - 1; } wd[wd_count].name = dir; wd[wd_count].fd = 0; return wd_count++; }
/* Strip up to STRIP_LEADING leading slashes. If STRIP_LEADING is negative, strip all leading slashes. Returns a pointer into NAME on success, and NULL otherwise. */ static bool strip_leading_slashes (char *name, int strip_leading) { int s = strip_leading; char *p, *n; for (p = n = name; *p; p++) { if (ISSLASH (*p)) { while (ISSLASH (p[1])) p++; if (strip_leading < 0 || --s >= 0) n = p+1; } } if ((strip_leading < 0 || s <= 0) && *n) { memmove (name, n, strlen (n) + 1); return true; } else return false; }
char * dir_name (char const *file) { size_t length = dir_len (file); bool append_dot = (length == 0 || (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE && length == FILE_SYSTEM_PREFIX_LEN (file) && file[2] != '\0' && ! ISSLASH (file[2]))); char *dir = xmalloc (length + append_dot + 1); memcpy (dir, file, length); if (append_dot) dir[length++] = '.'; dir[length] = '\0'; return dir; }
char *xine_private_basename(char *name) { char const *base = name + FILESYSTEM_PREFIX_LEN (name); char const *p; for (p = base; *p; p++) { if (ISSLASH (*p)) { /* Treat multiple adjacent slashes like a single slash. */ do p++; while (ISSLASH (*p)); /* If the file name ends in slash, use the trailing slash as the basename if no non-slashes have been found. */ if (! *p) { if (ISSLASH (*base)) base = p - 1; break; } /* *P is a non-slash preceded by a slash. */ base = p; } } return (char *)base; }
/* Return TRUE if the given file name denotes an UNC root. */ static BOOL is_unc_root (const char *rname) { /* Test whether it has the syntax '\\server\share'. */ if (ISSLASH (rname[0]) && ISSLASH (rname[1])) { /* It starts with two slashes. Find the next slash. */ const char *p = rname + 2; const char *q = p; while (*q != '\0' && !ISSLASH (*q)) q++; if (q > p && *q != '\0') { /* Found the next slash at q. */ q++; const char *r = q; while (*r != '\0' && !ISSLASH (*r)) r++; if (r > q && *r == '\0') return TRUE; } } return FALSE; }
/* Return nonzero if file NAME is excluded. */ bool excluded_name (char const *name, struct tar_stat_info *st) { struct exclist *ep; const char *rname = NULL; char *bname = NULL; bool result; int nr = 0; name += FILE_SYSTEM_PREFIX_LEN (name); /* Try global exclusion list first */ if (excluded_file_name (excluded, name)) return true; if (!st) return false; for (result = false; st && !result; st = st->parent, nr = EXCL_NON_RECURSIVE) { for (ep = st->exclude_list; ep; ep = ep->next) { if (ep->flags & nr) continue; if ((result = excluded_file_name (ep->excluded, name))) break; if (!rname) { rname = name; /* Skip leading ./ */ while (*rname == '.' && ISSLASH (rname[1])) rname += 2; } if ((result = excluded_file_name (ep->excluded, rname))) break; if (!bname) bname = base_name (name); if ((result = excluded_file_name (ep->excluded, bname))) break; } } free (bname); return result; }
static bool target_directory_operand (char const *file) { char const *b = last_component (file); size_t blen = strlen (b); bool looks_like_a_dir = (blen == 0 || ISSLASH (b[blen - 1])); struct stat st; int stat_result = (dereference_dest_dir_symlinks ? stat (file, &st) : lstat (file, &st)); int err = (stat_result == 0 ? 0 : errno); bool is_a_dir = !err && S_ISDIR (st.st_mode); if (err && err != ENOENT) error (EXIT_FAILURE, err, _("accessing %s"), quote (file)); if (is_a_dir < looks_like_a_dir) error (EXIT_FAILURE, err, _("target %s is not a directory"), quote (file)); return is_a_dir; }
/* Returns true if all names from the namelist were processed. P is the stat_info of the most recently processed entry. The decision is postponed until the next entry is read if: 1) P ended with a slash (i.e. it was a directory) 2) P matches any entry from the namelist *and* represents a subdirectory or a file lying under this entry (in the terms of directory structure). This is necessary to handle contents of directories. */ bool all_names_found (struct tar_stat_info *p) { struct name const *cursor; size_t len; if (!p->file_name || occurrence_option == 0 || p->had_trailing_slash) return false; len = strlen (p->file_name); for (cursor = namelist; cursor; cursor = cursor->next) { if ((cursor->name[0] && !WASFOUND (cursor)) || (len >= cursor->length && ISSLASH (p->file_name[cursor->length]))) return false; } return true; }
/* Make a directory entry for given NAME */ static struct directory * make_directory (const char *name) { size_t namelen = strlen (name); struct directory *directory = xmalloc (sizeof (*directory)); directory->next = NULL; directory->dump = directory->idump = NULL; directory->orig = NULL; directory->flags = false; if (namelen && ISSLASH (name[namelen - 1])) namelen--; directory->name = xmalloc (namelen + 1); memcpy (directory->name, name, namelen); directory->name[namelen] = 0; directory->tagfile = NULL; return directory; }
void replace_prefix (char **pname, const char *samp, size_t slen, const char *repl, size_t rlen) { char *name = *pname; size_t nlen = strlen (name); if (nlen > slen && memcmp (name, samp, slen) == 0 && ISSLASH (name[slen])) { if (rlen > slen) { name = xrealloc (name, nlen - slen + rlen + 1); *pname = name; } memmove (name + rlen, name + slen, nlen - slen + 1); memcpy (name, repl, rlen); } }
static void checkslash(char **argv) { char **t, **u; int complained; complained = 0; for (t = argv; *t;) { if (ISSLASH(*t)) { if (!complained++) warnx("\"/\" may not be removed"); eval = 1; for (u = t; u[0] != NULL; ++u) u[0] = u[1]; } else { ++t; } } }
/* Concatenate a directory filename, a relative filename and an optional suffix. The directory may end with the directory separator. The second argument may not start with the directory separator (it is relative). Return a freshly allocated filename. Return NULL and set errno upon memory allocation failure. */ char * concatenated_filename (const char *directory, const char *filename, const char *suffix) { char *result; char *p; if (strcmp (directory, ".") == 0) { /* No need to prepend the directory. */ result = (char *) malloc (strlen (filename) + (suffix != NULL ? strlen (suffix) : 0) + 1); if (result == NULL) return NULL; /* errno is set here */ p = result; } else { size_t directory_len = strlen (directory); int need_slash = (directory_len > FILE_SYSTEM_PREFIX_LEN (directory) && !ISSLASH (directory[directory_len - 1])); result = (char *) malloc (directory_len + need_slash + strlen (filename) + (suffix != NULL ? strlen (suffix) : 0) + 1); if (result == NULL) return NULL; /* errno is set here */ memcpy (result, directory, directory_len); p = result + directory_len; if (need_slash) *p++ = '/'; } p = stpcpy (p, filename); if (suffix != NULL) stpcpy (p, suffix); return result; }
char * normalize_filename (int cdidx, const char *name) { char *copy = NULL; if (IS_RELATIVE_FILE_NAME (name)) { /* Set COPY to the absolute path for this name. FIXME: There should be no need to get the absolute file name. tar_getcdpath does not return a true "canonical" path, so this following approach may lead to situations where the same file or directory is processed twice under different absolute paths without that duplication being detected. Perhaps we should use dev+ino pairs instead of names? (See listed03.at for a related test case.) */ const char *cdpath = tar_getcdpath (cdidx); size_t copylen; bool need_separator; if (!cdpath) call_arg_fatal ("getcwd", "."); copylen = strlen (cdpath); need_separator = ! (DOUBLE_SLASH_IS_DISTINCT_ROOT && copylen == 2 && ISSLASH (cdpath[1])); copy = xmalloc (copylen + need_separator + strlen (name) + 1); strcpy (copy, cdpath); copy[copylen] = DIRECTORY_SEPARATOR; strcpy (copy + copylen + need_separator, name); } if (!copy) copy = xstrdup (name); normalize_filename_x (copy); return copy; }
void create_backup (char const *to, const struct stat *to_st, bool leave_original) { /* When the input to patch modifies the same file more than once, patch only backs up the initial version of each file. To figure out which files have already been backed up, patch remembers the files that replace the original files. Files not known already are backed up; files already known have already been backed up before, and are skipped. When a patch tries to delete a file, in order to not break the above logic, we merely remember which file to delete. After the entire patch file has been read, we delete all files marked for deletion which have not been recreated in the meantime. */ if (to_st && ! (S_ISREG (to_st->st_mode) || S_ISLNK (to_st->st_mode))) fatal ("File %s is not a %s -- refusing to create backup", to, S_ISLNK (to_st->st_mode) ? "symbolic link" : "regular file"); if (to_st && lookup_file_id (to_st) == CREATED) { if (debug & 4) say ("File %s already seen\n", quotearg (to)); } else { int try_makedirs_errno = 0; char *bakname; if (origprae || origbase || origsuff) { char const *p = origprae ? origprae : ""; char const *b = origbase ? origbase : ""; char const *s = origsuff ? origsuff : ""; char const *t = to; size_t plen = strlen (p); size_t blen = strlen (b); size_t slen = strlen (s); size_t tlen = strlen (t); char const *o; size_t olen; for (o = t + tlen, olen = 0; o > t && ! ISSLASH (*(o - 1)); o--) /* do nothing */ ; olen = t + tlen - o; tlen -= olen; bakname = xmalloc (plen + tlen + blen + olen + slen + 1); memcpy (bakname, p, plen); memcpy (bakname + plen, t, tlen); memcpy (bakname + plen + tlen, b, blen); memcpy (bakname + plen + tlen + blen, o, olen); memcpy (bakname + plen + tlen + blen + olen, s, slen + 1); if ((origprae && (contains_slash (origprae + FILE_SYSTEM_PREFIX_LEN (origprae)) || contains_slash (to))) || (origbase && contains_slash (origbase))) try_makedirs_errno = ENOENT; } else { bakname = find_backup_file_name (to, backup_type); if (!bakname) xalloc_die (); } if (! to_st) { int fd; if (debug & 4) say ("Creating empty file %s\n", quotearg (bakname)); try_makedirs_errno = ENOENT; unlink (bakname); while ((fd = creat (bakname, 0666)) < 0) { if (errno != try_makedirs_errno) pfatal ("Can't create file %s", quotearg (bakname)); makedirs (bakname); try_makedirs_errno = 0; } if (close (fd) != 0) pfatal ("Can't close file %s", quotearg (bakname)); } else if (leave_original) create_backup_copy (to, bakname, to_st, try_makedirs_errno == 0); else { if (debug & 4) say ("Renaming file %s to %s\n", quotearg_n (0, to), quotearg_n (1, bakname)); while (rename (to, bakname) != 0) { if (errno == try_makedirs_errno) { makedirs (bakname); try_makedirs_errno = 0; } else if (errno == EXDEV) { create_backup_copy (to, bakname, to_st, try_makedirs_errno == 0); unlink (to); break; } else pfatal ("Can't rename file %s to %s", quotearg_n (0, to), quotearg_n (1, bakname)); } } free (bakname); } }
static #endif const char * compute_curr_prefix (const char *orig_installprefix, const char *orig_installdir, const char *curr_pathname) { const char *curr_installdir; const char *rel_installdir; if (curr_pathname == NULL) return NULL; /* Determine the relative installation directory, relative to the prefix. This is simply the difference between orig_installprefix and orig_installdir. */ if (strncmp (orig_installprefix, orig_installdir, strlen (orig_installprefix)) != 0) /* Shouldn't happen - nothing should be installed outside $(prefix). */ return NULL; rel_installdir = orig_installdir + strlen (orig_installprefix); /* Determine the current installation directory. */ { const char *p_base = curr_pathname + FILE_SYSTEM_PREFIX_LEN (curr_pathname); const char *p = curr_pathname + strlen (curr_pathname); char *q; while (p > p_base) { p--; if (ISSLASH (*p)) break; } q = (char *) xmalloc (p - curr_pathname + 1); #ifdef NO_XMALLOC if (q == NULL) return NULL; #endif memcpy (q, curr_pathname, p - curr_pathname); q[p - curr_pathname] = '\0'; curr_installdir = q; } /* Compute the current installation prefix by removing the trailing rel_installdir from it. */ { const char *rp = rel_installdir + strlen (rel_installdir); const char *cp = curr_installdir + strlen (curr_installdir); const char *cp_base = curr_installdir + FILE_SYSTEM_PREFIX_LEN (curr_installdir); while (rp > rel_installdir && cp > cp_base) { bool same = false; const char *rpi = rp; const char *cpi = cp; while (rpi > rel_installdir && cpi > cp_base) { rpi--; cpi--; if (ISSLASH (*rpi) || ISSLASH (*cpi)) { if (ISSLASH (*rpi) && ISSLASH (*cpi)) same = true; break; } #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__ /* Win32, Cygwin, OS/2, DOS - case insignificant filesystem */ if ((*rpi >= 'a' && *rpi <= 'z' ? *rpi - 'a' + 'A' : *rpi) != (*cpi >= 'a' && *cpi <= 'z' ? *cpi - 'a' + 'A' : *cpi)) break; #else if (*rpi != *cpi) break; #endif } if (!same) break; /* The last pathname component was the same. opi and cpi now point to the slash before it. */ rp = rpi; cp = cpi; } if (rp > rel_installdir) /* Unexpected: The curr_installdir does not end with rel_installdir. */ return NULL; { size_t curr_prefix_len = cp - curr_installdir; char *curr_prefix; curr_prefix = (char *) xmalloc (curr_prefix_len + 1); #ifdef NO_XMALLOC if (curr_prefix == NULL) return NULL; #endif memcpy (curr_prefix, curr_installdir, curr_prefix_len); curr_prefix[curr_prefix_len] = '\0'; return curr_prefix; } } }
/* Rename the file SRC to DST. This replacement is necessary on Windows, on which the system rename function will not replace an existing DST. */ int rpl_rename (char const *src, char const *dst) { int error; size_t src_len = strlen (src); size_t dst_len = strlen (dst); char *src_base = last_component (src); char *dst_base = last_component (dst); bool src_slash; bool dst_slash; bool dst_exists; struct stat src_st; struct stat dst_st; /* Filter out dot as last component. */ if (!src_len || !dst_len) { errno = ENOENT; return -1; } if (*src_base == '.') { size_t len = base_len (src_base); if (len == 1 || (len == 2 && src_base[1] == '.')) { errno = EINVAL; return -1; } } if (*dst_base == '.') { size_t len = base_len (dst_base); if (len == 1 || (len == 2 && dst_base[1] == '.')) { errno = EINVAL; return -1; } } /* Presence of a trailing slash requires directory semantics. If the source does not exist, or if the destination cannot be turned into a directory, give up now. Otherwise, strip trailing slashes before calling rename. There are no symlinks on mingw, so stat works instead of lstat. */ src_slash = ISSLASH (src[src_len - 1]); dst_slash = ISSLASH (dst[dst_len - 1]); if (stat (src, &src_st)) return -1; if (stat (dst, &dst_st)) { if (errno != ENOENT || (!S_ISDIR (src_st.st_mode) && dst_slash)) return -1; dst_exists = false; } else { if (S_ISDIR (dst_st.st_mode) != S_ISDIR (src_st.st_mode)) { errno = S_ISDIR (dst_st.st_mode) ? EISDIR : ENOTDIR; return -1; } dst_exists = true; } /* There are no symlinks, so if a file existed with a trailing slash, it must be a directory, and we don't have to worry about stripping strip trailing slash. However, mingw refuses to replace an existing empty directory, so we have to help it out. And canonicalize_file_name is not yet ported to mingw; however, for directories, getcwd works as a viable alternative. Ensure that we can get back to where we started before using it; later attempts to return are fatal. Note that we can end up losing a directory if rename then fails, but it was empty, so not much damage was done. */ if (dst_exists && S_ISDIR (dst_st.st_mode)) { char *cwd = getcwd (NULL, 0); char *src_temp; char *dst_temp; if (!cwd || chdir (cwd)) return -1; if (IS_ABSOLUTE_FILE_NAME (src)) { dst_temp = chdir (dst) ? NULL : getcwd (NULL, 0); src_temp = chdir (src) ? NULL : getcwd (NULL, 0); } else { src_temp = chdir (src) ? NULL : getcwd (NULL, 0); if (!IS_ABSOLUTE_FILE_NAME (dst) && chdir (cwd)) abort (); dst_temp = chdir (dst) ? NULL : getcwd (NULL, 0); } if (chdir (cwd)) abort (); free (cwd); if (!src_temp || !dst_temp) { free (src_temp); free (dst_temp); errno = ENOMEM; return -1; } src_len = strlen (src_temp); if (strncmp (src_temp, dst_temp, src_len) == 0 && (ISSLASH (dst_temp[src_len]) || dst_temp[src_len] == '\0')) { error = dst_temp[src_len]; free (src_temp); free (dst_temp); if (error) { errno = EINVAL; return -1; } return 0; } if (rmdir (dst)) { error = errno; free (src_temp); free (dst_temp); errno = error; return -1; } free (src_temp); free (dst_temp); } /* MoveFileEx works if SRC is a directory without any flags, but fails with MOVEFILE_REPLACE_EXISTING, so try without flags first. Thankfully, MoveFileEx handles hard links correctly, even though rename() does not. */ if (MoveFileEx (src, dst, 0)) return 0; /* Retry with MOVEFILE_REPLACE_EXISTING if the move failed due to the destination already existing. */ error = GetLastError (); if (error == ERROR_FILE_EXISTS || error == ERROR_ALREADY_EXISTS) { if (MoveFileEx (src, dst, MOVEFILE_REPLACE_EXISTING)) return 0; error = GetLastError (); } switch (error) { case ERROR_FILE_NOT_FOUND: case ERROR_PATH_NOT_FOUND: case ERROR_BAD_PATHNAME: case ERROR_DIRECTORY: errno = ENOENT; break; case ERROR_ACCESS_DENIED: case ERROR_SHARING_VIOLATION: errno = EACCES; break; case ERROR_OUTOFMEMORY: errno = ENOMEM; break; case ERROR_CURRENT_DIRECTORY: errno = EBUSY; break; case ERROR_NOT_SAME_DEVICE: errno = EXDEV; break; case ERROR_WRITE_PROTECT: errno = EROFS; break; case ERROR_WRITE_FAULT: case ERROR_READ_FAULT: case ERROR_GEN_FAILURE: errno = EIO; break; case ERROR_HANDLE_DISK_FULL: case ERROR_DISK_FULL: case ERROR_DISK_TOO_FRAGMENTED: errno = ENOSPC; break; case ERROR_FILE_EXISTS: case ERROR_ALREADY_EXISTS: errno = EEXIST; break; case ERROR_BUFFER_OVERFLOW: case ERROR_FILENAME_EXCED_RANGE: errno = ENAMETOOLONG; break; case ERROR_INVALID_NAME: case ERROR_DELETE_PENDING: errno = EPERM; /* ? */ break; # ifndef ERROR_FILE_TOO_LARGE /* This value is documented but not defined in all versions of windows.h. */ # define ERROR_FILE_TOO_LARGE 223 # endif case ERROR_FILE_TOO_LARGE: errno = EFBIG; break; default: errno = EINVAL; break; } return -1; }
/* Return a pointer to the contents of the charset.alias file. */ static const char * get_charset_aliases (void) { const char *cp; cp = charset_aliases; if (cp == NULL) { #if !(defined DARWIN7 || defined VMS || defined WINDOWS_NATIVE || defined __CYGWIN__) const char *dir; const char *base = "charset.alias"; char *file_name; /* Make it possible to override the charset.alias location. This is necessary for running the testsuite before "make install". */ dir = getenv ("CHARSETALIASDIR"); if (dir == NULL || dir[0] == '\0') dir = relocate (LIBDIR); /* Concatenate dir and base into freshly allocated file_name. */ { size_t dir_len = strlen (dir); size_t base_len = strlen (base); int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1])); file_name = (char *) malloc (dir_len + add_slash + base_len + 1); if (file_name != NULL) { memcpy (file_name, dir, dir_len); if (add_slash) file_name[dir_len] = DIRECTORY_SEPARATOR; memcpy (file_name + dir_len + add_slash, base, base_len + 1); } } if (file_name == NULL) /* Out of memory. Treat the file as empty. */ cp = ""; else { int fd; /* Open the file. Reject symbolic links on platforms that support O_NOFOLLOW. This is a security feature. Without it, an attacker could retrieve parts of the contents (namely, the tail of the first line that starts with "* ") of an arbitrary file by placing a symbolic link to that file under the name "charset.alias" in some writable directory and defining the environment variable CHARSETALIASDIR to point to that directory. */ fd = open (file_name, O_RDONLY | (HAVE_WORKING_O_NOFOLLOW ? O_NOFOLLOW : 0)); if (fd < 0) /* File not found. Treat it as empty. */ cp = ""; else { FILE *fp; fp = fdopen (fd, "r"); if (fp == NULL) { /* Out of memory. Treat the file as empty. */ close (fd); cp = ""; } else { /* Parse the file's contents. */ char *res_ptr = NULL; size_t res_size = 0; for (;;) { int c; char buf1[50+1]; char buf2[50+1]; size_t l1, l2; char *old_res_ptr; c = getc (fp); if (c == EOF) break; if (c == '\n' || c == ' ' || c == '\t') continue; if (c == '#') { /* Skip comment, to end of line. */ do c = getc (fp); while (!(c == EOF || c == '\n')); if (c == EOF) break; continue; } ungetc (c, fp); if (fscanf (fp, "%50s %50s", buf1, buf2) < 2) break; l1 = strlen (buf1); l2 = strlen (buf2); old_res_ptr = res_ptr; if (res_size == 0) { res_size = l1 + 1 + l2 + 1; res_ptr = (char *) malloc (res_size + 1); } else { res_size += l1 + 1 + l2 + 1; res_ptr = (char *) realloc (res_ptr, res_size + 1); } if (res_ptr == NULL) { /* Out of memory. */ res_size = 0; free (old_res_ptr); break; } strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1); strcpy (res_ptr + res_size - (l2 + 1), buf2); } fclose (fp); if (res_size == 0) cp = ""; else { *(res_ptr + res_size) = '\0'; cp = res_ptr; } } } free (file_name); } #else # if defined DARWIN7 /* To avoid the trouble of installing a file that is shared by many GNU packages -- many packaging systems have problems with this --, simply inline the aliases here. */ cp = "ISO8859-1" "\0" "ISO-8859-1" "\0" "ISO8859-2" "\0" "ISO-8859-2" "\0" "ISO8859-4" "\0" "ISO-8859-4" "\0" "ISO8859-5" "\0" "ISO-8859-5" "\0" "ISO8859-7" "\0" "ISO-8859-7" "\0" "ISO8859-9" "\0" "ISO-8859-9" "\0" "ISO8859-13" "\0" "ISO-8859-13" "\0" "ISO8859-15" "\0" "ISO-8859-15" "\0" "KOI8-R" "\0" "KOI8-R" "\0" "KOI8-U" "\0" "KOI8-U" "\0" "CP866" "\0" "CP866" "\0" "CP949" "\0" "CP949" "\0" "CP1131" "\0" "CP1131" "\0" "CP1251" "\0" "CP1251" "\0" "eucCN" "\0" "GB2312" "\0" "GB2312" "\0" "GB2312" "\0" "eucJP" "\0" "EUC-JP" "\0" "eucKR" "\0" "EUC-KR" "\0" "Big5" "\0" "BIG5" "\0" "Big5HKSCS" "\0" "BIG5-HKSCS" "\0" "GBK" "\0" "GBK" "\0" "GB18030" "\0" "GB18030" "\0" "SJIS" "\0" "SHIFT_JIS" "\0" "ARMSCII-8" "\0" "ARMSCII-8" "\0" "PT154" "\0" "PT154" "\0" /*"ISCII-DEV" "\0" "?" "\0"*/ "*" "\0" "UTF-8" "\0"; # endif # if defined VMS /* To avoid the troubles of an extra file charset.alias_vms in the sources of many GNU packages, simply inline the aliases here. */ /* The list of encodings is taken from the OpenVMS 7.3-1 documentation "Compaq C Run-Time Library Reference Manual for OpenVMS systems" section 10.7 "Handling Different Character Sets". */ cp = "ISO8859-1" "\0" "ISO-8859-1" "\0" "ISO8859-2" "\0" "ISO-8859-2" "\0" "ISO8859-5" "\0" "ISO-8859-5" "\0" "ISO8859-7" "\0" "ISO-8859-7" "\0" "ISO8859-8" "\0" "ISO-8859-8" "\0" "ISO8859-9" "\0" "ISO-8859-9" "\0" /* Japanese */ "eucJP" "\0" "EUC-JP" "\0" "SJIS" "\0" "SHIFT_JIS" "\0" "DECKANJI" "\0" "DEC-KANJI" "\0" "SDECKANJI" "\0" "EUC-JP" "\0" /* Chinese */ "eucTW" "\0" "EUC-TW" "\0" "DECHANYU" "\0" "DEC-HANYU" "\0" "DECHANZI" "\0" "GB2312" "\0" /* Korean */ "DECKOREAN" "\0" "EUC-KR" "\0"; # endif # if defined WINDOWS_NATIVE || defined __CYGWIN__ /* To avoid the troubles of installing a separate file in the same directory as the DLL and of retrieving the DLL's directory at runtime, simply inline the aliases here. */ cp = "CP936" "\0" "GBK" "\0" "CP1361" "\0" "JOHAB" "\0" "CP20127" "\0" "ASCII" "\0" "CP20866" "\0" "KOI8-R" "\0" "CP20936" "\0" "GB2312" "\0" "CP21866" "\0" "KOI8-RU" "\0" "CP28591" "\0" "ISO-8859-1" "\0" "CP28592" "\0" "ISO-8859-2" "\0" "CP28593" "\0" "ISO-8859-3" "\0" "CP28594" "\0" "ISO-8859-4" "\0" "CP28595" "\0" "ISO-8859-5" "\0" "CP28596" "\0" "ISO-8859-6" "\0" "CP28597" "\0" "ISO-8859-7" "\0" "CP28598" "\0" "ISO-8859-8" "\0" "CP28599" "\0" "ISO-8859-9" "\0" "CP28605" "\0" "ISO-8859-15" "\0" "CP38598" "\0" "ISO-8859-8" "\0" "CP51932" "\0" "EUC-JP" "\0" "CP51936" "\0" "GB2312" "\0" "CP51949" "\0" "EUC-KR" "\0" "CP51950" "\0" "EUC-TW" "\0" "CP54936" "\0" "GB18030" "\0" "CP65001" "\0" "UTF-8" "\0"; # endif #endif charset_aliases = cp; } return cp; }
/* Return a pointer to the contents of the charset.alias file. */ static const char * get_charset_aliases () { char *cp; cp = charset_aliases; if (cp == NULL) { #ifndef WIN32 FILE *fp; const char *dir = LIBDIR; const char *base = "charset.alias"; char *file_name; /* Concatenate dir and base into freshly allocated file_name. */ { size_t dir_len = strlen (dir); size_t base_len = strlen (base); int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1])); file_name = (char *) malloc (dir_len + add_slash + base_len + 1); if (file_name != NULL) { memcpy (file_name, dir, dir_len); if (add_slash) file_name[dir_len] = DIRECTORY_SEPARATOR; memcpy (file_name + dir_len + add_slash, base, base_len + 1); } } if (file_name == NULL || (fp = fopen (file_name, "r")) == NULL) /* Out of memory or file not found, treat it as empty. */ cp = ""; else { /* Parse the file's contents. */ int c; char buf1[50+1]; char buf2[50+1]; char *res_ptr = NULL; size_t res_size = 0; size_t l1, l2; for (;;) { c = getc (fp); if (c == EOF) break; if (c == '\n' || c == ' ' || c == '\t') continue; if (c == '#') { /* Skip comment, to end of line. */ do c = getc (fp); while (!(c == EOF || c == '\n')); if (c == EOF) break; continue; } ungetc (c, fp); if (fscanf(fp, "%50s %50s", buf1, buf2) < 2) break; l1 = strlen (buf1); l2 = strlen (buf2); if (res_size == 0) { res_size = l1 + 1 + l2 + 1; res_ptr = malloc (res_size + 1); } else { res_size += l1 + 1 + l2 + 1; res_ptr = realloc (res_ptr, res_size + 1); } if (res_ptr == NULL) { /* Out of memory. */ res_size = 0; break; } strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1); strcpy (res_ptr + res_size - (l2 + 1), buf2); } fclose (fp); if (res_size == 0) cp = ""; else { *(res_ptr + res_size) = '\0'; cp = res_ptr; } } if (file_name != NULL) free (file_name); #else /* WIN32 */ /* To avoid the troubles of installing a separate file in the same directory as the DLL and of retrieving the DLL's directory at runtime, simply inline the aliases here. */ cp = "CP936" "\0" "GBK" "\0" "CP1361" "\0" "JOHAB" "\0"; #endif charset_aliases = cp; } return cp; }
/* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is non-null and exists, uses it; otherwise uses the first of $TMPDIR, P_tmpdir, /tmp that exists. Copies into TMPL a template suitable for use with mk[s]temp. Will fail (-1) if DIR is non-null and doesn't exist, none of the searched dirs exists, or there's not enough space in TMPL. */ int path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx, bool try_tmpdir) { const char *d; size_t dlen, plen; bool add_slash; if (!pfx || !pfx[0]) { pfx = "file"; plen = 4; } else { plen = strlen (pfx); if (plen > 5) plen = 5; } if (try_tmpdir) { d = __libc_secure_getenv ("TMPDIR"); if (d != NULL && direxists (d)) dir = d; else if (dir != NULL && direxists (dir)) /* nothing */ ; else dir = NULL; } if (dir == NULL) { #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ char dirbuf[PATH_MAX]; DWORD retval; /* Find Windows temporary file directory. We try this before P_tmpdir because Windows defines P_tmpdir to "\\" and will therefore try to put all temporary files in the root directory (unless $TMPDIR is set). */ retval = GetTempPath (PATH_MAX, dirbuf); if (retval > 0 && retval < PATH_MAX && direxists (dirbuf)) dir = dirbuf; else #endif if (direxists (P_tmpdir)) dir = P_tmpdir; else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp")) dir = "/tmp"; else { __set_errno (ENOENT); return -1; } } dlen = strlen (dir); #ifdef __VMS add_slash = 0; #else add_slash = dlen != 0 && !ISSLASH (dir[dlen - 1]); #endif /* check we have room for "${dir}/${pfx}XXXXXX\0" */ if (tmpl_len < dlen + add_slash + plen + 6 + 1) { __set_errno (EINVAL); return -1; } memcpy (tmpl, dir, dlen); sprintf (tmpl + dlen, &"/%.*sXXXXXX"[!add_slash], (int) plen, pfx); return 0; }
int rpl_stat (char const *name, struct stat *st) { int result = orig_stat (name, st); #if REPLACE_FUNC_STAT_FILE /* Solaris 9 mistakenly succeeds when given a non-directory with a trailing slash. */ if (result == 0 && !S_ISDIR (st->st_mode)) { size_t len = strlen (name); if (ISSLASH (name[len - 1])) { errno = ENOTDIR; return -1; } } #endif /* REPLACE_FUNC_STAT_FILE */ #if REPLACE_FUNC_STAT_DIR /* The only known systems where REPLACE_FUNC_STAT_DIR is needed also have a constant PATH_MAX. */ # ifndef PATH_MAX # error "Please port this replacement to your platform" # endif if (result == -1 && errno == ENOENT) { /* Due to mingw's oddities, there are some directories (like c:\) where stat() only succeeds with a trailing slash, and other directories (like c:\windows) where stat() only succeeds without a trailing slash. But we want the two to be synonymous, since chdir() manages either style. Likewise, Mingw also reports ENOENT for names longer than PATH_MAX, when we want ENAMETOOLONG, and for stat("file/"), when we want ENOTDIR. Fortunately, mingw PATH_MAX is small enough for stack allocation. */ char fixed_name[PATH_MAX + 1] = {0}; size_t len = strlen (name); bool check_dir = false; verify (PATH_MAX <= 4096); if (PATH_MAX <= len) errno = ENAMETOOLONG; else if (len) { strcpy (fixed_name, name); if (ISSLASH (fixed_name[len - 1])) { check_dir = true; while (len && ISSLASH (fixed_name[len - 1])) fixed_name[--len] = '\0'; if (!len) fixed_name[0] = '/'; } else fixed_name[len++] = '/'; result = orig_stat (fixed_name, st); if (result == 0 && check_dir && !S_ISDIR (st->st_mode)) { result = -1; errno = ENOTDIR; } } } #endif /* REPLACE_FUNC_STAT_DIR */ return result; }
/* Returns the pathname, relocated according to the current installation directory. */ const char * relocate (const char *pathname) { #if defined PIC && defined INSTALLDIR static int initialized; /* Initialization code for a shared library. */ if (!initialized) { /* At this point, orig_prefix and curr_prefix likely have already been set through the main program's set_program_name_and_installdir function. This is sufficient in the case that the library has initially been installed in the same orig_prefix. But we can do better, to also cover the cases that 1. it has been installed in a different prefix before being moved to orig_prefix and (later) to curr_prefix, 2. unlike the program, it has not moved away from orig_prefix. */ const char *orig_installprefix = INSTALLPREFIX; const char *orig_installdir = INSTALLDIR; const char *curr_prefix_better; curr_prefix_better = compute_curr_prefix (orig_installprefix, orig_installdir, get_shared_library_fullname ()); if (curr_prefix_better == NULL) curr_prefix_better = curr_prefix; set_relocation_prefix (orig_installprefix, curr_prefix_better); initialized = 1; } #endif /* Note: It is not necessary to perform case insensitive comparison here, even for DOS-like filesystems, because the pathname argument was typically created from the same Makefile variable as orig_prefix came from. */ if (orig_prefix != NULL && curr_prefix != NULL && strncmp (pathname, orig_prefix, orig_prefix_len) == 0) { if (pathname[orig_prefix_len] == '\0') /* pathname equals orig_prefix. */ return curr_prefix; if (ISSLASH (pathname[orig_prefix_len])) { /* pathname starts with orig_prefix. */ const char *pathname_tail = &pathname[orig_prefix_len]; char *result = (char *) xmalloc (curr_prefix_len + strlen (pathname_tail) + 1); #ifdef NO_XMALLOC if (result != NULL) #endif { memcpy (result, curr_prefix, curr_prefix_len); strcpy (result + curr_prefix_len, pathname_tail); return result; } } } /* Nothing to relocate. */ return pathname; }
char * __realpath (const char *name, char *resolved) { char *rpath, *dest, *extra_buf = NULL; const char *start, *end, *rpath_limit; long int path_max; int num_links = 0; size_t prefix_len; if (name == NULL) { /* As per Single Unix Specification V2 we must return an error if either parameter is a null pointer. We extend this to allow the RESOLVED parameter to be NULL in case the we are expected to allocate the room for the return value. */ __set_errno (EINVAL); return NULL; } if (name[0] == '\0') { /* As per Single Unix Specification V2 we must return an error if the name argument points to an empty string. */ __set_errno (ENOENT); return NULL; } #ifdef PATH_MAX path_max = PATH_MAX; #else path_max = pathconf (name, _PC_PATH_MAX); if (path_max <= 0) path_max = 8192; #endif if (resolved == NULL) { rpath = malloc (path_max); if (rpath == NULL) { /* It's easier to set errno to ENOMEM than to rely on the 'malloc-posix' gnulib module. */ errno = ENOMEM; return NULL; } } else rpath = resolved; rpath_limit = rpath + path_max; /* This is always zero for Posix hosts, but can be 2 for MS-Windows and MS-DOS X:/foo/bar file names. */ prefix_len = FILE_SYSTEM_PREFIX_LEN (name); if (!IS_ABSOLUTE_FILE_NAME (name)) { if (!__getcwd (rpath, path_max)) { rpath[0] = '\0'; goto error; } dest = strchr (rpath, '\0'); start = name; prefix_len = FILE_SYSTEM_PREFIX_LEN (rpath); } else { dest = rpath; if (prefix_len) { memcpy (rpath, name, prefix_len); dest += prefix_len; } *dest++ = '/'; if (DOUBLE_SLASH_IS_DISTINCT_ROOT) { if (ISSLASH (name[1]) && !ISSLASH (name[2]) && !prefix_len) *dest++ = '/'; *dest = '\0'; } start = name + prefix_len; } for (end = start; *start; start = end) { #ifdef _LIBC struct stat64 st; #else struct stat st; #endif int n; /* Skip sequence of multiple path-separators. */ while (ISSLASH (*start)) ++start; /* Find end of path component. */ for (end = start; *end && !ISSLASH (*end); ++end) /* Nothing. */; if (end - start == 0) break; else if (end - start == 1 && start[0] == '.') /* nothing */; else if (end - start == 2 && start[0] == '.' && start[1] == '.') { /* Back up to previous component, ignore if at root already. */ if (dest > rpath + prefix_len + 1) for (--dest; dest > rpath && !ISSLASH (dest[-1]); --dest) continue; if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 && !prefix_len && ISSLASH (*dest) && !ISSLASH (dest[1])) dest++; } else { size_t new_size; if (!ISSLASH (dest[-1])) *dest++ = '/'; if (dest + (end - start) >= rpath_limit) { ptrdiff_t dest_offset = dest - rpath; char *new_rpath; if (resolved) { __set_errno (ENAMETOOLONG); if (dest > rpath + prefix_len + 1) dest--; *dest = '\0'; goto error; } new_size = rpath_limit - rpath; if (end - start + 1 > path_max) new_size += end - start + 1; else new_size += path_max; new_rpath = (char *) realloc (rpath, new_size); if (new_rpath == NULL) { /* It's easier to set errno to ENOMEM than to rely on the 'realloc-posix' gnulib module. */ errno = ENOMEM; goto error; } rpath = new_rpath; rpath_limit = rpath + new_size; dest = rpath + dest_offset; } #ifdef _LIBC dest = __mempcpy (dest, start, end - start); #else memcpy (dest, start, end - start); dest += end - start; #endif *dest = '\0'; #ifdef _LIBC if (__lxstat64 (_STAT_VER, rpath, &st) < 0) #else if (lstat (rpath, &st) < 0) #endif goto error; if (S_ISLNK (st.st_mode)) { char *buf; size_t len; if (++num_links > MAXSYMLINKS) { __set_errno (ELOOP); goto error; } buf = malloca (path_max); if (!buf) { errno = ENOMEM; goto error; } n = __readlink (rpath, buf, path_max - 1); if (n < 0) { int saved_errno = errno; freea (buf); errno = saved_errno; goto error; } buf[n] = '\0'; if (!extra_buf) { extra_buf = malloca (path_max); if (!extra_buf) { freea (buf); errno = ENOMEM; goto error; } } len = strlen (end); if ((long int) (n + len) >= path_max) { freea (buf); __set_errno (ENAMETOOLONG); goto error; } /* Careful here, end may be a pointer into extra_buf... */ memmove (&extra_buf[n], end, len + 1); name = end = memcpy (extra_buf, buf, n); if (IS_ABSOLUTE_FILE_NAME (buf)) { size_t pfxlen = FILE_SYSTEM_PREFIX_LEN (buf); if (pfxlen) memcpy (rpath, buf, pfxlen); dest = rpath + pfxlen; *dest++ = '/'; /* It's an absolute symlink */ if (DOUBLE_SLASH_IS_DISTINCT_ROOT) { if (ISSLASH (buf[1]) && !ISSLASH (buf[2]) && !pfxlen) *dest++ = '/'; *dest = '\0'; } /* Install the new prefix to be in effect hereafter. */ prefix_len = pfxlen; } else { /* Back up to previous component, ignore if at root already: */ if (dest > rpath + prefix_len + 1) for (--dest; dest > rpath && !ISSLASH (dest[-1]); --dest) continue; if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 && ISSLASH (*dest) && !ISSLASH (dest[1]) && !prefix_len) dest++; } } else if (!S_ISDIR (st.st_mode) && *end != '\0') { __set_errno (ENOTDIR); goto error; } } } if (dest > rpath + prefix_len + 1 && ISSLASH (dest[-1])) --dest; if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 && !prefix_len && ISSLASH (*dest) && !ISSLASH (dest[1])) dest++; *dest = '\0'; if (extra_buf) freea (extra_buf); return rpath; error: { int saved_errno = errno; if (extra_buf) freea (extra_buf); if (resolved == NULL) free (rpath); errno = saved_errno; } return NULL; }