home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / LharcUUCP1_0.lha / LharcUUCP / source / SupportRoutines.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-02  |  4.2 KB  |  163 lines

  1. /*
  2.  *      $Filename: SupportRoutines.c $
  3.  *      $Revision: 1.0 $
  4.  *      $Date: 1994/04/02 18:04:38 $
  5.  *
  6.  *      Copyright (C) 1993 by Peter Simons <simons@peti.GUN.de>
  7.  *
  8.  *      This program is free software; you can redistribute it and/or
  9.  *      modify it under the terms of the GNU General Public License as
  10.  *      published by the Free Software Foundation; either version 2 of
  11.  *      the License, or (at your option) any later version.
  12.  *
  13.  *      This program is distributed in the hope that it will be useful,
  14.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  *      General Public License for more details.
  17.  *
  18.  *      You should have received a copy of the GNU General Public License
  19.  *      along with this program; if not, write to the Free Software
  20.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  *      $Id: SupportRoutines.c,v 1.0 1994/04/02 18:04:38 simons Rel simons $
  23.  *
  24.  */
  25.  
  26.  
  27. /************************************* includes ***********/
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include <dos.h>
  33. #include <proto/dos.h>
  34. #include <proto/netsupport.h>
  35.  
  36. #include "protos.h"
  37.  
  38. /************************************* defines ************/
  39. #define SEQ "UULib:Seq"
  40.  
  41. /************************************* global variables ***/
  42. static const char __RCSId[] = "$Id: SupportRoutines.c,v 1.0 1994/04/02 18:04:38 simons Rel simons $";
  43.  
  44. /************************************* subroutines ********/
  45.  
  46.  /*
  47.   * Get an unique number!
  48.   */
  49.  
  50. unsigned int GetSequence(unsigned int bump)
  51. {
  52.         FILE *fh;
  53.         unsigned int seq = 0;
  54.  
  55.         LockFile(SEQ);
  56.         if (fh = fopen(SEQ, "r")) {
  57.                 fscanf(fh, "%u", &seq);
  58.                 fclose(fh);
  59.         }
  60.         else {
  61.                 fprintf(stderr, "LharcUUCP: Can't open seq file!\n");
  62.         }
  63.  
  64.         if (bump && seq >= 0) {
  65.                 if (bump + seq > 0xFFFFF)
  66.                         seq = 1;
  67.  
  68. endlessloop:
  69.                 if (fh = fopen(SEQ, "w")) {
  70.                         fprintf(fh, "%u", seq + bump);
  71.                         fclose(fh);
  72.                 }
  73.                 else {
  74.                         fprintf(stderr, "LharcUUCP: Can't update seq file!\n");
  75.                         sleep(3);       /* delay 3 seconds */
  76.                         goto endlessloop;
  77.                 }
  78.         }
  79.         UnLockFile(SEQ);
  80.         return seq;
  81. }
  82.  
  83.  
  84.  /*
  85.   * Convert a sequence number into a numeric/character combination. Names
  86.   * are unique and case-insensitive. The sequence number 0-0xFFFFF will be
  87.   * converted to a 4 character string each position containing 0-9 a-z,
  88.   * or base 36 (total of 1.6 million combinations)
  89.   */
  90.  
  91. char *SeqToName(unsigned int seqNo)
  92. {
  93.         static char Buf[5];
  94.         short i;
  95.  
  96.         seqNo &= 0xFFFFF;
  97.  
  98.         for (i = 3; i >= 0; --i) {
  99.                 short n = seqNo % 36;
  100.  
  101.                 if (n < 10)
  102.                         Buf[i] = n + '0';
  103.                 else
  104.                         Buf[i] = n - 10 + 'a';
  105.                 seqNo /= 36;
  106.         }
  107.         Buf[4] = '\0';
  108.         return (Buf);
  109. }
  110.  
  111.  
  112.  /*
  113.   * TruncName() duplicates the given string and truncates it to 7
  114.   * characters, which is required by a number of tasks in the UUCP
  115.   * enviroment.
  116.   */
  117.  
  118. char *TruncName(char *string)
  119. {
  120.         static char buffer[8];
  121.  
  122.         strncpy(buffer, string, 7);
  123.         buffer[7] = '\0';
  124.  
  125.         return buffer;
  126. }
  127.  
  128.  
  129.  /*
  130.   * GoNextLine() reads from the provided filehandle until either end-
  131.   * of-file or an end-of-line ('\n') occurs.
  132.   */
  133.  
  134. int GoNextLine(FILE *fh)
  135. {
  136.         int c;
  137.  
  138.         while ((c = fgetc(fh)) != EOF && c != '\n')
  139.                 ;
  140.  
  141.         if ((c = fgetc(fh)) != EOF)     /* Read next character to make */
  142.                 ungetc(c, fh);          /* feof() work.                */
  143.  
  144.         return c;
  145. }
  146.  
  147.  
  148.  /*
  149.   * This small routine returns a flag indicating wether the provided
  150.   * string was a filepath or just a filename! This is important, because
  151.   * filerequests containing paths can't be processed with LharcUUCP.
  152.   * This routine has to be customized for other systems.
  153.   */
  154.  
  155. int IsPath(char *filename)
  156. {
  157.         if (strchr(filename, '/') || strchr(filename, ':'))
  158.                 return 1;
  159.         else
  160.                 return 0;
  161. }
  162.  
  163.