]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/parse.c
licensing: Add missing headers and FSF address
[mirror_lxc.git] / src / lxc / parse.c
CommitLineData
cb698658 1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
9afe19d6 7 * Daniel Lezcano <daniel.lezcano at free.fr>
cb698658 8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
250b1eec 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
cb698658 22 */
5fa5aa7c 23#define _GNU_SOURCE
cb698658 24#include <stdio.h>
5fa5aa7c
CC
25#undef _GNU_SOURCE
26#include <string.h>
cb698658 27#include <stdlib.h>
28#include <errno.h>
29#include <dirent.h>
30
31#include "parse.h"
1ba0013f 32#include "config.h"
6a44839f 33#include "utils.h"
36eb9bde
CLG
34#include <lxc/log.h>
35
da9dd0f1
SG
36/* Workaround for the broken signature of alphasort() in bionic.
37 This was fixed upstream in 40e467ec668b59be25491bd44bf348a884d6a68d so the
38 workaround can probably be dropped with the next version of the Android NDK.
39 */
40#ifdef IS_BIONIC
41int bionic_alphasort(const struct dirent** a, const struct dirent** b) {
42 return strcoll((*a)->d_name, (*b)->d_name);
43}
44#endif
45
46
36eb9bde 47lxc_log_define(lxc_parse, lxc);
cb698658 48
49static int dir_filter(const struct dirent *dirent)
50{
51 if (!strcmp(dirent->d_name, ".") ||
52 !strcmp(dirent->d_name, ".."))
53 return 0;
54 return 1;
55}
56
57int lxc_dir_for_each(const char *name, const char *directory,
58 lxc_dir_cb callback, void *data)
59{
60 struct dirent **namelist;
8eec72f7 61 int n, ret = 0;
cb698658 62
da9dd0f1
SG
63#ifdef IS_BIONIC
64 n = scandir(directory, &namelist, dir_filter, bionic_alphasort);
65#else
cb698658 66 n = scandir(directory, &namelist, dir_filter, alphasort);
da9dd0f1 67#endif
cb698658 68 if (n < 0) {
36eb9bde 69 SYSERROR("failed to scan %s directory", directory);
cb698658 70 return -1;
71 }
72
73 while (n--) {
8eec72f7
CC
74 if (!ret &&
75 callback(name, directory, namelist[n]->d_name, data)) {
36eb9bde 76 ERROR("callback failed");
8eec72f7 77 ret = -1;
cb698658 78 }
79 free(namelist[n]);
80 }
8eec72f7 81 free(namelist);
cb698658 82
8eec72f7 83 return ret;
cb698658 84}
85
2382ecff 86int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data)
cb698658 87{
88 FILE *f;
4fcc0112 89 int err = 0;
2382ecff
CC
90 char *line = NULL;
91 size_t len = 0;
cb698658 92
93 f = fopen(file, "r");
94 if (!f) {
36eb9bde 95 SYSERROR("failed to open %s", file);
cb698658 96 return -1;
97 }
98
2382ecff
CC
99 while (getline(&line, &len, f) != -1) {
100 err = callback(line, data);
75b08ddd 101 if (err)
2382ecff 102 break;
cb698658 103 }
2382ecff
CC
104
105 if (line)
106 free(line);
cb698658 107 fclose(f);
108 return err;
109}
110
111int lxc_char_left_gc(char *buffer, size_t len)
112{
113 int i;
114 for (i = 0; i < len; i++) {
115 if (buffer[i] == ' ' ||
116 buffer[i] == '\t')
117 continue;
118 return i;
119 }
120 return 0;
121}
122
123int lxc_char_right_gc(char *buffer, size_t len)
124{
125 int i;
126 for (i = len - 1; i >= 0; i--) {
127 if (buffer[i] == ' ' ||
128 buffer[i] == '\t' ||
129 buffer[i] == '\n' ||
130 buffer[i] == '\0')
131 continue;
132 return i + 1;
133 }
134 return 0;
135}
136
137int lxc_is_line_empty(char *line)
138{
139 int i;
140 size_t len = strlen(line);
141
142 for (i = 0; i < len; i++)
143 if (line[i] != ' ' && line[i] != '\t' &&
144 line[i] != '\n' && line[i] != '\r' &&
145 line[i] != '\f' && line[i] != '\0')
146 return 0;
147 return 1;
148}