1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright (c) 1996, by Sun Microsystems, Inc.
24 * All rights reserved.
25 */
26
27 /*
28 * MKS Library -- basename -- produce base name of a file name
29 * NOTE: not standard SVID routine.
30 *
31 * Copyright 1985, 1995 by Mortice Kern Systems Inc. All rights reserved.
32 *
33 * james Partanen, June '95
34 *
35 */
36 #ifdef M_RCSID
37 #ifndef lint
38 static char rcsID[] = "$Header: /rd/src/libc/gen/rcs/base.c 1.26 1995/08/02 17:32:36 rodney Exp $";
39 #endif
40 #endif
41
42 #include <string.h>
43
44 #define DIRNAME 0
45 #define BASENAME 1
46
47 #define M_FSDELIM(c) ((c)=='/')
48 #define M_DRDELIM(c) (0)
49
50 static char curdir[] = ".";
51
52 /*f
53 * Since the goal is essentially common: find the last occurring
54 * M_FSDELIM, and since most of the degenerate cases end up with the
55 * same result, let's do most of the work in one function.
56 * - check degenerate arg forms
57 * - deal with a possible drive specifier
58 * - deal with degenerate paths
59 * - find last non-trailing M_FSDELIM
60 * - deal with degenerate dirname
61 * - deal with general case, returning prefix or suffix based on type
62 */
63 static char *
basedir(char * arg,int type)64 basedir(char *arg, int type)
65 {
66 register char *cp, *path;
67
68 if (arg==(char *)0 || *arg=='\0' ||
69 (*arg=='.' && (arg[1]=='\0' ||
70 (type==DIRNAME && arg[1]=='.' && arg[2]=='\0'))))
71
72 return curdir; /* arg NULL, empty, ".", or ".." in DIRNAME */
73
74 if (M_DRDELIM(arg[1])) /* drive-specified pathnames */
75 path = arg+2;
76 else
77 path = arg;
78
79 if (path[1]=='\0'&&M_FSDELIM(*path)) /* "/", or drive analog */
80 return arg;
81
82 cp = strchr(path, '\0');
83 cp--;
84
85 while (cp != path && M_FSDELIM(*cp))
86 *(cp--) = '\0';
87
88 for (;cp>path && !M_FSDELIM(*cp); cp--)
89 ;
90
91 if (!M_FSDELIM(*cp))
92 if (type==DIRNAME && path!=arg) {
93 *path = '\0';
94 return arg; /* curdir on the specified drive */
95 } else
96 return (type==DIRNAME)?curdir:path;
97 else if (cp == path && type == DIRNAME) {
98 cp[1] = '\0';
99 return arg; /* root directory involved */
100 } else if (cp == path && cp[1] == '\0')
101 return(arg);
102 else if (type == BASENAME)
103 return ++cp;
104 *cp = '\0';
105 return arg;
106 }
107
108 /*f
109 * Finds the dirname of the given file. Spec1170 conformant.
110 */
111 char *
dirname(char * arg)112 dirname(char *arg)
113 {
114 return(basedir(arg, DIRNAME));
115 }
116
117 /*f
118 * Finds the basename of the given file. Spec1170 conformant.
119 */
120 char *
basename(char * arg)121 basename(char *arg)
122 {
123 return(basedir(arg, BASENAME));
124 }
125
126 #ifdef TEST_MAIN_BASE_C
127 #include <stdio.h>
128
main(int argc,char ** argv)129 int main(int argc, char **argv)
130 {
131 int cnt;
132 char arg[128];
133 char *tmp;
134
135 if (argc>1) {
136 for (cnt=argc--;argc;argc--) {
137 tmp = strdup(argv[cnt-argc]);
138 printf("%s\t%s\n",
139 dirname(argv[cnt-argc]),
140 basename(tmp));
141 free(tmp);
142 }
143 return 0;
144 }
145
146 printf("enter pathnames less than 128 chars. enter 'q' to quit.\n");
147
148 while(gets(arg))
149 if (!strcmp(arg, "q"))
150 break;
151 else {
152 tmp = strdup(arg);
153 printf("%s\t%s\n", dirname(arg), basename(tmp));
154 free(tmp);
155 }
156
157 return 0;
158 }
159 #endif /* TEST_MAIN_BASE_C */
160