basename.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. The name of the author may not be used to endorse or promote products
00014  *    derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
00017  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00018  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
00019  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00020  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00021  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00022  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00023  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00024  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00025  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00042 #include <errno.h>
00043 #include <libgen.h>
00044 #include <stdlib.h>
00045 #include <string.h>
00046 
00047 #if !defined(MAXPATHLEN)
00048 #define MAXPATHLEN  256
00049 #endif
00050 
00067 char *basename(CONST char *path)
00068 {
00069     static char *bname = NULL;
00070     CONST char *endp, *startp;
00071 
00072     if (bname == NULL) {
00073         bname = (char *) malloc(MAXPATHLEN);
00074         if (bname == NULL)
00075             return (NULL);
00076     }
00077 
00078     /* Empty or NULL string gets treated as "." */
00079     if (path == NULL || *path == '\0') {
00080         strcpy(bname, ".");
00081         return (bname);
00082     }
00083 
00084     /* Strip trailing slashes */
00085     endp = path + strlen(path) - 1;
00086     while (endp > path && *endp == '/')
00087         endp--;
00088 
00089     /* All slashes becomes "/" */
00090     if (endp == path && *endp == '/') {
00091         strcpy(bname, "/");
00092         return (bname);
00093     }
00094 
00095     /* Find the start of the base */
00096     startp = endp;
00097     while (startp > path && *(startp - 1) != '/')
00098         startp--;
00099 
00100     if ((endp - startp) + 2 >= MAXPATHLEN) {
00101         errno = ENAMETOOLONG;
00102         return (NULL);
00103     }
00104     strcpy(bname, startp);
00105     bname[(endp - startp) + 1] = 0;
00106 
00107     return (bname);
00108 }

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/