home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / ns2tab / part01 / subr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-04  |  6.9 KB  |  273 lines

  1. /*
  2.  * Copyright (c) 1985,1989 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted provided
  6.  * that: (1) source distributions retain this entire copyright notice and
  7.  * comment, and (2) distributions including binaries display the following
  8.  * acknowledgement:  ``This product includes software developed by the
  9.  * University of California, Berkeley and its contributors'' in the
  10.  * documentation or other materials provided with the distribution and in
  11.  * all advertising materials mentioning features or use of this software.
  12.  * Neither the name of the University nor the names of its contributors may
  13.  * be used to endorse or promote products derived from this software without
  14.  * specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  16.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  17.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. /*
  21.  *******************************************************************************
  22.  *
  23.  *  subr.c --
  24.  *
  25.  *    Miscellaneous subroutines for the name server
  26.  *    lookup program.
  27.  *
  28.  *    Copyright (c) 1985
  29.  *    Andrew Cherenson
  30.  *    U.C. Berkeley
  31.  *    CS298-26  Fall 1985
  32.  *
  33.  *******************************************************************************
  34.  */
  35.  
  36. #include <sys/types.h>
  37. #include <netdb.h>
  38. #include <sys/socket.h>
  39. #include <netinet/in.h>
  40. #include <stdio.h>
  41. #include <arpa/nameser.h>
  42. #include <arpa/inet.h>
  43. #include <signal.h>
  44. #include <setjmp.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include "res.h"
  48.  
  49. /*
  50.  *******************************************************************************
  51.  *
  52.  *  OpenFile --
  53.  *
  54.  *    Parses a command string for a file name and opens
  55.  *    the file.
  56.  *
  57.  *  Results:
  58.  *    file pointer    - the open was successful.
  59.  *    NULL        - there was an error opening the file or
  60.  *              the input string was invalid.
  61.  *
  62.  *******************************************************************************
  63.  */
  64.  
  65. FILE *
  66. OpenFile(string, file)
  67.     char *string;
  68.     char *file;
  69. {
  70.     char    *redirect;
  71.     FILE    *tmpPtr;
  72.  
  73.     /*
  74.      *  Open an output file if we see '>' or >>'.
  75.      *  Check for overwrite (">") or concatenation (">>").
  76.      */
  77.  
  78.     redirect = strchr(string, '>');
  79.     if (redirect == NULL) {
  80.         return(NULL);
  81.     }
  82.     if (redirect[1] == '>') {
  83.         sscanf(redirect, ">> %s", file);
  84.         tmpPtr = fopen(file, "a+");
  85.     } else {
  86.         sscanf(redirect, "> %s", file);
  87.         tmpPtr = fopen(file, "w");
  88.     }
  89.  
  90.     if (tmpPtr != NULL) {
  91.         redirect[0] = '\0';
  92.     }
  93.  
  94.     return(tmpPtr);
  95. }
  96.  
  97. /*
  98.  *******************************************************************************
  99.  *
  100.  *  DecodeError --
  101.  *
  102.  *    Converts an error code into a character string.
  103.  *
  104.  *******************************************************************************
  105.  */
  106.  
  107. char *
  108. DecodeError(result)
  109.     int result;
  110. {
  111.     switch (result) {
  112.         case NOERROR:    return("Success"); break;
  113.         case FORMERR:    return("Format error"); break;
  114.         case SERVFAIL:    return("Server failed"); break;
  115.         case NXDOMAIN:    return("Non-existent domain"); break;
  116.         case NOTIMP:    return("Not implemented"); break;
  117.         case REFUSED:    return("Query refused"); break;
  118.         case NOCHANGE:    return("No change"); break;
  119.         case TIME_OUT:    return("Timed out"); break;
  120.         case NO_INFO:    return("No information"); break;
  121.         case ERROR:        return("Unspecified error"); break;
  122.         case NONAUTH:    return("Non-authoritative answer"); break;
  123.         case NO_RESPONSE:    return("No response from server"); break;
  124.         default:        break;
  125.     }
  126.     return("BAD ERROR VALUE");
  127. }
  128.  
  129.  
  130. int
  131. StringToClass(qclass, dflt)
  132.     char *qclass;
  133.     int dflt;
  134. {
  135.     if (strcasecmp(qclass, "IN") == 0)
  136.         return(C_IN);
  137.     if (strcasecmp(qclass, "CHAOS") == 0)
  138.         return(C_CHAOS);
  139.     if (strcasecmp(qclass, "ANY") == 0)
  140.         return(C_ANY);
  141. #ifndef C_HS
  142. #define C_HS 4
  143. #endif
  144.     if (strcasecmp(qclass, "HS") == 0)
  145.         return(C_HS);
  146.     fprintf(stderr, "unknown query class: %s\n", qclass);
  147.     return(dflt);
  148. }
  149.  
  150.  
  151. /*
  152.  *******************************************************************************
  153.  *
  154.  *  StringToType --
  155.  *
  156.  *    Converts a string form of a query type name to its
  157.  *    corresponding integer value.
  158.  *
  159.  *******************************************************************************
  160.  */
  161.  
  162. int
  163. StringToType(type, dflt)
  164.     char *type;
  165.     int dflt;
  166. {
  167.     if (strcasecmp(type, "A") == 0)
  168.         return(T_A);
  169.     if (strcasecmp(type, "NS") == 0)
  170.         return(T_NS);            /* authoritative server */
  171.     if (strcasecmp(type, "MX") == 0)
  172.         return(T_MX);            /* mail exchanger */
  173.     if (strcasecmp(type, "CNAME") == 0)
  174.         return(T_CNAME);        /* canonical name */
  175.     if (strcasecmp(type, "SOA") == 0)
  176.         return(T_SOA);            /* start of authority zone */
  177.     if (strcasecmp(type, "MB") == 0)
  178.         return(T_MB);            /* mailbox domain name */
  179.     if (strcasecmp(type, "MG") == 0)
  180.         return(T_MG);            /* mail group member */
  181.     if (strcasecmp(type, "MR") == 0)
  182.         return(T_MR);            /* mail rename name */
  183.     if (strcasecmp(type, "WKS") == 0)
  184.         return(T_WKS);            /* well known service */
  185.     if (strcasecmp(type, "PTR") == 0)
  186.         return(T_PTR);            /* domain name pointer */
  187.     if (strcasecmp(type, "HINFO") == 0)
  188.         return(T_HINFO);        /* host information */
  189.     if (strcasecmp(type, "MINFO") == 0)
  190.         return(T_MINFO);        /* mailbox information */
  191.     if (strcasecmp(type, "AXFR") == 0)
  192.         return(T_AXFR);            /* zone transfer */
  193.     if (strcasecmp(type, "MAILA") == 0)
  194.         return(T_MAILA);        /* mail agent */
  195.     if (strcasecmp(type, "MAILB") == 0)
  196.         return(T_MAILB);        /* mail box */
  197.     if (strcasecmp(type, "ANY") == 0)
  198.         return(T_ANY);            /* matches any type */
  199.     if (strcasecmp(type, "UINFO") == 0)
  200.         return(T_UINFO);        /* user info */
  201.     if (strcasecmp(type, "UID") == 0)
  202.         return(T_UID);            /* user id */
  203.     if (strcasecmp(type, "GID") == 0)
  204.         return(T_GID);            /* group id */
  205.     fprintf(stderr, "unknown query type: %s\n", type);
  206.     return(dflt);
  207. }
  208.  
  209. /*
  210.  *******************************************************************************
  211.  *
  212.  *  DecodeType --
  213.  *
  214.  *    Converts a query type to a descriptive name.
  215.  *    (A more verbose form of p_type.)
  216.  *
  217.  *
  218.  *******************************************************************************
  219.  */
  220.  
  221. static  char nbuf[20];
  222.  
  223. char *
  224. DecodeType(type)
  225.     int type;
  226. {
  227.     switch (type) {
  228.     case T_A:
  229.         return("address");
  230.     case T_NS:
  231.         return("name server");
  232.     case T_CNAME:
  233.         return("canonical name");
  234.     case T_SOA:
  235.         return("start of authority");
  236.     case T_MB:
  237.         return("mailbox");
  238.     case T_MG:
  239.         return("mail group member");
  240.     case T_MR:
  241.         return("mail rename");
  242.     case T_NULL:
  243.         return("null");
  244.     case T_WKS:
  245.         return("well-known service");
  246.     case T_PTR:
  247.         return("domain name pointer");
  248.     case T_HINFO:
  249.         return("host information");
  250.     case T_MINFO:
  251.         return("mailbox information");
  252.     case T_MX:
  253.         return("mail exchanger");
  254.     case T_UINFO:
  255.         return("user information");
  256.     case T_UID:
  257.         return("user ID");
  258.     case T_GID:
  259.         return("group ID");
  260.     case T_AXFR:
  261.         return("zone transfer");
  262.     case T_MAILB:
  263.         return("mailbox-related data");
  264.     case T_MAILA:
  265.         return("mail agent");
  266.     case T_ANY:
  267.         return("\"any\"");
  268.     default:
  269.         (void) sprintf(nbuf, "%d", type);
  270.         return (nbuf);
  271.     }
  272. }
  273.