Debian Codegraph @

libsmartcols/samples/tree.c

raw
1 /*
2 * Copyright (C) 2010-2014 Karel Zak <[email protected]>
3 *
4 * This file may be redistributed under the terms of the
5 * GNU Lesser General Public License.
6 */
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <dirent.h>
14 #include <getopt.h>
15
16 #include "c.h"
17 #include "nls.h"
18 #include "strutils.h"
19 #include "fileutils.h"
20
21 #include "libsmartcols.h"
22
23 static int add_children(struct libscols_table *tb,
24 struct libscols_line *ln, int fd);
25
26
27 enum { COL_MODE, COL_SIZE, COL_NAME };
28
29 struct libscols_column *sort_column;
30
31 /* add columns to the @tb */
32 static void setup_columns(struct libscols_table *tb, int notree)
33 {
34 if (!scols_table_new_column(tb, "MODE", 0.3, 0))
35 goto fail;
36 if (!scols_table_new_column(tb, "SIZE", 5, SCOLS_FL_RIGHT))
37 goto fail;
38
39 sort_column = scols_table_new_column(tb, "NAME", 0.5,
40 (notree ? 0 : SCOLS_FL_TREE) | SCOLS_FL_NOEXTREMES);
41 if (!sort_column)
42 goto fail;
43 scols_column_set_cmpfunc(sort_column, scols_cmpstr_cells, NULL);
44
45 return;
46 fail:
47 scols_unref_table(tb);
48 err(EXIT_FAILURE, "failed to create output columns");
49 }
50
51 /* add a new line to @tb, the content is based on @st */
52 static int add_line_from_stat(struct libscols_table *tb,
53 struct libscols_line *parent,
54 int parent_fd,
55 struct stat *st,
56 const char *name)
57 {
58 struct libscols_line *ln;
59 char modbuf[11], *p;
60 mode_t mode = st->st_mode;
61 int rc = 0;
62
63 ln = scols_table_new_line(tb, parent);
64 if (!ln)
65 err(EXIT_FAILURE, "failed to create output line");
66
67 /* MODE; local buffer, use scols_line_set_data() that calls strdup() */
68 xstrmode(mode, modbuf);
69 if (scols_line_set_data(ln, COL_MODE, modbuf))
70 goto fail;
71
72 /* SIZE; already allocated string, use scols_line_refer_data() */
73 p = size_to_human_string(0, st->st_size);
74 if (!p || scols_line_refer_data(ln, COL_SIZE, p))
75 goto fail;
76
77 /* NAME */
78 if (scols_line_set_data(ln, COL_NAME, name))
79 goto fail;
80
81 /* colors */
82 if (scols_table_colors_wanted(tb)) {
83 struct libscols_cell *ce = scols_line_get_cell(ln, COL_NAME);
84
85 if (S_ISDIR(mode))
86 scols_cell_set_color(ce, "blue");
87 else if (S_ISLNK(mode))
88 scols_cell_set_color(ce, "cyan");
89 else if (S_ISBLK(mode))
90 scols_cell_set_color(ce, "magenta");
91 else if ((mode & S_IXOTH) || (mode & S_IXGRP) || (mode & S_IXUSR))
92 scols_cell_set_color(ce, "green");
93 }
94
95 if (S_ISDIR(st->st_mode)) {
96 int fd;
97
98 if (parent_fd >= 0)
99 fd = openat(parent_fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC);
100 else
101 fd = open(name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC);
102 if (fd >= 0) {
103 rc = add_children(tb, ln, fd);
104 close(fd);
105 }
106 }
107 return rc;
108 fail:
109 err(EXIT_FAILURE, "failed to create cell data");
110 return -1;
111 }
112
113 /* read all entries from directory addressed by @fd */
114 static int add_children(struct libscols_table *tb,
115 struct libscols_line *ln,
116 int fd)
117 {
118 DIR *dir;
119 struct dirent *d;
120
121 dir = fdopendir(fd);
122 if (!dir)
123 return -errno;
124
125 while ((d = readdir(dir))) {
126 struct stat st;
127
128 if (is_dotdir_dirent(d))
129 continue;
130 if (fstatat(fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0)
131 continue;
132 add_line_from_stat(tb, ln, fd, &st, d->d_name);
133 }
134 closedir(dir);
135 return 0;
136 }
137
138 static void add_lines(struct libscols_table *tb, const char *dirname)
139 {
140 struct stat st;
141
142 if (lstat(dirname, &st))
143 err(EXIT_FAILURE, "%s", dirname);
144
145 add_line_from_stat(tb, NULL, -1, &st, dirname);
146 }
147
148 static void __attribute__((__noreturn__)) usage(FILE *out)
149 {
150 fprintf(out, " %s [options] [<dir> ...]\n\n", program_invocation_short_name);
151 fputs(" -c, --csv display a csv-like output\n", out);
152 fputs(" -i, --ascii use ascii characters only\n", out);
153 fputs(" -l, --list use list format output\n", out);
154 fputs(" -n, --noheadings don't print headings\n", out);
155 fputs(" -p, --pairs use key=\"value\" output format\n", out);
156 fputs(" -J, --json use JSON output format\n", out);
157 fputs(" -r, --raw use raw output format\n", out);
158 fputs(" -s, --sort sort by NAME\n", out);
159 fputs(" -x, --tree-sort keep tree-like order (also for --list)\n", out);
160 fputs(" -S, --range-start <n> first line to print\n", out);
161 fputs(" -E, --range-end <n> last line to print\n", out);
162
163 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
164 }
165
166 int main(int argc, char *argv[])
167 {
168 struct libscols_table *tb;
169 int c, notree = 0, nstart = -1, nend = -1, sort = 0, force_tree_sort = 0;
170
171
172 static const struct option longopts[] = {
173 { "ascii", 0, NULL, 'i' },
174 { "csv", 0, NULL, 'c' },
175 { "list", 0, NULL, 'l' },
176 { "noheadings", 0, NULL, 'n' },
177 { "pairs", 0, NULL, 'p' },
178 { "json", 0, NULL, 'J' },
179 { "raw", 0, NULL, 'r' },
180 { "range-start",1, NULL, 'S' },
181 { "range-end", 1, NULL, 'E' },
182 { "sort", 0, NULL, 's' },
183 { "tree-sort", 0, NULL, 'x' },
184 { NULL, 0, NULL, 0 },
185 };
186
187 setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */
188
189 scols_init_debug(0);
190
191 tb = scols_new_table();
192 if (!tb)
193 err(EXIT_FAILURE, "failed to create output table");
194
195 while((c = getopt_long(argc, argv, "ciJlnprS:sE:x", longopts, NULL)) != -1) {
196 switch(c) {
197 case 'c':
198 scols_table_set_column_separator(tb, ",");
199 scols_table_enable_raw(tb, 1);
200 notree = 1;
201 break;
202 case 'i':
203 scols_table_enable_ascii(tb, 1);
204 break;
205 case 'J':
206 scols_table_set_name(tb, "scolstest");
207 scols_table_enable_json(tb, 1);
208 break;
209 case 'l':
210 notree = 1;
211 break;
212 case 'n':
213 scols_table_enable_noheadings(tb, 1);
214 break;
215 case 'p':
216 scols_table_enable_export(tb, 1);
217 notree = 1;
218 break;
219 case 'r':
220 scols_table_enable_raw(tb, 1);
221 notree = 1;
222 break;
223 case 'S':
224 nstart = strtos32_or_err(optarg, "failed to parse range start") - 1;
225 break;
226 case 's':
227 sort = 1;
228 break;
229 case 'E':
230 nend = strtos32_or_err(optarg, "failed to parse range end") - 1;
231 break;
232 case 'x':
233 force_tree_sort = 1;
234 break;
235 default:
236 usage(stderr);
237 }
238 }
239
240 scols_table_enable_colors(tb, isatty(STDOUT_FILENO));
241 setup_columns(tb, notree);
242
243 if (optind == argc)
244 add_lines(tb, ".");
245 else while (optind < argc)
246 add_lines(tb, argv[optind++]);
247
248 if (sort)
249 scols_sort_table(tb, sort_column);
250 if (force_tree_sort)
251 scols_sort_table_by_tree(tb);
252
253 if (nstart >= 0 || nend >= 0) {
254 /* print subset */
255 struct libscols_line *start = NULL, *end = NULL;
256
257 if (nstart >= 0)
258 start = scols_table_get_line(tb, nstart);
259 if (nend >= 0)
260 end = scols_table_get_line(tb, nend);
261
262 if (start || end)
263 scols_table_print_range(tb, start, end);
264 } else
265 /* print all table */
266 scols_print_table(tb);
267
268 scols_unref_table(tb);
269 return EXIT_SUCCESS;
270 }