UNIXworkcode

/* * Copyright 2024 Olaf Wintermann * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ #include "pathutils.h" #include "nedit_malloc.h" #include <stdlib.h> #include <string.h> char* ConcatPath(const char *parent, const char *name) { size_t parentlen = strlen(parent); size_t namelen = strlen(name); size_t pathlen = parentlen + namelen + 2; char *path = NEditMalloc(pathlen); memcpy(path, parent, parentlen); if(parentlen > 0 && parent[parentlen-1] != '/') { path[parentlen] = '/'; parentlen++; } if(name[0] == '/') { name++; namelen--; } memcpy(path+parentlen, name, namelen); path[parentlen+namelen] = '\0'; return path; } char* FileName(char *path) { int si = 0; int osi = 0; int i = 0; int p = 0; char c; while((c = path[i]) != 0) { if(c == '/') { osi = si; si = i; p = 1; } i++; } char *name = path + si + p; if(name[0] == 0) { name = path + osi + p; if(name[0] == 0) { return path; } } return name; } char* ParentPath(char *path) { char *name = FileName(path); size_t namelen = strlen(name); size_t pathlen = strlen(path); size_t parentlen = pathlen - namelen; if(parentlen == 0) { parentlen++; } char *parent = NEditMalloc(parentlen + 1); memcpy(parent, path, parentlen); parent[parentlen] = '\0'; return parent; }