CS318 - Pintos
Pintos source browser for JHU CS318 course
dir-rm-tree.c
Go to the documentation of this file.
1 /** Creates directories /0/0/0 through /3/2/2 and files in the
2  leaf directories, then removes them. */
3 
4 #include <stdarg.h>
5 #include <stdio.h>
6 #include <syscall.h>
8 #include "tests/lib.h"
9 #include "tests/main.h"
10 
11 static void remove_tree (int at, int bt, int ct, int dt);
12 
13 void
14 test_main (void)
15 {
16  make_tree (4, 3, 3, 4);
17  remove_tree (4, 3, 3, 4);
18 }
19 
20 static void do_remove (const char *format, ...) PRINTF_FORMAT (1, 2);
21 
22 static void
23 remove_tree (int at, int bt, int ct, int dt)
24 {
25  char try[128];
26  int a, b, c, d;
27 
28  msg ("removing /0/0/0/0 through /%d/%d/%d/%d...",
29  at - 1, bt - 1, ct - 1, dt - 1);
30  quiet = true;
31  for (a = 0; a < at; a++)
32  {
33  for (b = 0; b < bt; b++)
34  {
35  for (c = 0; c < ct; c++)
36  {
37  for (d = 0; d < dt; d++)
38  do_remove ("/%d/%d/%d/%d", a, b, c, d);
39  do_remove ("/%d/%d/%d", a, b, c);
40  }
41  do_remove ("/%d/%d", a, b);
42  }
43  do_remove ("/%d", a);
44  }
45  quiet = false;
46 
47  snprintf (try, sizeof (try), "/%d/%d/%d/%d", at - 1, 0, ct - 1, 0);
48  CHECK (open (try) == -1, "open \"%s\" (must return -1)", try);
49 }
50 
51 static void
52 do_remove (const char *format, ...)
53 {
54  char name[128];
55  va_list args;
56 
57  va_start (args, format);
58  vsnprintf (name, sizeof name, format, args);
59  va_end (args);
60 
61  CHECK (remove (name), "remove \"%s\"", name);
62 }
name
char * name[]
Definition: insult.c:47
lib.h
snprintf
int snprintf(char *buffer, size_t buf_size, const char *format,...)
Like printf(), except that output is stored into BUFFER, which must have space for BUF_SIZE character...
Definition: stdio.c:62
va_end
#define va_end(LIST)
Definition: stdarg.h:10
va_start
#define va_start(LIST, ARG)
Definition: stdarg.h:9
test_main
void test_main(void)
tests/main.h
Definition: dir-rm-tree.c:14
CHECK
#define CHECK(SUCCESS,...)
Takes an expression to test for SUCCESS and a message, which may include printf-style arguments.
Definition: lib.h:29
make_tree
static void static void void make_tree(int at, int bt, int ct, int dt)
tests/filesys/extended/mk-tree.h
Definition: mk-tree.c:12
stdarg.h
remove
bool remove(const char *file)
Definition: syscall.c:97
open
int open(const char *file)
Definition: syscall.c:103
va_list
__builtin_va_list va_list
GCC has <stdarg.h> functionality as built-ins, so all we need is to use it.
Definition: stdarg.h:7
vsnprintf
int vsnprintf(char *buffer, size_t buf_size, const char *format, va_list args)
Like vprintf(), except that output is stored into BUFFER, which must have space for BUF_SIZE characte...
Definition: stdio.c:26
main.h
msg
void msg(const char *format,...)
Definition: lib.c:28
do_remove
static void do_remove(const char *format,...) PRINTF_FORMAT(1
Definition: dir-rm-tree.c:52
remove_tree
static void remove_tree(int at, int bt, int ct, int dt)
Creates directories /0/0/0 through /3/2/2 and files in the leaf directories, then removes them.
Definition: dir-rm-tree.c:23
quiet
bool quiet
Definition: lib.c:9
mk-tree.h
PRINTF_FORMAT
#define PRINTF_FORMAT(FMT, FIRST)
Definition: debug.h:10