2011-10-02 19:08:49 +02:00
|
|
|
/*
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
2015-04-04 02:17:56 +02:00
|
|
|
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
|
2011-10-02 19:08:49 +02:00
|
|
|
*
|
|
|
|
*/
|
2016-10-11 09:13:35 +02:00
|
|
|
#include "libi3.h"
|
|
|
|
|
2011-10-02 19:08:49 +02:00
|
|
|
#include <err.h>
|
2015-03-24 13:57:06 +01:00
|
|
|
#include <errno.h>
|
2020-04-19 09:43:48 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2011-10-02 19:08:49 +02: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;
|
|
|
|
}
|
|
|
|
|
2015-08-03 11:50:13 +02:00
|
|
|
void *scalloc(size_t num, size_t size) {
|
|
|
|
void *result = calloc(num, size);
|
2011-10-02 19:08:49 +02:00
|
|
|
if (result == NULL)
|
2015-08-03 11:50:13 +02:00
|
|
|
err(EXIT_FAILURE, "calloc(%zd, %zd)", num, size);
|
2011-10-02 19:08:49 +02:00
|
|
|
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 14:16:56 +02:00
|
|
|
|
2015-05-06 16:28:29 +02:00
|
|
|
char *sstrndup(const char *str, size_t size) {
|
|
|
|
char *result = strndup(str, size);
|
|
|
|
if (result == NULL)
|
|
|
|
err(EXIT_FAILURE, "strndup()");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-10-23 14:16:56 +02: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 13:57:06 +01:00
|
|
|
|
|
|
|
ssize_t writeall(int fd, const void *buf, size_t count) {
|
2015-03-29 17:12:20 +02:00
|
|
|
size_t written = 0;
|
2015-03-24 13:57:06 +01:00
|
|
|
|
|
|
|
while (written < count) {
|
2018-10-13 17:49:41 +02:00
|
|
|
const ssize_t n = write(fd, ((char *)buf) + written, count - written);
|
2015-03-24 13:57:06 +01:00
|
|
|
if (n == -1) {
|
|
|
|
if (errno == EINTR || errno == EAGAIN)
|
|
|
|
continue;
|
|
|
|
return n;
|
|
|
|
}
|
2015-03-29 17:12:20 +02:00
|
|
|
written += (size_t)n;
|
2015-03-24 13:57:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2018-04-23 11:20:05 +02:00
|
|
|
ssize_t writeall_nonblock(int fd, const void *buf, size_t count) {
|
|
|
|
size_t written = 0;
|
|
|
|
|
|
|
|
while (written < count) {
|
2018-10-13 17:49:41 +02:00
|
|
|
const ssize_t n = write(fd, ((char *)buf) + written, count - written);
|
2018-04-23 11:20:05 +02:00
|
|
|
if (n == -1) {
|
|
|
|
if (errno == EAGAIN) {
|
|
|
|
return written;
|
|
|
|
} else if (errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
written += (size_t)n;
|
|
|
|
}
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2015-03-24 13:57:06 +01:00
|
|
|
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;
|
|
|
|
}
|
2020-04-12 15:11:53 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Like strcasecmp but considers the case where either string is NULL.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int strcasecmp_nullable(const char *a, const char *b) {
|
|
|
|
if (a == b) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (a == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (b == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return strcasecmp(a, b);
|
|
|
|
}
|