summaryrefslogtreecommitdiffstats
path: root/common/file.c
blob: fc7e74d9ea810f9d04dfa93b025b7517b5af2405 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   xrdp: A Remote Desktop Protocol server.
   Copyright (C) Jay Sorg 2004-2005

   read a config file

*/

#include "arch.h"
#include "os_calls.h"
#include "list.h"
#include "file.h"
#include "parse.h"

/*****************************************************************************/
/* returns error
   returns 0 if everything is ok
   returns 1 if problem reading file */
int APP_CC
file_read_sections(int fd, struct list* names)
{
  struct stream* s;
  char text[256];
  char c;
  int in_it;
  int in_it_index;
  int len;
  int index;
  int rv;

  rv = 0;
  g_file_seek(fd, 0);
  in_it_index = 0;
  in_it = 0;
  g_memset(text, 0, 256);
  list_clear(names);
  make_stream(s);
  init_stream(s, 8192);
  len = g_file_read(fd, s->data, 8192);
  if (len > 0)
  {
    s->end = s->p + len;
    for (index = 0; index < len; index++)
    {
      in_uint8(s, c);
      if (c == '[')
      {
        in_it = 1;
      }
      else if (c == ']')
      {
        list_add_item(names, (long)g_strdup(text));
        in_it = 0;
        in_it_index = 0;
        g_memset(text, 0, 256);
      }
      else if (in_it)
      {
        text[in_it_index] = c;
        in_it_index++;
      }
    }
  }
  else if (len < 0)
  {
    rv = 1;
  }
  free_stream(s);
  return rv;
}

/*****************************************************************************/
int APP_CC
file_read_line(struct stream* s, char* text)
{
  int i;
  char c;
  char* hold;

  if (!s_check(s))
  {
    return 1;
  }
  hold = s->p;
  i = 0;
  in_uint8(s, c);
  while (c != 10 && c != 13 && s_check(s))
  {
    text[i] = c;
    i++;
    in_uint8(s, c);
  }
  if (c == 10 || c == 13)
  {
    while ((c == 10 || c == 13) && s_check(s))
    {
      in_uint8(s, c);
    }
    s->p--;
  }
  text[i] = 0;
  if (text[0] == '[')
  {
    s->p = hold;
    return 1;
  }
  if (g_strlen(text) > 0)
  {
    return 0;
  }
  else
  {
    return 1;
  }
}

/*****************************************************************************/
int APP_CC
file_split_name_value(char* text, char* name, char* value)
{
  int len;
  int i;
  int value_index;
  int name_index;
  int on_to;

  value_index = 0;
  name_index = 0;
  on_to = 0;
  name[0] = 0;
  value[0] = 0;
  len = g_strlen(text);
  for (i = 0; i < len; i++)
  {
    if (text[i] == '=')
    {
      on_to = 1;
    }
    else if (on_to)
    {
      value[value_index] = text[i];
      value_index++;
      value[value_index] = 0;
    }
    else
    {
      name[name_index] = text[i];
      name_index++;
      name[name_index] = 0;
    }
  }
  return 0;
}

/*****************************************************************************/
/* return error */
int APP_CC
file_read_section(int fd, char* section, struct list* names,
                  struct list* values)
{
  struct stream* s;
  char text[512];
  char name[512];
  char value[512];
  char c;
  int in_it;
  int in_it_index;
  int len;
  int index;

  g_file_seek(fd, 0);
  in_it_index = 0;
  in_it = 0;
  g_memset(text, 0, 512);
  list_clear(names);
  list_clear(values);
  make_stream(s);
  init_stream(s, 8192);
  len = g_file_read(fd, s->data, 8192);
  if (len > 0)
  {
    s->end = s->p + len;
    for (index = 0; index < len; index++)
    {
      in_uint8(s, c);
      if (c == '[')
      {
        in_it = 1;
      }
      else if (c == ']')
      {
        if (g_strncasecmp(section, text, 255) == 0)
        {
          file_read_line(s, text);
          while (file_read_line(s, text) == 0)
          {
            file_split_name_value(text, name, value);
            list_add_item(names, (long)g_strdup(name));
            list_add_item(values, (long)g_strdup(value));
          }
          free_stream(s);
          return 0;
        }
        in_it = 0;
        in_it_index = 0;
        g_memset(text, 0, 512);
      }
      else if (in_it)
      {
        text[in_it_index] = c;
        in_it_index++;
      }
    }
  }
  free_stream(s);
  return 1;
}