⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 list.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
	if (cmd != NULL) {	    fprintf(stdout, "%sReceived %d record%s.\n",		(numAnswers >= HASH_SIZE) ? "\n" : "",		numAnswers,		(numAnswers != 1) ? "s" : "");	}	(void) close(sockFD);	sockFD = -1;	if (cmd != NULL && filePtr != NULL) {	    fclose(filePtr);	    filePtr = NULL;	}	switch (error) {	    case NO_ERRORS:		return (SUCCESS);	    case ERR_READING_LEN:		return(ERROR);	    case ERR_PRINTING:		return(result);	    case ERR_READING_MSG:		headerPtr = (HEADER *) answer;		fprintf(stderr,"*** ls: error receiving zone transfer:\n");		fprintf(stderr,	       "  result: %s, answers = %d, authority = %d, additional = %d\n",			_res_resultcodes[headerPtr->rcode],			ntohs(headerPtr->ancount), ntohs(headerPtr->nscount),			ntohs(headerPtr->arcount));		return(ERROR);	    default:		return(ERROR);	}}/* ******************************************************************************* * *  PrintListInfo -- * *	Used by the ListInfo routine to print the answer *	received from the name server. Only the desired *	information is printed. * *  Results: *	SUCCESS		the answer was printed without a problem. *	NO_INFO		the answer packet did not contain an answer. *	ERROR		the answer was malformed. *      Misc. errors	returned in the packet header. * ******************************************************************************* */#define NAME_FORMAT " %-30s"static Booleanstrip_domain(string, domain)    char *string, *domain;{    register char *dot;    if (*domain != '\0') {	dot = string;	while ((dot = strchr(dot, '.')) != NULL && strcasecmp(domain, ++dot))		;	if (dot != NULL) {	    dot[-1] = '\0';	    return TRUE;	}    }    return FALSE;}PrintListInfo(file, msg, eom, qtype, domain)    FILE	*file;    u_char	*msg, *eom;    int		qtype;    char	*domain;{    register u_char	*cp;    HEADER		*headerPtr;    int			type, class, dlen, nameLen;    u_int32_t		ttl;    int			n, pref;    struct in_addr	inaddr;    char		name[NAME_LEN];    char		name2[NAME_LEN];    Boolean		stripped;    /*     * Read the header fields.     */    headerPtr = (HEADER *)msg;    cp = msg + sizeof(HEADER);    if (headerPtr->rcode != NOERROR) {	return(headerPtr->rcode);    }    /*     *  We are looking for info from answer resource records.     *  If there aren't any, return with an error. We assume     *  there aren't any question records.     */    if (ntohs(headerPtr->ancount) == 0) {	return(NO_INFO);    } else {	if (ntohs(headerPtr->qdcount) > 0) {	    nameLen = dn_skipname(cp, eom);	    if (nameLen < 0)		return (ERROR);	    cp += nameLen + QFIXEDSZ;	}	if ((nameLen =	    dn_expand(msg, eom, cp, (u_char *)name, sizeof(name))) < 0)	    return (ERROR);	cp += nameLen;	type = _getshort(cp);	cp += sizeof(u_short);	if (!(type == qtype || qtype == T_ANY) &&	    !((type == T_NS || type == T_PTR) && qtype == T_A))		return(SUCCESS);	class = _getshort(cp);	cp += sizeof(u_short);	ttl = _getlong(cp);	cp += sizeof(u_int32_t);	dlen = _getshort(cp);	cp += sizeof(u_short);	if (name[0] == 0)		strcpy(name, "(root)");	/* Strip the domain name from the data, if desired. */	stripped = FALSE;	if ((_res.options & RES_DEBUG) == 0) {	    if (type != T_SOA) {		stripped = strip_domain(name, domain);	    }	}	if (!stripped && nameLen < sizeof(name)-1) {	    strcat(name, ".");	}	fprintf(file, NAME_FORMAT, name);	if (qtype == T_ANY) {	    if (_res.options & RES_DEBUG) {		fprintf(file,"\t%lu %-5s", ttl, p_class(queryClass));	    }	    fprintf(file," %-5s", p_type(type));	}	/* XXX merge this into debug.c's print routines */	switch (type) {	    case T_A:		if (class == C_IN) {		    bcopy(cp, (char *)&inaddr, sizeof(inaddr));		    if (dlen == 4) {			fprintf(file," %s", inet_ntoa(inaddr));		    } else if (dlen == 7) {			fprintf(file," %s", inet_ntoa(inaddr));			fprintf(file," (%d, %d)", cp[4],(cp[5] << 8) + cp[6]);		    } else			fprintf(file, " (dlen = %d?)", dlen);		}		break;	    case T_CNAME:	    case T_MB:	    case T_MG:	    case T_MR:		if ((nameLen = dn_expand(msg, eom,		    cp, (u_char *)name2, sizeof(name2))) < 0) {		    fprintf(file, " ***\n");		    return (ERROR);		}		fprintf(file, " %s", name2);		break;	    case T_NS:	    case T_PTR:		putc(' ', file);		if (qtype != T_ANY)		    fprintf(file,"%s = ", type == T_PTR ? "host" : "server");		cp = (u_char *)Print_cdname2(cp, msg, eom, file);		break;	    case T_HINFO:		if (n = *cp++) {		    (void)sprintf(name,"%.*s", n, cp);		    fprintf(file," %-10s", name);		    cp += n;		} else {		    fprintf(file," %-10s", " ");		}		if (n = *cp++) {		    fprintf(file,"  %.*s", n, cp);		    cp += n;		}		break;	    case T_SOA:		if ((nameLen = dn_expand(msg,		    eom, cp, (u_char *)name2, sizeof(name2))) < 0) {		    fprintf(file, " ***\n");		    return (ERROR);		}		cp += nameLen;		fprintf(file, " %s", name2);		if ((nameLen = dn_expand(msg,		    eom, cp, (u_char *)name2, sizeof(name2))) < 0) {		    fprintf(file, " ***\n");		    return (ERROR);		}		cp += nameLen;		fprintf(file, " %s. (", name2);		for (n = 0; n < 5; n++) {		    u_int32_t u;		    u = _getlong(cp);		    cp += sizeof(u_int32_t);		    fprintf(file,"%s%lu", n? " " : "", u);		}		fprintf(file, ")");		break;	    case T_MX:	    case T_AFSDB:		pref = _getshort(cp);		cp += sizeof(u_short);		fprintf(file," %-3d ",pref);		if ((nameLen = dn_expand(msg,		    eom, cp, (u_char *)name2, sizeof(name2))) < 0) {		    fprintf(file, " ***\n");		    return (ERROR);		}		fprintf(file, " %s", name2);		break;	    case T_TXT:		{		    u_char *cp2 = cp + dlen;		    int c;		    (void) fputs(" \"", file);		    while (cp < cp2) {			    if (n = (unsigned char) *cp++) {				    for (c = n; c > 0 && cp < cp2; c--)					    if (*cp == '\n') {						(void) putc('\\', file);						(void) putc(*cp++, file);					    } else						(void) putc(*cp++, file);			    }		    }		    (void) putc('"', file);		}		break;	    case T_MINFO:	    case T_RP:		(void) putc(' ', file);		cp = (u_char *)Print_cdname(cp, msg, eom, file);		fprintf(file, "  ");		cp = (u_char *)Print_cdname(cp, msg, eom, file);		break;	    case T_UINFO:		fprintf(file, " %s", cp);		break;	    case T_UID:	    case T_GID:		fprintf(file, " %lu", _getlong(cp));		break;	    case T_WKS:		if (class == C_IN) {		    struct protoent *pp;		    struct servent *ss;		    u_short port;		    cp += 4; 	/* skip inet address */		    dlen -= 4;		    setprotoent(1);		    setservent(1);		    n = *cp & 0377;		    pp = getprotobynumber(n);		    if (pp == 0)			fprintf(file," %-3d ", n);		    else			fprintf(file," %-3s ", pp->p_name);		    cp++; dlen--;		    port = 0;		    while (dlen-- > 0) {			n = *cp++;			do {			    if (n & 0200) {				ss = getservbyport((int)htons(port),					    pp->p_name);				if (ss == 0)				    fprintf(file," %u", port);				else				    fprintf(file," %s", ss->s_name);			    }				n <<= 1;			} while (++port & 07);		    }		    endprotoent();		    endservent();		}		break;	}	fprintf(file,"\n");    }    return(SUCCESS);}/* ******************************************************************************* * *  ViewList -- * *	A hack to view the output of the ls command in sorted *	order using more. * ******************************************************************************* */ViewList(string)    char *string;{    char file[PATH_MAX];    char command[PATH_MAX];    sscanf(string, " view %s", file);    (void)sprintf(command, "grep \"^ \" %s | sort | %s", file, pager);    system(command);}/* ******************************************************************************* * *   Finger -- * *	Connects with the finger server for the current host *	to request info on the specified person (long form) *	who is on the system (short form). * *  Results: *	SUCCESS		the finger server was contacted. *	ERROR		the server could not be contacted because *			a socket could not be obtained or connected *			to or the service could not be found. * ******************************************************************************* */Finger(string, putToFile)    char *string;    int  putToFile;{	struct servent		*sp;	struct sockaddr_in	sin;	register FILE		*f;	register int		c;	register int		lastc;	char			name[NAME_LEN];	char			file[NAME_LEN];	/*	 *  We need a valid current host info to get an inet address.	 */	if (!curHostValid) {	    fprintf(stderr, "Finger: no current host defined.\n");	    return (ERROR);	}	if (sscanf(string, " finger %s", name) == 1) {	    if (putToFile && (name[0] == '>')) {		name[0] = '\0';	    }	} else {	    name[0] = '\0';	}	sp = getservbyname("finger", "tcp");	if (sp == 0) {	    fprintf(stderr, "Finger: unknown service\n");	    return (ERROR);	}	bzero((char *)&sin, sizeof(sin));	sin.sin_family	= curHostInfo.addrType;	sin.sin_port	= sp->s_port;	bcopy(curHostInfo.addrList[0], (char *)&sin.sin_addr,		curHostInfo.addrLen);	/*	 *  Set up a virtual circuit to the host.	 */	sockFD = socket(curHostInfo.addrType, SOCK_STREAM, 0);	if (sockFD < 0) {	    fflush(stdout);	    perror("finger: socket");	    return (ERROR);	}	if (connect(sockFD, (struct sockaddr *)&sin, sizeof (sin)) < 0) {	    fflush(stdout);	    perror("finger: connect");	    close(sockFD);	    sockFD = -1;	    return (ERROR);	}	if (!putToFile) {	    filePtr = stdout;	} else {	    filePtr = OpenFile(string, file);	    if (filePtr == NULL) {		fprintf(stderr, "*** Can't open %s for writing\n", file);		close(sockFD);		sockFD = -1;		return(ERROR);	    }	    fprintf(filePtr,"> %s\n", string);	}	fprintf(filePtr, "[%s]\n", curHostInfo.name);	if (name[0] != '\0') {	    write(sockFD, "/W ", 3);	}	write(sockFD, name, strlen(name));	write(sockFD, "\r\n", 2);	f = fdopen(sockFD, "r");	while ((c = getc(f)) != EOF) {	    switch (c) {		case 0210:		case 0211:		case 0212:		case 0214:			c -= 0200;			break;		case 0215:			c = '\n';			break;	    }	    putc(lastc = c, filePtr);	}	if (lastc != '\n') {	    putc('\n', filePtr);	}	putc('\n', filePtr);	close(sockFD);	sockFD = -1;	if (putToFile) {	    fclose(filePtr);	    filePtr = NULL;	}	return (SUCCESS);}ListHost_close(){    if (sockFD != -1) {	(void) close(sockFD);	sockFD = -1;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -