xv6, line by line
user/user.h

user/user.h

C header · 50 lines · annotated 100% · user program / library · upstream

Included by 23 filesuser/cat.c, user/dorphan.c, user/echo.c, user/forktest.c, user/forphan.c, user/grep.c, user/grind.c, user/init.c, user/kill.c, user/ln.c, user/logstress.c, user/ls.c, user/mkdir.c, user/printf.c, user/rm.c, user/sh.c, user/stressfs.c, user/sync.c, user/ulib.c, user/umalloc.c, user/usertests.c, user/wc.c, user/zombie.c
About this file

The one header every xv6 user program includes: the declarations of everything a program can call. It plays the role that <unistd.h>, <string.h>, <stdio.h> and <stdlib.h> play on a Unix system, in 50 lines.

It has four groups, each matching the file that defines it:

Lines Group Defined in
6–27 system calls user/usys.S (generated stubs)
30–42 string and helper functions user/ulib.c
45–46 formatted output user/printf.c
49–50 memory allocation user/umalloc.c

For a system call, the declaration is all the C compiler sees; the definition is a three-instruction system call stub that traps into the kernel. Each line note below names the kernel function that does the real work, so you can follow any call down, for example fork() → stub → ecall → syscall → sys_fork → kfork.

The header is not self-contained: it uses the type uint without defining it, so programs include kernel/types.h first. A program that reads the fields of a struct stat also includes kernel/stat.h; for the prototypes, the declaration on line 3 is enough.

Read next: user/usys.pl and user/usys.S.

1#define SBRK_ERROR ((char *)-1)
3struct stat;
5// system calls
6int fork(void);
7int exit(int) __attribute__((noreturn));
8int wait(int *);
9int pipe(int *);
10int write(int, const void *, int);
11int read(int, void *, int);
12int close(int);
13int kill(int);
14int exec(const char *, char **);
15int open(const char *, int);
16int mknod(const char *, short, short);
17int unlink(const char *);
18int fstat(int fd, struct stat *);
19int link(const char *, const char *);
20int mkdir(const char *);
21int chdir(const char *);
22int dup(int);
23int getpid(void);
24char *sys_sbrk(int, int);
25int pause(int);
26int uptime(void);
27int sync(void);
29// ulib.c
30int stat(const char *, struct stat *);
31char *strcpy(char *, const char *);
32void *memmove(void *, const void *, int);
33char *strchr(const char *, char c);
34int strcmp(const char *, const char *);
35char *gets(char *, int max);
36uint strlen(const char *);
37void *memset(void *, int, uint);
38int atoi(const char *);
39int memcmp(const void *, const void *, uint);
40void *memcpy(void *, const void *, uint);
41char *sbrk(int);
42char *sbrklazy(int);
44// printf.c
45void fprintf(int, const char *, ...) __attribute__((format(printf, 2, 3)));
46void printf(const char *, ...) __attribute__((format(printf, 1, 2)));
48// umalloc.c
49void *malloc(uint);
50void free(void *);