<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <pthread.h>
#include <stdarg.h>
#include <iwlib.h>
#include <libudev.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include "config.h"
#include "conkylite.h"

static volatile sig_atomic_t sig_status;

int main(void)
{
  struct info s;
  struct conky_monitor *monitors;
  int nt;
  info_create(&s);
  signal(SIGINT, catch_sigint);

  update(&s);
  nt = start_monitors(&s, &monitors);
  while (!sig_status) {
    update(&s);
    set_root_name(&s);
    sleep(CL_INTERVAL);
  }
  
  stop_monitors(monitors, nt);
  info_free(&s);
  return 0;
}

static void update(struct info *s)
{
  get_temp(s);
  get_mem(s);
  get_wireless(s);
  get_battery_capacity(s);
  get_cpu_usage(s);
  get_time(s);
}

static void set_root_name(struct info *s)
{
  char name[256];
  snprintf(name, 256, CL_FORMATSTRING, CL_ARGS);
  Display *dpy = XOpenDisplay(NULL);
  int screen = DefaultScreen(dpy);
  Window root = RootWindow(dpy, screen);
  XStoreName(dpy, root, name);
  XCloseDisplay(dpy);
}

static void info_create(struct info *s)
{
  s->winfo = malloc(sizeof(struct wireless_info));
  s->ba_status = 'U';
  int i;
  for (i = 0; i < CL_CPU_COUNT+1; i++)
    s->cpu[i].idle = s->cpu[i].nonidle = 0;
}

static void info_free(struct info *s)
{
  free(s->winfo);
}

static void cl_extract_cpu_times(char *line, int count, ...)
{
  va_list args;
  static char *stat;
  unsigned long long *cur;
  char num[21];
  int i, j;
  va_start(args, count);
  if (line != NULL)
    stat = line;
  if (stat == NULL)
    return;
  cur = va_arg(args, unsigned long long *);
  for (i = j = 0; j < count; stat++) {
    switch (*stat) {
    case ' ':
      num[i] = 0;
      *cur = atol(num);
      cur = va_arg(args, unsigned long long *);
      j++;
      i = 0;
      break;
    case '\n':
      num[i] = 0;
      *cur = atol(num);
      stat += 6;
      return;
    case 0:
      return;
    default:
      num[i++] = *stat;
    }
  }
  va_end(args);
}

