summaryrefslogtreecommitdiff
path: root/tools/common.h
blob: 4ca27bd8e8c83800861a3e525606f3280bc63dab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#ifndef COMMON_H
#define COMMON_H

#include <bits/errno.h>

#include <stdarg.h>

extern int main(int argc, char **argv, char **envp);

#define NULL ((void *)0)
#define _S(s) (s), (sizeof(s) - 1)
#define CSTR(s) ((struct str) { (s), (s) + sizeof(s) - 1 })
#define STR(s) ((struct str) { (s), (s) + strzlen(s) })

struct str *debug2str(char *s);

struct str {
	char *start;
	char *end;
};

enum {
	heap_saves_max = 16
};

extern int errno;
extern char *progname;

void exit(int code);
int chdir(char *path);
int read(int fd, char *buf, int len);
int write(int fd, char *buf, int len);

#define O_RDONLY        0
#define O_DIRECTORY	0200000

int open(char *fname, int flags, int mode);

/*
 * Taken from linux/arch/x86/include/uapi/asm/stat.h
 * Matches arch/x86_64/bits/stat.h
 */

struct time {
	unsigned long sec;
	unsigned long usec;
};

struct stat {
	unsigned long dev;
	unsigned long ino;
	unsigned long nlink;

	unsigned int mode;
	unsigned int uid;
	unsigned int gid;
	unsigned int __pad0;

	unsigned long rdev;
	unsigned long size;
	unsigned long blksize;
	unsigned long blocks;

	struct time atime;
	struct time mtime;
	struct time ctime;
	long __unused[3];
};

int stat(char *fname, struct stat *st);
int fstat(int fd, struct stat *st);

struct dir {
	int fd;

	char *buf;
	char *cur;
	char *end;

	char *path;
};

struct dirent {
	unsigned long inode;
	unsigned long offset;
	unsigned short dsize;
	unsigned char type;
	char name[1];
};

int opendir(struct dir *dir, char *path);
struct dirent *readdir(struct dir *dir);
void closedir(struct dir *dir);

#define F_OK 0

int access(char *path, int mode);

int dup2(int oldfd, int newfd);
int pipe(int *fds);
int close(int fd);

#define MAP_PRIVATE    0x02
#define MAP_ANON       0x20

#define PROT_NONE      0
#define PROT_READ      1
#define PROT_WRITE     2
#define PROT_EXEC      4

void *mmap(char *addr, unsigned long length, int prot, int flags, int fd,
		unsigned long offset);
int munmap(char *addr, int length);

#define MREMAP_MAYMOVE 1

void *mremap(void *old_addr, unsigned long old_size,
		unsigned long new_size, int flags, void *new_addr);

int fork();

#define WIFEXITED(s) (!WTERMSIG(s))
#define WEXITSTATUS(s) (((s) >> 8) & 0xff)
#define WIFSIGNALED(s) (((s) & 0xffff) -1 < 0xff)
#define WTERMSIG(s) ((s) & 0x7f)

int wait4(int pid, int *wstatus, int options, void *rusage);
int execve(char *path, char **argv, char **envp);

struct timeval {
	unsigned long long sec;
	unsigned long long msec;
};

struct rusage {
	struct timeval utime;
	struct timeval stime;
	long	maxrss;
	long	ixrss;
	long	idrss;
	long	isrss;
	long	minflt;
	long	majflt;
	long	nswap;
	long	inblock;
	long	oublock;
	long	msgsnd;
	long	msgrcv;
	long	nsignals;
	long	nvcsw;
	long	nivcsw;
};

#define RUSAGE_SELF 0

int getrusage(int who, struct rusage *rusage);

void hexit(int code);

extern struct iobuf stdout;
extern struct iobuf stderr;

void print_strz(struct iobuf *buf, char *s);
void print_str(struct iobuf *buf, struct str *s);

void printf(char *fmt, ...);
void eprintf(char *fmt, ...);
void evprintf(char *fmt, va_list vl);
void error(char *fmt, ...);

struct arena;

typedef void arena_grow_func(struct arena *a, int size);

struct arena {
	char *start;
	char *cur;
	char *end;

	arena_grow_func *grow;
};

struct heap {
	struct arena a;

	char *saved[heap_saves_max];
	int snum;

	char *last_alloc;
};

extern struct heap heap;

void heap_offset_save();
void heap_offset_remove();
void heap_offset_restore();

void *mem_alloc(int size);
void *amalloc(struct arena *a, int size);
void *malloc(int size);
void realloc(void *p, int newsize);

int strzlen(char *s);
int strzeq(char *s1, char *s2);

int streq(struct str *s1, struct str *s2);
int strge(struct str *s1, struct str *s2);
void flush(struct iobuf *buf);
void perror(char *s);
void syscall_error(char *fname);

void memmove(char *dest, char *src, int len);
void memcpy(void *restrict dest, void *restrict src, int len);
void memset(void *dest, char c, int len);
int memeq(void *m1, void *m2, int len);

#endif