]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/parse.c
consolidate missing C library functions into utils.h
[mirror_lxc.git] / src / lxc / parse.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #undef _GNU_SOURCE
26 #include <string.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <dirent.h>
30
31 #include "parse.h"
32 #include "config.h"
33 #include "utils.h"
34 #include <lxc/log.h>
35
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
41 int bionic_alphasort(const struct dirent** a, const struct dirent** b) {
42 return strcoll((*a)->d_name, (*b)->d_name);
43 }
44 #endif
45
46
47 lxc_log_define(lxc_parse, lxc);
48
49 static 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
57 int lxc_dir_for_each(const char *name, const char *directory,
58 lxc_dir_cb callback, void *data)
59 {
60 struct dirent **namelist;
61 int n, ret = 0;
62
63 #ifdef IS_BIONIC
64 n = scandir(directory, &namelist, dir_filter, bionic_alphasort);
65 #else
66 n = scandir(directory, &namelist, dir_filter, alphasort);
67 #endif
68 if (n < 0) {
69 SYSERROR("failed to scan %s directory", directory);
70 return -1;
71 }
72
73 while (n--) {
74 if (!ret &&
75 callback(name, directory, namelist[n]->d_name, data)) {
76 ERROR("callback failed");
77 ret = -1;
78 }
79 free(namelist[n]);
80 }
81 free(namelist);
82
83 return ret;
84 }
85
86 int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data)
87 {
88 FILE *f;
89 int err = 0;
90 char *line = NULL;
91 size_t len = 0;
92
93 f = fopen(file, "r");
94 if (!f) {
95 SYSERROR("failed to open %s", file);
96 return -1;
97 }
98
99 while (getline(&line, &len, f) != -1) {
100 err = callback(line, data);
101 if (err)
102 break;
103 }
104
105 if (line)
106 free(line);
107 fclose(f);
108 return err;
109 }
110
111 int 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
123 int 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
137 int 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 }