LIB.TXT
上传用户:jnzhq888
上传日期:2007-01-18
资源大小:51694k
文件大小:1006k
- 26645 }
- 26646 break;
- 26647 }
- 26648 return (cp - comp_dn);
- 26649 }
-
- 26651 /*
- 26652 * Search for expanded name from a list of previously compressed names.
- 26653 * Return the offset from msg if found or -1.
- 26654 * dnptrs is the pointer to the first name on the list,
- 26655 * not the pointer to the start of the message.
- 26656 */
- 26657 static int
- 26658 dn_find(exp_dn, msg, dnptrs, lastdnptr)
- 26659 CONST u_char *exp_dn, *msg;
- 26660 u_char **dnptrs, **lastdnptr;
- 26661 {
- 26662 CONST register u_char *dn, *cp;
- 26663 register u_char **cpp;
- 26664 register int n;
- 26665 CONST u_char *sp;
- 26666
- 26667 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
- 26668 dn = exp_dn;
- 26669 sp = cp = *cpp;
- 26670 while (n = *cp++) {
- 26671 /*
- 26672 * check for indirection
- 26673 */
- 26674 switch (n & INDIR_MASK) {
- 26675 case 0: /* normal case, n == len */
- 26676 while (--n >= 0) {
- 26677 if (*dn == '.')
- 26678 goto next;
- 26679 if (*dn == '\')
- 26680 dn++;
- 26681 if (*dn++ != *cp++)
- 26682 goto next;
- 26683 }
- 26684 if ((n = *dn++) == ' ' && *cp == ' ')
- 26685 return (sp - msg);
- 26686 if (n == '.')
- 26687 continue;
- 26688 goto next;
- 26689
- 26690 default: /* illegal type */
- 26691 return (-1);
- 26692
- 26693 case INDIR_MASK: /* indirection */
- 26694 cp = msg + (((n & 0x3f) << 8) | *cp);
- 26695 }
- 26696 }
- 26697 if (*dn == ' ')
- 26698 return (sp - msg);
- 26699 next: ;
- 26700 }
- 26701 return (-1);
- 26702 }
-
- 26704 /*
- 26705 * Routines to insert/extract short/long's. Must account for byte
- 26706 * order and non-alignment problems. This code at least has the
- 26707 * advantage of being portable.
- 26708 *
- 26709 * used by sendmail.
- 26710 */
- 26711
- 26712 u16_t
- 26713 getshort(msgp)
- 26714 CONST u8_t *msgp;
- 26715 {
- 26716 return ((msgp[0] << 8) | (msgp[1] << 0));
- 26717 }
-
- 26719 u32_t
- 26720 getlong(msgp)
- 26721 CONST u8_t *msgp;
- 26722 {
- 26723 return ( ((u32_t) msgp[0] << 24)
- 26724 | ((u32_t) msgp[1] << 16)
- 26725 | ((u32_t) msgp[2] << 8)
- 26726 | ((u32_t) msgp[3] << 0));
- 26727 }
-
-
- 26730 void
- 26731 putshort(s, msgp)
- 26732 register U16_t s;
- 26733 register u8_t *msgp;
- 26734 {
- 26735
- 26736 msgp[1] = s;
- 26737 msgp[0] = s >> 8;
- 26738 }
-
- 26740 void
- 26741 putlong(l, msgp)
- 26742 register u32_t l;
- 26743 register u8_t *msgp;
- 26744 {
- 26745
- 26746 msgp[3] = l;
- 26747 msgp[2] = (l >>= 8);
- 26748 msgp[1] = (l >>= 8);
- 26749 msgp[0] = l >> 8;
- 26750 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/ip/res_init.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 26800 /*-
- 26801 * Copyright (c) 1985, 1989 Regents of the University of California.
- 26802 * All rights reserved.
- 26803 *
- 26804 * Redistribution and use in source and binary forms are permitted provided
- 26805 * that: (1) source distributions retain this entire copyright notice and
- 26806 * comment, and (2) distributions including binaries display the following
- 26807 * acknowledgement: ``This product includes software developed by the
- 26808 * University of California, Berkeley and its contributors'' in the
- 26809 * documentation or other materials provided with the distribution and in
- 26810 * all advertising materials mentioning features or use of this software.
- 26811 * Neither the name of the University nor the names of its contributors may
- 26812 * be used to endorse or promote products derived from this software without
- 26813 * specific prior written permission.
- 26814 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
- 26815 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
- 26816 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- 26817 */
- 26818
- 26819 #if defined(LIBC_SCCS) && !defined(lint)
- 26820 static char sccsid[] = "@(#)res_init.c 6.14 (Berkeley) 6/27/90";
- 26821 #endif /* LIBC_SCCS and not lint */
- 26822
- 26823 #if _MINIX
- 26824 #include <sys/types.h>
- 26825 #include <stdio.h>
- 26826 #include <stdlib.h>
- 26827 #include <string.h>
- 26828 #include <unistd.h>
- 26829
- 26830 #include <net/hton.h>
- 26831 #include <net/gen/in.h>
- 26832 #include <net/gen/inet.h>
- 26833 #include <net/gen/nameser.h>
- 26834 #include <net/gen/netdb.h>
- 26835 #include <net/gen/resolv.h>
- 26836 #include <net/gen/socket.h>
- 26837
- 26838 #define index(s,c) strchr(s,c)
- 26839 #else
- 26840 #include <sys/types.h>
- 26841 #include <sys/socket.h>
- 26842 #include <netinet/in.h>
- 26843 #include <stdio.h>
- 26844 #include <arpa/nameser.h>
- 26845 #include <resolv.h>
- 26846 #endif
- 26847
- 26848 /*
- 26849 * Resolver state
- 26850 */
- 26851 struct state _res;
- 26852
- 26853 /*
- 26854 * Set up default settings. If the configuration file exist, the values
- 26855 * there will have precedence. Otherwise, the server address is set to
- 26856 * 127.0.0.1 (localhost) and the default domain name comes from gethostname().
- 26857 *
- 26858 * The configuration file should only be used if you want to redefine your
- 26859 * domain or run without a server on your machine.
- 26860 *
- 26861 * Return 0 if completes successfully, -1 on error
- 26862 */
- 26863 int
- 26864 res_init()
- 26865 {
- 26866 register FILE *fp;
- 26867 register char *cp, **pp;
- 26868 register int n;
- 26869 char buf[BUFSIZ];
- 26870 int haveenv = 0;
- 26871 int havesearch = 0;
- 26872 struct servent* servent;
- 26873 u16_t nameserver_port;
- 26874
- 26875 /* Resolver state default settings */
- 26876 _res.retrans = RES_TIMEOUT; /* retransmition time interval */
- 26877 _res.retry = 4; /* number of times to retransmit */
- 26878 _res.options = RES_DEFAULT; /* options flags */
- 26879 _res.nscount = 0; /* number of name servers */
- 26880
- 26881 servent= getservbyname("domain", NULL);
- 26882 if (!servent)
- 26883 {
- 26884 h_errno= NO_RECOVERY;
- 26885 return -1;
- 26886 }
- 26887 nameserver_port= servent->s_port;
- 26888
- 26889 /* Allow user to override the local domain definition */
- 26890 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
- 26891 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
- 26892 haveenv++;
- 26893 }
- 26894
- 26895 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
- 26896 /* read the config file */
- 26897 while (fgets(buf, sizeof(buf), fp) != NULL) {
- 26898 /* read default domain name */
- 26899 if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
- 26900 if (haveenv) /* skip if have from environ */
- 26901 continue;
- 26902 cp = buf + sizeof("domain") - 1;
- 26903 while (*cp == ' ' || *cp == 't')
- 26904 cp++;
- 26905 if ((*cp == ' ') || (*cp == 'n'))
- 26906 continue;
- 26907 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
- 26908 if ((cp = index(_res.defdname, 'n')) != NULL)
- 26909 *cp = ' ';
- 26910 havesearch = 0;
- 26911 continue;
- 26912 }
- 26913 /* set search list */
- 26914 if (!strncmp(buf, "search", sizeof("search") - 1)) {
- 26915 if (haveenv) /* skip if have from environ */
- 26916 continue;
- 26917 cp = buf + sizeof("search") - 1;
- 26918 while (*cp == ' ' || *cp == 't')
- 26919 cp++;
- 26920 if ((*cp == ' ') || (*cp == 'n'))
- 26921 continue;
- 26922 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
- 26923 if ((cp = index(_res.defdname, 'n')) != NULL)
- 26924 *cp = ' ';
- 26925 /*
- 26926 * Set search list to be blank-separated strings
- 26927 * on rest of line.
- 26928 */
- 26929 cp = _res.defdname;
- 26930 pp = _res.dnsrch;
- 26931 *pp++ = cp;
- 26932 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
- 26933 if (*cp == ' ' || *cp == 't') {
- 26934 *cp = 0;
- 26935 n = 1;
- 26936 } else if (n) {
- 26937 *pp++ = cp;
- 26938 n = 0;
- 26939 }
- 26940 }
- 26941 /* null terminate last domain if there are excess */
- 26942 while (*cp != ' ' && *cp != ' ' && *cp != 't')
- 26943 cp++;
- 26944 *cp = ' ';
- 26945 *pp++ = 0;
- 26946 havesearch = 1;
- 26947 continue;
- 26948 }
- 26949 /* read nameservers to query */
- 26950 if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) &&
- 26951 _res.nscount < MAXNS) {
- 26952 cp = buf + sizeof("nameserver") - 1;
- 26953 while (*cp == ' ' || *cp == 't')
- 26954 cp++;
- 26955 if ((*cp == ' ') || (*cp == 'n'))
- 26956 continue;
- 26957 if (!inet_aton(cp, &_res.nsaddr_list[_res.nscount]))
- 26958 continue;
- 26959 _res.nsport_list[_res.nscount]= nameserver_port;
- 26960 _res.nscount++;
- 26961 continue;
- 26962 }
- 26963 }
- 26964 (void) fclose(fp);
- 26965 }
- 26966 if (_res.nscount == 0) {
- 26967 /* "localhost" is the default nameserver. */
- 26968 _res.nsaddr_list[0]= HTONL(0x7F000001);
- 26969 _res.nsport_list[0]= nameserver_port;
- 26970 _res.nscount= 1;
- 26971 }
- 26972 if (_res.defdname[0] == 0) {
- 26973 if (gethostname(buf, sizeof(_res.defdname)) == 0 &&
- 26974 (cp = index(buf, '.')))
- 26975 (void)strcpy(_res.defdname, cp + 1);
- 26976 }
- 26977
- 26978 /* find components of local domain that might be searched */
- 26979 if (havesearch == 0) {
- 26980 pp = _res.dnsrch;
- 26981 *pp++ = _res.defdname;
- 26982 for (cp = _res.defdname, n = 0; *cp; cp++)
- 26983 if (*cp == '.')
- 26984 n++;
- 26985 cp = _res.defdname;
- 26986 for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH;
- 26987 n--) {
- 26988 cp = index(cp, '.');
- 26989 *pp++ = ++cp;
- 26990 }
- 26991 *pp++ = 0;
- 26992 }
- 26993 _res.options |= RES_INIT;
- 26994 return (0);
- 26995 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/ip/res_mkquery.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 27000 /*
- 27001 * Copyright (c) 1985 Regents of the University of California.
- 27002 * All rights reserved.
- 27003 *
- 27004 * Redistribution and use in source and binary forms are permitted
- 27005 * provided that: (1) source distributions retain this entire copyright
- 27006 * notice and comment, and (2) distributions including binaries display
- 27007 * the following acknowledgement: ``This product includes software
- 27008 * developed by the University of California, Berkeley and its contributors''
- 27009 * in the documentation or other materials provided with the distribution
- 27010 * and in all advertising materials mentioning features or use of this
- 27011 * software. Neither the name of the University nor the names of its
- 27012 * contributors may be used to endorse or promote products derived
- 27013 * from this software without specific prior written permission.
- 27014 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- 27015 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- 27016 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- 27017 */
- 27018
- 27019 #if defined(LIBC_SCCS) && !defined(lint)
- 27020 static char sccsid[] = "@(#)res_mkquery.c 6.12 (Berkeley) 6/1/90";
- 27021 #endif /* LIBC_SCCS and not lint */
- 27022
- 27023 #if _MINIX
- 27024 #include <sys/types.h>
- 27025 #include <stdio.h>
- 27026 #include <stdlib.h>
- 27027 #include <string.h>
- 27028
- 27029 #include <net/hton.h>
- 27030 #include <net/gen/in.h>
- 27031 #include <net/gen/nameser.h>
- 27032 #include <net/gen/resolv.h>
- 27033
- 27034 typedef u16_t u_short;
- 27035 typedef unsigned u_int;
- 27036 typedef u32_t u_long;
- 27037
- 27038 #define bzero(b,l) memset(b,0,l)
- 27039 #define bcopy(s,d,l) memcpy(d,s,l)
- 27040
- 27041 #define putshort __putshort
- 27042 #define putlong __putlong
- 27043 #else
- 27044 #include <stdio.h>
- 27045 #include <sys/types.h>
- 27046 #include <netinet/in.h>
- 27047 #include <arpa/nameser.h>
- 27048 #include <resolv.h>
- 27049 #endif
- 27050
- 27051 #ifdef __STDC__
- 27052 #define _CONST const
- 27053 #else
- 27054 #define _CONST
- 27055 #endif
- 27056
- 27057 /*
- 27058 * Form all types of queries.
- 27059 * Returns the size of the result or -1.
- 27060 */
- 27061 res_mkquery(op, dname, class, type, data, datalen, newrr, buf, buflen)
- 27062 int op; /* opcode of query */
- 27063 _CONST char *dname; /* domain name */
- 27064 int class, type; /* class and type of query */
- 27065 _CONST char *data; /* resource record data */
- 27066 int datalen; /* length of data */
- 27067 _CONST struct rrec *newrr; /* new rr for modify or append */
- 27068 char *buf; /* buffer to put query */
- 27069 int buflen; /* size of buffer */
- 27070 {
- 27071 register dns_hdr_t *hp;
- 27072 register char *cp;
- 27073 register int n;
- 27074 char *dnptrs[10], **dpp, **lastdnptr;
- 27075
- 27076 #ifdef DEBUG
- 27077 if (_res.options & RES_DEBUG)
- 27078 printf("res_mkquery(%d, %s, %d, %d)n", op, dname, class, type);
- 27079 #endif /* DEBUG */
- 27080 /*
- 27081 * Initialize header fields.
- 27082 */
- 27083 if ((buf == NULL) || (buflen < sizeof(dns_hdr_t)))
- 27084 return(-1);
- 27085 bzero(buf, sizeof(dns_hdr_t));
- 27086 hp = (dns_hdr_t *) buf;
- 27087 hp->dh_id = htons(++_res.id);
- 27088 hp->dh_flag1= 0;
- 27089 hp->dh_flag2= 0;
- 27090 hp->dh_flag1 |= (op << 3) & DHF_OPCODE;
- 27091 hp->dh_flag2 |= ((_res.options & RES_PRIMARY) != 0 ? 1 : 0) << 6;
- 27092 hp->dh_flag1 |= (_res.options & RES_RECURSE) != 0 ? 1 : 0;
- 27093 hp->dh_flag2 |= NOERROR & DHF_RCODE;
- 27094 cp = buf + sizeof(dns_hdr_t);
- 27095 buflen -= sizeof(dns_hdr_t);
- 27096 dpp = dnptrs;
- 27097 *dpp++ = buf;
- 27098 *dpp++ = NULL;
- 27099 lastdnptr = dnptrs + sizeof(dnptrs)/sizeof(dnptrs[0]);
- 27100 /*
- 27101 * perform opcode specific processing
- 27102 */
- 27103 switch (op) {
- 27104 case QUERY:
- 27105 if ((buflen -= QFIXEDSZ) < 0)
- 27106 return(-1);
- 27107 if ((n = dn_comp((u8_t *)dname, (u8_t *)cp, buflen,
- 27108 (u8_t **)dnptrs, (u8_t **)lastdnptr)) < 0)
- 27109 return (-1);
- 27110 cp += n;
- 27111 buflen -= n;
- 27112 putshort(type, (u8_t *)cp);
- 27113 cp += sizeof(u_short);
- 27114 putshort(class, (u8_t *)cp);
- 27115 cp += sizeof(u_short);
- 27116 hp->dh_qdcount = htons(1);
- 27117 if (op == QUERY || data == NULL)
- 27118 break;
- 27119 /*
- 27120 * Make an additional record for completion domain.
- 27121 */
- 27122 buflen -= RRFIXEDSZ;
- 27123 if ((n = dn_comp((u8_t *)data, (u8_t *)cp, buflen,
- 27124 (u8_t **)dnptrs, (u8_t **)lastdnptr)) < 0)
- 27125 return (-1);
- 27126 cp += n;
- 27127 buflen -= n;
- 27128 putshort(T_NULL, (u8_t *)cp);
- 27129 cp += sizeof(u_short);
- 27130 putshort(class, (u8_t *)cp);
- 27131 cp += sizeof(u_short);
- 27132 putlong(0, (u8_t *)cp);
- 27133 cp += sizeof(u_long);
- 27134 putshort(0, (u8_t *)cp);
- 27135 cp += sizeof(u_short);
- 27136 hp->dh_arcount = htons(1);
- 27137 break;
- 27138
- 27139 case IQUERY:
- 27140 /*
- 27141 * Initialize answer section
- 27142 */
- 27143 if (buflen < 1 + RRFIXEDSZ + datalen)
- 27144 return (-1);
- 27145 *cp++ = ' '; /* no domain name */
- 27146 putshort(type, (u8_t *)cp);
- 27147 cp += sizeof(u_short);
- 27148 putshort(class, (u8_t *)cp);
- 27149 cp += sizeof(u_short);
- 27150 putlong(0, (u8_t *)cp);
- 27151 cp += sizeof(u_long);
- 27152 putshort(datalen, (u8_t *)cp);
- 27153 cp += sizeof(u_short);
- 27154 if (datalen) {
- 27155 bcopy(data, cp, datalen);
- 27156 cp += datalen;
- 27157 }
- 27158 hp->dh_ancount = htons(1);
- 27159 break;
- 27160
- 27161 #ifdef ALLOW_UPDATES
- 27162 /*
- 27163 * For UPDATEM/UPDATEMA, do UPDATED/UPDATEDA followed by UPDATEA
- 27164 * (Record to be modified is followed by its replacement in msg.)
- 27165 */
- 27166 case UPDATEM:
- 27167 case UPDATEMA:
- 27168
- 27169 case UPDATED:
- 27170 /*
- 27171 * The res code for UPDATED and UPDATEDA is the same; user
- 27172 * calls them differently: specifies data for UPDATED; server
- 27173 * ignores data if specified for UPDATEDA.
- 27174 */
- 27175 case UPDATEDA:
- 27176 buflen -= RRFIXEDSZ + datalen;
- 27177 if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
- 27178 return (-1);
- 27179 cp += n;
- 27180 putshort(type, cp);
- 27181 cp += sizeof(u_short);
- 27182 putshort(class, cp);
- 27183 cp += sizeof(u_short);
- 27184 putlong(0, cp);
- 27185 cp += sizeof(u_long);
- 27186 putshort(datalen, cp);
- 27187 cp += sizeof(u_short);
- 27188 if (datalen) {
- 27189 bcopy(data, cp, datalen);
- 27190 cp += datalen;
- 27191 }
- 27192 if ( (op == UPDATED) || (op == UPDATEDA) ) {
- 27193 hp->ancount = htons(0);
- 27194 break;
- 27195 }
- 27196 /* Else UPDATEM/UPDATEMA, so drop into code for UPDATEA */
- 27197
- 27198 case UPDATEA: /* Add new resource record */
- 27199 buflen -= RRFIXEDSZ + datalen;
- 27200 if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
- 27201 return (-1);
- 27202 cp += n;
- 27203 putshort(newrr->r_type, cp);
- 27204 cp += sizeof(u_short);
- 27205 putshort(newrr->r_class, cp);
- 27206 cp += sizeof(u_short);
- 27207 putlong(0, cp);
- 27208 cp += sizeof(u_long);
- 27209 putshort(newrr->r_size, cp);
- 27210 cp += sizeof(u_short);
- 27211 if (newrr->r_size) {
- 27212 bcopy(newrr->r_data, cp, newrr->r_size);
- 27213 cp += newrr->r_size;
- 27214 }
- 27215 hp->ancount = htons(0);
- 27216 break;
- 27217
- 27218 #endif /* ALLOW_UPDATES */
- 27219 }
- 27220 return (cp - buf);
- 27221 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/ip/res_query.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 27300 /*
- 27301 * Copyright (c) 1988 Regents of the University of California.
- 27302 * All rights reserved.
- 27303 *
- 27304 * Redistribution and use in source and binary forms are permitted
- 27305 * provided that: (1) source distributions retain this entire copyright
- 27306 * notice and comment, and (2) distributions including binaries display
- 27307 * the following acknowledgement: ``This product includes software
- 27308 * developed by the University of California, Berkeley and its contributors''
- 27309 * in the documentation or other materials provided with the distribution
- 27310 * and in all advertising materials mentioning features or use of this
- 27311 * software. Neither the name of the University nor the names of its
- 27312 * contributors may be used to endorse or promote products derived
- 27313 * from this software without specific prior written permission.
- 27314 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- 27315 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- 27316 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- 27317 */
- 27318
- 27319 #if defined(LIBC_SCCS) && !defined(lint)
- 27320 static char sccsid[] = "@(#)res_query.c 5.7 (Berkeley) 6/1/90";
- 27321 #endif /* LIBC_SCCS and not lint */
- 27322
- 27323 #if _MINIX
- 27324 #include <sys/types.h>
- 27325 #include <ctype.h>
- 27326 #include <errno.h>
- 27327 #include <stdio.h>
- 27328 #include <stdlib.h>
- 27329 #include <string.h>
- 27330
- 27331 #include <net/hton.h>
- 27332 #include <net/gen/in.h>
- 27333 #include <net/gen/nameser.h>
- 27334 #include <net/gen/netdb.h>
- 27335 #include <net/gen/resolv.h>
- 27336
- 27337 typedef u8_t u_char;
- 27338
- 27339 #define bcopy(s,d,l) memcpy(d,s,l)
- 27340
- 27341 #define hostalias __hostalias
- 27342 #else
- 27343 #include <sys/param.h>
- 27344 #include <sys/socket.h>
- 27345 #include <netinet/in.h>
- 27346 #include <ctype.h>
- 27347 #include <netdb.h>
- 27348 #include <stdio.h>
- 27349 #include <errno.h>
- 27350 #include <string.h>
- 27351 #include <arpa/inet.h>
- 27352 #include <arpa/nameser.h>
- 27353 #include <resolv.h>
- 27354
- 27355 extern int errno;
- 27356 #endif
- 27357
- 27358 #if __STDC__
- 27359 #define CONST const
- 27360 #else
- 27361 #define CONST
- 27362 #endif
- 27363
- 27364 #if PACKETSZ > 1024
- 27365 #define MAXPACKET PACKETSZ
- 27366 #else
- 27367 #define MAXPACKET 1024
- 27368 #endif
- 27369
- 27370 int h_errno;
- 27371
- 27372 /*
- 27373 * Formulate a normal query, send, and await answer.
- 27374 * Returned answer is placed in supplied buffer "answer".
- 27375 * Perform preliminary check of answer, returning success only
- 27376 * if no error is indicated and the answer count is nonzero.
- 27377 * Return the size of the response on success, -1 on error.
- 27378 * Error number is left in h_errno.
- 27379 * Caller must parse answer and determine whether it answers the question.
- 27380 */
- 27381 int
- 27382 res_query(name, class, type, answer, anslen)
- 27383 char *name; /* domain name */
- 27384 int class, type; /* class and type of query */
- 27385 u_char *answer; /* buffer to put answer */
- 27386 int anslen; /* size of answer buffer */
- 27387 {
- 27388 char buf[MAXPACKET];
- 27389 dns_hdr_t *hp;
- 27390 int n;
- 27391
- 27392 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
- 27393 return (-1);
- 27394 #ifdef DEBUG
- 27395 if (_res.options & RES_DEBUG)
- 27396 printf("res_query(%s, %d, %d)n", name, class, type);
- 27397 #endif
- 27398 n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL,
- 27399 buf, sizeof(buf));
- 27400
- 27401 if (n <= 0) {
- 27402 #ifdef DEBUG
- 27403 if (_res.options & RES_DEBUG)
- 27404 printf("res_query: mkquery failedn");
- 27405 #endif
- 27406 h_errno = NO_RECOVERY;
- 27407 return (n);
- 27408 }
- 27409 n = res_send(buf, n, (char *)answer, anslen);
- 27410 if (n < 0) {
- 27411 #ifdef DEBUG
- 27412 if (_res.options & RES_DEBUG)
- 27413 printf("res_query: send error(%d)n", errno);
- 27414 #endif
- 27415 h_errno = TRY_AGAIN;
- 27416 return(n);
- 27417 }
- 27418
- 27419 hp = (dns_hdr_t *) answer;
- 27420 if ((hp->dh_flag2 & DHF_RCODE) != NOERROR ||
- 27421 ntohs(hp->dh_ancount) == 0) {
- 27422 #ifdef DEBUG
- 27423 if (_res.options & RES_DEBUG)
- 27424 printf("rcode = %d, ancount=%dn",
- 27425 hp->dh_flag2 & DHF_RCODE,
- 27426 ntohs(hp->dh_ancount));
- 27427 #endif
- 27428 switch (hp->dh_flag2 & DHF_RCODE) {
- 27429 case NXDOMAIN:
- 27430 h_errno = HOST_NOT_FOUND;
- 27431 break;
- 27432 case SERVFAIL:
- 27433 h_errno = TRY_AGAIN;
- 27434 break;
- 27435 case NOERROR:
- 27436 h_errno = NO_DATA;
- 27437 break;
- 27438 case FORMERR:
- 27439 case NOTIMP:
- 27440 case REFUSED:
- 27441 default:
- 27442 h_errno = NO_RECOVERY;
- 27443 break;
- 27444 }
- 27445 return (-1);
- 27446 }
- 27447 return(n);
- 27448 }
-
- 27450 /*
- 27451 * Formulate a normal query, send, and retrieve answer in supplied buffer.
- 27452 * Return the size of the response on success, -1 on error.
- 27453 * If enabled, implement search rules until answer or unrecoverable failure
- 27454 * is detected. Error number is left in h_errno.
- 27455 * Only useful for queries in the same name hierarchy as the local host
- 27456 * (not, for example, for host address-to-name lookups in domain in-addr.arpa).
- 27457 */
- 27458 res_search(name, class, type, answer, anslen)
- 27459 char *name; /* domain name */
- 27460 int class, type; /* class and type of query */
- 27461 u_char *answer; /* buffer to put answer */
- 27462 int anslen; /* size of answer */
- 27463 {
- 27464 register char *cp, **domain;
- 27465 int n, ret, got_nodata = 0;
- 27466
- 27467 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
- 27468 return (-1);
- 27469
- 27470 errno = 0;
- 27471 h_errno = HOST_NOT_FOUND; /* default, if we never query */
- 27472 for (cp = name, n = 0; *cp; cp++)
- 27473 if (*cp == '.')
- 27474 n++;
- 27475 if (n == 0 && (cp = hostalias(name)))
- 27476 return (res_query(cp, class, type, answer, anslen));
- 27477
- 27478 /*
- 27479 * We do at least one level of search if
- 27480 * - there is no dot and RES_DEFNAME is set, or
- 27481 * - there is at least one dot, there is no trailing dot,
- 27482 * and RES_DNSRCH is set.
- 27483 */
- 27484 if ((n == 0 && _res.options & RES_DEFNAMES) ||
- 27485 (n != 0 && *--cp != '.' && _res.options & RES_DNSRCH))
- 27486 for (domain = _res.dnsrch; *domain; domain++) {
- 27487 ret = res_querydomain(name, *domain, class, type,
- 27488 answer, anslen);
- 27489 if (ret > 0)
- 27490 return (ret);
- 27491 /*
- 27492 * If no server present, give up.
- 27493 * If name isn't found in this domain,
- 27494 * keep trying higher domains in the search list
- 27495 * (if that's enabled).
- 27496 * On a NO_DATA error, keep trying, otherwise
- 27497 * a wildcard entry of another type could keep us
- 27498 * from finding this entry higher in the domain.
- 27499 * If we get some other error (negative answer or
- 27500 * server failure), then stop searching up,
- 27501 * but try the input name below in case it's fully-qualified.
- 27502 */
- 27503 if (errno == ECONNREFUSED) {
- 27504 h_errno = TRY_AGAIN;
- 27505 return (-1);
- 27506 }
- 27507 if (h_errno == NO_DATA)
- 27508 got_nodata++;
- 27509 if ((h_errno != HOST_NOT_FOUND && h_errno != NO_DATA) ||
- 27510 (_res.options & RES_DNSRCH) == 0)
- 27511 break;
- 27512 }
- 27513 /*
- 27514 * If the search/default failed, try the name as fully-qualified,
- 27515 * but only if it contained at least one dot (even trailing).
- 27516 * This is purely a heuristic; we assume that any reasonable query
- 27517 * about a top-level domain (for servers, SOA, etc) will not use
- 27518 * res_search.
- 27519 */
- 27520 if (n && (ret = res_querydomain(name, (char *)NULL, class, type,
- 27521 answer, anslen)) > 0)
- 27522 return (ret);
- 27523 if (got_nodata)
- 27524 h_errno = NO_DATA;
- 27525 return (-1);
- 27526 }
-
- 27528 /*
- 27529 * Perform a call on res_query on the concatenation of name and domain,
- 27530 * removing a trailing dot from name if domain is NULL.
- 27531 */
- 27532 int
- 27533 res_querydomain(name, domain, class, type, answer, anslen)
- 27534 char *name, *domain;
- 27535 int class, type; /* class and type of query */
- 27536 u_char *answer; /* buffer to put answer */
- 27537 int anslen; /* size of answer */
- 27538 {
- 27539 char nbuf[2*MAXDNAME+2];
- 27540 char *longname = nbuf;
- 27541 int n;
- 27542
- 27543 #ifdef DEBUG
- 27544 if (_res.options & RES_DEBUG)
- 27545 printf("res_querydomain(%s, %s, %d, %d)n",
- 27546 name, domain, class, type);
- 27547 #endif
- 27548 if (domain == NULL) {
- 27549 /*
- 27550 * Check for trailing '.';
- 27551 * copy without '.' if present.
- 27552 */
- 27553 n = strlen(name) - 1;
- 27554 if (name[n] == '.' && n < sizeof(nbuf) - 1) {
- 27555 bcopy(name, nbuf, n);
- 27556 nbuf[n] = ' ';
- 27557 } else
- 27558 longname = name;
- 27559 } else
- 27560 (void)sprintf(nbuf, "%.*s.%.*s",
- 27561 MAXDNAME, name, MAXDNAME, domain);
- 27562
- 27563 return (res_query(longname, class, type, answer, anslen));
- 27564 }
-
- 27566 char *
- 27567 hostalias(name)
- 27568 register CONST char *name;
- 27569 {
- 27570 register char *C1, *C2;
- 27571 FILE *fp;
- 27572 char *file;
- 27573 char buf[BUFSIZ];
- 27574 static char abuf[MAXDNAME];
- 27575
- 27576 file = getenv("HOSTALIASES");
- 27577 if (file == NULL || (fp = fopen(file, "r")) == NULL)
- 27578 return (NULL);
- 27579 buf[sizeof(buf) - 1] = ' ';
- 27580 while (fgets(buf, sizeof(buf), fp)) {
- 27581 for (C1 = buf; *C1 && !isspace(*C1); ++C1);
- 27582 if (!*C1)
- 27583 break;
- 27584 *C1 = ' ';
- 27585 if (!strcasecmp(buf, name)) {
- 27586 while (isspace(*++C1));
- 27587 if (!*C1)
- 27588 break;
- 27589 for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2);
- 27590 abuf[sizeof(abuf) - 1] = *C2 = ' ';
- 27591 (void)strncpy(abuf, C1, sizeof(abuf) - 1);
- 27592 fclose(fp);
- 27593 return (abuf);
- 27594 }
- 27595 }
- 27596 fclose(fp);
- 27597 return (NULL);
- 27598 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/ip/res_send.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 27600 /*
- 27601 * Copyright (c) 1985, 1989 Regents of the University of California.
- 27602 * All rights reserved.
- 27603 *
- 27604 * Redistribution and use in source and binary forms, with or without
- 27605 * modification, are permitted provided that the following conditions
- 27606 * are met:
- 27607 * 1. Redistributions of source code must retain the above copyright
- 27608 * notice, this list of conditions and the following disclaimer.
- 27609 * 2. Redistributions in binary form must reproduce the above copyright
- 27610 * notice, this list of conditions and the following disclaimer in the
- 27611 * documentation and/or other materials provided with the distribution.
- 27612 * 3. All advertising materials mentioning features or use of this software
- 27613 * must display the following acknowledgement:
- 27614 * This product includes software developed by the University of
- 27615 * California, Berkeley and its contributors.
- 27616 * 4. Neither the name of the University nor the names of its contributors
- 27617 * may be used to endorse or promote products derived from this software
- 27618 * without specific prior written permission.
- 27619 *
- 27620 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- 27621 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- 27622 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- 27623 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- 27624 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- 27625 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- 27626 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- 27627 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- 27628 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- 27629 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- 27630 * SUCH DAMAGE.
- 27631 */
- 27632
- 27633 #if defined(LIBC_SCCS) && !defined(lint)
- 27634 static char sccsid[] = "@(#)res_send.c 6.27 (Berkeley) 2/24/91";
- 27635 #endif /* LIBC_SCCS and not lint */
- 27636
- 27637 /*
- 27638 * Send query to name server and wait for reply.
- 27639 */
- 27640
- 27641 #if !_MINIX
- 27642 #include <sys/param.h>
- 27643 #include <sys/time.h>
- 27644 #include <sys/socket.h>
- 27645 #include <sys/uio.h>
- 27646 #include <netinet/in.h>
- 27647 #include <arpa/nameser.h>
- 27648 #include <stdio.h>
- 27649 #include <errno.h>
- 27650 #include <resolv.h>
- 27651 #include <unistd.h>
- 27652 #include <string.h>
- 27653
- 27654 #else /* _MINIX */
- 27655
- 27656 #include <sys/types.h>
- 27657 #include <sys/ioctl.h>
- 27658 #include <sys/stat.h>
- 27659 #include <assert.h>
- 27660 #include <errno.h>
- 27661 #include <fcntl.h>
- 27662 #include <signal.h>
- 27663 #include <stdio.h>
- 27664 #include <stdlib.h>
- 27665 #include <string.h>
- 27666 #include <unistd.h>
- 27667
- 27668 #include <net/hton.h>
- 27669
- 27670 #include <net/netlib.h>
- 27671 #include <net/gen/in.h>
- 27672 #include <net/gen/inet.h>
- 27673 #include <net/gen/netdb.h>
- 27674 #include <net/gen/nameser.h>
- 27675 #include <net/gen/resolv.h>
- 27676 #include <net/gen/tcp.h>
- 27677 #include <net/gen/tcp_io.h>
- 27678 #include <net/gen/udp.h>
- 27679 #include <net/gen/udp_hdr.h>
- 27680 #include <net/gen/udp_io.h>
- 27681
- 27682 typedef u16_t u_short;
- 27683
- 27684 static int tcp_connect _ARGS(( ipaddr_t host, Tcpport_t port, int *terrno ));
- 27685 static int tcpip_writeall _ARGS(( int fd, const char *buf, size_t siz ));
- 27686 static int udp_connect _ARGS(( void ));
- 27687 static int udp_sendto _ARGS(( int fd, const char *buf, unsigned buflen,
- 27688 ipaddr_t addr, Udpport_t port ));
- 27689 static int udp_receive _ARGS(( int fd, char *buf, unsigned buflen,
- 27690 time_t timeout ));
- 27691 static void alarm_handler _ARGS(( int sig ));
- 27692
- 27693 #endif /* !_MINIX */
- 27694
- 27695 static int s = -1; /* socket used for communications */
- 27696 #if !_MINIX
- 27697 static struct sockaddr no_addr;
- 27698
- 27699 #ifndef FD_SET
- 27700 #define NFDBITS 32
- 27701 #define FD_SETSIZE 32
- 27702 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
- 27703 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
- 27704 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
- 27705 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
- 27706 #endif /* FD_SET */
- 27707 #endif /* _MINIX */
- 27708
- 27709 res_send(buf, buflen, answer, anslen)
- 27710 const char *buf;
- 27711 int buflen;
- 27712 char *answer;
- 27713 int anslen;
- 27714 {
- 27715 register int n;
- 27716 int try, v_circuit, resplen, ns;
- 27717 int gotsomewhere = 0, connected = 0;
- 27718 int connreset = 0;
- 27719 #if !_MINIX
- 27720 u_short id, len;
- 27721 #else /* _MINIX */
- 27722 u16_t id, len;
- 27723 #endif /* !_MINIX */
- 27724 char *cp;
- 27725 #if !_MINIX
- 27726 fd_set dsmask;
- 27727 struct timeval timeout;
- 27728 HEADER *hp = (HEADER *) buf;
- 27729 HEADER *anhp = (HEADER *) answer;
- 27730 struct iovec iov[2];
- 27731 #else /* _MINIX */
- 27732 time_t timeout;
- 27733 dns_hdr_t *hp = (dns_hdr_t *) buf;
- 27734 dns_hdr_t *anhp = (dns_hdr_t *) answer;
- 27735 #endif /* !_MINIX */
- 27736 int terrno = ETIMEDOUT;
- 27737 char junk[512];
- 27738
- 27739 #ifdef DEBUG
- 27740 if (_res.options & RES_DEBUG) {
- 27741 printf("res_send()n");
- 27742 __p_query(buf);
- 27743 }
- 27744 #endif /* DEBUG */
- 27745 if (!(_res.options & RES_INIT))
- 27746 if (res_init() == -1) {
- 27747 return(-1);
- 27748 }
- 27749
- 27750 v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ;
- 27751 #if !_MINIX
- 27752 id = hp->id;
- 27753 #else /* _MINIX */
- 27754 id = hp->dh_id;
- 27755 #endif /* !_MINIX */
- 27756 /*
- 27757 * Send request, RETRY times, or until successful
- 27758 */
- 27759 for (try = 0; try < _res.retry; try++) {
- 27760 for (ns = 0; ns < _res.nscount; ns++) {
- 27761 #ifdef DEBUG
- 27762 #if !_MINIX
- 27763 if (_res.options & RES_DEBUG)
- 27764 printf("Querying server (# %d) address = %sn", ns+1,
- 27765 inet_ntoa(_res.nsaddr_list[ns].sin_addr));
- 27766 #else /* _MINIX */
- 27767 if (_res.options & RES_DEBUG)
- 27768 printf("Querying server (# %d) address = %sn", ns+1,
- 27769 inet_ntoa(_res.nsaddr_list[ns]));
- 27770 #endif /* !_MINIX */
- 27771 #endif /* DEBUG */
- 27772 usevc:
- 27773 if (v_circuit) {
- 27774 #if !_MINIX
- 27775 int truncated = 0;
- 27776
- 27777 /*
- 27778 * Use virtual circuit;
- 27779 * at most one attempt per server.
- 27780 */
- 27781 try = _res.retry;
- 27782 if (s < 0) {
- 27783 s = socket(AF_INET, SOCK_STREAM, 0);
- 27784 if (s < 0) {
- 27785 terrno = errno;
- 27786 #ifdef DEBUG
- 27787 if (_res.options & RES_DEBUG)
- 27788 perror("socket (vc) failed");
- 27789 #endif /* DEBUG */
- 27790 continue;
- 27791 }
- 27792 if (connect(s,
- 27793 (struct sockaddr *)&(_res.nsaddr_list[ns]),
- 27794 sizeof(struct sockaddr)) < 0) {
- 27795 terrno = errno;
- 27796 #ifdef DEBUG
- 27797 if (_res.options & RES_DEBUG)
- 27798 perror("connect failed");
- 27799 #endif /* DEBUG */
- 27800 (void) close(s);
- 27801 s = -1;
- 27802 continue;
- 27803 }
- 27804 }
- 27805 /*
- 27806 * Send length & message
- 27807 */
- 27808 len = htons((u_short)buflen);
- 27809 iov[0].iov_base = (caddr_t)&len;
- 27810 iov[0].iov_len = sizeof(len);
- 27811 iov[1].iov_base = (char *)buf;
- 27812 iov[1].iov_len = buflen;
- 27813 if (writev(s, iov, 2) != sizeof(len) + buflen) {
- 27814 terrno = errno;
- 27815 #ifdef DEBUG
- 27816 if (_res.options & RES_DEBUG)
- 27817 perror("write failed");
- 27818 #endif /* DEBUG */
- 27819 (void) close(s);
- 27820 s = -1;
- 27821 continue;
- 27822 }
- 27823 /*
- 27824 * Receive length & response
- 27825 */
- 27826 cp = answer;
- 27827 len = sizeof(short);
- 27828 while (len != 0 &&
- 27829 (n = read(s, (char *)cp, (int)len)) > 0) {
- 27830 cp += n;
- 27831 len -= n;
- 27832 }
- 27833 if (n <= 0) {
- 27834 terrno = errno;
- 27835 #ifdef DEBUG
- 27836 if (_res.options & RES_DEBUG)
- 27837 perror("read failed");
- 27838 #endif /* DEBUG */
- 27839 (void) close(s);
- 27840 s = -1;
- 27841 /*
- 27842 * A long running process might get its TCP
- 27843 * connection reset if the remote server was
- 27844 * restarted. Requery the server instead of
- 27845 * trying a new one. When there is only one
- 27846 * server, this means that a query might work
- 27847 * instead of failing. We only allow one reset
- 27848 * per query to prevent looping.
- 27849 */
- 27850 if (terrno == ECONNRESET && !connreset) {
- 27851 connreset = 1;
- 27852 ns--;
- 27853 }
- 27854 continue;
- 27855 }
- 27856 cp = answer;
- 27857 if ((resplen = ntohs(*(u_short *)cp)) > anslen) {
- 27858 #ifdef DEBUG
- 27859 if (_res.options & RES_DEBUG)
- 27860 fprintf(stderr, "response truncatedn");
- 27861 #endif /* DEBUG */
- 27862 len = anslen;
- 27863 truncated = 1;
- 27864 } else
- 27865 len = resplen;
- 27866 while (len != 0 &&
- 27867 (n = read(s, (char *)cp, (int)len)) > 0) {
- 27868 cp += n;
- 27869 len -= n;
- 27870 }
- 27871 if (n <= 0) {
- 27872 terrno = errno;
- 27873 #ifdef DEBUG
- 27874 if (_res.options & RES_DEBUG)
- 27875 perror("read failed");
- 27876 #endif /* DEBUG */
- 27877 (void) close(s);
- 27878 s = -1;
- 27879 continue;
- 27880 }
- 27881 if (truncated) {
- 27882 /*
- 27883 * Flush rest of answer
- 27884 * so connection stays in synch.
- 27885 */
- 27886 anhp->tc = 1;
- 27887 len = resplen - anslen;
- 27888 while (len != 0) {
- 27889 n = (len > sizeof(junk) ?
- 27890 sizeof(junk) : len);
- 27891 if ((n = read(s, junk, n)) > 0)
- 27892 len -= n;
- 27893 else
- 27894 break;
- 27895 }
- 27896 }
- 27897 #else /* _MINIX */
- 27898 int truncated = 0;
- 27899 int nbytes;
- 27900
- 27901 /*
- 27902 * Use virtual circuit;
- 27903 * at most one attempt per server.
- 27904 */
- 27905 try = _res.retry;
- 27906 if (s < 0)
- 27907 {
- 27908 s= tcp_connect(_res.nsaddr_list[ns],
- 27909 _res.nsport_list[ns], &terrno);
- 27910 if (s == -1)
- 27911 continue;
- 27912 }
- 27913 /*
- 27914 * Send length & message
- 27915 */
- 27916 len = htons((u_short)buflen);
- 27917 nbytes= tcpip_writeall(s, (char *)&len,
- 27918 sizeof(len));
- 27919 if (nbytes != sizeof(len))
- 27920 {
- 27921 terrno= errno;
- 27922 #ifdef DEBUG
- 27923 if (_res.options & RES_DEBUG)
- 27924 fprintf(stderr, "write failed: %sn",
- 27925 strerror(terrno));
- 27926 #endif /* DEBUG */
- 27927 close(s);
- 27928 s= -1;
- 27929 continue;
- 27930 }
- 27931 nbytes= tcpip_writeall(s, buf, buflen);
- 27932 if (nbytes != buflen)
- 27933 {
- 27934 terrno= errno;
- 27935 #ifdef DEBUG
- 27936 if (_res.options & RES_DEBUG)
- 27937 fprintf(stderr, "write failed: %sn",
- 27938 strerror(terrno));
- 27939 #endif /* DEBUG */
- 27940 close(s);
- 27941 s= -1;
- 27942 continue;
- 27943 }
- 27944 /*
- 27945 * Receive length & response
- 27946 */
- 27947 cp = answer;
- 27948 len = sizeof(short);
- 27949 while (len != 0)
- 27950 {
- 27951 n = read(s, (char *)cp, (int)len);
- 27952 if (n <= 0)
- 27953 break;
- 27954 cp += n;
- 27955 assert(len >= n);
- 27956 len -= n;
- 27957 }
- 27958 if (len) {
- 27959 terrno = errno;
- 27960 #ifdef DEBUG
- 27961 if (_res.options & RES_DEBUG)
- 27962 fprintf(stderr, "read failed: %sn",
- 27963 strerror(terrno));
- 27964 #endif /* DEBUG */
- 27965 close(s);
- 27966 s= -1;
- 27967 /*
- 27968 * A long running process might get its TCP
- 27969 * connection reset if the remote server was
- 27970 * restarted. Requery the server instead of
- 27971 * trying a new one. When there is only one
- 27972 * server, this means that a query might work
- 27973 * instead of failing. We only allow one reset
- 27974 * per query to prevent looping.
- 27975 */
- 27976 if (terrno == ECONNRESET && !connreset) {
- 27977 connreset = 1;
- 27978 ns--;
- 27979 }
- 27980 continue;
- 27981 }
- 27982 cp = answer;
- 27983 if ((resplen = ntohs(*(u_short *)cp)) > anslen) {
- 27984 #ifdef DEBUG
- 27985 if (_res.options & RES_DEBUG)
- 27986 fprintf(stderr, "response truncatedn");
- 27987 #endif /* DEBUG */
- 27988 len = anslen;
- 27989 truncated = 1;
- 27990 } else
- 27991 len = resplen;
- 27992 while (len != 0)
- 27993 {
- 27994 n= read(s, (char *)cp, (int)len);
- 27995 if (n <= 0)
- 27996 break;
- 27997 cp += n;
- 27998 assert(len >= n);
- 27999 len -= n;
- 28000 }
- 28001 if (len) {
- 28002 terrno = errno;
- 28003 #ifdef DEBUG
- 28004 if (_res.options & RES_DEBUG)
- 28005 fprintf(stderr, "read failed: %sn",
- 28006 strerror(terrno));
- 28007 #endif /* DEBUG */
- 28008 close(s);
- 28009 s= -1;
- 28010 continue;
- 28011 }
- 28012 if (truncated) {
- 28013 /*
- 28014 * Flush rest of answer
- 28015 * so connection stays in synch.
- 28016 */
- 28017 anhp->dh_flag1 |= DHF_TC;
- 28018 len = resplen - anslen;
- 28019 while (len != 0) {
- 28020 n = (len > sizeof(junk) ?
- 28021 sizeof(junk) : len);
- 28022 n = read(s, junk, n);
- 28023 if (n <= 0)
- 28024 {
- 28025 assert(len >= n);
- 28026 len -= n;
- 28027 }
- 28028 else
- 28029 break;
- 28030 }
- 28031 }
- 28032 #endif /* _MINIX */
- 28033 } else {
- 28034 #if !_MINIX
- 28035 /*
- 28036 * Use datagrams.
- 28037 */
- 28038 if (s < 0) {
- 28039 s = socket(AF_INET, SOCK_DGRAM, 0);
- 28040 if (s < 0) {
- 28041 terrno = errno;
- 28042 #ifdef DEBUG
- 28043 if (_res.options & RES_DEBUG)
- 28044 perror("socket (dg) failed");
- 28045 #endif /* DEBUG */
- 28046 continue;
- 28047 }
- 28048 }
- 28049 #if BSD >= 43
- 28050 /*
- 28051 * I'm tired of answering this question, so:
- 28052 * On a 4.3BSD+ machine (client and server,
- 28053 * actually), sending to a nameserver datagram
- 28054 * port with no nameserver will cause an
- 28055 * ICMP port unreachable message to be returned.
- 28056 * If our datagram socket is "connected" to the
- 28057 * server, we get an ECONNREFUSED error on the next
- 28058 * socket operation, and select returns if the
- 28059 * error message is received. We can thus detect
- 28060 * the absence of a nameserver without timing out.
- 28061 * If we have sent queries to at least two servers,
- 28062 * however, we don't want to remain connected,
- 28063 * as we wish to receive answers from the first
- 28064 * server to respond.
- 28065 */
- 28066 if (_res.nscount == 1 || (try == 0 && ns == 0)) {
- 28067 /*
- 28068 * Don't use connect if we might
- 28069 * still receive a response
- 28070 * from another server.
- 28071 */
- 28072 if (connected == 0) {
- 28073 if (connect(s, (struct sockaddr *)&_res.nsaddr_list[ns],
- 28074 sizeof(struct sockaddr)) < 0) {
- 28075 #ifdef DEBUG
- 28076 if (_res.options & RES_DEBUG)
- 28077 perror("connect");
- 28078 #endif /* DEBUG */
- 28079 continue;
- 28080 }
- 28081 connected = 1;
- 28082 }
- 28083 if (send(s, buf, buflen, 0) != buflen) {
- 28084 #ifdef DEBUG
- 28085 if (_res.options & RES_DEBUG)
- 28086 perror("send");
- 28087 #endif /* DEBUG */
- 28088 continue;
- 28089 }
- 28090 } else {
- 28091 /*
- 28092 * Disconnect if we want to listen
- 28093 * for responses from more than one server.
- 28094 */
- 28095 if (connected) {
- 28096 (void) connect(s, &no_addr,
- 28097 sizeof(no_addr));
- 28098 connected = 0;
- 28099 }
- 28100 #endif /* BSD */
- 28101 if (sendto(s, buf, buflen, 0,
- 28102 (struct sockaddr *)&_res.nsaddr_list[ns],
- 28103 sizeof(struct sockaddr)) != buflen) {
- 28104 #ifdef DEBUG
- 28105 if (_res.options & RES_DEBUG)
- 28106 perror("sendto");
- 28107 #endif /* DEBUG */
- 28108 continue;
- 28109 }
- 28110 #if BSD >= 43
- 28111 }
- 28112 #endif /* BSD */
- 28113
- 28114 /*
- 28115 * Wait for reply
- 28116 */
- 28117 timeout.tv_sec = (_res.retrans << try);
- 28118 if (try > 0)
- 28119 timeout.tv_sec /= _res.nscount;
- 28120 if (timeout.tv_sec <= 0)
- 28121 timeout.tv_sec = 1;
- 28122 timeout.tv_usec = 0;
- 28123 wait:
- 28124 FD_ZERO(&dsmask);
- 28125 FD_SET(s, &dsmask);
- 28126 n = select(s+1, &dsmask, (fd_set *)NULL,
- 28127 (fd_set *)NULL, &timeout);
- 28128 if (n < 0) {
- 28129 #ifdef DEBUG
- 28130 if (_res.options & RES_DEBUG)
- 28131 perror("select");
- 28132 #endif /* DEBUG */
- 28133 continue;
- 28134 }
- 28135 if (n == 0) {
- 28136 /*
- 28137 * timeout
- 28138 */
- 28139 #ifdef DEBUG
- 28140 if (_res.options & RES_DEBUG)
- 28141 printf("timeoutn");
- 28142 #endif /* DEBUG */
- 28143 #if BSD >= 43
- 28144 gotsomewhere = 1;
- 28145 #endif
- 28146 continue;
- 28147 }
- 28148 if ((resplen = recv(s, answer, anslen, 0)) <= 0) {
- 28149 #ifdef DEBUG
- 28150 if (_res.options & RES_DEBUG)
- 28151 perror("recvfrom");
- 28152 #endif /* DEBUG */
- 28153 continue;
- 28154 }
- 28155 gotsomewhere = 1;
- 28156 if (id != anhp->id) {
- 28157 /*
- 28158 * response from old query, ignore it
- 28159 */
- 28160 #ifdef DEBUG
- 28161 if (_res.options & RES_DEBUG) {
- 28162 printf("old answer:n");
- 28163 __p_query(answer);
- 28164 }
- 28165 #endif /* DEBUG */
- 28166 goto wait;
- 28167 }
- 28168 if (!(_res.options & RES_IGNTC) && anhp->tc) {
- 28169 /*
- 28170 * get rest of answer;
- 28171 * use TCP with same server.
- 28172 */
- 28173 #ifdef DEBUG
- 28174 if (_res.options & RES_DEBUG)
- 28175 printf("truncated answern");
- 28176 #endif /* DEBUG */
- 28177 (void) close(s);
- 28178 s = -1;
- 28179 v_circuit = 1;
- 28180 goto usevc;
- 28181 }
- 28182 #else /* _MINIX */
- 28183 /*
- 28184 * Use datagrams.
- 28185 */
- 28186 if (s < 0) {
- 28187 s = udp_connect();
- 28188 if (s < 0) {
- 28189 terrno = errno;
- 28190 #ifdef DEBUG
- 28191 if (_res.options & RES_DEBUG)
- 28192 perror("udp_connect failed");
- 28193 #endif /* DEBUG */
- 28194 continue;
- 28195 }
- 28196 }
- 28197 if (udp_sendto(s, buf, buflen, _res.nsaddr_list[ns],
- 28198 _res.nsport_list[ns]) != buflen) {
- 28199 #ifdef DEBUG
- 28200 if (_res.options & RES_DEBUG)
- 28201 perror("sendto");
- 28202 #endif /* DEBUG */
- 28203 continue;
- 28204 }
- 28205
- 28206 /*
- 28207 * Wait for reply
- 28208 */
- 28209 timeout= (_res.retrans << try);
- 28210 if (try > 0)
- 28211 timeout /= _res.nscount;
- 28212 if (timeout <= 0)
- 28213 timeout= 1;
- 28214 wait:
- 28215 if ((resplen= udp_receive(s, answer, anslen, timeout))
- 28216 == -1)
- 28217 {
- 28218 if (errno == EINTR)
- 28219 {
- 28220 /*
- 28221 * timeout
- 28222 */
- 28223 #ifdef DEBUG
- 28224 if (_res.options & RES_DEBUG)
- 28225 printf("timeoutn");
- 28226 #endif /* DEBUG */
- 28227 gotsomewhere = 1;
- 28228 }
- 28229 else
- 28230 {
- 28231 #ifdef DEBUG
- 28232 if (_res.options & RES_DEBUG)
- 28233 perror("udp_receive");
- 28234 #endif /* DEBUG */
- 28235 }
- 28236 continue;
- 28237 }
- 28238 gotsomewhere = 1;
- 28239 if (id != anhp->dh_id) {
- 28240 /*
- 28241 * response from old query, ignore it
- 28242 */
- 28243 #ifdef DEBUG
- 28244 if (_res.options & RES_DEBUG) {
- 28245 printf("old answer:n");
- 28246 __p_query(answer);
- 28247 }
- 28248 #endif /* DEBUG */
- 28249 goto wait;
- 28250 }
- 28251 if (!(_res.options & RES_IGNTC) &&
- 28252 (anhp->dh_flag1 & DHF_TC)) {
- 28253 /*
- 28254 * get rest of answer;
- 28255 * use TCP with same server.
- 28256 */
- 28257 #ifdef DEBUG
- 28258 if (_res.options & RES_DEBUG)
- 28259 printf("truncated answern");
- 28260 #endif /* DEBUG */
- 28261 (void) close(s);
- 28262 s = -1;
- 28263 v_circuit = 1;
- 28264 goto usevc;
- 28265 }
- 28266 #endif /* !_MINIX */
- 28267 }
- 28268 #ifdef DEBUG
- 28269 if (_res.options & RES_DEBUG) {
- 28270 printf("got answer:n");
- 28271 __p_query(answer);
- 28272 }
- 28273 #endif /* DEBUG */
- 28274 /*
- 28275 * If using virtual circuits, we assume that the first server
- 28276 * is preferred * over the rest (i.e. it is on the local
- 28277 * machine) and only keep that one open.
- 28278 * If we have temporarily opened a virtual circuit,
- 28279 * or if we haven't been asked to keep a socket open,
- 28280 * close the socket.
- 28281 */
- 28282 if ((v_circuit &&
- 28283 ((_res.options & RES_USEVC) == 0 || ns != 0)) ||
- 28284 (_res.options & RES_STAYOPEN) == 0) {
- 28285 (void) close(s);
- 28286 s = -1;
- 28287 }
- 28288 return (resplen);
- 28289 }
- 28290 }
- 28291 if (s >= 0) {
- 28292 (void) close(s);
- 28293 s = -1;
- 28294 }
- 28295 if (v_circuit == 0)
- 28296 if (gotsomewhere == 0)
- 28297 errno = ECONNREFUSED; /* no nameservers found */
- 28298 else
- 28299 errno = ETIMEDOUT; /* no answer obtained */
- 28300 else
- 28301 errno = terrno;
- 28302 return (-1);
- 28303 }
-
- 28305 /*
- 28306 * This routine is for closing the socket if a virtual circuit is used and
- 28307 * the program wants to close it. This provides support for endhostent()
- 28308 * which expects to close the socket.
- 28309 *
- 28310 * This routine is not expected to be user visible.
- 28311 */
- 28312 void
- 28313 _res_close()
- 28314 {
- 28315 if (s != -1) {
- 28316 (void) close(s);
- 28317 s = -1;
- 28318 }
- 28319 }
-
- 28321 #if _MINIX
- 28322 static int tcp_connect(host, port, terrno)
- 28323 ipaddr_t host;
- 28324 tcpport_t port;
- 28325 int *terrno;
- 28326 {
- 28327 char *dev_name;
- 28328 int fd;
- 28329 int error;
- 28330 nwio_tcpconf_t tcpconf;
- 28331 nwio_tcpcl_t clopt;
- 28332
- 28333 dev_name= getenv("TCP_DEVICE");
- 28334 if (!dev_name)
- 28335 dev_name= TCP_DEVICE;
- 28336 fd= open(dev_name, O_RDWR);
- 28337 if (fd == -1)
- 28338 {
- 28339 *terrno= errno;
- 28340 return -1;
- 28341 }
- 28342 tcpconf.nwtc_flags= NWTC_EXCL | NWTC_LP_SEL | NWTC_SET_RA | NWTC_SET_RP;
- 28343 tcpconf.nwtc_remaddr= host;
- 28344 tcpconf.nwtc_remport= port;
- 28345 error= ioctl(fd, NWIOSTCPCONF, &tcpconf);
- 28346 if (error == -1)
- 28347 {
- 28348 *terrno= errno;
- 28349 close(fd);
- 28350 return -1;
- 28351 }
- 28352 clopt.nwtcl_flags= 0;
- 28353 error= ioctl(fd, NWIOTCPCONN, &clopt);
- 28354 if (error == -1)
- 28355 {
- 28356 *terrno= errno;
- 28357 close(fd);
- 28358 return -1;
- 28359 }
- 28360 *terrno= 0;
- 28361 return fd;
- 28362 }
-
- 28364 static int tcpip_writeall(fd, buf, siz)
- 28365 int fd;
- 28366 const char *buf;
- 28367 size_t siz;
- 28368 {
- 28369 size_t siz_org;
- 28370 int nbytes;
- 28371
- 28372 siz_org= siz;
- 28373
- 28374 while (siz)
- 28375 {
- 28376 nbytes= write(fd, buf, siz);
- 28377 if (nbytes <= 0)
- 28378 return siz_org-siz;
- 28379 assert(siz >= nbytes);
- 28380 buf += nbytes;
- 28381 siz -= nbytes;
- 28382 }
- 28383 return siz_org;
- 28384 }
-
-
- 28387 static int udp_connect()
- 28388 {
- 28389 nwio_udpopt_t udpopt;
- 28390 char *dev_name;
- 28391 int fd, r, terrno;
- 28392
- 28393 dev_name= getenv("UDP_DEVICE");
- 28394 if (!dev_name)
- 28395 dev_name= UDP_DEVICE;
- 28396 fd= open(dev_name, O_RDWR);
- 28397 if (fd == -1)
- 28398 return -1;
- 28399
- 28400 udpopt.nwuo_flags= NWUO_COPY | NWUO_LP_SEL | NWUO_EN_LOC |
- 28401 NWUO_EN_BROAD | NWUO_RP_ANY | NWUO_RA_ANY | NWUO_RWDATALL |
- 28402 NWUO_DI_IPOPT;
- 28403 r= ioctl(fd, NWIOSUDPOPT, &udpopt);
- 28404 if (r == -1)
- 28405 {
- 28406 terrno= errno;
- 28407 close(fd);
- 28408 errno= terrno;
- 28409 return -1;
- 28410 }
- 28411 return fd;
- 28412 }
-
- 28414 static int udp_sendto(fd, buf, buflen, addr, port)
- 28415 int fd;
- 28416 const char *buf;
- 28417 unsigned buflen;
- 28418 ipaddr_t addr;
- 28419 udpport_t port;
- 28420 {
- 28421 char *newbuf;
- 28422 udp_io_hdr_t *udp_io_hdr;
- 28423 int r, terrno;
- 28424
- 28425 newbuf= malloc(sizeof(*udp_io_hdr) + buflen);
- 28426 if (newbuf == NULL)
- 28427 {
- 28428 errno= ENOMEM;
- 28429 return -1;
- 28430 }
- 28431 udp_io_hdr= (udp_io_hdr_t *)newbuf;
- 28432 udp_io_hdr->uih_dst_addr= addr;
- 28433 udp_io_hdr->uih_dst_port= port;
- 28434 udp_io_hdr->uih_ip_opt_len= 0;
- 28435 udp_io_hdr->uih_data_len= buflen;
- 28436
- 28437 memcpy(newbuf + sizeof(*udp_io_hdr), buf, buflen);
- 28438 r= write(fd, newbuf, sizeof(*udp_io_hdr) + buflen);
- 28439 terrno= errno;
- 28440 free(newbuf);
- 28441 if (r >= sizeof(*udp_io_hdr))
- 28442 r -= sizeof(*udp_io_hdr);
- 28443 errno= terrno;
- 28444 return r;
- 28445 }
-
- 28447 static void alarm_handler(sig)
- 28448 int sig;
- 28449 {
- 28450 signal(SIGALRM, alarm_handler);
- 28451 alarm(1);
- 28452 }
-
- 28454 static int udp_receive(fd, buf, buflen, timeout)
- 28455 int fd;
- 28456 char *buf;
- 28457 unsigned buflen;
- 28458 time_t timeout;
- 28459 {
- 28460 char *newbuf;
- 28461 udp_io_hdr_t *udp_io_hdr;
- 28462 int r, terrno;
- 28463 void (*u_handler) _ARGS(( int sig ));
- 28464 time_t u_timeout;
- 28465
- 28466 newbuf= malloc(sizeof(*udp_io_hdr) + buflen);
- 28467 if (newbuf == NULL)
- 28468 {
- 28469 errno= ENOMEM;
- 28470 return -1;
- 28471 }
- 28472
- 28473 u_handler= signal(SIGALRM, alarm_handler);
- 28474 u_timeout= alarm(timeout);
- 28475
- 28476 r= read(fd, newbuf, sizeof(*udp_io_hdr) + buflen);
- 28477 terrno= errno;
- 28478
- 28479 if (r < 0 || r <= sizeof(*udp_io_hdr))
- 28480 {
- 28481 if (r > 0)
- 28482 r= 0;
- 28483 free(newbuf);
- 28484
- 28485
- 28486 alarm(0);
- 28487 signal(SIGALRM, u_handler);
- 28488 alarm(u_timeout);
- 28489
- 28490 errno= terrno;
- 28491 return r;
- 28492 }
- 28493
- 28494 memcpy(buf, newbuf + sizeof(*udp_io_hdr), r - sizeof(*udp_io_hdr));
- 28495 free(newbuf);
- 28496
- 28497 alarm(0);
- 28498 signal(SIGALRM, u_handler);
- 28499 alarm(u_timeout);
- 28500
- 28501 return r-sizeof(*udp_io_hdr);
- 28502 }
-
- 28504 #endif
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/ip/strcasecmp.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 28600 /*
- 28601 strcasecmp.c
- 28602
- 28603 Created Oct 14, 1991 by Philip Homburg
- 28604 */
- 28605
- 28606 #include <ctype.h>
- 28607 #include <string.h>
- 28608
- 28609 #ifdef __STDC__
- 28610 #define _CONST const
- 28611 #else
- 28612 #define _CONST
- 28613 #endif
- 28614
- 28615 int
- 28616 strcasecmp(s1, s2)
- 28617 _CONST char *s1, *s2;
- 28618 {
- 28619 int c1, c2;
- 28620 while (c1= toupper(*s1++), c2= toupper(*s2++), c1 == c2 && (c1 & c2))
- 28621 ;
- 28622 if (c1 & c2)
- 28623 return c1 < c2 ? -1 : 1;
- 28624 return c1 ? 1 : (c2 ? -1 : 0);
- 28625 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/liby/main.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 28700 /*-
- 28701 * Copyright (c) 1990 The Regents of the University of California.
- 28702 * All rights reserved.
- 28703 *
- 28704 * Redistribution and use in source and binary forms, with or without
- 28705 * modification, are permitted provided that the following conditions
- 28706 * are met:
- 28707 * 1. Redistributions of source code must retain the above copyright
- 28708 * notice, this list of conditions and the following disclaimer.
- 28709 * 2. Redistributions in binary form must reproduce the above copyright
- 28710 * notice, this list of conditions and the following disclaimer in the
- 28711 * documentation and/or other materials provided with the distribution.
- 28712 * 3. All advertising materials mentioning features or use of this software
- 28713 * must display the following acknowledgement:
- 28714 * This product includes software developed by the University of
- 28715 * California, Berkeley and its contributors.
- 28716 * 4. Neither the name of the University nor the names of its contributors
- 28717 * may be used to endorse or promote products derived from this software
- 28718 * without specific prior written permission.
- 28719 *
- 28720 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- 28721 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- 28722 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- 28723 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- 28724 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- 28725 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- 28726 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- 28727 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- 28728 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- 28729 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- 28730 * SUCH DAMAGE.
- 28731 */
- 28732
- 28733 #if defined(LIBC_SCCS) && !defined(lint)
- 28734 static char sccsid[] = "@(#)main.c 5.3 (Berkeley) 1/13/91";
- 28735 #endif /* not lint */
- 28736
- 28737 main()
- 28738 {
- 28739 exit(yyparse());
- 28740 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/liby/yyerror.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 28800 /*-
- 28801 * Copyright (c) 1990 The Regents of the University of California.
- 28802 * All rights reserved.
- 28803 *
- 28804 * Redistribution and use in source and binary forms, with or without
- 28805 * modification, are permitted provided that the following conditions
- 28806 * are met:
- 28807 * 1. Redistributions of source code must retain the above copyright
- 28808 * notice, this list of conditions and the following disclaimer.
- 28809 * 2. Redistributions in binary form must reproduce the above copyright
- 28810 * notice, this list of conditions and the following disclaimer in the
- 28811 * documentation and/or other materials provided with the distribution.
- 28812 * 3. All advertising materials mentioning features or use of this software
- 28813 * must display the following acknowledgement:
- 28814 * This product includes software developed by the University of
- 28815 * California, Berkeley and its contributors.
- 28816 * 4. Neither the name of the University nor the names of its contributors
- 28817 * may be used to endorse or promote products derived from this software
- 28818 * without specific prior written permission.
- 28819 *
- 28820 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- 28821 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- 28822 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- 28823 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- 28824 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- 28825 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- 28826 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- 28827 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- 28828 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- 28829 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- 28830 * SUCH DAMAGE.
- 28831 */
- 28832
- 28833 #if defined(LIBC_SCCS) && !defined(lint)
- 28834 static char sccsid[] = "@(#)yyerror.c 5.2 (Berkeley) 5/15/90";
- 28835 #endif /* not lint */
- 28836
- 28837 #include <stdio.h>
- 28838
- 28839 yyerror(msg)
- 28840 char *msg;
- 28841 {
- 28842 (void)fprintf(stderr, "%sn", msg);
- 28843 return(0);
- 28844 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/localmath.h
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 28900 /*
- 28901 * localmath.h - This header is used by the mathematical library.
- 28902 */
- 28903 /* $Header: localmath.h,v 1.2 89/12/18 15:43:05 eck Exp $ */
- 28904
- 28905 /* some constants (Hart & Cheney) */
- 28906 #define M_PI 3.14159265358979323846264338327950288
- 28907 #define M_2PI 6.28318530717958647692528676655900576
- 28908 #define M_3PI_4 2.35619449019234492884698253745962716
- 28909 #define M_PI_2 1.57079632679489661923132169163975144
- 28910 #define M_3PI_8 1.17809724509617246442349126872981358
- 28911 #define M_PI_4 0.78539816339744830961566084581987572
- 28912 #define M_PI_8 0.39269908169872415480783042290993786
- 28913 #define M_1_PI 0.31830988618379067153776752674502872
- 28914 #define M_2_PI 0.63661977236758134307553505349005744
- 28915 #define M_4_PI 1.27323954473516268615107010698011488
- 28916 #define M_E 2.71828182845904523536028747135266250
- 28917 #define M_LOG2E 1.44269504088896340735992468100189213
- 28918 #define M_LOG10E 0.43429448190325182765112891891660508
- 28919 #define M_LN2 0.69314718055994530941723212145817657
- 28920 #define M_LN10 2.30258509299404568401799145468436421
- 28921 #define M_SQRT2 1.41421356237309504880168872420969808
- 28922 #define M_1_SQRT2 0.70710678118654752440084436210484904
- 28923 #define M_EULER 0.57721566490153286060651209008240243
- 28924
- 28925 /* macros for constructing polynomials */
- 28926 #define POLYNOM1(x, a) ((a)[1]*(x)+(a)[0])
- 28927 #define POLYNOM2(x, a) (POLYNOM1((x),(a)+1)*(x)+(a)[0])
- 28928 #define POLYNOM3(x, a) (POLYNOM2((x),(a)+1)*(x)+(a)[0])
- 28929 #define POLYNOM4(x, a) (POLYNOM3((x),(a)+1)*(x)+(a)[0])
- 28930 #define POLYNOM5(x, a) (POLYNOM4((x),(a)+1)*(x)+(a)[0])
- 28931 #define POLYNOM6(x, a) (POLYNOM5((x),(a)+1)*(x)+(a)[0])
- 28932 #define POLYNOM7(x, a) (POLYNOM6((x),(a)+1)*(x)+(a)[0])
- 28933 #define POLYNOM8(x, a) (POLYNOM7((x),(a)+1)*(x)+(a)[0])
- 28934 #define POLYNOM9(x, a) (POLYNOM8((x),(a)+1)*(x)+(a)[0])
- 28935 #define POLYNOM10(x, a) (POLYNOM9((x),(a)+1)*(x)+(a)[0])
- 28936 #define POLYNOM11(x, a) (POLYNOM10((x),(a)+1)*(x)+(a)[0])
- 28937 #define POLYNOM12(x, a) (POLYNOM11((x),(a)+1)*(x)+(a)[0])
- 28938 #define POLYNOM13(x, a) (POLYNOM12((x),(a)+1)*(x)+(a)[0])
- 28939
- 28940 #define M_LN_MAX_D (M_LN2 * DBL_MAX_EXP)
- 28941 #define M_LN_MIN_D (M_LN2 * (DBL_MIN_EXP - 1))
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/asin.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 29000 /*
- 29001 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 29002 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 29003 *
- 29004 * Author: Ceriel J.H. Jacobs
- 29005 */
- 29006 /* $Header: asin.c,v 1.3 91/03/19 16:38:12 ceriel Exp $ */
- 29007
- 29008 #include <math.h>
- 29009 #include <errno.h>
- 29010 #include "localmath.h"
- 29011
- 29012 static double
- 29013 asin_acos(double x, int cosfl)
- 29014 {
- 29015 int negative = x < 0;
- 29016 int i;
- 29017 double g;
- 29018 static double p[] = {
- 29019 -0.27368494524164255994e+2,
- 29020 0.57208227877891731407e+2,
- 29021 -0.39688862997540877339e+2,
- 29022 0.10152522233806463645e+2,
- 29023 -0.69674573447350646411e+0
- 29024 };
- 29025 static double q[] = {
- 29026 -0.16421096714498560795e+3,
- 29027 0.41714430248260412556e+3,
- 29028 -0.38186303361750149284e+3,
- 29029 0.15095270841030604719e+3,
- 29030 -0.23823859153670238830e+2,
- 29031 1.0
- 29032 };
- 29033
- 29034 if (__IsNan(x)) {
- 29035 errno = EDOM;
- 29036 return x;
- 29037 }
- 29038
- 29039 if (negative) {
- 29040 x = -x;
- 29041 }
- 29042 if (x > 0.5) {
- 29043 i = 1;
- 29044 if (x > 1) {
- 29045 errno = EDOM;
- 29046 return 0;
- 29047 }
- 29048 g = 0.5 - 0.5 * x;
- 29049 x = - sqrt(g);
- 29050 x += x;
- 29051 }
- 29052 else {
- 29053 /* ??? avoid underflow ??? */
- 29054 i = 0;
- 29055 g = x * x;
- 29056 }
- 29057 x += x * g * POLYNOM4(g, p) / POLYNOM5(g, q);
- 29058 if (cosfl) {
- 29059 if (! negative) x = -x;
- 29060 }
- 29061 if ((cosfl == 0) == (i == 1)) {
- 29062 x = (x + M_PI_4) + M_PI_4;
- 29063 }
- 29064 else if (cosfl && negative && i == 1) {
- 29065 x = (x + M_PI_2) + M_PI_2;
- 29066 }
- 29067 if (! cosfl && negative) x = -x;
- 29068 return x;
- 29069 }
-
- 29071 double
- 29072 asin(double x)
- 29073 {
- 29074 return asin_acos(x, 0);
- 29075 }
-
- 29077 double
- 29078 acos(double x)
- 29079 {
- 29080 return asin_acos(x, 1);
- 29081 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/atan.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 29100 /*
- 29101 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 29102 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 29103 *
- 29104 * Author: Ceriel J.H. Jacobs
- 29105 */
- 29106 /* $Header: atan.c,v 1.3 91/03/19 16:38:21 ceriel Exp $ */
- 29107
- 29108 #include <float.h>
- 29109 #include <math.h>
- 29110 #include <errno.h>
- 29111 #include "localmath.h"
- 29112
- 29113 double
- 29114 atan(double x)
- 29115 {
- 29116 /* Algorithm and coefficients from:
- 29117 "Software manual for the elementary functions"
- 29118 by W.J. Cody and W. Waite, Prentice-Hall, 1980
- 29119 */
- 29120
- 29121 static double p[] = {
- 29122 -0.13688768894191926929e+2,
- 29123 -0.20505855195861651981e+2,
- 29124 -0.84946240351320683534e+1,
- 29125 -0.83758299368150059274e+0
- 29126 };
- 29127 static double q[] = {
- 29128 0.41066306682575781263e+2,
- 29129 0.86157349597130242515e+2,
- 29130 0.59578436142597344465e+2,
- 29131 0.15024001160028576121e+2,
- 29132 1.0
- 29133 };
- 29134 static double a[] = {
- 29135 0.0,
- 29136 0.52359877559829887307710723554658381, /* pi/6 */
- 29137 M_PI_2,
- 29138 1.04719755119659774615421446109316763 /* pi/3 */
- 29139 };
- 29140
- 29141 int neg = x < 0;
- 29142 int n;
- 29143 double g;
- 29144
- 29145 if (__IsNan(x)) {
- 29146 errno = EDOM;
- 29147 return x;
- 29148 }
- 29149 if (neg) {
- 29150 x = -x;
- 29151 }
- 29152 if (x > 1.0) {
- 29153 x = 1.0/x;
- 29154 n = 2;
- 29155 }
- 29156 else n = 0;
- 29157
- 29158 if (x > 0.26794919243112270647) { /* 2-sqtr(3) */
- 29159 n = n + 1;
- 29160 x = (((0.73205080756887729353*x-0.5)-0.5)+x)/
- 29161 (1.73205080756887729353+x);
- 29162 }
- 29163
- 29164 /* ??? avoid underflow ??? */
- 29165
- 29166 g = x * x;
- 29167 x += x * g * POLYNOM3(g, p) / POLYNOM4(g, q);
- 29168 if (n > 1) x = -x;
- 29169 x += a[n];
- 29170 return neg ? -x : x;
- 29171 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/atan2.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 29200 /*
- 29201 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 29202 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 29203 *
- 29204 * Author: Ceriel J.H. Jacobs
- 29205 */
- 29206 /* $Header: atan2.c,v 1.2 89/12/18 15:42:35 eck Exp $ */
- 29207
- 29208 #include <math.h>
- 29209 #include <errno.h>
- 29210 #include "localmath.h"
- 29211
- 29212 double
- 29213 atan2(double y, double x)
- 29214 {
- 29215 double absx, absy, val;
- 29216
- 29217 if (x == 0 && y == 0) {
- 29218 errno = EDOM;
- 29219 return 0;
- 29220 }
- 29221 absy = y < 0 ? -y : y;
- 29222 absx = x < 0 ? -x : x;
- 29223 if (absy - absx == absy) {
- 29224 /* x negligible compared to y */
- 29225 return y < 0 ? -M_PI_2 : M_PI_2;
- 29226 }
- 29227 if (absx - absy == absx) {
- 29228 /* y negligible compared to x */
- 29229 val = 0.0;
- 29230 }
- 29231 else val = atan(y/x);
- 29232 if (x > 0) {
- 29233 /* first or fourth quadrant; already correct */
- 29234 return val;
- 29235 }
- 29236 if (y < 0) {
- 29237 /* third quadrant */
- 29238 return val - M_PI;
- 29239 }
- 29240 return val + M_PI;
- 29241 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/ceil.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 29300 /*
- 29301 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 29302 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 29303 *
- 29304 * Author: Ceriel J.H. Jacobs
- 29305 */
- 29306 /* $Header: ceil.c,v 1.1 89/05/10 16:00:46 eck Exp $ */
- 29307
- 29308 #include <math.h>
- 29309
- 29310 double
- 29311 ceil(double x)
- 29312 {
- 29313 double val;
- 29314
- 29315 return modf(x, &val) > 0 ? val + 1.0 : val ;
- 29316 /* this also works if modf always returns a positive
- 29317 fractional part
- 29318 */
- 29319 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/exp.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 29400 /*
- 29401 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 29402 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 29403 *
- 29404 * Author: Ceriel J.H. Jacobs
- 29405 */
- 29406 /* $Header: exp.c,v 1.5 91/03/19 16:38:29 ceriel Exp $ */
- 29407
- 29408 #include <math.h>
- 29409 #include <float.h>
- 29410 #include <errno.h>
- 29411 #include "localmath.h"
- 29412
- 29413
- 29414 double
- 29415 exp(double x)
- 29416 {
- 29417 /* Algorithm and coefficients from:
- 29418 "Software manual for the elementary functions"
- 29419 by W.J. Cody and W. Waite, Prentice-Hall, 1980
- 29420 */
- 29421
- 29422 static double p[] = {
- 29423 0.25000000000000000000e+0,
- 29424 0.75753180159422776666e-2,
- 29425 0.31555192765684646356e-4
- 29426 };
- 29427
- 29428 static double q[] = {
- 29429 0.50000000000000000000e+0,
- 29430 0.56817302698551221787e-1,
- 29431 0.63121894374398503557e-3,
- 29432 0.75104028399870046114e-6
- 29433 };
- 29434 double xn, g;
- 29435 int n;
- 29436 int negative = x < 0;
- 29437
- 29438 if (__IsNan(x)) {
- 29439 errno = EDOM;
- 29440 return x;
- 29441 }
- 29442 if (x < M_LN_MIN_D) {
- 29443 errno = ERANGE;
- 29444 return 0.0;
- 29445 }
- 29446 if (x > M_LN_MAX_D) {
- 29447 errno = ERANGE;
- 29448 return HUGE_VAL;
- 29449 }
- 29450
- 29451 if (negative) x = -x;
- 29452
- 29453 /* ??? avoid underflow ??? */
- 29454
- 29455 n = x * M_LOG2E + 0.5; /* 1/ln(2) = log2(e), 0.5 added for rounding */
- 29456 xn = n;
- 29457 {
- 29458 double x1 = (long) x;
- 29459 double x2 = x - x1;
- 29460
- 29461 g = ((x1-xn*0.693359375)+x2) - xn*(-2.1219444005469058277e-4);
- 29462 }
- 29463 if (negative) {
- 29464 g = -g;
- 29465 n = -n;
- 29466 }
- 29467 xn = g * g;
- 29468 x = g * POLYNOM2(xn, p);
- 29469 n += 1;
- 29470 return (ldexp(0.5 + x/(POLYNOM3(xn, q) - x), n));
- 29471 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/fabs.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 29500 /*
- 29501 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 29502 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 29503 *
- 29504 * Author: Ceriel J.H. Jacobs
- 29505 */
- 29506 /* $Header: fabs.c,v 1.1 89/05/10 16:01:17 eck Exp $ */
- 29507
- 29508 double
- 29509 fabs(double x)
- 29510 {
- 29511 return x < 0 ? -x : x;
- 29512 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/floor.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 29600 /*
- 29601 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 29602 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 29603 *
- 29604 * Author: Ceriel J.H. Jacobs
- 29605 */
- 29606 /* $Header: floor.c,v 1.1 89/05/10 16:01:29 eck Exp $ */
- 29607
- 29608 #include <math.h>
- 29609
- 29610 double
- 29611 floor(double x)
- 29612 {
- 29613 double val;
- 29614
- 29615 return modf(x, &val) < 0 ? val - 1.0 : val ;
- 29616 /* this also works if modf always returns a positive
- 29617 fractional part
- 29618 */
- 29619 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/fmod.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 29700 /*
- 29701 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 29702 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 29703 *
- 29704 * Author: Hans van Eck
- 29705 */
- 29706 /* $Header: fmod.c,v 1.1 89/12/18 15:41:53 eck Exp $ */
- 29707
- 29708 #include <math.h>
- 29709 #include <errno.h>
- 29710
- 29711 double
- 29712 fmod(double x, double y)
- 29713 {
- 29714 long i;
- 29715 double val;
- 29716 double frac;
- 29717
- 29718 if (y == 0) {
- 29719 errno = EDOM;
- 29720 return 0;
- 29721 }
- 29722 frac = modf( x / y, &val);
- 29723
- 29724 return frac * y;
- 29725
- 29726 /*
- 29727 val = x / y;
- 29728 if (val > LONG_MIN && val < LONG_MAX) {
- 29729 i = val;
- 29730 return x - i * y;
- 29731 }
- 29732 */
- 29733 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/frexp.s
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 29800 #
- 29801 .sect .text; .sect .rom; .sect .data; .sect .bss
- 29802 .extern _frexp
- 29803 .sect .text
- 29804 _frexp:
- 29805 #if __i386
- 29806 push ebp
- 29807 mov ebp, esp
- 29808 push 12(ebp)
- 29809 push 8(ebp)
- 29810 mov eax, esp
- 29811 add eax, -4
- 29812 push eax
- 29813 call .fef8
- 29814 mov eax, 16(ebp)
- 29815 pop (eax)
- 29816 pop eax
- 29817 pop edx
- 29818 leave
- 29819 ret
- 29820 #else /* i86 */
- 29821 push bp
- 29822 mov bp, sp
- 29823 lea bx, 4(bp)
- 29824 mov cx, #8
- 29825 call .loi
- 29826 mov ax, sp
- 29827 add ax, #-2
- 29828 push ax
- 29829 call .fef8
- 29830 mov bx, 12(bp)
- 29831 pop (bx)
- 29832 call .ret8
- 29833 jmp .cret
- 29834 #endif
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/hugeval.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 29900 /*
- 29901 * (c) copyright 1990 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 29902 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 29903 *
- 29904 * Author: Hans van Eck
- 29905 */
- 29906 /* $Header: hugeval.c,v 1.1 90/10/24 14:29:42 eck Exp $ */
- 29907 #include <math.h>
- 29908
- 29909 double
- 29910 __huge_val(void)
- 29911 {
- 29912 return 1.0e+1000; /* This will generate a warning */
- 29913 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/isnan.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 30000 int __IsNan(double d)
- 30001 {
- 30002 #if defined(vax) || defined(pdp)
- 30003 #else
- 30004 float f = d;
- 30005
- 30006 if ((*((long *) &f) & 0x7f800000) == 0x7f800000 &&
- 30007 (*((long *) &f) & 0x007fffff) != 0) return 1;
- 30008 #endif
- 30009 return 0;
- 30010 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/ldexp.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 30100 /*
- 30101 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 30102 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 30103 */
- 30104 /* $Header: ldexp.c,v 1.5 91/03/19 16:38:37 ceriel Exp $ */
- 30105
- 30106 #include <math.h>
- 30107 #include <float.h>
- 30108 #include <errno.h>
- 30109
- 30110 double
- 30111 ldexp(double fl, int exp)
- 30112 {
- 30113 int sign = 1;
- 30114 int currexp;
- 30115
- 30116 if (__IsNan(fl)) {
- 30117 errno = EDOM;
- 30118 return fl;
- 30119 }
- 30120 if (fl == 0.0) return 0.0;
- 30121 if (fl<0) {
- 30122 fl = -fl;
- 30123 sign = -1;
- 30124 }
- 30125 if (fl > DBL_MAX) { /* for infinity */
- 30126 errno = ERANGE;
- 30127 return sign * fl;
- 30128 }
- 30129 fl = frexp(fl,&currexp);
- 30130 exp += currexp;
- 30131 if (exp > 0) {
- 30132 if (exp > DBL_MAX_EXP) {
- 30133 errno = ERANGE;
- 30134 return sign * HUGE_VAL;
- 30135 }
- 30136 while (exp>30) {
- 30137 fl *= (double) (1L << 30);
- 30138 exp -= 30;
- 30139 }
- 30140 fl *= (double) (1L << exp);
- 30141 }
- 30142 else {
- 30143 /* number need not be normalized */
- 30144 if (exp < DBL_MIN_EXP - DBL_MANT_DIG) {
- 30145 return 0.0;
- 30146 }
- 30147 while (exp<-30) {
- 30148 fl /= (double) (1L << 30);
- 30149 exp += 30;
- 30150 }
- 30151 fl /= (double) (1L << -exp);
- 30152 }
- 30153 return sign * fl;
- 30154 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/log.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 30200 /*
- 30201 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 30202 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 30203 *
- 30204 * Author: Ceriel J.H. Jacobs
- 30205 */
- 30206 /* $Header: log.c,v 1.3 91/03/19 16:38:45 ceriel Exp $ */
- 30207
- 30208 #include <math.h>
- 30209 #include <float.h>
- 30210 #include <errno.h>
- 30211 #include "localmath.h"
- 30212
- 30213 double
- 30214 log(double x)
- 30215 {
- 30216 /* Algorithm and coefficients from:
- 30217 "Software manual for the elementary functions"
- 30218 by W.J. Cody and W. Waite, Prentice-Hall, 1980
- 30219 */
- 30220 static double a[] = {
- 30221 -0.64124943423745581147e2,
- 30222 0.16383943563021534222e2,
- 30223 -0.78956112887491257267e0
- 30224 };
- 30225 static double b[] = {
- 30226 -0.76949932108494879777e3,
- 30227 0.31203222091924532844e3,
- 30228 -0.35667977739034646171e2,
- 30229 1.0
- 30230 };
- 30231
- 30232 double znum, zden, z, w;
- 30233 int exponent;
- 30234
- 30235 if (__IsNan(x)) {
- 30236 errno = EDOM;
- 30237 return x;
- 30238 }
- 30239 if (x < 0) {
- 30240 errno = EDOM;
- 30241 return -HUGE_VAL;
- 30242 }
- 30243 else if (x == 0) {
- 30244 errno = ERANGE;
- 30245 return -HUGE_VAL;
- 30246 }
- 30247
- 30248 if (x <= DBL_MAX) {
- 30249 }
- 30250 else return x; /* for infinity and Nan */
- 30251 x = frexp(x, &exponent);
- 30252 if (x > M_1_SQRT2) {
- 30253 znum = (x - 0.5) - 0.5;
- 30254 zden = x * 0.5 + 0.5;
- 30255 }
- 30256 else {
- 30257 znum = x - 0.5;
- 30258 zden = znum * 0.5 + 0.5;
- 30259 exponent--;
- 30260 }
- 30261 z = znum/zden; w = z * z;
- 30262 x = z + z * w * (POLYNOM2(w,a)/POLYNOM3(w,b));
- 30263 z = exponent;
- 30264 x += z * (-2.121944400546905827679e-4);
- 30265 return x + z * 0.693359375;
- 30266 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/log10.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 30300 /*
- 30301 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 30302 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 30303 *
- 30304 * Author: Ceriel J.H. Jacobs
- 30305 */
- 30306 /* $Header: log10.c,v 1.3 91/03/19 16:38:55 ceriel Exp $ */
- 30307
- 30308 #include <math.h>
- 30309 #include <errno.h>
- 30310 #include "localmath.h"
- 30311
- 30312 double
- 30313 log10(double x)
- 30314 {
- 30315 if (__IsNan(x)) {
- 30316 errno = EDOM;
- 30317 return x;
- 30318 }
- 30319 if (x < 0) {
- 30320 errno = EDOM;
- 30321 return -HUGE_VAL;
- 30322 }
- 30323 else if (x == 0) {
- 30324 errno = ERANGE;
- 30325 return -HUGE_VAL;
- 30326 }
- 30327
- 30328 return log(x) / M_LN10;
- 30329 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/modf.s
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 30400 #
- 30401 .sect .text; .sect .rom; .sect .data; .sect .bss
- 30402 .extern _modf
- 30403 .sect .text
- 30404 _modf:
- 30405 #if __i386
- 30406 push ebp
- 30407 mov ebp, esp
- 30408 push 12(ebp)
- 30409 push 8(ebp)
- 30410 push 1
- 30411 push 4
- 30412 call .cif8
- 30413 mov eax, esp
- 30414 push eax
- 30415 call .fif8
- 30416 pop ecx
- 30417 mov edx, 16(ebp)
- 30418 pop ecx
- 30419 pop ebx
- 30420 mov 0(edx), ecx
- 30421 mov 4(edx), ebx
- 30422 pop eax
- 30423 pop edx
- 30424 leave
- 30425 ret
- 30426 #else /* i86 */
- 30427 push bp
- 30428 mov bp, sp
- 30429 lea bx, 4(bp)
- 30430 mov cx, #8
- 30431 call .loi
- 30432 mov dx, #1
- 30433 push dx
- 30434 push dx
- 30435 push dx
- 30436 mov ax, #2
- 30437 push ax
- 30438 call .cif8
- 30439 mov ax, sp
- 30440 push ax
- 30441 call .fif8
- 30442 pop bx
- 30443 mov bx, 12(bp)
- 30444 mov cx, #8
- 30445 call .sti
- 30446 call .ret8
- 30447 jmp .cret
- 30448 #endif
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/pow.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 30500 /*
- 30501 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 30502 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 30503 *
- 30504 * Author: Ceriel J.H. Jacobs
- 30505 */
- 30506 /* $Header: pow.c,v 1.3 90/08/28 13:59:36 eck Exp $ */
- 30507
- 30508 #include <math.h>
- 30509 #include <float.h>
- 30510 #include <errno.h>
- 30511 #include "localmath.h"
- 30512
- 30513 double
- 30514 pow(double x, double y)
- 30515 {
- 30516 /* Simple version for now. The Cody and Waite book has
- 30517 a very complicated, much more precise version, but
- 30518 this version has machine-dependent arrays A1 and A2,
- 30519 and I don't know yet how to solve this ???
- 30520 */
- 30521 double dummy;
- 30522 int result_neg = 0;
- 30523
- 30524 if ((x == 0 && y == 0) ||
- 30525 (x < 0 && modf(y, &dummy) != 0)) {
- 30526 errno = EDOM;
- 30527 return 0;
- 30528 }
- 30529
- 30530 if (x == 0) return x;
- 30531
- 30532 if (x < 0) {
- 30533 if (modf(y/2.0, &dummy) != 0) {
- 30534 /* y was odd */
- 30535 result_neg = 1;
- 30536 }
- 30537 x = -x;
- 30538 }
- 30539 x = log(x);
- 30540
- 30541 if (x < 0) {
- 30542 x = -x;
- 30543 y = -y;
- 30544 }
- 30545 /* Beware of overflow in the multiplication */
- 30546 if (x > 1.0 && y > DBL_MAX/x) {
- 30547 errno = ERANGE;
- 30548 return result_neg ? -HUGE_VAL : HUGE_VAL;
- 30549 }
- 30550
- 30551 x = exp(x * y);
- 30552 return result_neg ? -x : x;
- 30553 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/sin.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 30600 /*
- 30601 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 30602 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 30603 *
- 30604 * Author: Ceriel J.H. Jacobs
- 30605 */
- 30606 /* $Header: sin.c,v 1.3 91/03/19 16:39:04 ceriel Exp $ */
- 30607
- 30608 #include <math.h>
- 30609 #include <float.h>
- 30610 #include <errno.h>
- 30611 #include "localmath.h"
- 30612
- 30613 static double
- 30614 sinus(double x, int cos_flag)
- 30615 {
- 30616 /* Algorithm and coefficients from:
- 30617 "Software manual for the elementary functions"
- 30618 by W.J. Cody and W. Waite, Prentice-Hall, 1980
- 30619 */
- 30620
- 30621 static double r[] = {
- 30622 -0.16666666666666665052e+0,
- 30623 0.83333333333331650314e-2,
- 30624 -0.19841269841201840457e-3,
- 30625 0.27557319210152756119e-5,
- 30626 -0.25052106798274584544e-7,
- 30627 0.16058936490371589114e-9,
- 30628 -0.76429178068910467734e-12,
- 30629 0.27204790957888846175e-14
- 30630 };
- 30631
- 30632 double y;
- 30633 int neg = 1;
- 30634
- 30635 if (__IsNan(x)) {
- 30636 errno = EDOM;
- 30637 return x;
- 30638 }
- 30639 if (x < 0) {
- 30640 x = -x;
- 30641 neg = -1;
- 30642 }
- 30643 if (cos_flag) {
- 30644 neg = 1;
- 30645 y = M_PI_2 + x;
- 30646 }
- 30647 else y = x;
- 30648
- 30649 /* ??? avoid loss of significance, if y is too large, error ??? */
- 30650
- 30651 y = y * M_1_PI + 0.5;
- 30652
- 30653 if (y >= DBL_MAX/M_PI) return 0.0;
- 30654
- 30655 /* Use extended precision to calculate reduced argument.
- 30656 Here we used 12 bits of the mantissa for a1.
- 30657 Also split x in integer part x1 and fraction part x2.
- 30658 */
- 30659 #define A1 3.1416015625
- 30660 #define A2 -8.908910206761537356617e-6
- 30661 {
- 30662 double x1, x2;
- 30663
- 30664 modf(y, &y);
- 30665 if (modf(0.5*y, &x1)) neg = -neg;
- 30666 if (cos_flag) y -= 0.5;
- 30667 x2 = modf(x, &x1);
- 30668 x = x1 - y * A1;
- 30669 x += x2;
- 30670 x -= y * A2;
- 30671 #undef A1
- 30672 #undef A2
- 30673 }
- 30674
- 30675 if (x < 0) {
- 30676 neg = -neg;
- 30677 x = -x;
- 30678 }
- 30679
- 30680 /* ??? avoid underflow ??? */
- 30681
- 30682 y = x * x;
- 30683 x += x * y * POLYNOM7(y, r);
- 30684 return neg==-1 ? -x : x;
- 30685 }
-
- 30687 double
- 30688 sin(double x)
- 30689 {
- 30690 return sinus(x, 0);
- 30691 }
-
- 30693 double
- 30694 cos(double x)
- 30695 {
- 30696 if (x < 0) x = -x;
- 30697 return sinus(x, 1);
- 30698 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/sinh.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 30700 /*
- 30701 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 30702 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 30703 *
- 30704 * Author: Ceriel J.H. Jacobs
- 30705 */
- 30706 /* $Header: sinh.c,v 1.3 91/03/19 16:39:12 ceriel Exp $ */
- 30707
- 30708 #include <math.h>
- 30709 #include <float.h>
- 30710 #include <errno.h>
- 30711 #include "localmath.h"
- 30712
- 30713 static double
- 30714 sinh_cosh(double x, int cosh_flag)
- 30715 {
- 30716 /* Algorithm and coefficients from:
- 30717 "Software manual for the elementary functions"
- 30718 by W.J. Cody and W. Waite, Prentice-Hall, 1980
- 30719 */
- 30720
- 30721 static double p[] = {
- 30722 -0.35181283430177117881e+6,
- 30723 -0.11563521196851768270e+5,
- 30724 -0.16375798202630751372e+3,
- 30725 -0.78966127417357099479e+0
- 30726 };
- 30727 static double q[] = {
- 30728 -0.21108770058106271242e+7,
- 30729 0.36162723109421836460e+5,
- 30730 -0.27773523119650701167e+3,
- 30731 1.0
- 30732 };
- 30733 int negative = x < 0;
- 30734 double y = negative ? -x : x;
- 30735
- 30736 if (__IsNan(x)) {
- 30737 errno = EDOM;
- 30738 return x;
- 30739 }
- 30740 if (! cosh_flag && y <= 1.0) {
- 30741 /* ??? check for underflow ??? */
- 30742 y = y * y;
- 30743 return x + x * y * POLYNOM3(y, p)/POLYNOM3(y,q);
- 30744 }
- 30745
- 30746 if (y >= M_LN_MAX_D) {
- 30747 /* exp(y) would cause overflow */
- 30748 #define LNV 0.69316101074218750000e+0
- 30749 #define VD2M1 0.52820835025874852469e-4
- 30750 double w = y - LNV;
- 30751
- 30752 if (w < M_LN_MAX_D+M_LN2-LNV) {
- 30753 x = exp(w);
- 30754 x += VD2M1 * x;
- 30755 }
- 30756 else {
- 30757 errno = ERANGE;
- 30758 x = HUGE_VAL;
- 30759 }
- 30760 }
- 30761 else {
- 30762 double z = exp(y);
- 30763
- 30764 x = 0.5 * (z + (cosh_flag ? 1.0 : -1.0)/z);
- 30765 }
- 30766 return negative ? -x : x;
- 30767 }
-
- 30769 double
- 30770 sinh(double x)
- 30771 {
- 30772 return sinh_cosh(x, 0);
- 30773 }
-
- 30775 double
- 30776 cosh(double x)
- 30777 {
- 30778 if (x < 0) x = -x;
- 30779 return sinh_cosh(x, 1);
- 30780 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/sqrt.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 30800 /*
- 30801 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 30802 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 30803 *
- 30804 * Author: Ceriel J.H. Jacobs
- 30805 */
- 30806 /* $Header: sqrt.c,v 1.3 91/03/19 16:39:21 ceriel Exp $ */
- 30807
- 30808 #include <math.h>
- 30809 #include <float.h>
- 30810 #include <errno.h>
- 30811
- 30812 #define NITER 5
- 30813
- 30814 double
- 30815 sqrt(double x)
- 30816 {
- 30817 int exponent;
- 30818 double val;
- 30819
- 30820 if (__IsNan(x)) {
- 30821 errno = EDOM;
- 30822 return x;
- 30823 }
- 30824 if (x <= 0) {
- 30825 if (x < 0) errno = EDOM;
- 30826 return 0;
- 30827 }
- 30828
- 30829 if (x > DBL_MAX) return x; /* for infinity */
- 30830
- 30831 val = frexp(x, &exponent);
- 30832 if (exponent & 1) {
- 30833 exponent--;
- 30834 val *= 2;
- 30835 }
- 30836 val = ldexp(val + 1.0, exponent/2 - 1);
- 30837 /* was: val = (val + 1.0)/2.0; val = ldexp(val, exponent/2); */
- 30838 for (exponent = NITER - 1; exponent >= 0; exponent--) {
- 30839 val = (val + x / val) / 2.0;
- 30840 }
- 30841 return val;
- 30842 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/tan.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 30900 /*
- 30901 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 30902 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 30903 *
- 30904 * Author: Ceriel J.H. Jacobs
- 30905 */
- 30906 /* $Header: tan.c,v 1.3 91/03/19 16:39:30 ceriel Exp $ */
- 30907
- 30908 #include <math.h>
- 30909 #include <float.h>
- 30910 #include <errno.h>
- 30911 #include "localmath.h"
- 30912
- 30913 double
- 30914 tan(double x)
- 30915 {
- 30916 /* Algorithm and coefficients from:
- 30917 "Software manual for the elementary functions"
- 30918 by W.J. Cody and W. Waite, Prentice-Hall, 1980
- 30919 */
- 30920
- 30921 int negative = x < 0;
- 30922 int invert = 0;
- 30923 double y;
- 30924 static double p[] = {
- 30925 1.0,
- 30926 -0.13338350006421960681e+0,
- 30927 0.34248878235890589960e-2,
- 30928 -0.17861707342254426711e-4
- 30929 };
- 30930 static double q[] = {
- 30931 1.0,
- 30932 -0.46671683339755294240e+0,
- 30933 0.25663832289440112864e-1,
- 30934 -0.31181531907010027307e-3,
- 30935 0.49819433993786512270e-6
- 30936 };
- 30937
- 30938 if (__IsNan(x)) {
- 30939 errno = EDOM;
- 30940 return x;
- 30941 }
- 30942 if (negative) x = -x;
- 30943
- 30944 /* ??? avoid loss of significance, error if x is too large ??? */
- 30945
- 30946 y = x * M_2_PI + 0.5;
- 30947
- 30948 if (y >= DBL_MAX/M_PI_2) return 0.0;
- 30949
- 30950 /* Use extended precision to calculate reduced argument.
- 30951 Here we used 12 bits of the mantissa for a1.
- 30952 Also split x in integer part x1 and fraction part x2.
- 30953 */
- 30954 #define A1 1.57080078125
- 30955 #define A2 -4.454455103380768678308e-6
- 30956 {
- 30957 double x1, x2;
- 30958
- 30959 modf(y, &y);
- 30960 if (modf(0.5*y, &x1)) invert = 1;
- 30961 x2 = modf(x, &x1);
- 30962 x = x1 - y * A1;
- 30963 x += x2;
- 30964 x -= y * A2;
- 30965 #undef A1
- 30966 #undef A2
- 30967 }
- 30968
- 30969 /* ??? avoid underflow ??? */
- 30970 y = x * x;
- 30971 x += x * y * POLYNOM2(y, p+1);
- 30972 y = POLYNOM4(y, q);
- 30973 if (negative) x = -x;
- 30974 return invert ? -y/x : x/y;
- 30975 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/math/tanh.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 31000 /*
- 31001 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
- 31002 * See the copyright notice in the ACK home directory, in the file "Copyright".
- 31003 *
- 31004 * Author: Ceriel J.H. Jacobs
- 31005 */
- 31006 /* $Header: tanh.c,v 1.3 91/03/19 16:39:40 ceriel Exp $ */
- 31007
- 31008 #include <float.h>
- 31009 #include <math.h>
- 31010 #include <errno.h>
- 31011 #include "localmath.h"
- 31012
- 31013 double
- 31014 tanh(double x)
- 31015 {
- 31016 /* Algorithm and coefficients from:
- 31017 "Software manual for the elementary functions"
- 31018 by W.J. Cody and W. Waite, Prentice-Hall, 1980
- 31019 */
- 31020
- 31021 static double p[] = {
- 31022 -0.16134119023996228053e+4,
- 31023 -0.99225929672236083313e+2,
- 31024 -0.96437492777225469787e+0
- 31025 };
- 31026 static double q[] = {
- 31027 0.48402357071988688686e+4,
- 31028 0.22337720718962312926e+4,
- 31029 0.11274474380534949335e+3,
- 31030 1.0
- 31031 };
- 31032 int negative = x < 0;
- 31033
- 31034 if (__IsNan(x)) {
- 31035 errno = EDOM;
- 31036 return x;
- 31037 }
- 31038 if (negative) x = -x;
- 31039
- 31040 if (x >= 0.5*M_LN_MAX_D) {
- 31041 x = 1.0;
- 31042 }
- 31043 #define LN3D2 0.54930614433405484570e+0 /* ln(3)/2 */
- 31044 else if (x > LN3D2) {
- 31045 x = 0.5 - 1.0/(exp(x+x)+1.0);
- 31046 x += x;
- 31047 }
- 31048 else {
- 31049 /* ??? avoid underflow ??? */
- 31050 double g = x*x;
- 31051 x += x * g * POLYNOM2(g, p)/POLYNOM3(g, q);
- 31052 }
- 31053 return negative ? -x : x;
- 31054 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/other/_brk.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 31100 #include <lib.h>
- 31101 #define brk _brk
- 31102 #define sbrk _sbrk
- 31103 #include <unistd.h>
- 31104
- 31105 extern char *_brksize;
- 31106
- 31107 /* Both OSF/1 and SYSVR4 man pages specify that brk(2) returns int.
- 31108 * However, BSD4.3 specifies that brk() returns char*. POSIX omits
- 31109 * brk() on the grounds that it imposes a memory model on an architecture.
- 31110 * For this reason, brk() and sbrk() are not in the lib/posix directory.
- 31111 * On the other hand, they are so crucial to correct operation of so many
- 31112 * parts of the system, that we have chosen to hide the name brk using _brk,
- 31113 * as with system calls. In this way, if a user inadvertently defines a
- 31114 * procedure brk, MINIX may continue to work because the true call is _brk.
- 31115 */
- 31116 PUBLIC int brk(addr)
- 31117 char *addr;
- 31118 {
- 31119 message m;
- 31120
- 31121 m.m1_p1 = addr;
- 31122 if (_syscall(MM, BRK, &m) < 0) return(-1);
- 31123 _brksize = m.m2_p1;
- 31124 return(0);
- 31125 }
-
-
- 31128 PUBLIC char *sbrk(incr)
- 31129 int incr;
- 31130 {
- 31131 char *newsize, *oldsize;
- 31132
- 31133 oldsize = _brksize;
- 31134 newsize = _brksize + incr;
- 31135 if (incr > 0 && newsize < oldsize || incr < 0 && newsize > oldsize)
- 31136 return( (char *) -1);
- 31137 if (brk(newsize) == 0)
- 31138 return(oldsize);
- 31139 else
- 31140 return( (char *) -1);
- 31141 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/other/_longjerr.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 31200 #include <lib.h>
- 31201 #define longjerr _longjerr
- 31202 #define write _write
- 31203 #include <minix/minlib.h>
- 31204 #include <stdlib.h>
- 31205 #include <string.h>
- 31206 #include <unistd.h>
- 31207
- 31208
- 31209 _PROTOTYPE( void longjerr, (void));
- 31210
- 31211 PUBLIC void longjerr()
- 31212 {
- 31213 static char errmsg[] = "longj errorn";
- 31214
- 31215 write(2, errmsg, strlen(errmsg)); /* hope it's stderr */
- 31216 while(1) abort(); /* XXX - maybe just exit */
- 31217 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/other/_reboot.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 31300 /* reboot.c - Systemcall interface to mm/signal.c::do_reboot()
- 31301
- 31302 author: Edvard Tuinder v892231@si.hhs.NL
- 31303 */
- 31304
- 31305 #include <lib.h>
- 31306 #define reboot _reboot
- 31307 #include <unistd.h>
- 31308 #include <stdarg.h>
- 31309
- 31310 int reboot(int how, ...)
- 31311 {
- 31312 message m;
- 31313 va_list ap;
- 31314
- 31315 va_start(ap, how);
- 31316 if ((m.m1_i1 = how) == RBT_MONITOR) {
- 31317 m.m1_p1 = va_arg(ap, char *);
- 31318 m.m1_i2 = va_arg(ap, size_t);
- 31319 }
- 31320 va_end(ap);
- 31321
- 31322 return _syscall(MM, REBOOT, &m);
- 31323 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/other/_seekdir.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 31400 /* seekdir() Author: Kees J. Bot
- 31401 * 24 Apr 1989
- 31402 */
- 31403 #define nil 0
- 31404 #include <lib.h>
- 31405 #define lseek _lseek
- 31406 #define readdir _readdir
- 31407 #define seekdir _seekdir
- 31408 #include <sys/types.h>
- 31409 #include <dirent.h>
- 31410 #include <unistd.h>
- 31411 #include <errno.h>
- 31412
- 31413 int seekdir(DIR *dp, off_t pos)
- 31414 /* Seek to position pos in a directory. */
- 31415 {
- 31416 int off;
- 31417
- 31418 if (dp == nil) { errno= EBADF; return -1; }
- 31419
- 31420 dp->_count= 0;
- 31421 dp->_ptr= dp->_buf;
- 31422
- 31423 off= pos & (sizeof(dp->_buf) - 1);
- 31424 dp->_pos= pos - off;
- 31425
- 31426 if (lseek(dp->_fd, dp->_pos, SEEK_SET) == -1) return -1;
- 31427
- 31428 while (dp->_pos < pos && readdir(dp) != nil) {}
- 31429
- 31430 return 0;
- 31431 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/other/asynchio.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 31500 /* asyn_init(), asyn_read(), asyn_write(), asyn_ioctl(),
- 31501 * asyn_wait(), asyn_synch(), asyn_close()
- 31502 * Author: Kees J. Bot
- 31503 * 26 Jan 1995
- 31504 * Thise are just stub routines that are call compatible with
- 31505 * the asynchio(3) library of Minix-vmd. See asynchio.h.
- 31506 */
- 31507 #define nil 0
- 31508 #define alarm _alarm
- 31509 #define ioctl _ioctl
- 31510 #define read _read
- 31511 #define sigaction _sigaction
- 31512 #define sigfillset _sigfillset
- 31513 #define time _time
- 31514 #define write _write
- 31515 #include <lib.h>
- 31516 #include <sys/ioctl.h>
- 31517 #include <sys/asynchio.h>
- 31518 #include <stdlib.h>
- 31519 #include <unistd.h>
- 31520 #include <string.h>
- 31521 #include <time.h>
- 31522 #include <signal.h>
- 31523
- 31524 #define IDLE 0
- 31525 #define INPROGRESS 1
- 31526 #define RESULT 2
- 31527
- 31528 #define OP_READ 0
- 31529 #define OP_WRITE 1
- 31530 #define OP_IOCTL 2
- 31531
- 31532 static int *asyn_current;
- 31533 static int asyn_op;
- 31534 static int asyn_fd;
- 31535 static int asyn_req;
- 31536 static void *asyn_data;
- 31537 static ssize_t asyn_count;
- 31538 static int asyn_errno;
- 31539
- 31540 void asyn_init(asynchio_t *asyn)
- 31541 {
- 31542 *asyn= IDLE;
- 31543 }
-
- 31545 static ssize_t operation(int op, asynchio_t *asyn, int fd, int req,
- 31546 void *data, ssize_t count)
- 31547 {
- 31548 switch (*asyn) {
- 31549 case INPROGRESS:
- 31550 if (asyn_current != asyn && asyn_op != op) abort();
- 31551 /*FALL THROUGH*/
- 31552 case IDLE:
- 31553 asyn_current= asyn;
- 31554 asyn_op= op;
- 31555 asyn_fd= fd;
- 31556 asyn_req= req;
- 31557 asyn_data= data;
- 31558 asyn_count= count;
- 31559 *asyn= INPROGRESS;
- 31560 errno= EINPROGRESS;
- 31561 return -1;
- 31562 case RESULT:
- 31563 if (asyn_current != asyn && asyn_op != op) abort();
- 31564 *asyn= IDLE;
- 31565 errno= asyn_errno;
- 31566 return asyn_count;
- 31567 }
- 31568 }
-
- 31570 ssize_t asyn_read(asynchio_t *asyn, int fd, void *buf, size_t len)
- 31571 {
- 31572 return operation(OP_READ, asyn, fd, 0, buf, len);
- 31573 }
-
- 31575 ssize_t asyn_write(asynchio_t *asyn, int fd, const void *buf, size_t len)
- 31576 {
- 31577 return operation(OP_WRITE, asyn, fd, 0, (void *) buf, len);
- 31578 }
-
- 31580 int asyn_ioctl(asynchio_t *asyn, int fd, unsigned long request, void *data)
- 31581 {
- 31582 return operation(OP_IOCTL, asyn, fd, request, data, 0);
- 31583 }
-
- 31585 static void time_out(int sig)
- 31586 {
- 31587 alarm(1);
- 31588 }
-
- 31590 int asyn_wait(asynchio_t *asyn, int flags, struct timeval *to)
- 31591 {
- 31592 time_t now;
- 31593 unsigned old_timer, new_timer;
- 31594 struct sigaction old_sa, new_sa;
- 31595
- 31596 if (*asyn == IDLE) return 0;
- 31597 if (asyn_current != asyn || *asyn != INPROGRESS) abort();
- 31598 if (flags & ASYN_NONBLOCK) abort();
- 31599
- 31600 if (to != nil) {
- 31601 now= time(nil);
- 31602 if (to->tv_sec <= now) { errno= EINTR; return -1; }
- 31603 old_timer= alarm(0);
- 31604 new_sa.sa_handler= time_out;
- 31605 sigfillset(&new_sa.sa_mask);
- 31606 new_sa.sa_flags= 0;
- 31607 sigaction(SIGALRM, &new_sa, &old_sa);
- 31608 new_timer= to->tv_sec - now;
- 31609 if (new_timer < old_timer) {
- 31610 new_timer= old_timer;
- 31611 }
- 31612 alarm(new_timer);
- 31613 }
- 31614 switch (asyn_op) {
- 31615 case OP_READ:
- 31616 asyn_count= read(asyn_fd, asyn_data, asyn_count);
- 31617 asyn_errno= errno;
- 31618 break;
- 31619 case OP_WRITE:
- 31620 asyn_count= write(asyn_fd, asyn_data, asyn_count);
- 31621 asyn_errno= errno;
- 31622 break;
- 31623 case OP_IOCTL:
- 31624 asyn_count= ioctl(asyn_fd, asyn_req, asyn_data);
- 31625 asyn_errno= errno;
- 31626 break;
- 31627 }
- 31628 if (to != nil) {
- 31629 alarm(0);
- 31630 sigaction(SIGALRM, &old_sa, (struct sigaction *)0);
- 31631 alarm(old_timer);
- 31632 }
- 31633
- 31634 if (asyn_count == -1 && asyn_errno == EINTR) {
- 31635 errno= EINTR;
- 31636 return -1;
- 31637 } else {
- 31638 *asyn= RESULT;
- 31639 return 0;
- 31640 }
- 31641 }
-
- 31643 int asyn_synch(asynchio_t *asyn, int fd)
- 31644 {
- 31645 }
-
- 31647 int asyn_close(asynchio_t *asyn, int fd)
- 31648 {
- 31649 *asyn= IDLE;
- 31650 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/other/bcmp.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 31700 #include <lib.h>
- 31701 /* bcmp - Berklix equivalent of memcmp */
- 31702
- 31703 #include <string.h>
- 31704
- 31705 int bcmp(s1, s2, length) /* == 0 or != 0 for equality and inequality */
- 31706 _CONST void *s1;
- 31707 _CONST void *s2;
- 31708 int length;
- 31709 {
- 31710 return(memcmp((_CONST _VOIDSTAR) s1, (_CONST _VOIDSTAR) s2, (_SIZET) length));
- 31711 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/other/bcopy.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 31800 #include <lib.h>
- 31801 /* bcopy - Berklix equivalent of memcpy */
- 31802
- 31803 #include <string.h>
- 31804
- 31805 void bcopy(src, dst, length)
- 31806 _CONST void *src;
- 31807 void *dst;
- 31808 int length;
- 31809 {
- 31810 (void) memcpy((_VOIDSTAR) dst, (_CONST _VOIDSTAR) src, (_SIZET) length);
- 31811 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/other/bzero.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 31900 #include <lib.h>
- 31901 /* bzero - Berklix subset of memset */
- 31902
- 31903 #include <string.h>
- 31904
- 31905 void bzero(dst, length)
- 31906 void *dst;
- 31907 int length;
- 31908 {
- 31909 (void) memset((_VOIDSTAR) dst, 0, (_SIZET) length);
- 31910 }
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/other/crypt.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 32000 /* crypt() - one-way password encryption function Author: Kees J. Bot
- 32001 * 7 Feb 1994
- 32002 * This routine does not encrypt anything, it uses the pwdauth
- 32003 * program to do the hard work.
- 32004 */
- 32005 #define nil 0
- 32006 #define pipe _pipe
- 32007 #define fork _fork
- 32008 #define close _close
- 32009 #define dup2 _dup2
- 32010 #define execl _execl
- 32011 #define read _read
- 32012 #define _exit __exit
- 32013 #define write _write
- 32014 #define waitpid _waitpid
- 32015 #include <sys/types.h>
- 32016 #include <unistd.h>
- 32017 #include <string.h>
- 32018 #include <stdio.h>
- 32019 #include <sys/wait.h>
- 32020
- 32021 /* Set-uid root program to read /etc/shadow or encrypt passwords. */
- 32022 static char PWDAUTH[] = "/usr/lib/pwdauth";
- 32023 #define LEN 1024
- 32024
- 32025 char *crypt(const char *key, const char *salt)
- 32026 {
- 32027 pid_t pid;
- 32028 int status;
- 32029 int pfd[2];
- 32030 static char pwdata[LEN];
- 32031 char *p= pwdata;
- 32032 const char *k= key;
- 32033 const char *s= salt;
- 32034 int n;
- 32035
- 32036 /* Fill pwdata[] with the key and salt. */
- 32037 while ((*p++ = *k++) != 0) if (p == pwdata+LEN-1) goto fail;
- 32038 while ((*p++ = *s++) != 0) if (p == pwdata+LEN-0) goto fail;
- 32039
- 32040 if (pipe(pfd) < 0) goto fail;
- 32041
- 32042 /* Prefill the pipe. */
- 32043 (void) write(pfd[1], pwdata, p - pwdata);
- 32044
- 32045 switch ((pid= fork())) {
- 32046 case -1:
- 32047 close(pfd[0]);
- 32048 close(pfd[1]);
- 32049 goto fail;
- 32050 case 0:
- 32051 /* Connect both input and output to the pipe. */
- 32052 if (pfd[0] != 0) {
- 32053 dup2(pfd[0], 0);
- 32054 close(pfd[0]);
- 32055 }
- 32056 if (pfd[1] != 1) {
- 32057 dup2(pfd[1], 1);
- 32058 close(pfd[1]);
- 32059 }
- 32060
- 32061 execl(PWDAUTH, PWDAUTH, (char *) nil);
- 32062
- 32063 /* No pwdauth? Fail! */
- 32064 (void) read(0, pwdata, LEN);
- 32065 _exit(1);
- 32066 }
- 32067 close(pfd[1]);
- 32068
- 32069 if (waitpid(pid, &status, 0) < 0 || status != 0) {
- 32070 close(pfd[0]);
- 32071 goto fail;
- 32072 }
- 32073
- 32074 /* Read and return the result. Check if it contains exactly one
- 32075 * string.
- 32076 */
- 32077 n= read(pfd[0], pwdata, LEN);
- 32078 close(pfd[0]);
- 32079 if (n < 0) goto fail;
- 32080 p = pwdata + n;
- 32081 n = 0;
- 32082 while (p > pwdata) if (*--p == 0) n++;
- 32083 if (n != 1) goto fail;
- 32084 return pwdata;
- 32085
- 32086 fail:
- 32087 pwdata[0] = salt[0] ^ 1; /* make result != salt */
- 32088 pwdata[1] = 0;
- 32089 return pwdata;
- 32090 }
-
- 32092 /*
- 32093 * $PchHeader: /mount/hd2/minix/lib/misc/RCS/crypt.c,v 1.3 1994/12/22 13:51:49 philip Exp $
- 32094 */
-
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- src/lib/other/ctermid.c
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- 32100 /* ctermid(3)