|
|||
Prex Home / Browse Source - Prex Version: 0.9.0 |
|||
root/usr/lib/libc/gen/dirname.c/* [<][>][^][v][top][bottom][index][help] */DEFINITIONSThis source file includes following definitions.1 /* 2 * Copyright (c) 1997, 2004 Todd C. Miller <Todd.Miller@courtesan.com> 3 * 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #include <errno.h> 18 #include <libgen.h> 19 #include <string.h> 20 #include <sys/param.h> 21 22 char * 23 dirname(const char *path) 24 { 25 static char dname[MAXPATHLEN]; 26 size_t len; 27 const char *endp; 28 29 /* Empty or NULL string gets treated as "." */ 30 if (path == NULL || *path == '\0') { 31 dname[0] = '.'; 32 dname[1] = '\0'; 33 return (dname); 34 } 35 36 /* Strip any trailing slashes */ 37 endp = path + strlen(path) - 1; 38 while (endp > path && *endp == '/') 39 endp--; 40 41 /* Find the start of the dir */ 42 while (endp > path && *endp != '/') 43 endp--; 44 45 /* Either the dir is "/" or there are no slashes */ 46 if (endp == path) { 47 dname[0] = *endp == '/' ? '/' : '.'; 48 dname[1] = '\0'; 49 return (dname); 50 } else { 51 /* Move forward past the separating slashes */ 52 do { 53 endp--; 54 } while (endp > path && *endp == '/'); 55 } 56 57 len = endp - path + 1; 58 if (len >= sizeof(dname)) { 59 errno = ENAMETOOLONG; 60 return (NULL); 61 } 62 memcpy(dname, path, len); 63 dname[len] = '\0'; 64 return (dname); 65 } /* [<][>][^][v][top][bottom][index][help] */ | |||
Copyright© 2005-2009 Kohsuke Ohtani |