summaryrefslogtreecommitdiffstats
path: root/hw2/xwrap.c
blob: 4f0d92b10668b2ccc236e8dcf9cae22cc744d43d (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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "xwrap.h"

#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#ifdef OS_IS_FREEBSD
# include <sys/sysctl.h>
#endif

#define RETRY_SEC     0
#define RETRY_NSEC    250000000

static const char fail_msg[] = "Fail to allocate memory. Retry ...\n";
static const size_t fail_len = STATIC_STRLEN (fail_msg);

int xatol (const char* str, long* result) {
    int errno_save, rval;
    long lres;
    char* endptr;

    errno_save = errno;
    errno = 0, rval = 0;

    lres = strtol (str, &endptr, 10);
    if (str == endptr || errno != 0) {
        rval = -1;
    } else {
        *result = lres;
    }

    errno = errno_save;

    return rval;
}

void* xmalloc (size_t size) {
    void* memptr;

    while ((memptr = malloc (size)) == NULL) {
        nanosleep (&(struct timespec) { RETRY_SEC, RETRY_NSEC }, NULL);
        write (STDERR_FILENO, fail_msg, fail_len);
    }

    return memptr;
}

void* xrealloc (void* ptr, size_t size) {
    void* newptr;

    while ((newptr = realloc (ptr, size)) == NULL) {
        nanosleep (&(struct timespec) { RETRY_SEC, RETRY_NSEC }, NULL);
        write (STDERR_FILENO, fail_msg, fail_len);
    }

    return newptr;
}

bool xstrend (const char* str, const char* suffix) {
    size_t len = strlen (str);
    size_t suflen = strlen (suffix);
    int i, j;
    for (i = len - 1, j = suflen - 1; i >= 0 && j >= 0; i--, j--) {
        if (str[i] != suffix[j]) {
            return false;
        }
    }
    if (i < 0 && j >= 0) {
        return false;
    }
    return true;
}

char* xstrcat (const char* str, ...) {
    va_list ap;
    char* strp;
    char* strnow;
    char* newstr;
    int len = strlen (str);

    va_start (ap, str);
    while ((strp = va_arg (ap, char*)) != NULL) {
        len += strlen (strp);
    }
    va_end (ap);

    newstr = xmalloc (len + 1);

    va_start (ap, str);
    strnow = stpcpy (newstr, str);
    while ((strp = va_arg (ap, char*)) != NULL) {
        strnow = stpcpy (strnow, strp);
    }
    newstr[len] = '\0';
    va_end (ap);

    return newstr;
}

char* xstrdup (const char* str) {
    char* newstr;

    while ((newstr = strdup (str)) == NULL) {
        nanosleep (&(struct timespec) { RETRY_SEC, RETRY_NSEC }, NULL);
        write (STDERR_FILENO, fail_msg, fail_len);
    }

    return newstr;
}

char* xsprintf (const char* format, ...) {
    va_list ap;
    char* newstr;
    int len;

    va_start (ap, format);
    len = vsnprintf (NULL, 0, format, ap) + 1;
    va_end (ap);

    newstr = xmalloc (len);

    va_start (ap, format);
    vsnprintf (newstr, len, format, ap);
    va_end (ap);

    return newstr;
}

int xfaddfd (int fd, int fdflags) {
    int orgfd = fcntl (fd, F_GETFD);
    if (orgfd < 0) {
        return -1;
    }
    return fcntl (fd, F_SETFD, orgfd | fdflags);
}

int xfdelfd (int fd, int fdflags) {
    int orgfd = fcntl (fd, F_GETFD);
    if (orgfd < 0) {
        return -1;
    }
    return fcntl (fd, F_SETFD, orgfd & ~(fdflags));
}

int xfaddfl (int fd, int flflags) {
    int orgfl = fcntl (fd, F_GETFL);
    if (orgfl < 0) {
        return -1;
    }
    return fcntl (fd, F_SETFL, orgfl | flflags);
}

int xfdelfl (int fd, int flflags) {
    int orgfl = fcntl (fd, F_GETFL);
    if (orgfl < 0) {
        return -1;
    }
    return fcntl (fd, F_SETFL, orgfl & ~(flflags));
}

char* xreadlink (const char* filename) {
    struct stat st;
    if (lstat (filename, &st) < 0) {
        return NULL;
    }
    size_t bufsize = st.st_size ? st.st_size : 8192;
    char* buf = malloc (bufsize + 1);
    if (buf == NULL) {
        return NULL;
    }
    ssize_t written = readlink (filename, buf, bufsize);
    if (written < 0) {
        free (buf);
        return NULL;
    }
    buf[written] = '\0';
    return buf;
}

char* xgetcwd (void) {
    char *cwd, *result;
    size_t size = pathconf (".", _PC_PATH_MAX);

    size = size > 0 ? size : 256;
    cwd = xmalloc (sizeof (char) * size);

    while ((result = getcwd (cwd, size)) == NULL && errno == ERANGE) {
        size *= 2;
        cwd = xrealloc (cwd, size);
    }

    return cwd;
}

char* xgetexe (void) {
    char* myexec;

#ifdef OS_IS_FREEBSD
    int fb_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
    int fb_rval;
    size_t fb_size = 256;
    myexec = xmalloc (fb_size);
    while ((fb_rval = sysctl (fb_mib, 4, myexec, &fb_size, NULL, 0)) < 0 &&
         errno == ENOMEM) {

        fb_size *= 2;
        myexec = xrealloc (myexec, fb_size);
    }
    if (fb_rval >= 0) {
        return myexec;
    } else {
        free (myexec);
    }
#endif

    if ((myexec = xreadlink ("/proc/self/exe")) != NULL) {
        return myexec;
    }
    if ((myexec = xreadlink ("/proc/curproc/exe")) != NULL) {
        return myexec;
    }
    if ((myexec = xreadlink ("/proc/curproc/file")) != NULL) {
        return myexec;
    }

    return NULL;
}

char* xgetres (const char* filename) {
    char *myexec, *myres;
    bool myexec_static = false;

    myexec = xgetexe ();
    if (myexec == NULL) {
        myexec = xgetcwd ();
        if (myexec == NULL) {
            myexec = "./";
            myexec_static = true;
        }
    } else {
        char* dirsep = strrchr (myexec, '/');
        if (dirsep != NULL && dirsep != myexec) {
            *dirsep = '\0';
        }
    }

    if (xstrend (myexec, "/")) {
        myres = xstrcat (myexec, filename, (char*)NULL);
    } else {
        myres = xstrcat (myexec, "/", filename, (char*)NULL);
    }
    if (!myexec_static) {
        free (myexec);
    }

    return myres;
}

size_t xwrite (int fd, const char* str, size_t size) {
    ssize_t wtn = 0;
    if (size <= 0) {
        size = strlen (str);
    }
    size_t rem = size;
    while (rem > 0) {
        wtn = write (fd, str, rem);
        if (wtn < 0) {
            if (errno != EINTR && errno != EAGAIN) {
                break;
            }
            continue;
        }
        str += wtn;
        rem -= wtn;
    }

    rem = rem > 0 ? rem : 0;
    return size - rem;
}

void xbufinit (XBuf* buf) {
    buf->buf_start = 0;
    buf->buf_len = 0;
    buf->buf_line = NULL;
    buf->buf_line_len = 0;
    buf->buf_error = false;
    buf->buf_eof = false;
}

char* xgetline (int fd, XBuf* buf, int delim) {
    if (buf->buf_error || buf->buf_eof) {
        return NULL;
    }

    if (buf->buf_len == 0) {
        int rval = read (fd, buf->buf, XBUFSIZ);
        if (rval < 0) {
            if (errno != EAGAIN && errno != EINTR) {
                buf->buf_error = true;
            }
            return NULL;
        }
        if (rval == 0) {
            buf->buf_eof = true;
            return NULL;
        }
        buf->buf_start = 0;
        buf->buf_len = rval;
    }

    int i;
    for (i = 0; i < buf->buf_len; i++) {
        if (buf->buf[buf->buf_start + i] == delim) {
            break;
        }
    }

    int buf_line_start = buf->buf_line_len;
    buf->buf_line_len += i;
    buf->buf_line = xrealloc (buf->buf_line, buf->buf_line_len + 1);
    memcpy (buf->buf_line + buf_line_start, buf->buf + buf->buf_start, i);
    buf->buf_line[buf->buf_line_len] = '\0';

    /* remove CR if delim is LF and delim is found */
    if (i < buf->buf_len && delim == '\n' && buf->buf_line_len - 1 >= 0 &&
        buf->buf_line[buf->buf_line_len - 1] == '\r') {
        buf->buf_line[buf->buf_line_len - 1] = '\0';
        buf->buf_line_len--;
    }

    int buf_len_saved = buf->buf_len;
    buf->buf_start += i + 1;
    buf->buf_len -= i + 1;
    if (buf->buf_len <= 0) {
        buf->buf_start = 0;
        buf->buf_len = 0;
    }

    if (i < buf_len_saved) {
        /* delim is found */
        char* newstr = buf->buf_line;
        buf->buf_line = NULL;
        buf->buf_line_len = 0;
        memmove (buf->buf, buf->buf + buf->buf_start, buf->buf_len);
        buf->buf_start = 0;
        return newstr;
    }

    return NULL;
}