libexplain  1.4.D001
libexplain/dirname.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2008-2010, 2013 Peter Miller
00004  * Written by Peter Miller <pmiller@opensource.org.au>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 3 of the License, or (at
00009  * your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <libexplain/ac/limits.h> /* for PATH_MAX on Solaris */
00021 #include <libexplain/ac/string.h>
00022 #include <libexplain/ac/sys/param.h> /* for PATH_MAX except Solaris */
00023 #include <libexplain/ac/unistd.h>
00024 
00025 #include <libexplain/dirname.h>
00026 
00027 
00028 void
00029 explain_dirname(char *dir, const char *path, size_t dir_size)
00030 {
00031     const char      *slash;
00032     size_t          len;
00033     char            current_directory[PATH_MAX + 1];
00034 
00035     if (dir_size == 0)
00036         return;
00037     --dir_size;
00038 
00039     if (0 == strcmp(path, "/"))
00040     {
00041         explain_strendcpy(dir, "/", dir + dir_size);
00042         return;
00043     }
00044     if (0 == strcmp(path, "."))
00045     {
00046         if (!getcwd(current_directory, sizeof(current_directory)))
00047         {
00048             explain_strendcpy(dir, "..", dir + dir_size);
00049             return;
00050         }
00051         path = current_directory;
00052     }
00053 
00054     slash = strrchr(path, '/');
00055     if (!slash)
00056     {
00057         path = "./";
00058         slash = path + 1;
00059     }
00060     if (slash == path)
00061     {
00062         explain_strendcpy(dir, "/", dir + dir_size);
00063         return;
00064     }
00065     len = slash - path;
00066     if (len > dir_size)
00067         len = dir_size;
00068     memcpy(dir, path, len);
00069     dir[len] = '\0';
00070 }
00071 
00072 
00073 void
00074 explain_basename(char *filename, const char *path, size_t filename_size)
00075 {
00076     char *          filename_end;
00077     const char      *slash;
00078 
00079     filename_end = filename + filename_size;
00080     slash = strrchr(path, '/');
00081     if (slash)
00082     {
00083         ++slash;
00084         if (*slash)
00085             explain_strendcpy(filename, slash, filename_end);
00086         else
00087             explain_strendcpy(filename, ".", filename_end);
00088     }
00089     else
00090     {
00091         explain_strendcpy(filename, path, filename_end);
00092     }
00093 }
00094 
00095 
00096 /* vim: set ts=8 sw=4 et : */