| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2008 The Android Open Source Project | 
|  | 3 | * All rights reserved. | 
|  | 4 | * | 
|  | 5 | * Redistribution and use in source and binary forms, with or without | 
|  | 6 | * modification, are permitted provided that the following conditions | 
|  | 7 | * are met: | 
|  | 8 | *  * Redistributions of source code must retain the above copyright | 
|  | 9 | *    notice, this list of conditions and the following disclaimer. | 
|  | 10 | *  * Redistributions in binary form must reproduce the above copyright | 
|  | 11 | *    notice, this list of conditions and the following disclaimer in | 
|  | 12 | *    the documentation and/or other materials provided with the | 
|  | 13 | *    distribution. | 
|  | 14 | * | 
|  | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|  | 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|  | 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
|  | 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
|  | 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|  | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
|  | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
|  | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
|  | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
|  | 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
|  | 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|  | 26 | * SUCH DAMAGE. | 
|  | 27 | */ | 
|  | 28 | #include <libgen.h> | 
|  | 29 | #include <errno.h> | 
|  | 30 | #include <string.h> | 
|  | 31 | #include <sys/param.h> | 
|  | 32 |  | 
|  | 33 | int | 
|  | 34 | dirname_r(const char*  path, char*  buffer, size_t  bufflen) | 
|  | 35 | { | 
|  | 36 | const char *endp; | 
|  | 37 | int         result, len; | 
|  | 38 |  | 
|  | 39 | /* Empty or NULL string gets treated as "." */ | 
|  | 40 | if (path == NULL || *path == '\0') { | 
|  | 41 | path = "."; | 
|  | 42 | len  = 1; | 
|  | 43 | goto Exit; | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | /* Strip trailing slashes */ | 
|  | 47 | endp = path + strlen(path) - 1; | 
|  | 48 | while (endp > path && *endp == '/') | 
|  | 49 | endp--; | 
|  | 50 |  | 
|  | 51 | /* Find the start of the dir */ | 
|  | 52 | while (endp > path && *endp != '/') | 
|  | 53 | endp--; | 
|  | 54 |  | 
|  | 55 | /* Either the dir is "/" or there are no slashes */ | 
|  | 56 | if (endp == path) { | 
|  | 57 | path = (*endp == '/') ? "/" : "."; | 
|  | 58 | len  = 1; | 
|  | 59 | goto Exit; | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | do { | 
|  | 63 | endp--; | 
|  | 64 | } while (endp > path && *endp == '/'); | 
|  | 65 |  | 
|  | 66 | len = endp - path +1; | 
|  | 67 |  | 
|  | 68 | Exit: | 
|  | 69 | result = len; | 
|  | 70 | if (len+1 > MAXPATHLEN) { | 
|  | 71 | errno = ENAMETOOLONG; | 
|  | 72 | return -1; | 
|  | 73 | } | 
|  | 74 | if (buffer == NULL) | 
|  | 75 | return result; | 
|  | 76 |  | 
|  | 77 | if (len > (int)bufflen-1) { | 
|  | 78 | len    = (int)bufflen-1; | 
|  | 79 | result = -1; | 
|  | 80 | errno  = ERANGE; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | if (len >= 0) { | 
|  | 84 | memcpy( buffer, path, len ); | 
|  | 85 | buffer[len] = 0; | 
|  | 86 | } | 
|  | 87 | return result; | 
|  | 88 | } |