Minor cleanup
[eleutheria.git] / proplib / prop_du.c
blob4eae7f43abe16bd41fa944d925445a46c447ce17
1 #include <err.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/wait.h> /* for waitpid() macros */
6 #include <prop/proplib.h>
8 #define INIT_CAPACITY 100
9 #define MAX_STR 100
10 #define MAX_TOKENS 3
12 int main(void)
14 char str[MAX_STR];
15 char *tokens[MAX_TOKENS]; /* for ``du'' output parse */
16 char *last, *p;
17 int i, ret;
18 FILE *fp;
19 prop_dictionary_t pd;
20 prop_number_t pn;
22 /* Initiate pipe stream to ``du'' */
23 fp = popen("du", "r");
24 if (fp == NULL) {
25 perror("popen()");
26 exit(EXIT_FAILURE);
29 /* Create dictionary */
30 pd = prop_dictionary_create_with_capacity(INIT_CAPACITY);
31 if (pd == NULL) {
32 pclose(fp);
33 errx(EXIT_FAILURE, "prop_dictionary_create_with_capacity()");
36 /* Read from stream */
37 while (fgets(str, MAX_STR, fp) != NULL) {
38 /* Parse output of ``du'' command */
39 i = 0;
40 for ((p = strtok_r(str, "\t", &last)); p;
41 (p = strtok_r(NULL, "\t", &last)), i++) {
42 if (i < MAX_TOKENS - 1)
43 tokens[i] = p;
45 tokens[i] = NULL;
47 /* Trim '\n' from tokens[1] */
48 (tokens[1])[strlen(tokens[1]) - 1] = '\0';
51 * We use a signed prop_number_t object, so that
52 * when externalized it will be represented as decimal
53 * (unsigned numbers are externalized in base-16)
55 pn = prop_number_create_integer(atoi(tokens[0]));
56 prop_dictionary_set(pd, tokens[1], pn);
57 prop_object_release(pn);
60 prop_dictionary_externalize_to_file(pd, "./data.xml");
61 prop_object_release(pd);
63 /* Close pipe stream */
64 ret = pclose(fp);
65 if (ret == -1) {
66 perror("pclose()");
67 exit(EXIT_FAILURE);
70 return EXIT_SUCCESS;