5 #include <sys/wait.h> /* for waitpid() macros */
6 #include <prop/proplib.h>
8 #define INIT_CAPACITY 100
15 char *tokens
[MAX_TOKENS
]; /* for ``du'' output parse */
22 /* Initiate pipe stream to ``du'' */
23 fp
= popen("du", "r");
29 /* Create dictionary */
30 pd
= prop_dictionary_create_with_capacity(INIT_CAPACITY
);
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 */
40 p
= strtok_r(str
, "\t", &last
);
41 while (p
&& i
< MAX_TOKENS
- 1) {
43 p
= strtok_r(NULL
, "\t", &last
);
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 * Note: atoi() does not detect errors, but we trust
56 * ``du'' to provide us with valid input. Otherwise,
57 * we should use strtol(3) or sscanf(3).
59 pn
= prop_number_create_integer(atoi(tokens
[0]));
61 /* Add a <path, size> pair in our dictionary */
62 if (prop_dictionary_set(pd
, tokens
[1], pn
) == FALSE
) {
63 prop_object_release(pn
);
64 prop_object_release(pd
);
65 errx(EXIT_FAILURE
, "prop_dictionary_set()");
68 /* Release prop_number_t object */
69 prop_object_release(pn
);
72 /* Externalize dictionary to file in XML representation */
73 if (prop_dictionary_externalize_to_file(pd
, "./data.xml") == FALSE
) {
74 prop_object_release(pd
);
75 errx(EXIT_FAILURE
, "prop_dictionary_externalize_to_file()");
78 /* Release dictionary */
79 prop_object_release(pd
);
81 /* Close pipe stream */