/**                     opt_utils.c                     **/

/** This file contains routines that might be needed for the various
     machines that the mailer can run on.  Please check the Makefile
     for more help and/or information.

     (C) Copyright 1986 Dave Taylor
**/

#include "headers.h"

char NodeName[128];
char UserName[128];

#ifdef BSD
# include <pwd.h>
#endif

char *
getlogin()
{
  return (char *)UserName;
}

#ifdef NEED_GETHOSTNAME

gethostname(hostname,size) /* get name of current host */
int size;
char *hostname;
{
        /** Return the name of the current host machine. **/

        (void) strncpy(hostname, NodeName ,size-1);
        hostname[size - 1] = '\0';

}

#endif

#ifdef NEED_CUSERID

char *cuserid(uname)
char *uname;
{
          return strcpy( uname, UserName );
}

#endif

#ifdef BSD

/** some supplementary string functions for Berkeley Unix systems **/

strspn(source, keys)
char *source, *keys;
{
        /** This function returns the length of the substring of
            'source' (starting at zero) that consists ENTIRELY of
            characters from 'keys'.  This is used to skip over a
            defined set of characters with parsing, usually.
        **/

        register int loc = 0, key_index = 0;

        while (source[loc] != '\0') {
          key_index = 0;
          while (keys[key_index] != source[loc])
            if (keys[key_index++] == '\0')
              return(loc);
          loc++;
        }

        return(loc);
}

strcspn(source, keys)
char *source, *keys;
{
        /** This function returns the length of the substring of
            'source' (starting at zero) that consists entirely of
            characters NOT from 'keys'.  This is used to skip to a
            defined set of characters with parsing, usually.
            NOTE that this is the opposite of strspn() above
        **/

        register int loc = 0, key_index = 0;

        while (source[loc] != '\0') {
          key_index = 0;
          while (keys[key_index] != '\0')
            if (keys[key_index++] == source[loc])
              return(loc);
          loc++;
        }

        return(loc);
}

char *strtok(source, keys)
char *source, *keys;
{
        /** This function returns a pointer to the next word in source
            with the string considered broken up at the characters
            contained in 'keys'.  Source should be a character pointer
            when this routine is first called, then NULL subsequently.
            When strtok has exhausted the source string, it will
            return NULL as the next word.

            WARNING: This routine will DESTROY the string pointed to
            by 'source' when first invoked.  If you want to keep the
            string, make a copy before using this routine!!
         **/

        register int  last_ch;
        static   char *sourceptr;
                 char *return_value;

        if (source != NULL)
          sourceptr = source;

        if (*sourceptr == '\0')
          return(NULL);         /* we hit end-of-string last time!? */

        sourceptr += strspn(sourceptr, keys);   /* skip leading crap */

        if (*sourceptr == '\0')
          return(NULL);         /* we've hit end-of-string */

        last_ch = strcspn(sourceptr, keys);     /* end of good stuff */

        return_value = sourceptr;               /* and get the ret   */

        sourceptr += last_ch;                   /* ...value          */

        if (*sourceptr != '\0')         /* don't forget if we're at END! */
          sourceptr++;                     /* and skipping for next time */

        return_value[last_ch] = '\0';           /* ..ending right    */

        return((char *) return_value);          /* and we're outta here! */
}

char *strpbrk(source, keys)
char *source, *keys;
{
        /** Returns a pointer to the first character of source that is any
            of the specified keys, or NULL if none of the keys are present
            in the source string.
        **/

        register int loc = 0, key_index = 0;

        while (source[loc] != '\0') {
          key_index = 0;
          while (keys[key_index] != '\0')
            if (keys[key_index++] == source[loc])
              return((char *) (source + loc));
          loc++;
        }

        return(NULL);
}

char *strchr(buffer, ch)
char *buffer, ch;
{
        /** Returns a pointer to the first occurance of the character
            'ch' in the specified string or NULL if it doesn't occur **/

        char *address;

        address = buffer;

        while (*address != ch) {
          if (*address == '\0')
            return (NULL);
          address++;
        }

        return ( (char *) address);
}

#endif

/*
 * Read the control file and grab a few parameters.
 */
void
read_ctl()
{
        FILE  *fd;
        char  buf[128];
#define CTL_DELIM " \t\n\r"

        if (! (fd = fopen("uucp:lib/config", "r"))) {
                printf("Can't Find config file");
                perror("config");
                exit(3);
        }

        while (NULL != fgets(buf, sizeof buf, fd)) {
                     if (strncmp(buf, "UserName", 8) == 0)
                        strcpy(UserName, strtok(&buf[9], CTL_DELIM));
                else if (strncmp(buf, "NodeName", 8) == 0)
                        strcpy(NodeName, strtok(&buf[9], CTL_DELIM)) ;
        }

        fclose(fd);
}
