1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
/*++
/* NAME
/* sane_basename 3
/* SUMMARY
/* split pathname into last component and parent directory
/* SYNOPSIS
/* #include <stringops.h>
/*
/* char *sane_basename(buf, path)
/* VSTRING *buf;
/* const char *path;
/*
/* char *sane_dirname(buf, path)
/* VSTRING *buf;
/* const char *path;
/* DESCRIPTION
/* These functions split a pathname into its last component
/* and its parent directory, excluding any trailing "/"
/* characters from the input. The result is a pointer to "/"
/* when the input is all "/" characters, or a pointer to "."
/* when the input is a null pointer or zero-length string.
/*
/* sane_basename() and sane_dirname() differ as follows
/* from standard basename() and dirname() implementations:
/* .IP \(bu
/* They can use caller-provided storage or private storage.
/* .IP \(bu
/* They never modify their input.
/* .PP
/* sane_basename() returns a pointer to string with the last
/* pathname component.
/*
/* sane_dirname() returns a pointer to string with the parent
/* directory. The result is a pointer to "." when the input
/* contains no '/' character.
/*
/* Arguments:
/* .IP buf
/* Result storage. If a null pointer is specified, each function
/* uses its own private memory that is overwritten upon each call.
/* .IP path
/* The input pathname.
/* LICENSE
/* .ad
/* .fi
/* The Secure Mailer license must be distributed with this
/* software.
/* AUTHOR(S)
/* Wietse Venema
/* IBM T.J. Watson Research
/* P.O. Box 704
/* Yorktown Heights, NY 10598, USA
/*--*/
/* System library. */
#include <sys_defs.h>
#include <string.h>
/* Utility library. */
#include <vstring.h>
#include <stringops.h>
#define STR(x) vstring_str(x)
/* sane_basename - skip directory prefix */
char *sane_basename(VSTRING *bp, const char *path)
{
static VSTRING *buf;
const char *first;
const char *last;
/*
* Your buffer or mine?
*/
if (bp == 0) {
bp = buf;
if (bp == 0)
bp = buf = vstring_alloc(10);
}
/*
* Special case: return "." for null or zero-length input.
*/
if (path == 0 || *path == 0)
return (STR(vstring_strcpy(bp, ".")));
/*
* Remove trailing '/' characters from input. Return "/" if input is all
* '/' characters.
*/
last = path + strlen(path) - 1;
while (*last == '/') {
if (last == path)
return (STR(vstring_strcpy(bp, "/")));
last--;
}
/*
* The pathname does not end in '/'. Skip to last '/' character if any.
*/
first = last - 1;
while (first >= path && *first != '/')
first--;
return (STR(vstring_strncpy(bp, first + 1, last - first)));
}
/* sane_dirname - keep directory prefix */
char *sane_dirname(VSTRING *bp, const char *path)
{
static VSTRING *buf;
const char *last;
/*
* Your buffer or mine?
*/
if (bp == 0) {
bp = buf;
if (bp == 0)
bp = buf = vstring_alloc(10);
}
/*
* Special case: return "." for null or zero-length input.
*/
if (path == 0 || *path == 0)
return (STR(vstring_strcpy(bp, ".")));
/*
* Remove trailing '/' characters from input. Return "/" if input is all
* '/' characters.
*/
last = path + strlen(path) - 1;
while (*last == '/') {
if (last == path)
return (STR(vstring_strcpy(bp, "/")));
last--;
}
/*
* This pathname does not end in '/'. Skip to last '/' character if any.
*/
while (last >= path && *last != '/')
last--;
if (last < path) /* no '/' */
return (STR(vstring_strcpy(bp, ".")));
/*
* Strip trailing '/' characters from dirname (not strictly needed).
*/
while (last > path && *last == '/')
last--;
return (STR(vstring_strncpy(bp, path, last - path + 1)));
}
#ifdef TEST
#include <vstring_vstream.h>
int main(int argc, char **argv)
{
VSTRING *buf = vstring_alloc(10);
char *dir;
char *base;
while (vstring_get_nonl(buf, VSTREAM_IN) > 0) {
dir = sane_dirname((VSTRING *) 0, STR(buf));
base = sane_basename((VSTRING *) 0, STR(buf));
vstream_printf("input=\"%s\" dir=\"%s\" base=\"%s\"\n",
STR(buf), dir, base);
}
vstream_fflush(VSTREAM_OUT);
vstring_free(buf);
return (0);
}
#endif
|