char *path_prefix_root(const char *root, const char *path) { char *n, *p; size_t l; /* If root is passed, prefixes path with it. Otherwise returns * it as is. */ /* First, drop duplicate prefixing slashes from the path */ while (path[0] == '/' && path[1] == '/') path++; if (!root || !root[0] || (path_compare(root, "/") == 0) || (path_compare(root, path) == 0)) return strdup(path); l = strlen(root) + 1 + strlen(path) + 1; n = malloc(l); if (!n) return NULL; p = stpcpy(n, root); while (p > n && p[-1] == '/') p--; if (path[0] != '/') *(p++) = '/'; strcpy(p, path); return n; }
bool file_specification::make_relative(const file_specification& rootspec) { if (relative()) return true; DEBUG_ASSERT(rootspec.absolute()); // now compare elements of the root with elements of this to find the common path // if the drives are different, no conversion can take place, else clear the drive if (!path_compare(drive(), rootspec.drive())) return true; set_drive(""); // first remove leading elements that are identical to the corresponding element in root unsigned i = 0; while(subpath_size() > 0 && i < rootspec.subpath_size() && path_compare(subpath_element(0), rootspec.subpath_element(i))) { subpath_erase(0); i++; } // now add a .. prefix for every element in root that is different from this while (i < rootspec.subpath_size()) { m_path.insert(m_path.begin(), ".."); i++; } set_relative(); return true; }
static int mount_path_compare(const void *a, const void *b) { const MountEntry *p = a, *q = b; int d; /* If the paths are not equal, then order prefixes first */ d = path_compare(mount_entry_path(p), mount_entry_path(q)); if (d != 0) return d; /* If the paths are equal, check the mode */ if (p->mode < q->mode) return -1; if (p->mode > q->mode) return 1; return 0; }
/** * Match mountpoint * @param file Path to file * @param parent If FS of parent directory of file is needed * @return FS List item */ static struct fslist_item *mp_match(char *file,int parent) { size_t i,curcmp; size_t maxcmp = 0; struct fslist_item *fs; struct fslist_item *maxfs = NULL; path_t *path = path_parse(file); if (parent) path_parent(path); for (i=0;(fs = llist_get(fslist,i));i++) { curcmp = path_compare(path,fs->mountpoint); if (curcmp>maxcmp) { maxcmp = curcmp; maxfs = fs; } } if (maxfs!=NULL) memmove(file,file+strlen(maxfs->mountpoint_str),strlen(file)-strlen(maxfs->mountpoint_str)+1); if (file[0]==0) strcpy(file,"/"); return maxfs; }
static int group_compare(const void*a, const void *b) { const Group *x = *(Group**)a, *y = *(Group**)b; if (arg_order != ORDER_TASKS || arg_recursive) { /* Let's make sure that the parent is always before * the child. Except when ordering by tasks and * recursive summing is off, since that is actually * not accumulative for all children. */ if (path_startswith(y->path, x->path)) return -1; if (path_startswith(x->path, y->path)) return 1; } switch (arg_order) { case ORDER_PATH: break; case ORDER_CPU: if (arg_cpu_type == CPU_PERCENT) { if (x->cpu_valid && y->cpu_valid) { if (x->cpu_fraction > y->cpu_fraction) return -1; else if (x->cpu_fraction < y->cpu_fraction) return 1; } else if (x->cpu_valid) return -1; else if (y->cpu_valid) return 1; } else { if (x->cpu_usage > y->cpu_usage) return -1; else if (x->cpu_usage < y->cpu_usage) return 1; } break; case ORDER_TASKS: if (x->n_tasks_valid && y->n_tasks_valid) { if (x->n_tasks > y->n_tasks) return -1; else if (x->n_tasks < y->n_tasks) return 1; } else if (x->n_tasks_valid) return -1; else if (y->n_tasks_valid) return 1; break; case ORDER_MEMORY: if (x->memory_valid && y->memory_valid) { if (x->memory > y->memory) return -1; else if (x->memory < y->memory) return 1; } else if (x->memory_valid) return -1; else if (y->memory_valid) return 1; break; case ORDER_IO: if (x->io_valid && y->io_valid) { if (x->io_input_bps + x->io_output_bps > y->io_input_bps + y->io_output_bps) return -1; else if (x->io_input_bps + x->io_output_bps < y->io_input_bps + y->io_output_bps) return 1; } else if (x->io_valid) return -1; else if (y->io_valid) return 1; } return path_compare(x->path, y->path); }
IFile* CPackageFileSystem::Open(const char* filename, int mode) { //FILE* debug_file = fopen("c:\\windows\\desktop\\sphere.txt", "a+"); //if (debug_file) fprintf (debug_file, "filename: '%s'\n", filename); /* for (unsigned int i = 0; i < m_directory.size(); i++) { if (path_compare(filename, m_directory[i].name.c_str())) { if (debug_file) fprintf (debug_file, "found filename: '%s'\n", filename); } } */ // first of all, look in package if (mode == IFileSystem::read) { // however, loading from filesystem preempts the package if (m_LoadFromFileSystem) { FILE* file = fopen(filename, "rb"); if (file != NULL) { return new CPackageFile(file); } } // now look in package for (unsigned int i = 0; i < m_directory.size(); i++) { if (path_compare(filename, m_directory[i].name.c_str())) { return new CPackageFile( m_file, m_directory[i].file_offset, m_directory[i].file_size, m_directory[i].compressed_size ); } } } // make fopen()-style mode string char md[4] = { 0, 0, 0, 0 }; char* p = md; if (mode & IFileSystem::read) { *p++ = 'r'; } if (mode & IFileSystem::write) { *p++ = 'w'; } *p++ = 'b'; // otherwise, open from filesystem FILE* file = fopen(filename, md); if (file == NULL) { return NULL; } return new CPackageFile(file); }
int path_compare_func(const void *a, const void *b) { return path_compare(a, b); }