home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / auucp+-1.02 / fuucp_plus_src.lzh / uucplib / ufuncs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-10  |  6.2 KB  |  292 lines

  1. #include <string.h>
  2. #include "uucpbase.h"
  3. #include "uucpproto.h"
  4. #include <time.h>
  5.  
  6. #define ZERO 0
  7. #define CTLZ ('z'&0x1F)
  8.  
  9.  
  10. // Interne Prototypes:
  11. extern void __stdargs SPRINTF(UBYTE *, UBYTE *, ...);
  12.  
  13. // Konstante(!) globale Variablen:
  14. const UWORD Days_in_Month[] =
  15. {
  16.   31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  17. };
  18.  
  19.  
  20. /*
  21.    NAME
  22.         FindConfig -- find a config entry in uulib:config
  23.  
  24.    SYNOPSIS
  25.         configentry = FindConfig(keyword)
  26.         D0                       A0
  27.  
  28.    FUNCTION
  29.         This function searches for the keyword in UULIB:config and
  30.         returns, if found, the line behind it.
  31.  
  32.    INPUTS
  33.         keyword - the keyword to search for.
  34.  
  35.    RESULT
  36.         The approprate line
  37.  
  38. */
  39.  
  40. char *__asm _FindConfig(register __a6 struct UUCPBase *UUCPBase,
  41.                         register __a0 char *field)
  42. {
  43.   char *buf;
  44.   char *str;
  45.   short flen = strlen(field);
  46.  
  47.   buf = UUCPBase->ConfigBuffer;
  48.   if(buf == NULL)
  49.   {
  50.     BPTR fh;
  51.     fh = Open("S:UUConfig", MODE_OLDFILE);
  52.     if (fh == ZERO)
  53.       fh = Open("UULIB:Config", MODE_OLDFILE);
  54.  
  55.     if(fh != ZERO)
  56.     {
  57.       long buflen;
  58.       Seek(fh, 0L, OFFSET_END);
  59.       buflen = Seek(fh, 0L, OFFSET_BEGINNING);
  60.       if (buflen > 0 && (buf = AllocMem(buflen + 1, MEMF_PUBLIC)))
  61.       {
  62.         UUCPBase->ConfigBuffer = buf;        // Pointer auf buffer merken
  63.         UUCPBase->CB_Length    = buflen + 1; // LΣnge in Library eintragen
  64.  
  65.         Read(fh, buf, buflen);
  66.         buf[buflen] = CTLZ;     /*    can't use \0 */
  67.         for (str = (char *)buf; *str; ++str)
  68.         {
  69.           char *bup;
  70.           if (*str == '\n')     /*  make separate strs */
  71.           {
  72.             *str = 0;
  73.             /*  remove white space at end */
  74.             for (bup = str - 1; bup >= (char *)buf && (*bup == ' ' || *bup == 9); --bup)
  75.               *bup = 0;
  76.           }
  77.         }
  78.       }
  79.       else
  80.       {
  81.         buf = NULL;
  82.       }
  83.       Close(fh);
  84.     }
  85.     else
  86.     {
  87.       UUCPBase->Error = "Couldn't open S:UUConfig or UULIB:Config";
  88.     }
  89.   }
  90.  
  91.   if(buf == NULL)
  92.     return(NULL);
  93.  
  94.   /*
  95.    *  Search ConfBuf for Field<space/tab>
  96.    */
  97.  
  98.   for (str = (char *)buf; *str != CTLZ; str += strlen(str) + 1)
  99.   {
  100.     if(*str == 0 || *str == '#')
  101.       continue;
  102.     if (strncmp(str, field, flen) == 0 && (str[flen] == ' ' || str[flen] == '\t'))
  103.     {
  104.       str += flen;
  105.       while (*str == ' ' || *str == 9)
  106.         ++str;
  107.       return(str);
  108.     }
  109.   }
  110.   return(NULL);
  111. }
  112.  
  113. char *__asm _GetConfig(register __a6 struct UUCPBase *UUCPBase,
  114.                        register __a0 char *field,
  115.                        register __a1 char *def)
  116. {
  117.   char *result = FindConfig(field);
  118.  
  119.   if(result == NULL)
  120.     result = def;
  121.  
  122.   return(result);
  123. }
  124.  
  125. char *__asm _GetConfigDir(register __a6 struct UUCPBase *UUCPBase,
  126.                           register __a0 char *field)
  127. {
  128.   char *result = FindConfig(field);
  129.  
  130.   if(result == NULL)
  131.     result = field + strlen(field) + 1;
  132.  
  133.   return(result);
  134. }
  135.  
  136. char *__asm _MakeConfigPath(register __a6 struct UUCPBase *UUCPBase,
  137.                             register __a0 char *field,
  138.                             register __a1 char *trailer)
  139. {
  140.   char *Buf;
  141.   char *result = GetConfigDir(field);
  142.   short len = strlen(result) - 1;
  143.  
  144.   if((Buf = 
  145.     UUAllocMem((ULONG)(strlen(result) + strlen(trailer) + 5), MEMF_PUBLIC)) != NULL)
  146.   {
  147.     if(len > 0 && result[len] == '/' || result[len] == ':')
  148.     {
  149.       strcpy(Buf, result);
  150.       strcat(Buf, trailer);
  151.     }
  152.     else
  153.     {
  154.       strcpy(Buf, result);
  155.       strcat(Buf, "/");
  156.       strcat(Buf, trailer);
  157.     }
  158.   }
  159.   return(Buf);
  160. }
  161.  
  162.  
  163. /*
  164.  * void UULog(char *)
  165.  *
  166.  */
  167. VOID __asm UULog(register __a6 struct UUCPBase *UUCPBase,
  168.                  register __a0 char *format,
  169.                  register __a1 LONG *args)
  170. {
  171.  
  172. }
  173.  
  174.  
  175. /*
  176.  *  Verschiedene nⁿtzliche Routinen
  177.  */
  178.  
  179. /*
  180.    GetTime()
  181.  
  182.    Liefert die Sekunden seit dem 1.1.1970 zurⁿck.
  183. */
  184. struct tm *__asm _GetTime(register __a6 struct UUCPBase *UUCPBase)
  185. {
  186. #define SECSFROM70TO78 ((long)(2*(4*365+1)*(60*60*24)))
  187.   struct MsgPort *mp;
  188.   struct timerequest tr;
  189.   LONG secs = -1;
  190.  
  191.   if((mp = CreatePort(NULL, NULL)) != NULL)
  192.   {
  193.     if((OpenDevice(TIMERNAME, UNIT_VBLANK, &tr.tr_node, 0L)) == 0)
  194.     {
  195.       tr.tr_node.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  196.       tr.tr_node.io_Message.mn_Node.ln_Pri  = 0;
  197.       tr.tr_node.io_Message.mn_Node.ln_Name = NULL;
  198.       tr.tr_node.io_Message.mn_ReplyPort    = mp;
  199.       tr.tr_node.io_Command                 = TR_GETSYSTIME;
  200.       DoIO((struct IORequest *)&tr);
  201.  
  202.       secs = tr.tr_time.tv_secs + ((tr.tr_time.tv_micro + 500000) / 1000000);
  203.       secs += SECSFROM70TO78;
  204.  
  205.       CloseDevice((struct IORequest *)&tr);
  206.     }
  207.     DeletePort(mp);
  208.   }
  209.  
  210.   if(secs != -1L)
  211.   {
  212.     struct tm *tm;
  213.  
  214.     LONG l, t;
  215.     int i;
  216.  
  217.     if((tm = UUAllocMem(sizeof(struct tm), MEMF_CLEAR)) != NULL)
  218.     {
  219.       l = secs;
  220.  
  221.       tm->tm_sec = l % 60;
  222.       l /= 60;
  223.       tm->tm_min = l % 60;
  224.       l /= 60;
  225.       tm->tm_hour = l % 24;
  226.       l /= 24;
  227.       tm->tm_wday = (l + 4) % 7;  /* add 4 days since first day of 70 not sunday */
  228.       tm->tm_year = 70 + (l / (4 * 365 + 1)) * 4;
  229.       l %= 4 * 365 + 1;
  230.  
  231.       while(l)
  232.       {
  233.         t = 365;
  234.         if((tm->tm_year & 3) == 0)
  235.           t++;
  236.         if(l < t)
  237.           break;
  238.         l -= t;
  239.         tm->tm_year++;
  240.       }
  241.       tm->tm_yday = l++;
  242.  
  243.       for(i = 0; i < 12; i++)
  244.       {
  245.         t = Days_in_Month[i];
  246.         if(i == 1 && (tm->tm_year & 3) == 0)
  247.           t++;
  248.         if(l <= t)
  249.           break;
  250.         l -= t;
  251.       }
  252.       tm->tm_mon = i;
  253.       tm->tm_mday = l;
  254.  
  255.       return tm;
  256.     }
  257.   }
  258.   return NULL;
  259. }
  260.  
  261.  
  262. /*
  263.  * TimeStamp()
  264.  *
  265.  * Liefert die aktuelle Zeit in einem Format zurⁿck, das direkt von den
  266.  * Logging Routinen verwendet werden kann.
  267.  *
  268.  */
  269. UBYTE *__asm _TimeStamp(register __a6 struct UUCPBase *UUCPBase)
  270. {
  271.   struct tm *tp;
  272.   static char str[30];
  273.  
  274.   tp = GetTime();     // aktuelle Zeit holen
  275.  
  276.   SPRINTF(str, "%ld/%ld-%ld:%02ld:%02ld", tp->tm_mon + 1,
  277.                tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
  278.  
  279.   return str;
  280. }
  281.  
  282.  
  283. /*
  284.    SPrintfA()
  285. void __asm _SPrintfA(register __a6 struct UUCPBase *UUCPBase,
  286.                      register __a0 UBYTE *buffer,
  287.                      register __a1 UBYTE *format,
  288.                      register __a2 LONG  *args)
  289. {
  290.   RawDoFmt(format, (APTR)args, prbuf, (APTR)buffer);
  291. }
  292. */