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 #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * MKS Library -- basename -- produce base name of a file name 31*7c478bd9Sstevel@tonic-gate * NOTE: not standard SVID routine. 32*7c478bd9Sstevel@tonic-gate * 33*7c478bd9Sstevel@tonic-gate * Copyright 1985, 1995 by Mortice Kern Systems Inc. All rights reserved. 34*7c478bd9Sstevel@tonic-gate * 35*7c478bd9Sstevel@tonic-gate * james Partanen, June '95 36*7c478bd9Sstevel@tonic-gate * 37*7c478bd9Sstevel@tonic-gate */ 38*7c478bd9Sstevel@tonic-gate #ifdef M_RCSID 39*7c478bd9Sstevel@tonic-gate #ifndef lint 40*7c478bd9Sstevel@tonic-gate static char rcsID[] = "$Header: /rd/src/libc/gen/rcs/base.c 1.26 1995/08/02 17:32:36 rodney Exp $"; 41*7c478bd9Sstevel@tonic-gate #endif 42*7c478bd9Sstevel@tonic-gate #endif 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate #include <string.h> 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate #define DIRNAME 0 47*7c478bd9Sstevel@tonic-gate #define BASENAME 1 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate #define M_FSDELIM(c) ((c)=='/') 50*7c478bd9Sstevel@tonic-gate #define M_DRDELIM(c) (0) 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate static char curdir[] = "."; 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate /*f 55*7c478bd9Sstevel@tonic-gate * Since the goal is essentially common: find the last occurring 56*7c478bd9Sstevel@tonic-gate * M_FSDELIM, and since most of the degenerate cases end up with the 57*7c478bd9Sstevel@tonic-gate * same result, let's do most of the work in one function. 58*7c478bd9Sstevel@tonic-gate * - check degenerate arg forms 59*7c478bd9Sstevel@tonic-gate * - deal with a possible drive specifier 60*7c478bd9Sstevel@tonic-gate * - deal with degenerate paths 61*7c478bd9Sstevel@tonic-gate * - find last non-trailing M_FSDELIM 62*7c478bd9Sstevel@tonic-gate * - deal with degenerate dirname 63*7c478bd9Sstevel@tonic-gate * - deal with general case, returning prefix or suffix based on type 64*7c478bd9Sstevel@tonic-gate */ 65*7c478bd9Sstevel@tonic-gate static char * 66*7c478bd9Sstevel@tonic-gate basedir(char *arg, int type) 67*7c478bd9Sstevel@tonic-gate { 68*7c478bd9Sstevel@tonic-gate register char *cp, *path; 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate if (arg==(char *)0 || *arg=='\0' || 71*7c478bd9Sstevel@tonic-gate (*arg=='.' && (arg[1]=='\0' || 72*7c478bd9Sstevel@tonic-gate (type==DIRNAME && arg[1]=='.' && arg[2]=='\0')))) 73*7c478bd9Sstevel@tonic-gate 74*7c478bd9Sstevel@tonic-gate return curdir; /* arg NULL, empty, ".", or ".." in DIRNAME */ 75*7c478bd9Sstevel@tonic-gate 76*7c478bd9Sstevel@tonic-gate if (M_DRDELIM(arg[1])) /* drive-specified pathnames */ 77*7c478bd9Sstevel@tonic-gate path = arg+2; 78*7c478bd9Sstevel@tonic-gate else 79*7c478bd9Sstevel@tonic-gate path = arg; 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate if (path[1]=='\0'&&M_FSDELIM(*path)) /* "/", or drive analog */ 82*7c478bd9Sstevel@tonic-gate return arg; 83*7c478bd9Sstevel@tonic-gate 84*7c478bd9Sstevel@tonic-gate cp = strchr(path, '\0'); 85*7c478bd9Sstevel@tonic-gate cp--; 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate while (cp != path && M_FSDELIM(*cp)) 88*7c478bd9Sstevel@tonic-gate *(cp--) = '\0'; 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate for (;cp>path && !M_FSDELIM(*cp); cp--) 91*7c478bd9Sstevel@tonic-gate ; 92*7c478bd9Sstevel@tonic-gate 93*7c478bd9Sstevel@tonic-gate if (!M_FSDELIM(*cp)) 94*7c478bd9Sstevel@tonic-gate if (type==DIRNAME && path!=arg) { 95*7c478bd9Sstevel@tonic-gate *path = '\0'; 96*7c478bd9Sstevel@tonic-gate return arg; /* curdir on the specified drive */ 97*7c478bd9Sstevel@tonic-gate } else 98*7c478bd9Sstevel@tonic-gate return (type==DIRNAME)?curdir:path; 99*7c478bd9Sstevel@tonic-gate else if (cp == path && type == DIRNAME) { 100*7c478bd9Sstevel@tonic-gate cp[1] = '\0'; 101*7c478bd9Sstevel@tonic-gate return arg; /* root directory involved */ 102*7c478bd9Sstevel@tonic-gate } else if (cp == path && cp[1] == '\0') 103*7c478bd9Sstevel@tonic-gate return(arg); 104*7c478bd9Sstevel@tonic-gate else if (type == BASENAME) 105*7c478bd9Sstevel@tonic-gate return ++cp; 106*7c478bd9Sstevel@tonic-gate *cp = '\0'; 107*7c478bd9Sstevel@tonic-gate return arg; 108*7c478bd9Sstevel@tonic-gate } 109*7c478bd9Sstevel@tonic-gate 110*7c478bd9Sstevel@tonic-gate /*f 111*7c478bd9Sstevel@tonic-gate * Finds the dirname of the given file. Spec1170 conformant. 112*7c478bd9Sstevel@tonic-gate */ 113*7c478bd9Sstevel@tonic-gate char * 114*7c478bd9Sstevel@tonic-gate dirname(char *arg) 115*7c478bd9Sstevel@tonic-gate { 116*7c478bd9Sstevel@tonic-gate return(basedir(arg, DIRNAME)); 117*7c478bd9Sstevel@tonic-gate } 118*7c478bd9Sstevel@tonic-gate 119*7c478bd9Sstevel@tonic-gate /*f 120*7c478bd9Sstevel@tonic-gate * Finds the basename of the given file. Spec1170 conformant. 121*7c478bd9Sstevel@tonic-gate */ 122*7c478bd9Sstevel@tonic-gate char * 123*7c478bd9Sstevel@tonic-gate basename(char *arg) 124*7c478bd9Sstevel@tonic-gate { 125*7c478bd9Sstevel@tonic-gate return(basedir(arg, BASENAME)); 126*7c478bd9Sstevel@tonic-gate } 127*7c478bd9Sstevel@tonic-gate 128*7c478bd9Sstevel@tonic-gate #ifdef TEST_MAIN_BASE_C 129*7c478bd9Sstevel@tonic-gate #include <stdio.h> 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate int main(int argc, char **argv) 132*7c478bd9Sstevel@tonic-gate { 133*7c478bd9Sstevel@tonic-gate int cnt; 134*7c478bd9Sstevel@tonic-gate char arg[128]; 135*7c478bd9Sstevel@tonic-gate char *tmp; 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate if (argc>1) { 138*7c478bd9Sstevel@tonic-gate for (cnt=argc--;argc;argc--) { 139*7c478bd9Sstevel@tonic-gate tmp = strdup(argv[cnt-argc]); 140*7c478bd9Sstevel@tonic-gate printf("%s\t%s\n", 141*7c478bd9Sstevel@tonic-gate dirname(argv[cnt-argc]), 142*7c478bd9Sstevel@tonic-gate basename(tmp)); 143*7c478bd9Sstevel@tonic-gate free(tmp); 144*7c478bd9Sstevel@tonic-gate } 145*7c478bd9Sstevel@tonic-gate return 0; 146*7c478bd9Sstevel@tonic-gate } 147*7c478bd9Sstevel@tonic-gate 148*7c478bd9Sstevel@tonic-gate printf("enter pathnames less than 128 chars. enter 'q' to quit.\n"); 149*7c478bd9Sstevel@tonic-gate 150*7c478bd9Sstevel@tonic-gate while(gets(arg)) 151*7c478bd9Sstevel@tonic-gate if (!strcmp(arg, "q")) 152*7c478bd9Sstevel@tonic-gate break; 153*7c478bd9Sstevel@tonic-gate else { 154*7c478bd9Sstevel@tonic-gate tmp = strdup(arg); 155*7c478bd9Sstevel@tonic-gate printf("%s\t%s\n", dirname(arg), basename(tmp)); 156*7c478bd9Sstevel@tonic-gate free(tmp); 157*7c478bd9Sstevel@tonic-gate } 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gate return 0; 160*7c478bd9Sstevel@tonic-gate } 161*7c478bd9Sstevel@tonic-gate #endif /* TEST_MAIN_BASE_C */ 162