github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/lib9/cleanname.c (about)

     1  /*
     2  Inferno libkern/cleanname.c
     3  http://code.google.com/p/inferno-os/source/browse/libkern/cleanname.c
     4  
     5  	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     6  	Revisions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com).  All rights reserved.
     7  
     8  Permission is hereby granted, free of charge, to any person obtaining a copy
     9  of this software and associated documentation files (the "Software"), to deal
    10  in the Software without restriction, including without limitation the rights
    11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    12  copies of the Software, and to permit persons to whom the Software is
    13  furnished to do so, subject to the following conditions:
    14  
    15  The above copyright notice and this permission notice shall be included in
    16  all copies or substantial portions of the Software.
    17  
    18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    24  THE SOFTWARE.
    25  */
    26  
    27  #include <u.h>
    28  #include <libc.h>
    29  
    30  /*
    31   * In place, rewrite name to compress multiple /, eliminate ., and process ..
    32   */
    33  #define SEP(x)	((x)=='/' || (x) == 0)
    34  char*
    35  cleanname(char *name)
    36  {
    37  	char *p, *q, *dotdot;
    38  	int rooted;
    39  
    40  	rooted = name[0] == '/';
    41  
    42  	/*
    43  	 * invariants:
    44  	 *	p points at beginning of path element we're considering.
    45  	 *	q points just past the last path element we wrote (no slash).
    46  	 *	dotdot points just past the point where .. cannot backtrack
    47  	 *		any further (no slash).
    48  	 */
    49  	p = q = dotdot = name+rooted;
    50  	while(*p) {
    51  		if(p[0] == '/')	/* null element */
    52  			p++;
    53  		else if(p[0] == '.' && SEP(p[1]))
    54  			p += 1;	/* don't count the separator in case it is nul */
    55  		else if(p[0] == '.' && p[1] == '.' && SEP(p[2])) {
    56  			p += 2;
    57  			if(q > dotdot) {	/* can backtrack */
    58  				while(--q > dotdot && *q != '/')
    59  					;
    60  			} else if(!rooted) {	/* /.. is / but ./../ is .. */
    61  				if(q != name)
    62  					*q++ = '/';
    63  				*q++ = '.';
    64  				*q++ = '.';
    65  				dotdot = q;
    66  			}
    67  		} else {	/* real path element */
    68  			if(q != name+rooted)
    69  				*q++ = '/';
    70  			while((*q = *p) != '/' && *q != 0)
    71  				p++, q++;
    72  		}
    73  	}
    74  	if(q == name)	/* empty string is really ``.'' */
    75  		*q++ = '.';
    76  	*q = '\0';
    77  	return name;
    78  }