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