xv6, line by line
user/mkdir.c

user/mkdir.c

C · 23 lines · annotated 100% · user program / library · upstream

About this file

mkdir name ... creates one empty directory for each name: mkdir d makes d, containing only the entries . (itself) and .. (its parent). The whole job is done by one system call per name, mkdir, whose kernel side is sys_mkdir. That calls create with type T_DIR, which allocates an inode, adds the . and .. entries, and links the new directory into its parent, all inside one file system transaction so a crash cannot leave a half-made directory.

Read before: user/cat.c (argument handling). Read next: user/rm.c and user/ln.c, which have the same shape.

5int
6main(int argc, char *argv[])
7{
8 int i;
10 if (argc < 2) {
11 fprintf(2, "Usage: mkdir files...\n");
12 exit(1);
13 }
15 for (i = 1; i < argc; i++) {
16 if (mkdir(argv[i]) < 0) {
17 fprintf(2, "mkdir: %s failed to create\n", argv[i]);
18 break;
19 }
20 }
22 exit(0);