put example files to dedicated dir
[monster.git] / path.c
blob2b51dd248b652ed0e6d92b36ba1a804cee97c4f2
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 *****************************************************************************/
24 #include <string.h>
25 #include <assert.h>
27 #define DISABLE_DEBUG_OUTPUT
29 #include "log.h"
30 #include "path.h"
32 void
33 path_normalize(char * path_ptr)
35 char * temp_ptr;
36 unsigned int dirs_available;
37 size_t left;
38 char * previous_dir_ptr;
40 DEBUG_OUT("Normalizing filesystem path \"%s\"", path_ptr);
42 temp_ptr = path_ptr;
43 dirs_available = 0;
44 left = strlen(temp_ptr)+1;
46 loop:
47 //DEBUG_OUT("%u chars left to process", left);
49 if (*temp_ptr == 0)
50 goto done;
52 if ((temp_ptr == path_ptr || temp_ptr[-1] == '/') &&
53 temp_ptr[0] == '.' &&
54 temp_ptr[1] == '/')
56 DEBUG_OUT("");
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);
61 left -= 2;
62 goto loop;
65 if (temp_ptr[0] == '.' &&
66 temp_ptr[1] == '.' &&
67 temp_ptr[2] == '/' &&
68 dirs_available > 0)
70 DEBUG_OUT("");
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 != '/')
79 previous_dir_ptr--;
82 if (*previous_dir_ptr == '/')
84 previous_dir_ptr++;
87 DEBUG_OUT("Previous dir \"%s\"", previous_dir_ptr);
89 left -= 3;
90 memmove(previous_dir_ptr, temp_ptr + 3, left);
91 temp_ptr = previous_dir_ptr;
93 dirs_available--;
95 DEBUG_OUT("Skipping '..' result is \"%s\"", temp_ptr);
96 DEBUG_OUT("Skipping '..' global result is \"%s\"", path_ptr);
98 goto loop;
101 if (temp_ptr > path_ptr &&
102 *temp_ptr == '/')
104 dirs_available++;
107 temp_ptr++;
108 left--;
109 goto loop;
111 done:
112 DEBUG_OUT("Filesystem path normalized to \"%s\"", path_ptr);
113 assert(left == 1); /* the terminating zero */