Divine Blood
You are not logged in.


Source Code: http://www.divineblood.org/snippets/help.c

Last Modified: Wed Jun 02, 2004 5:47pm
Download without markup
Download as zipfile


#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>

int running_level = 91;
#define TRUE 1
#define FALSE 0

inline char *one_argument( char *argument, char *arg_first )
{
  char cEnd;
  while (*argument == ' ')
    argument++;
  cEnd = ' ';
  if (*argument == '\'' || *argument == '\"')
    cEnd = *argument++;
  while (*argument != '\0')
  {
    if (*argument == cEnd)
    {
      argument++;
      break;
    }
    *arg_first++ = *argument++;
  }
  *arg_first = '\0';
  while (*argument == ' ')
    argument++;
  return argument;
}

inline int is_exact_name(char *str, char *namelist )
{
  char name[256];
  if (namelist == NULL)
    return FALSE;

  for (;;)
  {
    namelist = one_argument(namelist, name);
    if (name[0] == '\0')
      return FALSE;
    if (!strcasecmp(str,name))
      return TRUE;
  }
}

void search_help(char *file, int length, char *keyword)
{
  int i;
  char level[1024];
  char keywords[1024];
  file = strstr(file, "#HELPS");
  if (!file)
  {
    printf("HELPS section not found.\n");
    return;
  }
  file += 6;
  while (1)
  {
    i = 0;
    while (*file <= ' ') file++;
    while (*file > ' ')
      level[i++] = *file++;
    level[i] = '\0';
    file++;
    if (!strcmp(level, "#$"))
      return;
    while (*file <= ' ') file++;
    i = 0;
    while (*file != '~')
      keywords[i++] = *file++;
    keywords[i] = '\0';
    if (keywords[0] == '$' && !keywords[1])
      return;
    file++;
    if (atoi(level) <= running_level && is_exact_name(keyword, keywords))
    {
      while (*file != '~')
        putchar(*file++);
      exit(0);
    }
    else
      while (*file != '~')
        file++;
    file++;
  }
}

int main(int argc, char **argv)
{
  FILE *fp = fopen("/home/divine/Rom24/area/help.are", "r");
  struct stat buf;
  char *keyword = "summary";
  if (argc == 2)
    keyword = argv[1];
  else printf("Using default help subject..");
  if (fp)
  {
    int length = fstat(fileno(fp), &buf);
    char *file = mmap(NULL, buf.st_size, PROT_READ, MAP_SHARED, fileno(fp), 0);
    if (file)
      search_help(file, buf.st_size, keyword);
    else printf("MMAP failed.\n");
  }
  else printf("NULL fp\n");
  fp = fopen("/home/divine/Rom24/area/rom.are", "r");
  if (fp)
  {
    int length = fstat(fileno(fp), &buf);
    char *file = mmap(NULL, buf.st_size, PROT_READ, MAP_SHARED, fileno(fp), 0);
    if (file)
      search_help(file, buf.st_size, keyword);
    else printf("MMAP failed.\n");
  }
  else printf("NULL fp\n");
  fp = fopen("/home/divine/Rom24/area/group.are", "r");
  if (fp)
  {
    int length = fstat(fileno(fp), &buf);
    char *file = mmap(NULL, buf.st_size, PROT_READ, MAP_SHARED, fileno(fp), 0);
    if (file)
      search_help(file, buf.st_size, keyword);
    else printf("MMAP failed.\n");
  }
  else printf("NULL fp\n");
  printf("Help File Not Found.");
}

© 2005 Divine Blood Staff -- Web page by Palrich (palrichatgmail.com)