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