]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/parse.c
remove warning in parse.c:80
[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 <dlezcano at fr.ibm.com>
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 <lxc/log.h>
33
34 lxc_log_define(lxc_parse, lxc);
35
36 static int dir_filter(const struct dirent *dirent)
37 {
38 if (!strcmp(dirent->d_name, ".") ||
39 !strcmp(dirent->d_name, ".."))
40 return 0;
41 return 1;
42 }
43
44 int lxc_dir_for_each(const char *name, const char *directory,
45 lxc_dir_cb callback, void *data)
46 {
47 struct dirent **namelist;
48 int n, ret = 0;
49
50 n = scandir(directory, &namelist, dir_filter, alphasort);
51 if (n < 0) {
52 SYSERROR("failed to scan %s directory", directory);
53 return -1;
54 }
55
56 while (n--) {
57 if (!ret &&
58 callback(name, directory, namelist[n]->d_name, data)) {
59 ERROR("callback failed");
60 ret = -1;
61 }
62 free(namelist[n]);
63 }
64 free(namelist);
65
66 return ret;
67 }
68
69 int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data)
70 {
71 FILE *f;
72 int err = 0;
73 char *line = NULL;
74 size_t len = 0;
75
76 f = fopen(file, "r");
77 if (!f) {
78 SYSERROR("failed to open %s", file);
79 return -1;
80 }
81
82 while (getline(&line, &len, f) != -1) {
83 err = callback(line, data);
84 if (err) {
85 ERROR("failed to process '%s'", line);
86 break;
87 }
88 }
89
90 if (line)
91 free(line);
92 fclose(f);
93 return err;
94 }
95
96 int lxc_char_left_gc(char *buffer, size_t len)
97 {
98 int i;
99 for (i = 0; i < len; i++) {
100 if (buffer[i] == ' ' ||
101 buffer[i] == '\t')
102 continue;
103 return i;
104 }
105 return 0;
106 }
107
108 int lxc_char_right_gc(char *buffer, size_t len)
109 {
110 int i;
111 for (i = len - 1; i >= 0; i--) {
112 if (buffer[i] == ' ' ||
113 buffer[i] == '\t' ||
114 buffer[i] == '\n' ||
115 buffer[i] == '\0')
116 continue;
117 return i + 1;
118 }
119 return 0;
120 }
121
122 int lxc_is_line_empty(char *line)
123 {
124 int i;
125 size_t len = strlen(line);
126
127 for (i = 0; i < len; i++)
128 if (line[i] != ' ' && line[i] != '\t' &&
129 line[i] != '\n' && line[i] != '\r' &&
130 line[i] != '\f' && line[i] != '\0')
131 return 0;
132 return 1;
133 }