Changeset 808 for trunk/ithildin/lib/util.c
- Timestamp:
- 06/05/07 10:46:00 (5 years ago)
- File:
-
- 1 edited
-
trunk/ithildin/lib/util.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ithildin/lib/util.c
r787 r808 41 41 char *canonize_make_human(int size, char type); 42 42 43 /* mmap a file into a single string */ 43 /* memory map a file into a single string (using malloc, though, not 44 * mmap...) */ 44 45 char *mmap_file(char *file) { 45 46 struct stat sb; … … 63 64 64 65 fp = fopen(file, "r"); 65 if (fp ==NULL) {66 if (fp == NULL) { 66 67 free(data); 67 68 return NULL; … … 127 128 } 128 129 130 #define PATH_SEPARATOR '/' 131 /* A function to determine whether a filename given is an absolute path or 132 * not. For unix systems this is a trivial check for a '/', on win32 it 133 * requires that we examine the path for drive letter/etc */ 134 bool is_absolute_path(const char *fname) { 135 if (*fname == PATH_SEPARATOR) 136 return true; 137 return false; 138 } 139 140 /* get the base/dirname of a file (portably). these are also provided by 141 * some operating systems, and we can happily interoperate with those 142 * implementations. */ 143 char bdname_buf[PATH_MAX]; 144 char *basename(const char *fname) { 145 char *s; 146 147 if (strlen(fname) >= PATH_MAX) { 148 errno = EINVAL; 149 return NULL; 150 } 151 152 /* chew off any trailing slashes */ 153 strlcpy(bdname_buf, fname, PATH_MAX); 154 s = bdname_buf + strlen(fname) - 1; 155 while (*s == PATH_SEPARATOR && s >= bdname_buf) 156 *s-- = '\0'; 157 158 if ((s = strrchr(bdname_buf, PATH_SEPARATOR)) == NULL) 159 /* no path separator, easy.. */ 160 return bdname_buf; 161 else { 162 /* just move the end of the string to the beginning with memcpy */ 163 s++; 164 memcpy(bdname_buf, s, strlen(s) + 1); 165 return bdname_buf; 166 } 167 } 168 169 char *dirname(const char *fname) { 170 char *s; 171 172 if (strlen(fname) >= PATH_MAX) { 173 errno = EINVAL; 174 return NULL; 175 } 176 177 /* chew off any trailing slashes */ 178 strlcpy(bdname_buf, fname, PATH_MAX); 179 s = bdname_buf + strlen(fname) - 1; 180 while (*s == PATH_SEPARATOR && s >= bdname_buf) 181 *s-- = '\0'; 182 183 if ((s = strrchr(bdname_buf, PATH_SEPARATOR)) == NULL) { 184 strcpy(bdname_buf, "."); 185 return bdname_buf; 186 } else { 187 while (*s == PATH_SEPARATOR && s >= bdname_buf) 188 *s-- = '\0'; 189 if (*bdname_buf == '\0') 190 strcpy(bdname_buf, "."); 191 return bdname_buf; 192 } 193 } 194 129 195 struct timeval *subtract_timeval(struct timeval tv1, struct timeval tv2) { 130 196 static struct timeval result;
Note: See TracChangeset
for help on using the changeset viewer.
