static char *getcwd_common(char *buf, size_t size, const char *realfnname, char *cwd) { char *sbox_path = NULL; if (*cwd != '\0') { sbox_path = scratchbox_reverse_path(realfnname, cwd, SB2_INTERFACE_CLASS_GETCWD); } if (sbox_path) { SB_LOG(SB_LOGLEVEL_DEBUG, "GETCWD: '%s'", sbox_path); if(buf) { if (strlen(sbox_path) >= size) { /* path does not fit to the buffer */ free(sbox_path); errno = ERANGE; return(NULL); } strncpy(buf, sbox_path, size); free(sbox_path); } else { /* buf==NULL: real getcwd() used malloc() to * allocate cwd (some implementations) [or the * behavior may be unspecified (posix definition)] * Assume memory was allocated, because the real * getcwd() already returned a pointer to us... */ free(cwd); cwd = sbox_path; } } SB_LOG(SB_LOGLEVEL_DEBUG, "GETCWD: returns '%s'", cwd); return cwd; }
char *sb2show__reverse_path__(const char *func_name, const char *abs_path, uint32_t classmask) { char *reversed__path = NULL; reversed__path = scratchbox_reverse_path( func_name, abs_path, classmask); SB_LOG(SB_LOGLEVEL_DEBUG, "%s '%s' => '%s'", __func__, abs_path, reversed__path ? reversed__path : "<none>"); return(reversed__path); }
char *realpath_gate( int *result_errno_ptr, char *(*real_realpath_ptr)(const char *name, char *resolved), const char *realfnname, const mapping_results_t *mapped_name, char *resolved) { char *sbox_path = NULL; char *rp; char *rpath = resolved; if (resolved == NULL) { /* The real_realpath_ptr may point to __old_realpath, which * does not allow a NULL resolved ptr. __old_realpath only * checks if resolved is NULL and fails or calls * __realpath, which allows NULL resolved and in that case * will malloc it. This way we'll just call malloc a bit * earlier. */ rpath = malloc(PATH_MAX); } errno = *result_errno_ptr; /* restore to orig.value */ if ((rp = (*real_realpath_ptr)(mapped_name->mres_result_path,rpath)) == NULL) { *result_errno_ptr = errno; return NULL; } *result_errno_ptr = errno; if (*rp != '\0') { sbox_path = scratchbox_reverse_path(realfnname, rp, SB2_INTERFACE_CLASS_REALPATH); if (sbox_path) { if (resolved) { strncpy(resolved, sbox_path, PATH_MAX); rp = resolved; free(sbox_path); } else { /* resolved was null - assume that glibc * allocated memory */ free(rp); rp = sbox_path; } } /* else not reversed, just return rp */ } SB_LOG(SB_LOGLEVEL_NOISE, "REALPATH: returns '%s'", rp); return(rp); }
char *bindtextdomain_gate(int *result_errno_ptr, char *(*realfn)(const char *, const char *), const char *realfn_name, const char *domainname, const char *dirname) { mapping_results_t res; const char *mapped_dirname = dirname; char *result = NULL; clear_mapping_results_struct(&res); if (dirname != NULL) { sbox_map_path(realfn_name, dirname, 0, &res); assert(res.mres_result_path != NULL); mapped_dirname = res.mres_result_path; SB_LOG(SB_LOGLEVEL_DEBUG, "binding domain %s to %s", domainname, mapped_dirname); } errno = *result_errno_ptr; /* restore to orig.value */ result = (*realfn)(domainname, mapped_dirname); *result_errno_ptr = errno; free_mapping_results(&res); if (result && (*result != '\0')) { char *sbox_path = NULL; sbox_path = scratchbox_reverse_path(realfn_name, result); if (sbox_path) { /* FIXME: this will leak memory. nobody will free * the buffer at *sbox_path; in fact the manual * page denies that ("..must not be modified * or freed") */ result = sbox_path; } } return (result); }
char * get_current_dir_name_gate( int *result_errno_ptr, char * (*real_get_current_dir_name_ptr)(void), const char *realfnname) { char *sbox_path = NULL; char *cwd; errno = *result_errno_ptr; /* restore to orig.value */ if ((cwd = (*real_get_current_dir_name_ptr)()) == NULL) { *result_errno_ptr = errno; return NULL; } *result_errno_ptr = errno; if (*cwd != '\0') { sbox_path = scratchbox_reverse_path(realfnname, cwd, SB2_INTERFACE_CLASS_GETCWD); } if (sbox_path) { free(cwd); return sbox_path; } return(cwd); /* failed to reverse it */ }
static char *getwd_common(char *cwd, const char *realfnname, char *buf) { char *sbox_path = NULL; if (*cwd != '\0') { sbox_path = scratchbox_reverse_path(realfnname, cwd, SB2_INTERFACE_CLASS_GETCWD); } if (sbox_path) { if(buf) { if (strlen(sbox_path) >= PATH_MAX) { free(sbox_path); return(NULL); } strcpy(buf, sbox_path); free(sbox_path); } else { /* buf==NULL: next_getwd used malloc() to allocate cwd */ free(cwd); cwd = sbox_path; } } return cwd; }