1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * Filesystem path helpers
5 * This file is part of monster
7 * Copyright (C) 2006,2007 Nedko Arnaudov <nedko@arnaudov.name>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22 *****************************************************************************/
27 #define DISABLE_DEBUG_OUTPUT
33 path_normalize(char * path_ptr
)
36 unsigned int dirs_available
;
38 char * previous_dir_ptr
;
40 DEBUG_OUT("Normalizing filesystem path \"%s\"", path_ptr
);
44 left
= strlen(temp_ptr
)+1;
47 //DEBUG_OUT("%u chars left to process", left);
52 if ((temp_ptr
== path_ptr
|| temp_ptr
[-1] == '/') &&
57 DEBUG_OUT("Skipping '.' from \"%s\"", temp_ptr
);
58 memmove(temp_ptr
, temp_ptr
+ 2, left
);
59 DEBUG_OUT("Skipping '.' result is \"%s\"", temp_ptr
);
60 DEBUG_OUT("Skipping '.' global result is \"%s\"", path_ptr
);
65 if (temp_ptr
[0] == '.' &&
71 DEBUG_OUT("Skipping '..' from \"%s\"", temp_ptr
);
73 assert(temp_ptr
> path_ptr
+ 2);
75 previous_dir_ptr
= temp_ptr
- 2;
76 while (previous_dir_ptr
> path_ptr
&&
77 *previous_dir_ptr
!= '/')
82 if (*previous_dir_ptr
== '/')
87 DEBUG_OUT("Previous dir \"%s\"", previous_dir_ptr
);
90 memmove(previous_dir_ptr
, temp_ptr
+ 3, left
);
91 temp_ptr
= previous_dir_ptr
;
95 DEBUG_OUT("Skipping '..' result is \"%s\"", temp_ptr
);
96 DEBUG_OUT("Skipping '..' global result is \"%s\"", path_ptr
);
101 if (temp_ptr
> path_ptr
&&
112 DEBUG_OUT("Filesystem path normalized to \"%s\"", path_ptr
);
113 assert(left
== 1); /* the terminating zero */