2011-10-02 13:08:49 -04:00
|
|
|
/*
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
2011-10-25 16:19:38 -04:00
|
|
|
* © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
|
2011-10-02 13:08:49 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2011-10-23 08:16:56 -04:00
|
|
|
#include <stdarg.h>
|
2015-03-24 08:57:06 -04:00
|
|
|
#include <unistd.h>
|
2011-10-23 08:16:56 -04:00
|
|
|
#include <stdio.h>
|
2011-10-02 13:08:49 -04:00
|
|
|
#include <err.h>
|
2015-03-24 08:57:06 -04:00
|
|
|
#include <errno.h>
|
2011-10-02 13:08:49 -04:00
|
|
|
|
2011-10-23 12:38:21 -04:00
|
|
|
#include "libi3.h"
|
2011-10-02 13:08:49 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The s* functions (safe) are wrappers around malloc, strdup, …, which exits if one of
|
|
|
|
* the called functions returns NULL, meaning that there is no more memory available
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void *smalloc(size_t size) {
|
|
|
|
void *result = malloc(size);
|
|
|
|
if (result == NULL)
|
|
|
|
err(EXIT_FAILURE, "malloc(%zd)", size);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *scalloc(size_t size) {
|
|
|
|
void *result = calloc(size, 1);
|
|
|
|
if (result == NULL)
|
|
|
|
err(EXIT_FAILURE, "calloc(%zd)", size);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *srealloc(void *ptr, size_t size) {
|
|
|
|
void *result = realloc(ptr, size);
|
|
|
|
if (result == NULL && size > 0)
|
|
|
|
err(EXIT_FAILURE, "realloc(%zd)", size);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *sstrdup(const char *str) {
|
|
|
|
char *result = strdup(str);
|
|
|
|
if (result == NULL)
|
|
|
|
err(EXIT_FAILURE, "strdup()");
|
|
|
|
return result;
|
|
|
|
}
|
2011-10-23 08:16:56 -04:00
|
|
|
|
|
|
|
int sasprintf(char **strp, const char *fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
if ((result = vasprintf(strp, fmt, args)) == -1)
|
|
|
|
err(EXIT_FAILURE, "asprintf(%s)", fmt);
|
|
|
|
va_end(args);
|
|
|
|
return result;
|
|
|
|
}
|
2015-03-24 08:57:06 -04:00
|
|
|
|
|
|
|
ssize_t writeall(int fd, const void *buf, size_t count) {
|
2015-03-29 11:12:20 -04:00
|
|
|
size_t written = 0;
|
2015-03-24 08:57:06 -04:00
|
|
|
ssize_t n = 0;
|
|
|
|
|
|
|
|
while (written < count) {
|
|
|
|
n = write(fd, buf + written, count - written);
|
|
|
|
if (n == -1) {
|
|
|
|
if (errno == EINTR || errno == EAGAIN)
|
|
|
|
continue;
|
|
|
|
return n;
|
|
|
|
}
|
2015-03-29 11:12:20 -04:00
|
|
|
written += (size_t)n;
|
2015-03-24 08:57:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t swrite(int fd, const void *buf, size_t count) {
|
|
|
|
ssize_t n;
|
|
|
|
|
|
|
|
n = writeall(fd, buf, count);
|
|
|
|
if (n == -1)
|
|
|
|
err(EXIT_FAILURE, "Failed to write %d", fd);
|
|
|
|
else
|
|
|
|
return n;
|
|
|
|
}
|