CS318 - Pintos
Pintos source browser for JHU CS318 course
mk-tree.c
Go to the documentation of this file.
1 /** Library function for creating a tree of directories. */
2 
3 #include <stdio.h>
4 #include <syscall.h>
6 #include "tests/lib.h"
7 
8 static void do_mkdir (const char *format, ...) PRINTF_FORMAT (1, 2);
9 static void do_touch (const char *format, ...) PRINTF_FORMAT (1, 2);
10 
11 void
12 make_tree (int at, int bt, int ct, int dt)
13 {
14  char try[128];
15  int a, b, c, d;
16  int fd;
17 
18  msg ("creating /0/0/0/0 through /%d/%d/%d/%d...",
19  at - 1, bt - 1, ct - 1, dt - 1);
20  quiet = true;
21  for (a = 0; a < at; a++)
22  {
23  do_mkdir ("/%d", a);
24  for (b = 0; b < bt; b++)
25  {
26  do_mkdir ("/%d/%d", a, b);
27  for (c = 0; c < ct; c++)
28  {
29  do_mkdir ("/%d/%d/%d", a, b, c);
30  for (d = 0; d < dt; d++)
31  do_touch ("/%d/%d/%d/%d", a, b, c, d);
32  }
33  }
34  }
35  quiet = false;
36 
37  snprintf (try, sizeof try, "/%d/%d/%d/%d", 0, bt - 1, 0, dt - 1);
38  CHECK ((fd = open (try)) > 1, "open \"%s\"", try);
39  msg ("close \"%s\"", try);
40  close (fd);
41 }
42 
43 static void
44 do_mkdir (const char *format, ...)
45 {
46  char dir[128];
47  va_list args;
48 
49  va_start (args, format);
50  vsnprintf (dir, sizeof dir, format, args);
51  va_end (args);
52 
53  CHECK (mkdir (dir), "mkdir \"%s\"", dir);
54 }
55 
56 static void
57 do_touch (const char *format, ...)
58 {
59  char file[128];
60  va_list args;
61 
62  va_start (args, format);
63  vsnprintf (file, sizeof file, format, args);
64  va_end (args);
65 
66  CHECK (create (file, 0), "create \"%s\"", file);
67 }
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
CHECK
#define CHECK(SUCCESS,...)
Takes an expression to test for SUCCESS and a message, which may include printf-style arguments.
Definition: lib.h:29
file
An open file.
Definition: file.c:7
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
mkdir
bool mkdir(const char *dir)
Definition: syscall.c:163
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
do_mkdir
static void do_mkdir(const char *format,...) PRINTF_FORMAT(1
Library function for creating a tree of directories.
Definition: mk-tree.c:44
do_touch
static void static void do_touch(const char *format,...) PRINTF_FORMAT(1
Definition: mk-tree.c:57
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
close
void close(int fd)
Definition: syscall.c:139
msg
void msg(const char *format,...)
Definition: lib.c:28
dir
A directory.
Definition: directory.c:10
quiet
bool quiet
Definition: lib.c:9
create
bool create(const char *file, unsigned initial_size)
Definition: syscall.c:91
mk-tree.h
PRINTF_FORMAT
#define PRINTF_FORMAT(FMT, FIRST)
Definition: debug.h:10