* 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>
* 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];
Cash m1;
Cash m2;
Cash m3;
-
+ text *result;
+
/* work with positive numbers */
if (*value < 0)
{
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() */
* 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>
}
/* 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: */
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
{
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!
*/
return (1);
return (0);
}
+
*
* 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
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
*
* 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.
/* 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);
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);