static void get_cpu_usage(struct info *s)
{
  unsigned long long usertime, nicetime, systemtime, idletime;
  unsigned long long ioWait, irq, softIrq, steal, guest, guestnice;
  unsigned long long systemalltime, idlealltime, totaltime, virtalltime;
  unsigned long long nonidlealltime, prevnonidle, previdle, prevtotaltime;
  int i;
  char tmp[512];
  int fd = open(CL_STAT, O_RDONLY);
  if (fd != -1) {
      lseek(fd, 5, SEEK_CUR);
      read(fd, tmp, 512);
      close(fd);
  }
  tmp[255] = 0;
  for (i = 0; i < CL_CPU_COUNT + 1; i++) {
    /* sscanf(tmp, "%llu %llu %llu %llu %llu %llu %llu " */
    /*        "%llu %llu %llu", &usertime, &nicetime, &systemtime, */
    /*        &idletime, &ioWait, &irq, &softIrq, &steal, &guest, &guestnice); */
    cl_extract_cpu_times(i == 0 ? tmp : NULL, 10, &usertime, &nicetime, &systemtime,
                         &idletime, &ioWait, &irq, &softIrq, &steal, &guest, &guestnice);
    systemalltime = systemtime + irq + softIrq;
    virtalltime = guest + guestnice;
    idlealltime = idletime + ioWait;
    nonidlealltime = usertime + nicetime + systemalltime + steal + guest + guestnice;
    totaltime = nonidlealltime + idlealltime;

    prevnonidle = s->cpu[i].nonidle;<commit_msg>Oops.<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <pthread.h>
#include <stdarg.h>
#include <iwlib.h>
#include <libudev.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include "config.h"
#include "conkylite.h"

static volatile sig_atomic_t sig_status;

int main(void)
{
  struct info s;
  struct conky_monitor *monitors;
  int nt;
  info_create(&s);
  signal(SIGINT, catch_sigint);

  update(&s);
  nt = start_monitors(&s, &monitors);
  while (!sig_status) {
    update(&s);
    set_root_name(&s);
    sleep(CL_INTERVAL);
  }
  
  stop_monitors(monitors, nt);
  info_free(&s);
  return 0;
}

static void update(struct info *s)
{
  get_temp(s);
  get_mem(s);
  get_wireless(s);
  get_battery_capacity(s);
  get_cpu_usage(s);
  get_time(s);
}

static void set_root_name(struct info *s)
{
  char name[256];
  snprintf(name, 256, CL_FORMATSTRING, CL_ARGS);
  Display *dpy = XOpenDisplay(NULL);
  int screen = DefaultScreen(dpy);
  Window root = RootWindow(dpy, screen);
  XStoreName(dpy, root, name);
  XCloseDisplay(dpy);
}

static void info_create(struct info *s)
{
  s->winfo = malloc(sizeof(struct wireless_info));
  s->ba_status = 'U';
  int i;
  for (i = 0; i < CL_CPU_COUNT+1; i++)
    s->cpu[i].idle = s->cpu[i].nonidle = 0;
}

static void info_free(struct info *s)
{
  free(s->winfo);
}

static void cl_extract_cpu_times(char *line, int count, ...)
{
  va_list args;
  static char *stat;
  unsigned long long *cur;
  char num[21];
  int i, j;
  va_start(args, count);
  if (line != NULL)
    stat = line;
  if (stat == NULL)
    return;
  cur = va_arg(args, unsigned long long *);
  for (i = j = 0; j < count; stat++) {
    switch (*stat) {
    case ' ':
      num[i] = 0;
      *cur = atol(num);
      cur = va_arg(args, unsigned long long *);
      j++;
      i = 0;
      break;
    case '\n':
      num[i] = 0;
      *cur = atol(num);
      stat += 6;
      return;
    case 0:
      return;
    default:
      num[i++] = *stat;
    }
  }
  va_end(args);
}

static void get_cpu_usage(struct info *s)
{
  unsigned long long usertime, nicetime, systemtime, idletime;
  unsigned long long ioWait, irq, softIrq, steal, guest, guestnice;
  unsigned long long systemalltime, idlealltime, totaltime, virtalltime;
  unsigned long long nonidlealltime, prevnonidle, previdle, prevtotaltime;
  int i;
  char tmp[512];
  int fd = open(CL_STAT, O_RDONLY);
  if (fd != -1) {
      lseek(fd, 5, SEEK_CUR);
      read(fd, tmp, 512);
      close(fd);
  }
  tmp[511] = 0;
  for (i = 0; i < CL_CPU_COUNT + 1; i++) {
    cl_extract_cpu_times(i == 0 ? tmp : NULL, 10, &usertime, &nicetime, &systemtime,
                         &idletime, &ioWait, &irq, &softIrq, &steal, &guest, &guestnice);
    systemalltime = systemtime + irq + softIrq;
    virtalltime = guest + guestnice;
    idlealltime = idletime + ioWait;
    nonidlealltime = usertime + nicetime + systemalltime + steal + guest + guestnice;
    totaltime = nonidlealltime + idlealltime;

    prevnonidle = s->cpu[i].nonidle;
{'fraction_non_alphanumeric': 0.15137614678899083, 'fraction_numerical': 0.012970578930718128, 'mean_word_length': 3.26078167115903, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 37, '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': '3987206', 'n_tokens_mistral': 2561, 'n_tokens_neox': 2492, 'n_words': 608}