summaryrefslogtreecommitdiff
path: root/tools/common.h
blob: 43e2dda02ac31e8fa8b67931b521ede76e3ba191 (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
#ifndef COMMON_H
#define COMMON_H

#include <bits/errno.h>

#include <stdarg.h>

#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 {
	char *start;
	char *end;
};

extern int errno;
extern char *progname;

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

#define O_RDONLY        0

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);

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

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

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 save_heap_offset();
void restore_heap_offset();
void *malloc(int size);

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

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

void memmove(char *dest, char *src, int len);
void memcpy(void *dest, void *src, int len);
void memset(void *dest, char c, int len);
#endif