<commit_before>#ifndef __wlc_os_compatibility_h__
#define __wlc_os_compatibility_h__

#if __GLIBC__
#  ifdef _STDLIB_H
#     error "You must include this file before stdlib.h"
#  endif
#  ifndef _GNU_SOURCE
#     define _GNU_SOURCE
#  endif
#  define HAVE_MKOSTEMP 1
#endif

#if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L
#  define HAVE_POSIX_FALLOCATE 1
#endif

#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h>
#include <chck/string/string.h>

static int
set_cloexec_or_close(int fd)
{
   if (fd == -1)
      return -1;

   long flags;
   if ((flags = fcntl(fd, F_GETFD)) == -1)
      goto err;

   if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
      goto err;

   return fd;

err:
   close(fd);
   return -1;
}

static int
create_tmpfile_cloexec(char *tmpname)
{
   int fd;

#ifdef HAVE_MKOSTEMP
   if ((fd = mkostemp(tmpname, O_CLOEXEC)) >= 0)
      unlink(tmpname);
#else
   if ((fd = mkstemp(tmpname)) >= 0) {
      fd = set_cloexec_or_close(fd);
      unlink(tmpname);
   }
#endif

   return fd;
}

static int
os_create_anonymous_file(off_t size)
{
   static const char template[] = "/loliwm-shared-XXXXXX";

   const char *path = getenv("XDG_RUNTIME_DIR");
   if (chck_cstr_is_empty(path))
      return -1;

   struct chck_string name = {0};
   if (!chck_string_set_format(&name, "%s%s%s", path, (chck_cstr_ends_with(path, "/") ? "" : "/"), template))
      return -1;

   int fd = create_tmpfile_cloexec(name.data);
   chck_string_release(&name);

   if (fd < 0)
      return -1;

   int ret;
#ifdef HAVE_POSIX_FALLOCATE
   if ((ret = posix_fallocate(fd, 0, size)) != 0) {
      close(fd);
      errno = ret;
      return -1;
   }
#else
   if ((ret = ftruncate(fd, size)) < 0) {
      close(fd);
      return -1;
   }<commit_msg>Rename the shared file to wipe the last remains of loliwm in wlc<commit_after>#ifndef __wlc_os_compatibility_h__
#define __wlc_os_compatibility_h__

#if __GLIBC__
#  ifdef _STDLIB_H
#     error "You must include this file before stdlib.h"
#  endif
#  ifndef _GNU_SOURCE
#     define _GNU_SOURCE
#  endif
#  define HAVE_MKOSTEMP 1
#endif

#if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L
#  define HAVE_POSIX_FALLOCATE 1
#endif

#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h>
#include <chck/string/string.h>

static int
set_cloexec_or_close(int fd)
{
   if (fd == -1)
      return -1;

   long flags;
   if ((flags = fcntl(fd, F_GETFD)) == -1)
      goto err;

   if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
      goto err;

   return fd;

err:
   close(fd);
   return -1;
}

static int
create_tmpfile_cloexec(char *tmpname)
{
   int fd;

#ifdef HAVE_MKOSTEMP
   if ((fd = mkostemp(tmpname, O_CLOEXEC)) >= 0)
      unlink(tmpname);
#else
   if ((fd = mkstemp(tmpname)) >= 0) {
      fd = set_cloexec_or_close(fd);
      unlink(tmpname);
   }
#endif

   return fd;
}

static int
os_create_anonymous_file(off_t size)
{
   static const char template[] = "/wlc-shared-XXXXXX";

   const char *path = getenv("XDG_RUNTIME_DIR");
   if (chck_cstr_is_empty(path))
      return -1;

   struct chck_string name = {0};
   if (!chck_string_set_format(&name, "%s%s%s", path, (chck_cstr_ends_with(path, "/") ? "" : "/"), template))
      return -1;

   int fd = create_tmpfile_cloexec(name.data);
   chck_string_release(&name);

   if (fd < 0)
      return -1;

   int ret;
#ifdef HAVE_POSIX_FALLOCATE
   if ((ret = posix_fallocate(fd, 0, size)) != 0) {
      close(fd);
      errno = ret;
      return -1;
   }
#else
   if ((ret = ftruncate(fd, size)) < 0) {
      close(fd);
      return -1;
   }
{'fraction_non_alphanumeric': 0.13706140350877194, 'fraction_numerical': 0.015350877192982455, 'mean_word_length': 3.1990794016110473, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2195886', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1472, 'n_words': 344}