aboutsummaryrefslogtreecommitdiffstats
path: root/daemon/pathnames.c
blob: 8d4b09a90ca5334b7a9f585afa97d15f5a0df827 (plain) (blame)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * pathnames.c - syslogd implementation for windows, miscellaneous functions
 *               operating with file names and paths
 *
 * Created by Alexander Yaworsky
 *
 * THIS SOFTWARE IS NOT COPYRIGHTED
 *
 * This source code is offered for use in the public domain. You may
 * use, modify or distribute it freely.
 *
 * This code is distributed in the hope that it will be useful but
 * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
 * DISCLAIMED. This includes but is not limited to warranties of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>

#include <glib.h>

#include <syslog.h>
#include <syslogd.h>

/******************************************************************************
 * make_absolute_log_pathname
 *
 * Make absolute pathname inside logdir.
 */
gchar* make_absolute_log_pathname( char* path_appendix )
{
    gchar *path, *ret;

    if( logdir && g_path_is_absolute( logdir ) )
        path = g_strdup( logdir );
    else
    {
        gchar *dir = g_path_get_dirname( __argv[0] );
        path = g_build_filename( dir, logdir, NULL );
        g_free( dir );
    }
    ret = g_build_filename( path, path_appendix, NULL );
    g_free( path );
    return ret;
}

/******************************************************************************
 * create_directories
 *
 * Create all the directories in pathname.
 */
void create_directories( gchar* pathname )
{
    gchar *p;
    gchar saved;

    TRACE_ENTER( "%s\n", pathname );
    p = (gchar*) g_path_skip_root( pathname );
    if( !p )
        p = pathname;
    for(;;)
    {
        while( *p != '\0' && *p != '/' && *p != '\\' )
            p++;
        if( *p == '\0' )
            break;
        saved = *p;
        *p = '\0';
        CreateDirectory( pathname, NULL );
        *p++ = saved;
    } 
    TRACE_LEAVE( "done\n" );
}

/******************************************************************************
 * normalize_pathname
 *
 * Remove . and .. from pathname.
 * Pathname should be in UTF-8 encoding.
 * Return NULL if pathname is invalid or normalized pathname
 * in locale encoding.
 */
gchar* normalize_pathname( const gchar* pathname )
{
    gchar *ret = g_locale_from_utf8( pathname, -1, NULL, NULL, NULL );
    gchar *first_element, *current_element, *next_element;

    TRACE_ENTER( "%s\n", ret );

    first_element = (gchar*) g_path_skip_root( ret );
    if( !first_element )
        first_element = ret;

    for( current_element = first_element; *current_element != '\0'; )
    {
        next_element = current_element;
        while( *next_element != '\0' && *next_element != '/' && *next_element != '\\' )
            next_element++;
        if( *next_element != '\0' )
            next_element++;  /* skip separator */

        if( current_element[0] != '.' )
            current_element = next_element;
        else
        {
            if( current_element[1] == '/'
                || current_element[1] == '\\' || current_element[1] == '\0' )
                /* /./ remove current element */
                memmove( current_element, next_element, strlen( next_element ) + 1 );
            else if( current_element[1] == '.'
                     && (current_element[2] == '/'
                         || current_element[2] == '\\' || current_element[2] == '\0') )
            {
                /* /../ find and remove previous element */
                gchar *prev_element = current_element - 2;
                if( prev_element < first_element )
                {
                    ERR( "Invalid pathname: %s\n", pathname );
                    g_free( ret );
                    ret = NULL;
                    break;
                }
                while( prev_element > first_element && *prev_element != '/' && *prev_element != '\\' )
                    prev_element--;
                if( prev_element > first_element )
                    prev_element++;  /* skip separator */
                memmove( prev_element, next_element, strlen( next_element ) + 1 );
                current_element = prev_element;
            }
            else
                current_element = next_element;
        }
    }
    TRACE_LEAVE( "ret=%s\n", ret? ret : "NULL" );
    return ret;
}