format code

This commit is contained in:
DreamMaoMao 2025-04-28 10:04:18 +08:00
parent b3ccf3b8b8
commit 7e66d5390e
8 changed files with 1085 additions and 1174 deletions

View file

@ -1,48 +1,40 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Based on labwc (https://github.com/labwc/labwc) */
#define _POSIX_C_SOURCE 200809L
#include "mem.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "mem.h"
static void
die_if_null(void *ptr)
{
if (!ptr) {
perror("Failed to allocate memory");
exit(EXIT_FAILURE);
}
static void die_if_null(void *ptr) {
if (!ptr) {
perror("Failed to allocate memory");
exit(EXIT_FAILURE);
}
}
void *
xzalloc(size_t size)
{
if (!size) {
return NULL;
}
void *ptr = calloc(1, size);
die_if_null(ptr);
return ptr;
void *xzalloc(size_t size) {
if (!size) {
return NULL;
}
void *ptr = calloc(1, size);
die_if_null(ptr);
return ptr;
}
void *
xrealloc(void *ptr, size_t size)
{
if (!size) {
free(ptr);
return NULL;
}
ptr = realloc(ptr, size);
die_if_null(ptr);
return ptr;
void *xrealloc(void *ptr, size_t size) {
if (!size) {
free(ptr);
return NULL;
}
ptr = realloc(ptr, size);
die_if_null(ptr);
return ptr;
}
char *
xstrdup(const char *str)
{
assert(str);
char *copy = strdup(str);
die_if_null(copy);
return copy;
char *xstrdup(const char *str) {
assert(str);
char *copy = strdup(str);
die_if_null(copy);
return copy;
}

View file

@ -24,8 +24,8 @@ void *xzalloc(size_t size);
* struct wlr_box *box = znew(*box);
* char *buf = znew_n(char, 80);
*/
#define znew(expr) ((__typeof__(expr) *)xzalloc(sizeof(expr)))
#define znew_n(expr, n) ((__typeof__(expr) *)xzalloc((n) * sizeof(expr)))
#define znew(expr) ((__typeof__(expr) *)xzalloc(sizeof(expr)))
#define znew_n(expr, n) ((__typeof__(expr) *)xzalloc((n) * sizeof(expr)))
/*
* As defined in FreeBSD.
@ -49,16 +49,20 @@ char *xstrdup(const char *str);
* Same as ptr = xstrdup(str) but free
* <ptr> before assigning the result.
*/
#define xstrdup_replace(ptr, str) do { \
free(ptr); (ptr) = xstrdup(str); \
} while (0)
#define xstrdup_replace(ptr, str) \
do { \
free(ptr); \
(ptr) = xstrdup(str); \
} while (0)
/*
* Frees memory pointed to by <ptr> and sets <ptr> to NULL.
* Does nothing if <ptr> is already NULL.
*/
#define zfree(ptr) do { \
free(ptr); (ptr) = NULL; \
} while (0)
#define zfree(ptr) \
do { \
free(ptr); \
(ptr) = NULL; \
} while (0)
#endif /* LABWC_MEM_H */

View file

@ -1,51 +1,47 @@
/* See LICENSE.dwm file for copyright and license details. */
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "util.h"
void
die(const char *fmt, ...) {
va_list ap;
void die(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
fputc(' ', stderr);
perror(NULL);
} else {
fputc('\n', stderr);
}
if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
fputc(' ', stderr);
perror(NULL);
} else {
fputc('\n', stderr);
}
exit(1);
exit(1);
}
void *
ecalloc(size_t nmemb, size_t size)
{
void *p;
void *ecalloc(size_t nmemb, size_t size) {
void *p;
if (!(p = calloc(nmemb, size)))
die("calloc:");
return p;
if (!(p = calloc(nmemb, size)))
die("calloc:");
return p;
}
int
fd_set_nonblock(int fd) {
int flags = fcntl(fd, F_GETFL);
if (flags < 0) {
perror("fcntl(F_GETFL):");
return -1;
}
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
perror("fcntl(F_SETFL):");
return -1;
}
int fd_set_nonblock(int fd) {
int flags = fcntl(fd, F_GETFL);
if (flags < 0) {
perror("fcntl(F_GETFL):");
return -1;
}
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
perror("fcntl(F_SETFL):");
return -1;
}
return 0;
return 0;
}