summaryrefslogtreecommitdiffstats
path: root/hw2/xwrap.h
blob: 05504c34f4a90e0e01f41a187106966bc468543d (plain) (blame)
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
#ifndef X_GENERAL_WRAPPER
#define X_GENERAL_WRAPPER

#include <stdbool.h>
#include <stdlib.h>

#define STATIC_STRLEN(x) (sizeof(x)/sizeof(char) - 1)
#define ARRAY_LEN(x,t)   (sizeof(x)/sizeof(t))

int     xatol (const char* str, long* result);
void*   xmalloc (size_t size);
void*   xrealloc (void* ptr, size_t size);
bool    xstrend (const char* str, const char* suffix);
char*   xstrcat (const char* str, ...);
char*   xstrdup (const char* str);
char*   xsprintf (const char* format, ...);
int     xfaddfd (int fd, int fdflags);
int     xfdelfd (int fd, int fdflags);
int     xfaddfl (int fd, int flflags);
int     xfdelfl (int fd, int flflags);
char*   xreadlink (const char* filename);
char*   xgetcwd (void);
char*   xgetexe (void);
char*   xgetres (const char* filename);
size_t  xwrite (int fd, const char* str, size_t size);

static inline int xmax (int a, int b) {
    return a > b ? a : b;
}
static inline int xmin (int a, int b) {
    return a < b ? a : b;
}

#define XBUFSIZ 512

typedef struct {
    char    buf[XBUFSIZ];
    off_t   buf_start;
    off_t   buf_len;
    char*   buf_line;
    ssize_t buf_line_len;
    int     buf_error : 1;
    int     buf_eof   : 1;
} XBuf;

void    xbufinit (XBuf* buf);
char*   xgetline (int fd, XBuf* buf, int delim);

#endif /* X_GENERAL_WRAPPER */