1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
#include <stdio.h>
#include <stdlib.h> /* calloc */
#include <string.h> /* memmove */
#include <unistd.h> /* chdir */
#include "shared.h"
static char *read_dir(FILE *dir_dump, char *buf)
{
static char *contents = NULL;
int read, size = 0;
while ((read = fread(buf, 1, bufsize, dir_dump)) > 0) {
size += read;
contents = realloc(contents, size);
memmove(&contents[size-read], buf, read);
}
return contents;
}
const char *key_delim = "\":";
/* Return character pointer to beginning of value. */
char *
getval(char *str, char *key)
{
key = realloc(key, strlen(key) + strlen(key_delim) + 1);
strcat(key, key_delim);
char *rt = strstr(str, key);
if (rt == NULL) {
fprintf(stderr, "%s\n", "Invalid key.\n");
}
return rt;
}
/* Count directories. */
int
count_dir(char *contents)
{
int count = 0;
char *p = contents;
while ((p = strstr(p, "\n")) != NULL) {
count++;
p = &p[1]; /* Increment p past the current '\n'. */
}
return count;
}
int
main(int argc, char **argv)
{
char *path = NULL;
if (argc > 1) {
path = argv[1];
}
chdir("dump");
FILE *root_stream = fopen("folders", "r");
if (root_stream == NULL) {
fprintf(stderr, "Generate a dump of your Canvas"
" account directory structure"
" using dump.sh.\n");
return 1;
}
char buf[bufsize];
char *root_contents = read_dir(root_stream, buf);
int root_count = count_dir(root_contents);
printf("%d\n", root_count);
return 0;
}
|