summaryrefslogtreecommitdiff
path: root/cleopatre/application/libspid/src/config_line.c
blob: 6344d3b75eaed4963962bf22b1a21681c84878eb (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/* SPC300 bundle {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    application/libspid/src/config_line.c
 * \brief   line management in configuration file
 * \ingroup libspid
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "libspid.h"

/**
 * Read a configuration line from a file.<BR>
 * A configuration line is composed of a unique key, which
 * is playing the role of index, and several elements. All are
 * separated by a delimiter and terminated by a carriage
 * return.<BR>
 * When line is read, it is put inside a provided buffer, and
 * then processed. If the searched key is found, the provided
 * element table is filled with pointers to each found line
 * elements.If the key is an empty string, the key of the
 * first line is returned into the key buffer.<BR>
 * Key buffer must not be smaller than LIBSPID_MAX_KEY_LEN.<BR>
 * When line elements are returned, the key buffer if filled
 * with the next found key in the next line. If there is no more
 * line to read, the key buffer contains a empty string. The
 * element number parameter is updated with the number of
 * elements filled into the element table.
 *
 * \param filename full path filename of a configuration file
 * \param delimiters string containing all accepted delimiters
 * (usually space and tab characters)
 * \param key key of the line to read; updated with the key of
 * the next line
 * \param elt_number maximum number of elements to read, updated
 * with the real number of read elements if less is found
 * \param elt array of pointers where are stored pointers to
 * elements
 * \param buffer whole line working buffer
 * \param buffer_len working buffer length
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_NOT_FOUND: key not found or no more
 * line to read
 * \return LIBSPID_ERROR_NO_SPACE: no enough space in buffer or
 * to much elements to put into element table
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */

libspid_error_t libspid_config_read_line(const char *filename, const char* delimiters, char *key, unsigned int *elt_number, char *elt[], char *buffer, unsigned int buffer_len)
{
    FILE *fp;
    char line_buffer[LIBSPID_LINE_MAX_LEN];
    char line2_buffer[LIBSPID_LINE_MAX_LEN];
    char *ptr, *strtok_ctx;
#ifdef USE_LOCK
    struct flock lock;
#endif /* USE_LOCK */
    int max_elt_number;
    int is_key_found;

    /* check input parameters */
    if ( (filename == NULL) || (delimiters == NULL) || (key == NULL) || (elt_number == NULL)
            || (elt == NULL) || (buffer == NULL) )
    {
        return LIBSPID_ERROR_PARAM;
    }

    if ( (strlen(delimiters) <= 0) || (*elt_number > LIBSPID_ELT_MAX_NB)
            || (buffer_len <= 0) )
    {
        return LIBSPID_ERROR_PARAM;
    }

    /* check delimiter validity */
    if ( strchr(delimiters, '\n') || strchr(delimiters, '\r') )
    {
        return LIBSPID_ERROR_PARAM;
    }

    /* check key validity */
    if ( strlen(key) > LIBSPID_KEY_MAX_LEN )
    {
        return LIBSPID_ERROR_PARAM;
    }

    if ( strpbrk(key, delimiters) )
    {
        return LIBSPID_ERROR_PARAM;
    }

    /* open config file */
    if ( (fp = fopen(filename, "r")) == NULL )
    {
        //syslog(LOG_WARNING, "%s: cannot open %s (errno=%d)\n", __FUNCTION__, filename, errno);
        return LIBSPID_ERROR_SYSTEM;
    }

#ifdef USE_LOCK
    /* set the read lock */
    lock.l_type = F_RDLCK;
    lock.l_whence = SEEK_SET;
    lock.l_start = 0;
    lock.l_len = 0;
    if ( fcntl(fileno(fp), F_SETLKW, &lock) < 0 )
    {
        //syslog(LOG_WARNING, "%s: lock set failed (errno=%d)\n)", __FUNCTION__, errno);
        fclose(fp);
        return LIBSPID_ERROR_SYSTEM;
    }
#endif /* USE_LOCK */

    max_elt_number = *elt_number;
    *elt_number = 0;
    is_key_found = LIBSPID_FALSE;

    if ( strlen(key) > 0 )
    {
        while ( fgets(buffer, buffer_len, fp) )
        {
            /* remove the newline character*/
            if ( buffer[strlen(buffer) - 1] == '\n' )
            {
                if ( buffer[strlen(buffer) - 2] == '\r')
                {
                    /* DOS format */
                    buffer[strlen(buffer) - 2] = '\0';
                }
                else
                {
                    /* Unix format */
                    buffer[strlen(buffer) - 1] = '\0';
                }
            }
            else
            {
                /* chops chars until end of line */
                fgets(line_buffer, LIBSPID_LINE_MAX_LEN - 1, fp);
            }
            /* get the index */
            ptr = strtok_r(buffer, delimiters, &strtok_ctx);
            //printf("key=%s (look for '%s')\n", ptr, key);
            /* check if it is looked key */
            if ( (ptr == NULL) || (strlen(ptr) <= 0) || (*ptr == '#') || strcmp(ptr, key) )
                continue;

            is_key_found = LIBSPID_TRUE;

            /* get all elements */
            while ( (ptr = strtok_r(NULL, delimiters, &strtok_ctx)) && (*elt_number < max_elt_number) )
            {
                elt[*elt_number] = ptr;
                //printf("elt[%d]='%s'\n", *elt_number, elt[*elt_number]);
                *elt_number += 1;
            }
            break;
        }
    }
    else
    {
        /* just find the first key */
        is_key_found = LIBSPID_TRUE;
    }
    /* check if key has been found */
    if (!is_key_found)
    {
#ifdef USE_LOCK
        /* close lock */
        fcntl(fileno(fp), F_UNLCK, &lock);
#endif /* USE_LOCK */
        /* close file */
        fclose(fp);
        return LIBSPID_ERROR_NOT_FOUND;
    }
    /* get next key */
    *key = '\0';
    while(fgets(line2_buffer, LIBSPID_LINE_MAX_LEN, fp))
    {
        /* remove the newline */
        if(line2_buffer[strlen(line2_buffer) - 1] == '\n')
        {
            line2_buffer[strlen(line2_buffer) - 1] = '\0';
        }
        /* get the index */
        ptr = strtok_r(line2_buffer, delimiters, &strtok_ctx);
        //printf("key2=%s\n", ptr);
        /* check if it is looked key */
        if( (ptr == NULL) || (strlen(ptr) <= 0) || (*ptr == '#') )
            continue;
        strcpy(key, ptr);
        break;
    }

#ifdef USE_LOCK
    /* close lock */
    fcntl(fileno(fp), F_UNLCK, &lock);
#endif /* USE_LOCK */
    /* close file */
    fclose(fp);

    return LIBSPID_SUCCESS;
}

/**
 * Write a configuration line into a configuration file.<BR>
 * A configuration line is made of a key and several elements,
 * separated by a delimiter.<BR>
 * A key item is used as unique line reference. If the key
 * already exists, the line is updated with new elements.
 * If the key does not exist, the line is added to the end of
 * the file.<BR>
 * The key length must not be longer than LIBSPID_KEY_MAX_LEN.
 *
 * \param filename full path filename of a configuration file
 * \param delimiter char used for delimiting key and elements
 * \param key the key (index) of the line
 * \param elt_number number of elements to write
 * \param elt list of elements to write
 * \result error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_NO_SPACE: no enough space in buffer or
 * to much elements to put into element table
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */

libspid_error_t libspid_config_write_line(const char *filename, const char delimiter, const char *key, unsigned int elt_number, char *elt[])
{
    char buffer[LIBSPID_LINE_MAX_LEN], key_buffer[LIBSPID_KEY_MAX_LEN + 1];
    char out_filename[64];
    char *ptr, *strtok_ctx;
    FILE *fp_in, *fp_out;
    int fd_in, index = 0;
    int is_key_found = LIBSPID_FALSE;
    char delimiters[8] = LIBSPID_CONFIG_DELIMITER " \t";
#ifdef USE_LOCK
    struct flock lock;
#endif /* USE_LOCK */

    /* add given delimiter to the set of delimiters */
    strncat(delimiters, &delimiter, 1);

    /* check input parameters */
    if ( (filename == NULL) || (key == NULL)  || (elt == NULL) || (elt_number > LIBSPID_ELT_MAX_NB) )
    {
        return LIBSPID_ERROR_PARAM;
    }

    /* check sizes */
    if ( (strlen(key) == 0) )
    {
        return LIBSPID_ERROR_PARAM;
    }

    /* check delimiter validity */
    if ( (delimiter == '\n') || (delimiter == '\r') )
    {
        return LIBSPID_ERROR_PARAM;
    }

    /* check key validity */
    if ( strchr(key, delimiter) != NULL )
    {
        return LIBSPID_ERROR_PARAM;
    }
    if ( strlen(key) > LIBSPID_KEY_MAX_LEN )
    {
        return LIBSPID_ERROR_PARAM;
    }

    /* open config file */
    if ( (fd_in = open(filename, O_RDWR | O_CREAT, 0666)) < 0 )
    {
        //syslog(LOG_WARNING, "%s: cannot open %s (errno=%d)", __FUNCTION__, filename, errno);
        return LIBSPID_ERROR_SYSTEM;
    }
    fp_in = fdopen(fd_in, "r");
    /* create the modified config file */
    sprintf(out_filename, "%sXXXXXX", filename);
    if ( (fp_out = fdopen(mkstemp(out_filename), "w")) == NULL )
    {
        fclose(fp_out);
        fclose(fp_in);
        close(fd_in);
        return LIBSPID_ERROR_SYSTEM;
    }

#ifdef USE_LOCK
    /* set the write lock */
    lock.l_type = F_WRLCK;
    lock.l_whence = SEEK_SET;
    lock.l_start = 0;
    lock.l_len = 0;
    if (fcntl(fd_in, F_SETLKW, &lock) < 0)
    {
        //syslog(LOG_WARNING, "%s: lock set failed (errno=%d)\n)", __FUNCTION__, errno);
        fclose(fp_out);
        fclose(fp_in);
        close(fd_in);
        return LIBSPID_ERROR_SYSTEM;
    }
#endif /* USE_LOCK */
    /* find the label to modify */
    while ( fgets(buffer, LIBSPID_LINE_MAX_LEN - 1, fp_in) )
    {
        /* fill working copy for key */
        strncpy(key_buffer, buffer, LIBSPID_KEY_MAX_LEN);
        /* get the index */
        ptr = strtok_r(key_buffer, delimiters, &strtok_ctx);
        if ( (ptr == NULL) || (strlen(ptr) <= 0) || (*ptr == '#') || strcmp(ptr, key) )
        {
            fprintf(fp_out, "%s", buffer);
            continue;
        }

        is_key_found = LIBSPID_TRUE;
        /* create the new line */
        sprintf(buffer, "%s%c", key, delimiter);
        for (index = 0; index < elt_number; index++)
        {
            /* check for buffer overflow */
            if ( ( strlen(buffer) + 1 /*delimiter*/ + strlen(elt[index]) + 1 /*'\0'*/ ) > LIBSPID_LINE_MAX_LEN )
            {
                fclose(fp_out);
                fclose(fp_in);
#ifdef USE_LOCK
                /* close lock */
                fcntl(fd_in, F_UNLCK, &lock);
#endif /* USE_LOCK */
                close(fd_in);
                return LIBSPID_ERROR_NO_SPACE;
            }
            sprintf(buffer + strlen(buffer), "%s%c", elt[index],  delimiter);
        }
        /* string should end with last element, so erase last delimiter and finish the line */
        buffer[strlen(buffer) - 1] = '\n';
        fprintf(fp_out, "%s", buffer);
    }
    if (!is_key_found)
    {
        /* add new key + element at the end of file */
        sprintf(buffer, "%s%c", key, delimiter);
        for(index = 0; index < elt_number; index++)
        {
            /* check for buffer overflow */
            if ( ( strlen(buffer) + 1 /*delimiter*/ + strlen(elt[index]) + 1 /*'\0'*/ ) > LIBSPID_LINE_MAX_LEN )
            {
                fclose(fp_out);
                fclose(fp_in);
#ifdef USE_LOCK
                /* close lock */
                fcntl(fd_in, F_UNLCK, &lock);
#endif /* USE_LOCK */
                close(fd_in);
                return LIBSPID_ERROR_NO_SPACE;
            }
            sprintf(buffer + strlen(buffer), "%s%c", elt[index], delimiter );
        }
        /* string should end with last element, so erase last delimiter and finish the line */
        buffer[strlen(buffer) - 1] = '\n';
        fprintf(fp_out, "%s", buffer);
    }
    fclose(fp_out);
    fclose(fp_in);
#ifdef USE_LOCK
    /* close lock */
    fcntl(fd_in, F_UNLCK, &lock);
#endif /* USE_LOCK */
    close(fd_in);
    if ( rename(out_filename, filename) < 0 )
    {
        //syslog(LOG_WARNING, "%s : rename (errno=%d)\n", __FUNCTION__, errno);
    }

    return LIBSPID_SUCCESS;
}

/**
 * Remove a line from a configuration file.<BR>
 * Line to remove is referred according to a provided key. A
 * line is composed of a key and several elements, separated by
 * a common delimiter.<BR>
 * The provided key must not be longer than LIBSPID_MAX_KEY_LEN.
 *
 * \param filename full path filename of a configuration file
 * \param delimiters chars used for delimiting key and elements
 * \param key key (index) of the line to remove
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_NOT_FOUND: key not found
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */

libspid_error_t libspid_config_remove_line(const char *filename, const char *delimiters, const char *key)
{
    char buffer[LIBSPID_LINE_MAX_LEN], key_buffer[LIBSPID_KEY_MAX_LEN + 1];
    char out_filename[64];
    char *ptr, *strtok_ctx;
    FILE *fp_in, *fp_out;
    int fd_in;
    int is_key_found = LIBSPID_FALSE;
#ifdef USE_LOCK
    struct flock lock;
#endif /* USE_LOCK */

    /* check input parameters */
    if ( (filename == NULL) || (delimiters == NULL) || (key == NULL) )
    {
        return LIBSPID_ERROR_PARAM;
    }

    /* check sizes */
    if ( (strlen(key) == 0) || (strlen(delimiters) == 0) )
    {
        return LIBSPID_ERROR_PARAM;
    }

    /* check delimiter validity */
    if ( strchr(delimiters, '\n') || strchr(delimiters, '\r') )
    {
        return LIBSPID_ERROR_PARAM;
    }

    /* check key validity */
    if ( strpbrk(key, delimiters) )
    {
        return LIBSPID_ERROR_PARAM;
    }
    if ( strlen(key) > LIBSPID_KEY_MAX_LEN )
    {
        return LIBSPID_ERROR_PARAM;
    }

    /* open config file */
    if ( (fd_in = open(filename, O_RDWR | O_CREAT, 0666)) < 0 )
    {
        //syslog(LOG_WARNING, "%s: cannot open %s (errno=%d)", __FUNCTION__, filename, errno);
        return LIBSPID_ERROR_SYSTEM;
    }
    fp_in = fdopen(fd_in, "r");
    /* create the modified config file */
    sprintf(out_filename, "%sXXXXXX", filename);
    if((fp_out = fdopen(mkstemp(out_filename), "w")) == NULL)
    {
        fclose(fp_in);
        close(fd_in);
        return LIBSPID_ERROR_SYSTEM;
    }

#ifdef USE_LOCK
    /* set the write lock */
    lock.l_type = F_WRLCK;
    lock.l_whence = SEEK_SET;
    lock.l_start = 0;
    lock.l_len = 0;
    if ( fcntl(fd_in, F_SETLKW, &lock) < 0 )
    {
        //syslog(LOG_WARNING, "%s: lock set failed (errno=%d)\n)", __FUNCTION__, errno);
        fclose(fp_out);
        fclose(fp_in);
        close(fd_in);
        return LIBSPID_ERROR_SYSTEM;
    }
#endif /* USE_LOCK */
    /* find the label to modify */
    while ( fgets(buffer, LIBSPID_LINE_MAX_LEN - 1, fp_in) )
    {
        /* fill working copy for key */
        memset(key_buffer, '\0', LIBSPID_KEY_MAX_LEN + 1);
        strncpy(key_buffer, buffer, LIBSPID_KEY_MAX_LEN);
        /* get the index */
        ptr = strtok_r(key_buffer, delimiters, &strtok_ctx);

        if ( (ptr == NULL) || (strlen(ptr) <= 0) || (*ptr == '#') || strcmp(ptr, key) )
        {
            fprintf(fp_out, "%s", buffer);
            continue;
        }

        is_key_found = LIBSPID_TRUE;
    }
    fclose(fp_out);
    fclose(fp_in);
#ifdef USE_LOCK
    /* close lock */
    fcntl(fd_in, F_UNLCK, &lock);
#endif /* USE_LOCK */
    close(fd_in);
    if(is_key_found)
    {
        if ( rename(out_filename, filename) < 0 )
        {
            //syslog(LOG_WARNING, "%s : rename (errno=%d)\n", __FUNCTION__, errno);
        }
    }
    else
    {
        remove(out_filename);
        return LIBSPID_ERROR_NOT_FOUND;
    }
    return LIBSPID_SUCCESS;
}