Nut/OS  4.10.3
API Reference
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 
00045 #include <errno.h>
00046 #include <libgen.h>
00047 #include <stdlib.h>
00048 #include <string.h>
00049 #include <memdebug.h>
00050 
00051 #if !defined(MAXPATHLEN)
00052 #define MAXPATHLEN  256
00053 #endif
00054 
00071 char *basename(CONST char *path)
00072 {
00073     static char *bname = NULL;
00074     CONST char *endp, *startp;
00075 
00076     if (bname == NULL) {
00077         bname = (char *) malloc(MAXPATHLEN);
00078         if (bname == NULL)
00079             return (NULL);
00080     }
00081 
00082     /* Empty or NULL string gets treated as "." */
00083     if (path == NULL || *path == '\0') {
00084         strcpy(bname, ".");
00085         return (bname);
00086     }
00087 
00088     /* Strip trailing slashes */
00089     endp = path + strlen(path) - 1;
00090     while (endp > path && *endp == '/')
00091         endp--;
00092 
00093     /* All slashes becomes "/" */
00094     if (endp == path && *endp == '/') {
00095         strcpy(bname, "/");
00096         return (bname);
00097     }
00098 
00099     /* Find the start of the base */
00100     startp = endp;
00101     while (startp > path && *(startp - 1) != '/')
00102         startp--;
00103 
00104     if ((endp - startp) + 2 >= MAXPATHLEN) {
00105         errno = ENAMETOOLONG;
00106         return (NULL);
00107     }
00108     strcpy(bname, startp);
00109     bname[(endp - startp) + 1] = 0;
00110 
00111     return (bname);
00112 }