<commit_before>/*
 * vim:ts=8:expandtab
 *
 * i3status – Generates a status line for dzen2 or xmobar
 *
 * Copyright © 2008-2009 <NAME> and contributors
 * Copyright © 2009 <NAME> <atsutane at freethoughts dot de>
 * Copyright © 2010 <NAME> <mail at merovius dot de>
 * Copyright © 2010 <NAME> <fernandotcl at gmail dot com>
 *
 * See file LICENSE for license information.
 *
 */
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <getopt.h>
#include <signal.h>
#include <confuse.h>
#include <glob.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <sys/time.h>

#include "i3status.h"

#define exit_if_null(pointer, ...) { if (pointer == NULL) die(__VA_ARGS__); }

/* socket file descriptor for general purposes */
int general_socket;

cfg_t *cfg, *cfg_general;

/*
 * Exit upon SIGPIPE because when we have nowhere to write to, gathering
 * system information is pointless.
 *
 */
void sigpipe(int signum) {
        fprintf(stderr, "Received SIGPIPE, exiting\n");
        exit(1);
}

/*
 * Checks if the given path exists by calling stat().
 *
 */
static bool path_exists(const char *path) {
        struct stat buf;
        return (stat(path, &buf) == 0);
}

static void *scalloc(size_t size) {
        void *result = calloc(size, 1);
        exit_if_null(result, "Error: out of memory (calloc(%zd))\n", size);
        return result;
}

static char *sstrdup(const char *str) {
        char *result = strdup(str);
        exit_if_null(result, "Error: out of memory (strdup())\n");
        return result;
}


/*
 * Validates a color in "#RRGGBB" format
 *
 */
static int valid_color(const char *value)
{
        if (strlen(value) != 7) return 0;
        if (value[0] != '#') return 0;
        for (int i = 1; i < 7; ++i) {
                if (value[i] >= '0' && value[i] <= '9') continue;
                if (value[i] >= 'a' && value[i] <= 'f') continue;
                if (value[i] >= 'A' && value[i] <= 'F') continue;
                return 0;
        }
        return 1;
}

/*
 * This function resolves ~ in pathnames.
 * It may resolve wildcards in the first part of the path, but if no match
 * or multiple matches are found, it just returns a copy of path as given.
 *
 */
static char *resolve_tilde(const char *path) {
        static glob_t globbuf;
        char *head, *tail, *result;

        tail = strchr(path, '/');
        head = strndup(path, tail ? (size_t)(tail - path) : strlen(path));

        int res = glob(head, GLOB_TILDE, NULL, &globbuf);
        free(head);
        /* no match, or many wildcard matches are bad */
        if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
                result = sstrdup(path);
        else if (res != 0) {
                die("glob() failed");
        } else {
                head = globbuf.gl_pathv[0];
                result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
                strncpy(result, head, strlen(head));
                strncat(result, tail, strlen(tail));
        }
        globfree(&globbuf);

        return result;
}

static char *get_config_path() {
        /* 1: check for $XDG_CONFIG_HOME/i3/config */
        char *xdg_config_home, *xdg_config_dirs, *config_path;

        if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
                xdg_config_home = "~/.config";

        xdg_config_home = resolve_tilde(xdg_config_home);<commit_msg>initialize result to NULL to shun compiler warning<commit_after>/*
 * vim:ts=8:expandtab
 *
 * i3status – Generates a status line for dzen2 or xmobar
 *
 * Copyright © 2008-2009 <NAME> and contributors
 * Copyright © 2009 <NAME> <atsutane at freethoughts dot de>
 * Copyright © 2010 <NAME> <mail at merovius dot de>
 * Copyright © 2010 <NAME> <fernandotcl at gmail dot com>
 *
 * See file LICENSE for license information.
 *
 */
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <getopt.h>
#include <signal.h>
#include <confuse.h>
#include <glob.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <sys/time.h>

#include "i3status.h"

#define exit_if_null(pointer, ...) { if (pointer == NULL) die(__VA_ARGS__); }

/* socket file descriptor for general purposes */
int general_socket;

cfg_t *cfg, *cfg_general;

/*
 * Exit upon SIGPIPE because when we have nowhere to write to, gathering
 * system information is pointless.
 *
 */
void sigpipe(int signum) {
        fprintf(stderr, "Received SIGPIPE, exiting\n");
        exit(1);
}

/*
 * Checks if the given path exists by calling stat().
 *
 */
static bool path_exists(const char *path) {
        struct stat buf;
        return (stat(path, &buf) == 0);
}

static void *scalloc(size_t size) {
        void *result = calloc(size, 1);
        exit_if_null(result, "Error: out of memory (calloc(%zd))\n", size);
        return result;
}

static char *sstrdup(const char *str) {
        char *result = strdup(str);
        exit_if_null(result, "Error: out of memory (strdup())\n");
        return result;
}


/*
 * Validates a color in "#RRGGBB" format
 *
 */
static int valid_color(const char *value)
{
        if (strlen(value) != 7) return 0;
        if (value[0] != '#') return 0;
        for (int i = 1; i < 7; ++i) {
                if (value[i] >= '0' && value[i] <= '9') continue;
                if (value[i] >= 'a' && value[i] <= 'f') continue;
                if (value[i] >= 'A' && value[i] <= 'F') continue;
                return 0;
        }
        return 1;
}

/*
 * This function resolves ~ in pathnames.
 * It may resolve wildcards in the first part of the path, but if no match
 * or multiple matches are found, it just returns a copy of path as given.
 *
 */
static char *resolve_tilde(const char *path) {
        static glob_t globbuf;
        char *head, *tail, *result = NULL;

        tail = strchr(path, '/');
        head = strndup(path, tail ? (size_t)(tail - path) : strlen(path));

        int res = glob(head, GLOB_TILDE, NULL, &globbuf);
        free(head);
        /* no match, or many wildcard matches are bad */
        if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
                result = sstrdup(path);
        else if (res != 0) {
                die("glob() failed");
        } else {
                head = globbuf.gl_pathv[0];
                result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
                strncpy(result, head, strlen(head));
                strncat(result, tail, strlen(tail));
        }
        globfree(&globbuf);

        return result;
}

static char *get_config_path() {
        /* 1: check for $XDG_CONFIG_HOME/i3/config */
        char *xdg_config_home, *xdg_config_dirs, *config_path;

        if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
                xdg_config_home = "~/.config";

        xdg_config_home = resolve_tilde(xdg_config_home);
{'fraction_non_alphanumeric': 0.1422659817351598, 'fraction_numerical': 0.012557077625570776, 'mean_word_length': 2.5651068158697865, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 53, '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': '7072889', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2330, 'n_words': 699}