💾 Archived View for stack.tilde.cafe › code › gemlog › gemlog_index.c captured on 2024-05-12 at 15:32:35.

View Raw

More Information

⬅️ Previous capture (2023-09-28)

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

/***********************************************************************
                 (c) Copyright 2021 StackSmith      All Rights Reserverd
                 Distributed under BSD 3-clause license (attached below)

gemlog_index a gemlog directory and generate a gmi index file

Each gemlog entry  must be a file named YYYY-MM-DDwhatever.gmi and
contain a #HEADER.  Each file is processed to generate a link.  Non-
compliant files are ignored.

Files are processed in the order they are stored in the directory, and
the resultant index may be sorted in the shell.  Likewise, headers and
footers may be attached with cat.  Make a script or an alias, like:

alias gemlog='cat header.txt <(gemlog_index . | sort -r) > index.gmi'

To compile:

gcc gemlog_index.c -o gemlog_index


#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h> 

// A macro to validate date digits in filename
#define DIG(i) (isdigit(name[i]))

char* validate_name(char* name){
  unsigned len = strlen(name);
  // 14 chars at minimum: 10 for date, 4 for .gmi
  if( (len >= 14)  &&
      (!strncmp(name + len - 4, ".gmi", 4)) &&
      DIG(0) && DIG(1) && DIG(2) && DIG(3) &&
      (name[4] == '-') && DIG(5) && DIG(6) &&
      (name[7] == '-') && DIG(8) && DIG(9) )
    return name;
  else
    return NULL;
}

void make_link(char* name){
  FILE*f;
  char buf[1024];
  if(name) {
    if(!(f = fopen(name,"r"))) {
      fprintf(stderr,"Could not read file %s\n",name);
    }
    if((fgets(buf,1024,f)) && (buf[0] == '#')){
      fprintf(stdout,"=>%s %.10s  %s", name, name, buf+1);
    } else {
      fprintf(stderr,"Could not process file %s\n", name);
    }
    fclose(f);
  }
}


int main(int argc, char **argv)
{
    struct dirent *de;  // Pointer for directory entry
    DIR *dr;
    if (argc < 2) {
      printf("Usage: gemlog <dirname>\n");
      printf("Generates an index of all gemlog entries in the directory\n");
      return EXIT_FAILURE;
    }
    if(!(dr = opendir(argv[1]))){
        printf("Could not open current directory" );
        return EXIT_FAILURE;
    }
    while((de = readdir(dr)))
      (make_link(validate_name(de->d_name)));
         
    closedir(dr);    
    return EXIT_SUCCESS;
}

/*******************************************************************************

                 BSD 3-Clause License

                 (c) Copyright 2021 StackSmith      All Rights Reserverd

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation 
and/or other materials provided with the distribution.

  3. Neither the name of the copyright holder nor the names of its contributors 
may be used to endorse or promote products derived from this software without 
specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS 
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.