home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / contrib / smail / smail-3.1 / smail-3 / smail-3.1.28 / src / qualify.c < prev    next >
C/C++ Source or Header  |  1992-07-11  |  3KB  |  121 lines

  1. /* @(#)src/qualify.c    1.3 7/11/92 11:50:00 */
  2.  
  3. /*
  4.  *    Copyright (C) 1987, 1988 Ronald S. Karr and Landon Curt Noll
  5.  *    Copyright (C) 1992  Ronald S. Karr
  6.  * 
  7.  * See the file COPYING, distributed with smail, for restriction
  8.  * and warranty information.
  9.  */
  10.  
  11. /*
  12.  * qualify.c:
  13.  *      Fully qualify a domain.  In lieu of full qualification rules,
  14.  *      this routine appends the first visible domain.
  15.  *
  16.  *    This source file was contributed by Chip Salzenberg,
  17.  *    chip@ateng.ateng.com.  It has been modified.
  18.  */
  19.  
  20. #include <stdio.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include "defs.h"
  24. #include "smail.h"
  25. #include "parse.h"
  26. #include "dys.h"
  27. #ifndef DEPEND
  28. # include "extern.h"
  29. #endif
  30.  
  31. struct qualify {
  32.     char    *host;                      /* host name */
  33.     char    *domain;                    /* domain where host lives */
  34. };
  35.  
  36. static struct qualify *domains = NULL;  /* Array of host/domain pairs */
  37. static int domain_count = 0;            /* Count of valid pairs */
  38. static int domain_max = 0;              /* Count of allocated pairs */
  39.  
  40. char *
  41. read_qualify_file()
  42. {
  43.     FILE *f;
  44.     char *entry;
  45.     char *p;
  46.     struct stat statbuf;
  47.  
  48.     /*
  49.      * ignore any previously-read qualifier data
  50.      */
  51.     domain_count = 0;
  52.  
  53.     /*
  54.      * try to open qualifier file, stat file if possible
  55.      */
  56.     if (qualify_file == NULL || EQ(qualify_file, "-")) {
  57.     return NULL;
  58.     }
  59.     f = fopen(qualify_file, "r");
  60.     if (f == NULL) {
  61.     if (require_configs) {
  62.         return xprintf("cannot open %s: %s", qualify_file, strerrno());
  63.     }
  64.  
  65.     add_config_stat(qualify_file, (struct stat *)NULL);
  66.     return NULL;
  67.     }
  68.  
  69.     (void)fstat(fileno(f), &statbuf);
  70.     add_config_stat(qualify_file, &statbuf);
  71.  
  72.     /* loop and read all of the table entries in the domains file */
  73.  
  74.     if (!domains) {
  75.     domain_max = 16;
  76.     domains = (struct qualify *) xmalloc(domain_max * sizeof(*domains));
  77.     }
  78.     while (entry = read_entry(f)) {
  79.     struct attribute *new;
  80.     char *error;
  81.  
  82.     new = parse_table(entry, &error);
  83.     if (new == NULL) {
  84.         continue;
  85.     }
  86.     if (domain_count >= domain_max) {
  87.         domain_max += 16;
  88.         domains = (struct qualify *)xrealloc((char *)domains,
  89.                 domain_max * sizeof(struct qualify));
  90.     }
  91.     domains[domain_count].host = new->name;
  92.     domains[domain_count].domain = new->value;
  93.     ++domain_count;
  94.     xfree((char *)new);
  95.     }
  96.     (void) fclose(f);
  97.  
  98.     return NULL;
  99. }
  100.  
  101. char *
  102. qualify_domain(s)
  103.     char *s;                /* domain to fully qualify */
  104. {
  105.     int i;
  106.  
  107.     if (index(s, '.') != NULL) {
  108.     return NULL;
  109.     }
  110.  
  111.     for (i = 0; i < domain_count; ++i) {
  112.     char *host = domains[i].host;
  113.  
  114.     if (EQIC(host, s) || EQIC(host, "*")) {
  115.         return domains[i].domain;
  116.     }
  117.     }
  118.  
  119.     return NULL;
  120. }
  121.