📄 main.c
字号:
/* * ++Copyright++ 1985, 1989 * - * Copyright (c) 1985, 1989 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * Portions Copyright (c) 1993 by Digital Equipment Corporation. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies, and that * the name of Digital Equipment Corporation not be used in advertising or * publicity pertaining to distribution of the document or software without * specific, written prior permission. * * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS * SOFTWARE. * - * --Copyright-- */#ifndef lintchar copyright[] ="@(#) Copyright (c) 1985,1989 Regents of the University of California.\n\ All rights reserved.\n";#endif /* not lint */#ifndef lintstatic char sccsid[] = "@(#)main.c 5.42 (Berkeley) 3/3/91";static char rcsid[] = "$Id: main.c,v 8.4 1996/11/11 06:36:54 vixie Exp $";#endif /* not lint *//* ****************************************************************************** * * main.c -- * * Main routine and some action routines for the name server * lookup program. * * Andrew Cherenson * U.C. Berkeley Computer Science Div. * CS298-26, Fall 1985 * ****************************************************************************** */#ifndef WINNT#include <sys/param.h>#include <netinet/in.h>#include <setjmp.h>#endif#include <netdb.h>#ifndef WINNT#include <sys/socket.h>#endif#include <nameser.h>#include <inet.h>#include <resolv.h>#include <signal.h>#include <ctype.h>#include <stdio.h>#include <errno.h>#include <limits.h>#include "res.h"#include "pathnames.h"#include "portability.h" /* * Forward references */ void Usage(); void LocalServer(HostInfo *defaultPtr); int LookupHost(char *string, Boolean putToFile); void res_re_init(); void res_dnsrch(register char *cp); int SetOption(register char *option);#ifdef WINNT#ifdef GUI int PrintToBox(char *, int); void gui(); extern char output[10000]; extern char final_output[10000];#endif#endif/* * Name of a top-level name server. Can be changed with * the "set root" command. */#ifndef ROOT_SERVER#define ROOT_SERVER "a.root-servers.net."#endifchar rootServerName[NAME_LEN] = ROOT_SERVER; #ifdef WINNT HANDLE hReadWriteEvent = NULL; #endif/* * Import the state information from the resolver library. */extern struct __res_state _res;/* * Info about the most recently queried host. */HostInfo curHostInfo;int curHostValid = FALSE;/* * Info about the default name server. */HostInfo *defaultPtr = NULL;char defaultServer[NAME_LEN];struct in_addr defaultAddr;/* * Initial name server query type is Address. */int queryType = T_A;int queryClass = C_IN;/* * Stuff for Interrupt (control-C) signal handler. */#ifndef WINNTextern SIG_FN IntrHandler();jmp_buf env;#else char *pathnslookuprc, *pathpagercmd, *pathhelpfile;#endifFILE *filePtr;/* * Browser command for help and view. */char *pager;static void CvtAddrToPtr();static void ReadRC();#ifdef WINNT/* Expand environmental variables in file pathnames *//* XXX - Would it be better to store/retrieve pathnames from the registry? */voidexpand_paths(void){ pathnslookuprc = (char *)malloc(MAX_PATH); if (!ExpandEnvironmentStrings(_PATH_NSLOOKUPRC, pathnslookuprc, MAX_PATH)) { syslog(LOG_ERR, "ExpandEnvironmentStrings(_PATH_NSLOOKUPRC) failed: %m\n"); } else pathpagercmd = (char *)malloc(MAX_PATH); if (!ExpandEnvironmentStrings(_PATH_PAGERCMD, pathpagercmd, MAX_PATH)) { syslog(LOG_ERR, "ExpandEnvironmentStrings(_PATH_PAGERCMD) failed: %m\n"); } else pathhelpfile = (char *)malloc(MAX_PATH); if (OnWinnt() == TRUE) { if (!ExpandEnvironmentStrings(_PATH_HELPFILE, pathhelpfile, MAX_PATH)) { syslog(LOG_ERR, "ExpandEnvironmentStrings(_PATH_HELPFILE) failed: %m\n"); } } else { if (!ExpandEnvironmentStrings(_PATH_95HELPFILE, pathhelpfile, MAX_PATH)) { syslog(LOG_ERR, "ExpandEnvironmentStrings(_PATH_95HELPFILE) failed: %m\n"); } }}#endif /* WINNT *//* ****************************************************************************** * * main -- * * Initializes the resolver library and determines the address * of the initial name server. The yylex routine is used to * read and perform commands. * ****************************************************************************** */
main(argc, argv) int argc; char **argv;{ char *wantedHost = NULL; Boolean useLocalServer; int result; int i; struct hostent *hp; /* * Initialize the resolver library routines. */ #ifdef WINNT /* lgk moved startup to beginning.. */ hReadWriteEvent = CreateEvent(NULL, FALSE, FALSE, NULL); { WORD wVersionRequested; WSADATA wsaData; expand_paths(); wVersionRequested = MAKEWORD(1,1); if (WSAStartup(wVersionRequested, &wsaData)) { syslog(LOG_ERR, "No useable winsock.dll: %m"); return (-1); } }
#endif /* WINNT */ if (res_init() == -1) { fprintf(stderr,"*** Can't initialize resolver.\n"); exit(1); } /* * Allocate space for the default server's host info and * find the server's address and name. If the resolver library * already has some addresses for a potential name server, * then use them. Otherwise, see if the current host has a server. * Command line arguments may override the choice of initial server. */ defaultPtr = (HostInfo *) Calloc(1, sizeof(HostInfo)); /* * Parse the arguments: * no args = go into interactive mode, use default host as server * 1 arg = use as host name to be looked up, default host will be server * non-interactive mode * 2 args = 1st arg: * if it is '-', then * ignore but go into interactive mode * else * use as host name to be looked up, * go into non-interactive mode * 2nd arg: name or inet address of server * * "Set" options are specified with a leading - and must come before * any arguments. For example, to find the well-known services for * a host, type "nslookup -query=wks host" */ ReadRC(); /* look for options file */ ++argv; --argc; /* skip prog name */ while (argc && *argv[0] == '-' && argv[0][1]) { (void) SetOption (&(argv[0][1])); ++argv; --argc; } if (argc > 2) { Usage(); } if (argc && *argv[0] != '-') { wantedHost = *argv; /* name of host to be looked up */ } useLocalServer = FALSE; if (argc == 2) { struct in_addr addr; /* * Use an explicit name server. If the hostname lookup fails, * default to the server(s) in resolv.conf. */ if (inet_aton(*++argv, &addr)) { _res.nscount = 1; _res.nsaddr.sin_addr = addr; } else {#ifndef WINNT hp = gethostbyname(*argv);#else hp = gethostbyname_nt(*argv);#endif if (hp == NULL) { fprintf(stderr, "*** Can't find server address for '%s': ", *argv); herror((char *)NULL); fputc('\n', stderr); } else { for (i = 0; i < MAXNS && hp->h_addr_list[i] != NULL; i++) { bcopy(hp->h_addr_list[i], (char *)&_res.nsaddr_list[i].sin_addr, hp->h_length); } _res.nscount = i; } } } if (_res.nscount == 0 || useLocalServer) { LocalServer(defaultPtr); } else { for (i = 0; i < _res.nscount; i++) { if (_res.nsaddr_list[i].sin_addr.s_addr == INADDR_ANY) { LocalServer(defaultPtr); break; } else { result = GetHostInfoByAddr(&(_res.nsaddr_list[i].sin_addr), &(_res.nsaddr_list[i].sin_addr), defaultPtr); if (result != SUCCESS) { fprintf(stderr, "*** Can't find server name for address %s: %s\n", inet_ntoa(_res.nsaddr_list[i].sin_addr), DecodeError(result)); } else { defaultAddr = _res.nsaddr_list[i].sin_addr; break; } } } /* * If we have exhausted the list, tell the user about the * command line argument to specify an address. */ if (i == _res.nscount) { fprintf(stderr, "*** Default servers are not available\n"); exit(1); } } strcpy(defaultServer, defaultPtr->name);#ifdef DEBUG#ifdef DEBUG2 _res.options |= RES_DEBUG2;#endif _res.options |= RES_DEBUG; _res.retry = 2;#endif /* DEBUG */ /* * If we're in non-interactive mode, look up the wanted host and quit. * Otherwise, print the initial server's name and continue with * the initialization. */ if (wantedHost != (char *) NULL) { LookupHost(wantedHost, 0); } else {#ifndef GUI PrintHostInfo(stdout, "Default Server:", defaultPtr);#else PrintHostInfo(outputfile, "Default Server:", defaultPtr);#endif pager = getenv("PAGER"); if (pager == NULL) {#ifndef WINNT pager = _PATH_PAGERCMD;#else pager = pathpagercmd;#endif } /* * Setup the environment to allow the interrupt handler to return here. */#ifndef WINNT (void) setjmp(env); /* * Return here after a longjmp. */ signal(SIGINT, IntrHandler); signal(SIGPIPE, SIG_IGN);#endif /* * Read and evaluate commands. The commands are described in commands.l * Yylex returns 0 when ^D or 'exit' is typed. */ printf("> "); fflush(stdout);#ifdef GUI gui();#else while(yylex()) { printf("> "); fflush(stdout); } #endif }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -