summaryrefslogtreecommitdiff
path: root/file-utils.c
blob: 99806e4c02e9e1f9a9339c4ba90fb733096ab07b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* libnul
 *
 * Copyright (C) 2006-2007 Red Hat, Inc.
 * Copyright (C) 2009 Soren Sandmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

#include <string.h>
#include "libnul.h"

/* nul_canonicalize_filename cutted and pasted from GIO
 *
 * Author: Alexander Larsson
 */
char *
nul_canonicalize_filename (const char *filename)
{
    char *canon, *start, *p, *q;
    char *cwd;
    int i;

    g_return_val_if_fail (filename != NULL, NULL);
    
    if (!g_path_is_absolute (filename))
    {
	cwd = g_get_current_dir ();
	canon = g_build_filename (cwd, filename, NULL);
	g_free (cwd);
    }
    else
	canon = g_strdup (filename);
    
    start = (char *)g_path_skip_root (canon);
    
    /* POSIX allows double slashes at the start to
     * mean something special (as does windows too).
     * So, "//" != "/", but more than two slashes
     * is treated as "/".
     */
    i = 0;
    for (p = start - 1;
	 (p >= canon) &&
	     G_IS_DIR_SEPARATOR (*p);
	 p--)
	i++;
    if (i > 2)
    {
	i -= 1;
	start -= i;
	memmove (start, start+i, strlen (start+i)+1);
    }
    
    p = start;
    while (*p != 0)
    {
	if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
        {
	    memmove (p, p+1, strlen (p+1)+1);
        }
	else if (p[0] == '.' && p[1] == '.' &&
		 (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
        {
	    q = p + 2;
	    /* Skip previous separator */
	    
	    p = p - 2;
	    if (p < start)
		p = start;
	    while (p > start && !G_IS_DIR_SEPARATOR (*p))
		p--;
	    if (G_IS_DIR_SEPARATOR (*p))
		*p++ = G_DIR_SEPARATOR;
	    memmove (p, q, strlen (q)+1);
        }
	else
        {
	    /* Skip until next separator */
	    while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
		p++;
	    
	    if (*p != 0)
            {
		/* Canonicalize one separator */
		*p++ = G_DIR_SEPARATOR;
            }
        }
	
	/* Remove additional separators */
	q = p;
	while (*q && G_IS_DIR_SEPARATOR (*q))
	    q++;
	
	if (p != q)
	    memmove (p, q, strlen (q)+1);
    }
    
    /* Remove trailing slashes */
    if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
	*(p-1) = 0;
    
    return canon;
}