coan 4.2.4
|
00001 /*************************************************************************** 00002 * Copyright (C) 2004, 2006 Symbian Software Ltd. * 00003 * All rights reserved. * 00004 * Copyright (C) 2007-2011 Mike Kinghan, imk@strudl.org * 00005 * All rights reserved. * 00006 * * 00007 * Redistribution and use in source and binary forms, with or without * 00008 * modification, are permitted provided that the following conditions * 00009 * are met: * 00010 * * 00011 * Redistributions of source code must retain the above copyright * 00012 * notice, this list of conditions and the following disclaimer. * 00013 * * 00014 * Redistributions in binary form must reproduce the above copyright * 00015 * notice, this list of conditions and the following disclaimer in the * 00016 * documentation and/or other materials provided with the distribution. * 00017 * * 00018 * Neither the name of Symbian Software Ltd. nor the names of its * 00019 * contributors may be used to endorse or promote products derived from * 00020 * this software without specific prior written permission. * 00021 * * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * 00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * 00029 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * 00030 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,* 00031 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * 00032 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * 00033 * DAMAGE. * 00034 * * 00035 **************************************************************************/ 00036 00037 #include "filesys.h" 00038 #include "platform.h" 00039 #include "report.h" 00040 00047 /* API */ 00048 00049 heap_str 00050 fs_compose_filename(char const *path, char const *leafname) 00051 { 00052 size_t path_len = path ? strlen(path) : 0; 00053 size_t leafname_len = strlen(leafname); 00054 heap_str filename = zallocate(path_len + sizeof(PATH_DELIM) + 00055 leafname_len + 1); 00056 if (path) { 00057 strcpy(filename,path)[path_len++] = PATH_DELIM; 00058 } 00059 strcpy(filename + path_len,leafname); 00060 return filename; 00061 } 00062 00063 fs_obj_type_t 00064 fs_file_or_dir(char const *name) 00065 { 00066 fs_obj_type_t obj_type = fs_obj_type(name); 00067 if (obj_type == FS_OBJ_NONE) { 00068 bail(GRIPE_NO_FILE,"No such file or directory as \"%s\"",name); 00069 } 00070 return obj_type; 00071 } 00072 00073 char * 00074 fs_tempname(char * template) 00075 { 00076 char *tempname = NULL; 00077 char *suffix = strstr(template,"XXXXXX"); 00078 if (suffix) { 00079 if (!suffix[6]) { 00080 unsigned lim = 0xffffff + 1; 00081 unsigned i = 0; 00082 for ( ; i < lim; ++i) { 00083 sprintf(suffix,"%06x",i); 00084 if (fs_obj_type(template) == FS_OBJ_NONE) { 00085 tempname = template; 00086 break; 00087 } 00088 } 00089 } 00090 } 00091 return tempname; 00092 } 00093 00094 fs_path_type_t 00095 fs_path_type(char const *filename) 00096 { 00097 /* This lets the style of the first path delimiter 00098 in filename determine whether we call it a 00099 unix path or a windows path, unless windows 00100 drive prefix has already settled it. 00101 */ 00102 fs_path_type_t pathtype = 0; 00103 char lastch; 00104 if (filename[0] == '/') { 00105 pathtype |= FS_ABS_PATH | FS_UNIX_PATH; 00106 if (!filename[1]) { 00107 return pathtype |= FS_ROOT_PATH | FS_DANGLING_PATH; 00108 } 00109 } else if (isalpha(filename[0]) && filename[1] == ':') { 00110 pathtype |= FS_ABS_PATH | FS_WIN_PATH; 00111 if (!filename[2]) { 00112 return pathtype |= FS_ROOT_PATH; 00113 } else if (filename[2] == '\\' && !filename[3]) { 00114 return pathtype |= FS_ROOT_PATH | FS_DANGLING_PATH; 00115 } 00116 } else if (strchr(filename,'/')) { 00117 pathtype |= FS_UNIX_PATH; 00118 } else if (strchr(filename,'\\')) { 00119 pathtype |= FS_WIN_PATH; 00120 } 00121 lastch = filename[strlen(filename) - 1]; 00122 if (lastch == '/' || lastch == '\\') { 00123 pathtype |= FS_DANGLING_PATH; 00124 } 00125 return pathtype; 00126 } 00127 00128 char const * 00129 fs_path_comp(char const *first, char const *second, size_t *sharedlen) 00130 { 00131 char const *delim = NULL; 00132 char const *lhs = first; 00133 char const *rhs = second; 00134 for ( ; *lhs && *lhs == *rhs; ++lhs,++rhs) { 00135 if (*lhs == PATH_DELIM) { 00136 delim = lhs; 00137 } 00138 } 00139 if (*lhs != *rhs) { 00140 if (!*lhs) { 00141 *sharedlen = strlen(first); 00142 return first; 00143 } 00144 if (!*rhs) { 00145 *sharedlen = strlen(second); 00146 return second; 00147 } 00148 if (delim) { 00149 *sharedlen = lhs - first; 00150 return first; 00151 } 00152 *sharedlen = 0; 00153 return NULL; 00154 } 00155 *sharedlen = 0; 00156 return first; 00157 } 00158 00159 heap_str 00160 fs_split_filename(char const *path, char **leafname) 00161 { 00162 heap_str parent = NULL; 00163 char const *child = NULL; 00164 char *delim = strrchr(path,PATH_DELIM); 00165 if (!delim) { 00166 fs_obj_type_t obj_type = fs_file_or_dir(path); 00167 if (FS_IS_FILE(obj_type)) { 00168 child = path; 00169 } else { 00170 parent = zallocate(strlen(path) + 1); 00171 strcpy(parent,path); 00172 } 00173 } else { 00174 size_t len = delim - path; 00175 parent = zallocate(len + 1); 00176 memcpy(parent,path,len); 00177 child = delim + 1; 00178 } 00179 if (leafname) { 00180 *leafname = delim; 00181 } 00182 return parent; 00183 } 00184 00185 00186 00187 /* EOF*/