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

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