Skip to content

Instantly share code, notes, and snippets.

@mah0x211
Created February 19, 2015 19:37
Show Gist options
  • Select an option

  • Save mah0x211/4ebbbafe786493544733 to your computer and use it in GitHub Desktop.

Select an option

Save mah0x211/4ebbbafe786493544733 to your computer and use it in GitHub Desktop.
realpathを使わないパスの正規化。realpathを使うと実際にファイルシステムを辿るためその前に正規化したいケースで使う。
/*
* Copyright 2015 Masatoshi Teruya. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Created by Masatoshi Teruya on 2015/02/20.
*
*/
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
static char *normalize( const char *path, int *is_cwd )
{
size_t len = strlen( path );
char *rpath = NULL;
char *ptr = NULL;
char *prev = NULL;
char c = 0;
// prepend slash if path is cwd
if( ( *is_cwd = path[0] != '/' ) )
{
len += 1;
if( !( rpath = malloc( len + 2 ) ) ){
return NULL;
}
*rpath = '/';
memcpy( rpath + 1, path, len );
rpath[len] = 0;
*is_cwd = 1;
}
else if( !( rpath = strndup( path, len ) ) ){
return NULL;
}
ptr = rpath;
while( *ptr )
{
if( *ptr == '/' )
{
prev = ptr;
// skip multiple slashes
if( ptr[1] == '/' )
{
while( *ptr == '/' ){
ptr++;
}
if( *ptr ){
memcpy( prev + 1, ptr, strlen( ptr ) + 1 );
}
else {
prev[1] = 0;
}
ptr = prev;
}
}
else if( *ptr == '.' && !c || c == '/' )
{
// path: ./bar
if( ptr[1] == '/' )
{
// path: /foo/./bar -> /foo/bar
if( prev ){
memcpy( prev + 1, ptr + 2, strlen( ptr + 2 ) + 1 );
ptr = prev;
}
// path: ./bar -> /bar
else {
memcpy( rpath, ptr + 1, len );
prev = rpath;
ptr = prev;
}
}
// path: ../bar
else if( ptr[1] == '.' && ptr[2] == '/' )
{
// path: /foo/../bar -> /bar
if( prev )
{
while( prev != rpath )
{
prev--;
if( *prev == '/' ){
break;
}
}
memcpy( prev + 1, ptr + 3, strlen( ptr + 3 ) + 1 );
ptr = prev;
}
// path: ../bar -> /bar
else {
memcpy( rpath, ptr + 2, len - 1 );
prev = rpath;
ptr = prev;
}
}
}
c = *ptr;
ptr++;
}
return rpath;
}
int main (int argc, const char * argv[])
{
char *str = NULL;
char *cwd = getcwd( NULL, 0 );
int is_cwd = 0;
char *pathz[] = {
"foo/bar/baz",
".foo/bar/baz",
"/foo/./bar/../baz",
"/foo/../bar/./baz",
"/foo/../bar/../baz",
"/foo/../bar/../baz./",
"/foo/../bar/../.baz/",
"/foo/../bar/../baz../",
"/foo/../bar/../..baz/",
NULL
};
char **ptr = pathz;
while( *ptr )
{
str = normalize( *ptr, &is_cwd );
// prepend cwd
if( is_cwd ){
printf("%s|%s\n", cwd, str );
}
else {
printf("%s\n", str );
}
free( str );
ptr++;
}
free( cwd );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment