/*
 *      $Filename: SupportRoutines.c $
 *      $Revision: 1.0 $
 *      $Date: 1994/04/02 18:04:38 $
 *
 *      Copyright (C) 1993 by Peter Simons <simons@peti.GUN.de>
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License as
 *      published by the Free Software Foundation; either version 2 of
 *      the License, or (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *      General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *      $Id: SupportRoutines.c,v 1.0 1994/04/02 18:04:38 simons Rel simons $
 *
 */


/************************************* includes ***********/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dos.h>
#include <proto/dos.h>
#include <proto/netsupport.h>

#include "protos.h"

/************************************* defines ************/
#define SEQ "UULib:Seq"

/************************************* global variables ***/
static const char __RCSId[] = "$Id: SupportRoutines.c,v 1.0 1994/04/02 18:04:38 simons Rel simons $";

/************************************* subroutines ********/

 /*
  * Get an unique number!
  */

unsigned int GetSequence(unsigned int bump)
{
        FILE *fh;
        unsigned int seq = 0;

        LockFile(SEQ);
        if (fh = fopen(SEQ, "r")) {
                fscanf(fh, "%u", &seq);
                fclose(fh);
        }
        else {
                fprintf(stderr, "LharcUUCP: Can't open seq file!\n");
        }

        if (bump && seq >= 0) {
                if (bump + seq > 0xFFFFF)
                        seq = 1;

endlessloop:
                if (fh = fopen(SEQ, "w")) {
                        fprintf(fh, "%u", seq + bump);
                        fclose(fh);
                }
                else {
                        fprintf(stderr, "LharcUUCP: Can't update seq file!\n");
                        sleep(3);       /* delay 3 seconds */
                        goto endlessloop;
                }
        }
        UnLockFile(SEQ);
        return seq;
}


 /*
  * Convert a sequence number into a numeric/character combination. Names
  * are unique and case-insensitive. The sequence number 0-0xFFFFF will be
  * converted to a 4 character string each position containing 0-9 a-z,
  * or base 36 (total of 1.6 million combinations)
  */

char *SeqToName(unsigned int seqNo)
{
        static char Buf[5];
        short i;

        seqNo &= 0xFFFFF;

        for (i = 3; i >= 0; --i) {
                short n = seqNo % 36;

                if (n < 10)
                        Buf[i] = n + '0';
                else
                        Buf[i] = n - 10 + 'a';
                seqNo /= 36;
        }
        Buf[4] = '\0';
        return (Buf);
}


 /*
  * TruncName() duplicates the given string and truncates it to 7
  * characters, which is required by a number of tasks in the UUCP
  * enviroment.
  */

char *TruncName(char *string)
{
        static char buffer[8];

        strncpy(buffer, string, 7);
        buffer[7] = '\0';

        return buffer;
}


 /*
  * GoNextLine() reads from the provided filehandle until either end-
  * of-file or an end-of-line ('\n') occurs.
  */

int GoNextLine(FILE *fh)
{
        int c;

        while ((c = fgetc(fh)) != EOF && c != '\n')
                ;

        if ((c = fgetc(fh)) != EOF)     /* Read next character to make */
                ungetc(c, fh);          /* feof() work.                */

        return c;
}


 /*
  * This small routine returns a flag indicating wether the provided
  * string was a filepath or just a filename! This is important, because
  * filerequests containing paths can't be processed with LharcUUCP.
  * This routine has to be customized for other systems.
  */

int IsPath(char *filename)
{
        if (strchr(filename, '/') || strchr(filename, ':'))
                return 1;
        else
                return 0;
}

