💾 Archived View for home.tobot.dev › cgi › iso-date.c captured on 2022-03-01 at 15:13:20.

View Raw

More Information

-=-=-=-=-=-=-

// Uncomment to disable inlining
// #define inline

#include "./std.h"

void print_header()
{
  print("20 text/gemini\r\n# Hello, C!\r\nThis page was generated from "
        "C code (without any libraries like glibc!)");
}

void print_argv(int const argc, char const * const * const argv)
{
  if(argc > 0)
  {
    print("\r\n## argv:");
    for(int i = 0; i < argc; i++)
    {
      print("\r\n");
      print(argv[i]);
    }
  }
}

void print_envp(char const * const * const envp)
{
  if(envp[0] != 0)
  {
    print("\r\n## envp:");
    unsigned char i = 0;
    while(envp[i] != 0 && i < 254)
    {
      print("\r\n");
      print(envp[i]);
      i++;
    }
  }
}

char * zpad(char * const str, unsigned int const target_length)
{
  size_t const current_length = strlen(str);
  for(size_t i = 0; i <= target_length; i++)
  {
    str[target_length - i] = i > current_length
      ? '0'
      : str[current_length - i];
  }
  return str;
}

void print_footer()
{
  struct timeval tv;

  struct timeval * const restrict tvp = &tv;

  gettimeofday(tvp, 0);

  char str[32];
  prints("\r\n\r\n### This page was generated at: ", 36);

  struct tm time;
  struct tm * restrict const timep = &time;

  time_t t_tmp = tv.tc_sec;
  __gmtime_r(&t_tmp, timep);
  prints(itoa(time.tm_year + 1900, str), 4);
  prints("-", 1);
  prints(zpad(itoa(time.tm_mon + 1, str), 2), 2);
  prints("-", 1);
  prints(zpad(itoa(time.tm_mday, str), 2), 2);
  prints("T", 1);
  prints(zpad(itoa(time.tm_hour, str), 2), 2);
  prints(":", 1);
  prints(zpad(itoa(time.tm_min, str), 2), 2);
  prints(":", 1);
  prints(zpad(itoa(time.tm_sec, str), 2), 2);
  prints(".", 1);
  prints(zpad(itoa(tv.tv_usec, str), 6), 6);
  prints("Z (Unix: ", 9);
  print(itoa(tv.tc_sec, str));
  prints(".", 1);
  prints(zpad(itoa(tv.tv_usec, str), 6), 6);
  prints(")\r\n", 3);
}

int main(int const argc,
         char const * const * const argv,
         char const * const * const envp) {
  print_header();
  print_argv(argc, argv);
  print_envp(envp);
  print_footer();

  return 0;
}