diff --git a/src/backend/utils/adt/cash.c b/src/backend/utils/adt/cash.c index 262d2d4deda7cf5b4a87f13f59f77ce4e95e4f35..a6e9711a463e4d19815949ce3834b007d1b2aa11 100644 --- a/src/backend/utils/adt/cash.c +++ b/src/backend/utils/adt/cash.c @@ -9,7 +9,7 @@ * workings can be found in the book "Software Solutions in C" by * Dale Schumacher, Academic Press, ISBN: 0-12-632360-7. * - * $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.26 1998/09/01 04:32:26 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.27 1998/10/12 04:07:44 momjian Exp $ */ #include <stdio.h> @@ -672,7 +672,7 @@ cashsmaller(Cash *c1, Cash *c2) * This converts a int4 as well but to a representation using words * Obviously way North American centric - sorry */ -const char * +text * cash_words_out(Cash *value) { static char buf[128]; @@ -681,7 +681,8 @@ cash_words_out(Cash *value) Cash m1; Cash m2; Cash m3; - + text *result; + /* work with positive numbers */ if (*value < 0) { @@ -718,8 +719,16 @@ cash_words_out(Cash *value) strcat(buf, (int) (*value / 100) == 1 ? " dollar and " : " dollars and "); strcat(buf, num_word(m0)); strcat(buf, m0 == 1 ? " cent" : " cents"); + + /* capitalize output */ *buf = toupper(*buf); - return buf; + + /* make a text type for output */ + result = (text *) palloc(strlen(buf) + VARHDRSZ); + VARSIZE(result) = strlen(buf) + VARHDRSZ; + StrNCpy(VARDATA(result), buf, strlen(buf)); + + return result; } /* cash_words_out() */ diff --git a/src/backend/utils/adt/inet.c b/src/backend/utils/adt/inet.c index f98b36fd01aae4c12306394289b71dd37a50e71f..23b9158c4b312bd03c1c698ad3eedd6b8435cf9a 100644 --- a/src/backend/utils/adt/inet.c +++ b/src/backend/utils/adt/inet.c @@ -3,7 +3,7 @@ * is for IP V4 CIDR notation, but prepared for V6: just * add the necessary bits where the comments indicate. * - * $Id: inet.c,v 1.2 1998/10/08 02:08:44 momjian Exp $ + * $Id: inet.c,v 1.3 1998/10/12 04:07:46 momjian Exp $ */ #include <sys/types.h> @@ -57,7 +57,9 @@ inet_in(char *src) } /* First, try for an IP V4 address: */ ip_family(dst) = AF_INET; - bits = inet_net_pton(ip_family(dst), src, &ip_v4addr(dst), ip_addrsize(dst)); +#ifdef BAD + bits = inet_net_pton(ip_family(dst), src, &ip_v4addr(dst), ip_addrsize(dst), NULL); +#endif if ((bits < 0) || (bits > 32)) { /* Go for an IPV6 address here, before faulting out: */ @@ -84,13 +86,15 @@ inet_out(inet *src) if (ip_family(src) == AF_INET) { +#ifdef BAD /* It's an IP V4 address: */ - if (inet_net_ntop(AF_INET, &ip_v4addr(src), ip_bits(src), + if (inet_net_ntop(AF_INET, &ip_v4addr(src), 4, ip_bits(src), tmp, sizeof(tmp)) < 0) { elog(ERROR, "unable to print address (%s)", strerror(errno)); return (NULL); } +#endif } else { @@ -265,6 +269,194 @@ inet_cmp(inet *a1, inet *a2) return 0; } +text * +inet_netmask(inet *ip) +{ + char *dst, + tmp[sizeof("255.255.255.255/32")]; + + if (ip_family(ip) == AF_INET) + { + /* It's an IP V4 address: */ + int addr = -1 << (32 - ip_bits(ip)); + + /* a little wasteful by why reinvent the wheel? */ +#ifdef BAD + if (inet_cidr_ntop(AF_INET, &addr, 4, -1, tmp, sizeof(tmp)) < 0) + { + elog(ERROR, "unable to print netmask (%s)", strerror(errno)); + return (NULL); + } +#endif + } + else + { + /* Go for an IPV6 address here, before faulting out: */ + elog(ERROR, "unknown address family (%d)", ip_family(ip)); + return (NULL); + } + dst = palloc(strlen(tmp) + 1); + if (dst == NULL) + { + elog(ERROR, "unable to allocate memory in inet_netmask()"); + return (NULL); + } + strcpy(dst, tmp); + return (dst); + +} + +int4 +inet_masklen(inet *ip) +{ + return ip_bits(ip); +} + +text * +inet_host(inet *ip) +{ + char *dst, + tmp[sizeof("255.255.255.255/32")]; + + if (ip_family(ip) == AF_INET) + { +#ifdef BAD + /* It's an IP V4 address: */ + if (inet_cidr_ntop(AF_INET, &ip_v4addr(ip), 4, -1, + tmp, sizeof(tmp)) < 0) + { + elog(ERROR, "unable to print host (%s)", strerror(errno)); + return (NULL); + } +#endif + } + else + { + /* Go for an IPV6 address here, before faulting out: */ + elog(ERROR, "unknown address family (%d)", ip_family(ip)); + return (NULL); + } + dst = palloc(strlen(tmp) + 1); + if (dst == NULL) + { + elog(ERROR, "unable to allocate memory in inet_out()"); + return (NULL); + } + strcpy(dst, tmp); + return (dst); + +} + +text * +inet_network_without_bits(inet *ip) +{ + char *dst, + tmp[sizeof("255.255.255.255/32")]; + + if (ip_family(ip) == AF_INET) + { + /* It's an IP V4 address: */ + int addr = ip_v4addr(ip) & (-1 << (32 - ip_bits(ip))); +#ifdef BAD + + if (inet_cidr_ntop(AF_INET, &addr, (int)(ip_bits(ip)/8), -1, + tmp, sizeof(tmp)) < 0) + { + elog(ERROR, "unable to print address (%s)", strerror(errno)); + return (NULL); + } +#endif + } + else + { + /* Go for an IPV6 address here, before faulting out: */ + elog(ERROR, "unknown address family (%d)", ip_family(ip)); + return (NULL); + } + dst = palloc(strlen(tmp) + 1); + if (dst == NULL) + { + elog(ERROR, "unable to allocate memory in inet_out()"); + return (NULL); + } + strcpy(dst, tmp); + return (dst); + +} + +text * +inet_network_with_bits(inet *ip) +{ + char *dst, + tmp[sizeof("255.255.255.255/32")]; + + if (ip_family(ip) == AF_INET) + { + /* It's an IP V4 address: */ + int addr = ip_v4addr(ip) & (-1 << (32 - ip_bits(ip))); +#ifdef BAD + + if (inet_cidr_ntop(AF_INET, &addr, (int)(ip_bits(ip)/8), + ip_bits(ip), tmp, sizeof(tmp)) < 0) + { + elog(ERROR, "unable to print address (%s)", strerror(errno)); + return (NULL); + } +#endif + } + else + { + /* Go for an IPV6 address here, before faulting out: */ + elog(ERROR, "unknown address family (%d)", ip_family(ip)); + return (NULL); + } + dst = palloc(strlen(tmp) + 1); + if (dst == NULL) + { + elog(ERROR, "unable to allocate memory in inet_out()"); + return (NULL); + } + strcpy(dst, tmp); + return (dst); + +} + +text * +inet_broadcast(inet *ip) +{ + char *dst, + tmp[sizeof("255.255.255.255/32")]; + + if (ip_family(ip) == AF_INET) + { + /* It's an IP V4 address: */ + int addr = ip_v4addr(ip) | ~(-1 << (32 - ip_bits(ip))); +#ifdef BAD + + if (inet_cidr_ntop(AF_INET,&addr,4,ip_bits(ip),tmp,sizeof(tmp)) < 0) + { + elog(ERROR, "unable to print address (%s)", strerror(errno)); + return (NULL); + } +#endif + } + else + { + /* Go for an IPV6 address here, before faulting out: */ + elog(ERROR, "unknown address family (%d)", ip_family(ip)); + return (NULL); + } + dst = palloc(strlen(tmp) + 1); + if (dst == NULL) + { + elog(ERROR, "unable to allocate memory in inet_out()"); + return (NULL); + } + strcpy(dst, tmp); + return (dst); + +} + /* * Bitwise comparison for V4 addresses. Add V6 implementation! */ @@ -285,3 +477,4 @@ v4bitncmp(unsigned int a1, unsigned int a2, int bits) return (1); return (0); } + diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h index 969470a253c83d58cd63fb240fc95d9340cdd242..f454342be93ece50ece0c0494788ce9ff64bea68 100644 --- a/src/include/catalog/pg_proc.h +++ b/src/include/catalog/pg_proc.h @@ -6,7 +6,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: pg_proc.h,v 1.72 1998/10/08 00:19:40 momjian Exp $ + * $Id: pg_proc.h,v 1.73 1998/10/12 04:07:48 momjian Exp $ * * NOTES * The script catalog/genbki.sh reads this file and generates .bki @@ -2097,6 +2097,19 @@ DESCR("is-supernet"); DATA(insert OID = 930 ( inet_supeq PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); DESCR("is-supernet-or-equal"); +DATA(insert OID = 940 ( inet_netmask PGUID 11 f t f 1 f 25 "869" 100 0 0 100 foo bar )); +DESCR("netmask of inet address"); +DATA(insert OID = 941 ( inet_masklen PGUID 11 f t f 1 f 23 "869" 100 0 0 100 foo bar )); +DESCR("netmask length"); +DATA(insert OID = 942 ( inet_host PGUID 11 f t f 1 f 25 "869" 100 0 0 100 foo bar )); +DESCR("host adress"); +DATA(insert OID = 943 ( inet_network_without_bits PGUID 11 f t f 1 f 25 "869" 100 0 0 100 foo bar )); +DESCR("netmask without bits"); +DATA(insert OID = 944 ( inet_network_with_bits PGUID 11 f t f 1 f 25 "869" 100 0 0 100 foo bar )); +DESCR("netmask with bits"); +DATA(insert OID = 945 ( inet_broadcast PGUID 11 f t f 1 f 25 "869" 100 0 0 100 foo bar )); +DESCR("broadcast address"); + /* * prototypes for functions pg_proc.c diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index f3c57d4deb5e5855edc1efee6b460ad07a340462..49b849de09c6973a726580c6c9101488cb408503 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -6,7 +6,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: builtins.h,v 1.60 1998/10/08 18:30:49 momjian Exp $ + * $Id: builtins.h,v 1.61 1998/10/12 04:07:51 momjian Exp $ * * NOTES * This should normally only be included by fmgr.h. @@ -515,6 +515,9 @@ char *inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size); /* inet_net_pton.c */ int inet_net_pton(int af, const char *src, void *dst, size_t size); +char *inet_cidr_ntop(int af, const void *src, size_t len, int bits, char *dst, size_t size); +int inet_cidr_pton(int af, const void *src, void *dst, size_t size, int *used); + /* inet.c */ inet *inet_in(char *str); char *inet_out(inet * addr); @@ -530,6 +533,13 @@ bool inet_sup(inet * a1, inet * a2); bool inet_supeq(inet * a1, inet * a2); int4 inet_cmp(inet * a1, inet * a2); +text *inet_netmask(inet * addr); +int4 inet_masklen(inet * addr); +text *inet_host(inet * addr); +text *inet_network_without_bits(inet * addr); +text *inet_network_with_bits(inet * addr); +text *inet_broadcast(inet * addr); + /* mac.c */ macaddr *macaddr_in(char *str); diff --git a/src/include/utils/cash.h b/src/include/utils/cash.h index b5e45fc8dafea4a30a0413082a897a1b7b9ee2f7..0530266f7ec4b71b31903846ad7a4eef8c1643ea 100644 --- a/src/include/utils/cash.h +++ b/src/include/utils/cash.h @@ -44,6 +44,6 @@ extern Cash *int2_mul_cash(int2 s, Cash *c); extern Cash *cashlarger(Cash *c1, Cash *c2); extern Cash *cashsmaller(Cash *c1, Cash *c2); -extern const char *cash_words_out(Cash *value); +extern text *cash_words_out(Cash *value); #endif /* CASH_H */