Ungraded Practice
This volume is ungraded and is just for your practice
| Exercise | Focus |
|---|---|
| 0. Getting Statistics of a Directory | Count folders and files, and sum file sizes |
| 1. Listing All Pathnames | Print the full paths in a directory tree |
| 2. Finding A Directory | Find a folder or file by its path |
Introduction
The best way to overcome our fear of trees is to expose ourselves to more trees. So here are 3 more basic questions to work on trees. 1

Background
We’re going to work on more directory related tasks since it’s the closest motivation that you might have seen in recent history.
For the following tasks, we will presume that paths always start with ./. If a path is specified without ./, it will be automatically added for you.
For the sake of simplicity, let’s say we will only allow alphabetical and numerical characters, dashes -, and underscores _ in our filenames. Importantly, $ should never be part of a filename.
In the input listing used to build the directory, when a pathname ends in a /, it must specify a folder. On the other hand, when a pathname does not end in /, it must specify a file.
For all of the following tasks, since we are the ones reading the input and preparing the directories, we have given it to you in the following struct:
typedef struct Directory {
char *name;
size_t num_children;
struct Directory *subdirectories;
bool is_file;
size_t file_size;
} Directory;Hopefully the struct field names are self-explanatory. You should also think of subdirectories as an array, where there are num_children many elements. name is a null-terminated string that is either the name of the folder or the name of the corresponding file.
The base of the directory will always begin with a folder named . and thus all paths are assumed to always be prefaced with ./.
For example, given the following list of pathnames:
a/b/c 500
a/b/d/e 600
a/aoeu 200
aoeu 1
asdf/
This corresponds to the following tree:
(Sorry if the lines are shiddy, excalidraw doesn’t have a brace shape.)
0. Getting Statistics of a Directory
Given a directory in the form of the struct above, populate the following struct:
typedef struct Stat {
size_t num_folders;
size_t num_files;
size_t total_size;
} Stat;Again, hopefully the struct field names are self-explanatory, we want the total number of folders, the number of files, and total size of all the files summed up in the directory.
For example, based on the example directory given in the background section, we should have:
num folders: 5
num files: 4
total size: 1301
Your task: Given the function below, implement the required behaviour. Just populate the fields of the struct with the correct values and we will print it for you.
Stat dir_stat(Directory const *dir) {
Stat stat = {0};
// Your code here
return stat;
}Providing input:
To provide input, write out your list of pathnames followed by $ on the final line to denote the end of your input. If it’s a file, follow it up with a space and the filesize. If it’s a folder, be sure it ends with a /.
For example:
a/b/c 500
a/b/d/e 600
a/aoeu 200
aoeu 1
asdf/
$
1. Listing All Pathnames
You’re given a directory in the form of a struct Directory. Print out all the full possible pathnames.
For example, based on the directory given in the background, the desired output should be:
./
./a/
./a/aoeu 200
./a/b/
./a/b/c 500
./a/b/d/
./a/b/d/e 600
./aoeu 1
./asdf/
Your task: Given the function below, implement the required behaviour.
void pretty_print(Directory const *dir) {
}Providing input:
To provide input, write out your list of pathnames followed by $ on the final line to denote the end of your input. If it’s a file, follow it up with a space and the filesize. If it’s a folder, be sure it ends with a /.
For example:
a/b/c 500
a/b/d/e 600
a/aoeu 200
aoeu 1
asdf/
$
Note: We will make sure that pathnames are sorted. This way, every sub-directory will be arranged lexicographically. Intuitively, think of this as being arranged in “dictionary order” where the names are arranged “alphabetically”.2
This makes things slightly simpler for you and us. For you, you only have to iterate in order from the first to last sub-directory. For us, it’s easier to test because the output is normalised3.
2. Finding A Directory
You’re given a directory in the form of a struct Directory, and another pathname. Return a pointer to the Directory that corresponds to that pathname. Otherwise, return nullptr if it doesn’t exist.
For example, given the directory in the background section, if we tried looking for the directory . itself, then we should just return the pointer to that corresponding node.
As a slightly more elaborate example, if we wish to look for ./a/, this specifies the folder a right below the root. So you’re required to return the pointer to that node. And upon doing so, we will print out its contents for you. For example, upon returning the correct pointer, we will print:
a
+---aoeu 200
\---b
+---c 500
\---d
\---e 600
Note that if the pathname you’re looking for has a trailing slash '/', then you are looking for a folder. Whereas if the pathname does not end on a slash, then you are looking for either a folder or a file.
Your task: Given the function below, implement the required behaviour.
Directory *find(Directory *basedir, char const *path) {
}Providing input:
To provide input, write out your list of pathnames followed by $ to denote the end of the pathnames. Then, on the very final line, enter the pathname you wish to search for. Adding a leading ./ to any pathname is optional.