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