1 /*
2  *      cook - file construction tool
3  *      Copyright (C) 1999, 2001, 2006, 2007 Peter Miller;
4  *      All rights reserved.
5  *
6  *      This program is free software; you can redistribute it and/or modify
7  *      it under the terms of the GNU General Public License as published by
8  *      the Free Software Foundation; either version 3 of the License, or
9  *      (at your option) any later version.
10  *
11  *      This program is distributed in the hope that it will be useful,
12  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *      GNU General Public License for more details.
15  *
16  *      You should have received a copy of the GNU General Public License
17  *      along with this program. If not, see
18  *      <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <common/ac/string.h>
22 
23 #include <cook/os_interface.h>
24 #include <common/trace.h>
25 
26 
27 /*
28  * NAME
29  *      os_dirname_relative - take path apart
30  *
31  * SYNOPSIS
32  *      string_ty *os_dirname_relative(string_ty *path);
33  *
34  * DESCRIPTION
35  *      os_dirname_relative is used to extract the directory part
36  *      of a pathname.
37  *
38  * RETURNS
39  *      pointer to dynamically allocated string.
40  *      A null pointer is returned on error.
41  *
42  * CAVEAT
43  *      Use str_free() when you are done with the value returned.
44  */
45 
46 string_ty *
os_dirname_relative(string_ty * path)47 os_dirname_relative(string_ty *path)
48 {
49     char            *cp;
50 
51     trace(("os_dirname_relative(path = %08lX)\n{\n", path));
52     trace_string(path->str_text);
53     cp = strrchr(path->str_text, '/');
54     if (cp)
55     {
56         if (cp > path->str_text)
57         {
58             path = str_n_from_c(path->str_text, cp - path->str_text);
59         }
60         else
61             path = str_from_c("/");
62     }
63     else
64         path = str_from_c(".");
65     trace_string(path->str_text);
66     trace(("return %08lX;\n", path));
67     trace(("}\n"));
68     return path;
69 }
70