Nut/OS  4.10.3
API Reference
dirname.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 
00028 #include <errno.h>
00029 #include <libgen.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include <memdebug.h>
00033 
00034 #if !defined(MAXPATHLEN)
00035 #define MAXPATHLEN  256
00036 #endif
00037 
00070 char *dirname(CONST char *path)
00071 {
00072     static char *bname;
00073     CONST char *endp;
00074 
00075     if (bname == NULL) {
00076         bname = (char *) malloc(MAXPATHLEN);
00077         if (bname == NULL)
00078             return (NULL);
00079     }
00080 
00081     /* Empty or NULL string gets treated as "." */
00082     if (path == NULL || *path == '\0') {
00083         strncpy(bname, ".", MAXPATHLEN);
00084         return (bname);
00085     }
00086 
00087     /* Strip trailing slashes */
00088     endp = path + strlen(path) - 1;
00089     while (endp > path && *endp == '/')
00090         endp--;
00091 
00092     /* Find the start of the dir */
00093     while (endp > path && *endp != '/')
00094         endp--;
00095 
00096     /* Either the dir is "/" or there are no slashes */
00097     if (endp == path) {
00098         strncpy(bname, *endp == '/' ? "/" : ".", MAXPATHLEN);
00099         return (bname);
00100     }
00101 
00102     do {
00103         endp--;
00104     } while (endp > path && *endp == '/');
00105 
00106     if (endp - path + 2 > MAXPATHLEN) {
00107         errno = ENAMETOOLONG;
00108         return (NULL);
00109     }
00110     strcpy(bname, path);
00111     bname[(endp - path) + 1] = 0;
00112 
00113     return (bname);
00114 }