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 /* Copyright (c) 1988 AT&T */ 23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate /* 27*7c478bd9Sstevel@tonic-gate * Copyright (c) 1999 by Sun Microsystems, Inc. 28*7c478bd9Sstevel@tonic-gate * All rights reserved. 29*7c478bd9Sstevel@tonic-gate */ 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate /* 34*7c478bd9Sstevel@tonic-gate * compath(pathname) 35*7c478bd9Sstevel@tonic-gate * 36*7c478bd9Sstevel@tonic-gate * This compresses pathnames. All strings of multiple slashes are 37*7c478bd9Sstevel@tonic-gate * changed to a single slash. All occurrences of "./" are removed. 38*7c478bd9Sstevel@tonic-gate * Whenever possible, strings of "/.." are removed together with 39*7c478bd9Sstevel@tonic-gate * the directory names that they follow. 40*7c478bd9Sstevel@tonic-gate * 41*7c478bd9Sstevel@tonic-gate * WARNING: since pathname is altered by this function, it should 42*7c478bd9Sstevel@tonic-gate * be located in a temporary buffer. This avoids the problem 43*7c478bd9Sstevel@tonic-gate * of accidently changing strings obtained from makefiles 44*7c478bd9Sstevel@tonic-gate * and stored in global structures. 45*7c478bd9Sstevel@tonic-gate */ 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate #include <string.h> 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate char * 50*7c478bd9Sstevel@tonic-gate compath(char *pathname) 51*7c478bd9Sstevel@tonic-gate { 52*7c478bd9Sstevel@tonic-gate char *nextchar; 53*7c478bd9Sstevel@tonic-gate char *lastchar; 54*7c478bd9Sstevel@tonic-gate char *sofar; 55*7c478bd9Sstevel@tonic-gate char *pnend; 56*7c478bd9Sstevel@tonic-gate 57*7c478bd9Sstevel@tonic-gate int pnlen; 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate /* 60*7c478bd9Sstevel@tonic-gate * do not change the path if it has no "/" 61*7c478bd9Sstevel@tonic-gate */ 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate if (strchr(pathname, '/') == 0) 64*7c478bd9Sstevel@tonic-gate return (pathname); 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate /* 67*7c478bd9Sstevel@tonic-gate * find all strings consisting of more than one '/' 68*7c478bd9Sstevel@tonic-gate */ 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate for (lastchar = pathname + 1; *lastchar != '\0'; lastchar++) 71*7c478bd9Sstevel@tonic-gate if ((*lastchar == '/') && (*(lastchar - 1) == '/')) { 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate /* 74*7c478bd9Sstevel@tonic-gate * find the character after the last slash 75*7c478bd9Sstevel@tonic-gate */ 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate nextchar = lastchar; 78*7c478bd9Sstevel@tonic-gate while (*++lastchar == '/') { 79*7c478bd9Sstevel@tonic-gate } 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate /* 82*7c478bd9Sstevel@tonic-gate * eliminate the extra slashes by copying 83*7c478bd9Sstevel@tonic-gate * everything after the slashes over the slashes 84*7c478bd9Sstevel@tonic-gate */ 85*7c478bd9Sstevel@tonic-gate 86*7c478bd9Sstevel@tonic-gate sofar = nextchar; 87*7c478bd9Sstevel@tonic-gate while ((*nextchar++ = *lastchar++) != '\0') 88*7c478bd9Sstevel@tonic-gate ; 89*7c478bd9Sstevel@tonic-gate lastchar = sofar; 90*7c478bd9Sstevel@tonic-gate } 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate /* 93*7c478bd9Sstevel@tonic-gate * find all strings of "./" 94*7c478bd9Sstevel@tonic-gate */ 95*7c478bd9Sstevel@tonic-gate 96*7c478bd9Sstevel@tonic-gate for (lastchar = pathname + 1; *lastchar != '\0'; lastchar++) 97*7c478bd9Sstevel@tonic-gate if ((*lastchar == '/') && (*(lastchar - 1) == '.') && 98*7c478bd9Sstevel@tonic-gate ((lastchar - 1 == pathname) || (*(lastchar - 2) == '/'))) { 99*7c478bd9Sstevel@tonic-gate 100*7c478bd9Sstevel@tonic-gate /* 101*7c478bd9Sstevel@tonic-gate * copy everything after the "./" over the "./" 102*7c478bd9Sstevel@tonic-gate */ 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate nextchar = lastchar - 1; 105*7c478bd9Sstevel@tonic-gate sofar = nextchar; 106*7c478bd9Sstevel@tonic-gate while ((*nextchar++ = *++lastchar) != '\0') 107*7c478bd9Sstevel@tonic-gate ; 108*7c478bd9Sstevel@tonic-gate lastchar = sofar; 109*7c478bd9Sstevel@tonic-gate } 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate /* 112*7c478bd9Sstevel@tonic-gate * find each occurrence of "/.." 113*7c478bd9Sstevel@tonic-gate */ 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate for (lastchar = pathname + 1; *lastchar != '\0'; lastchar++) 116*7c478bd9Sstevel@tonic-gate if ((lastchar != pathname) && (*lastchar == '/') && 117*7c478bd9Sstevel@tonic-gate (*(lastchar + 1) == '.') && (*(lastchar + 2) == '.') && 118*7c478bd9Sstevel@tonic-gate ((*(lastchar + 3) == '/') || (*(lastchar + 3) == '\0'))) { 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate /* 121*7c478bd9Sstevel@tonic-gate * find the directory name preceding the "/.." 122*7c478bd9Sstevel@tonic-gate */ 123*7c478bd9Sstevel@tonic-gate 124*7c478bd9Sstevel@tonic-gate nextchar = lastchar - 1; 125*7c478bd9Sstevel@tonic-gate while ((nextchar != pathname) && 126*7c478bd9Sstevel@tonic-gate (*(nextchar - 1) != '/')) 127*7c478bd9Sstevel@tonic-gate --nextchar; 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate /* 130*7c478bd9Sstevel@tonic-gate * make sure the preceding directory's name 131*7c478bd9Sstevel@tonic-gate * is not "." or ".." 132*7c478bd9Sstevel@tonic-gate */ 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate if ((*nextchar == '.') && 135*7c478bd9Sstevel@tonic-gate (*(nextchar + 1) == '/') || 136*7c478bd9Sstevel@tonic-gate ((*(nextchar + 1) == '.') && 137*7c478bd9Sstevel@tonic-gate (*(nextchar + 2) == '/'))) { 138*7c478bd9Sstevel@tonic-gate /* EMPTY */; 139*7c478bd9Sstevel@tonic-gate } else { 140*7c478bd9Sstevel@tonic-gate 141*7c478bd9Sstevel@tonic-gate /* 142*7c478bd9Sstevel@tonic-gate * prepare to eliminate either 143*7c478bd9Sstevel@tonic-gate * "dir_name/../" or "dir_name/.." 144*7c478bd9Sstevel@tonic-gate */ 145*7c478bd9Sstevel@tonic-gate 146*7c478bd9Sstevel@tonic-gate if (*(lastchar + 3) == '/') 147*7c478bd9Sstevel@tonic-gate lastchar += 4; 148*7c478bd9Sstevel@tonic-gate else 149*7c478bd9Sstevel@tonic-gate lastchar += 3; 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gate /* 152*7c478bd9Sstevel@tonic-gate * copy everything after the "/.." to 153*7c478bd9Sstevel@tonic-gate * before the preceding directory name 154*7c478bd9Sstevel@tonic-gate */ 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate sofar = nextchar - 1; 157*7c478bd9Sstevel@tonic-gate while ((*nextchar++ = *lastchar++) != '\0'); 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gate lastchar = sofar; 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate /* 162*7c478bd9Sstevel@tonic-gate * if the character before what was taken 163*7c478bd9Sstevel@tonic-gate * out is '/', set up to check if the 164*7c478bd9Sstevel@tonic-gate * slash is part of "/.." 165*7c478bd9Sstevel@tonic-gate */ 166*7c478bd9Sstevel@tonic-gate 167*7c478bd9Sstevel@tonic-gate if ((sofar + 1 != pathname) && (*sofar == '/')) 168*7c478bd9Sstevel@tonic-gate --lastchar; 169*7c478bd9Sstevel@tonic-gate } 170*7c478bd9Sstevel@tonic-gate } 171*7c478bd9Sstevel@tonic-gate 172*7c478bd9Sstevel@tonic-gate /* 173*7c478bd9Sstevel@tonic-gate * if the string is more than a character long and ends 174*7c478bd9Sstevel@tonic-gate * in '/', eliminate the '/'. 175*7c478bd9Sstevel@tonic-gate */ 176*7c478bd9Sstevel@tonic-gate 177*7c478bd9Sstevel@tonic-gate pnlen = strlen(pathname); 178*7c478bd9Sstevel@tonic-gate pnend = strchr(pathname, '\0') - 1; 179*7c478bd9Sstevel@tonic-gate 180*7c478bd9Sstevel@tonic-gate if ((pnlen > 1) && (*pnend == '/')) { 181*7c478bd9Sstevel@tonic-gate *pnend-- = '\0'; 182*7c478bd9Sstevel@tonic-gate pnlen--; 183*7c478bd9Sstevel@tonic-gate } 184*7c478bd9Sstevel@tonic-gate 185*7c478bd9Sstevel@tonic-gate /* 186*7c478bd9Sstevel@tonic-gate * if the string has more than two characters and ends in 187*7c478bd9Sstevel@tonic-gate * "/.", remove the "/.". 188*7c478bd9Sstevel@tonic-gate */ 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate if ((pnlen > 2) && (*(pnend - 1) == '/') && (*pnend == '.')) 191*7c478bd9Sstevel@tonic-gate *--pnend = '\0'; 192*7c478bd9Sstevel@tonic-gate 193*7c478bd9Sstevel@tonic-gate /* 194*7c478bd9Sstevel@tonic-gate * if all characters were deleted, return "."; 195*7c478bd9Sstevel@tonic-gate * otherwise return pathname 196*7c478bd9Sstevel@tonic-gate */ 197*7c478bd9Sstevel@tonic-gate 198*7c478bd9Sstevel@tonic-gate if (*pathname == '\0') 199*7c478bd9Sstevel@tonic-gate (void) strcpy(pathname, "."); 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate return (pathname); 202*7c478bd9Sstevel@tonic-gate } 203