First small part of lib cleanup. Mainly "constification" of arguments and
adding FIXME's.
diff --git a/lib/ChangeLog b/lib/ChangeLog
index 3049b95..eec006c 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,12 @@
+2004-10-04 Hasso Tepper <hasso at quagga.net>
+
+ * memory.c, memory.h: Make char * argument of strdup functions const.
+ * prefix.c, prefix.h: Make many arguments const. Reorder stuff in
+ header.
+ * log.h: Make log message const in struct message.
+ * log.c: Fix some indenting.
+ * network.c, network.h: Make second argument of writen() const.
+
2004-10-03 Hasso Tepper <hasso at quagga.net>
* command.h: Introduce SERVICE_NODE for "service <...>" commands.
diff --git a/lib/log.c b/lib/log.c
index 5f6b32f..2090b91 100644
--- a/lib/log.c
+++ b/lib/log.c
@@ -113,7 +113,8 @@
if (zl->flags & ZLOG_FILE)
{
time_print (zl->fp);
- if (zl->record_priority) fprintf (zl->fp, "%s: ", zlog_priority[priority]);
+ if (zl->record_priority)
+ fprintf (zl->fp, "%s: ", zlog_priority[priority]);
fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]);
vfprintf (zl->fp, format, args[ZLOG_FILE_INDEX]);
fprintf (zl->fp, "\n");
@@ -124,7 +125,8 @@
if (zl->flags & ZLOG_STDOUT)
{
time_print (stdout);
- if (zl->record_priority) fprintf (stdout, "%s: ", zlog_priority[priority]);
+ if (zl->record_priority)
+ fprintf (stdout, "%s: ", zlog_priority[priority]);
fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]);
vfprintf (stdout, format, args[ZLOG_STDOUT_INDEX]);
fprintf (stdout, "\n");
@@ -135,7 +137,8 @@
if (zl->flags & ZLOG_STDERR)
{
time_print (stderr);
- if (zl->record_priority) fprintf (stderr, "%s: ", zlog_priority[priority]);
+ if (zl->record_priority)
+ fprintf (stderr, "%s: ", zlog_priority[priority]);
fprintf (stderr, "%s: ", zlog_proto_names[zl->protocol]);
vfprintf (stderr, format, args[ZLOG_STDERR_INDEX]);
fprintf (stderr, "\n");
@@ -484,7 +487,7 @@
}
/* Very old hacky version of message lookup function. Still partly
- used in bgpd and ospfd. */
+ used in bgpd and ospfd. FIXME Seems that it's not used any more. */
char *
mes_lookup (struct message *meslist, int max, int index)
{
diff --git a/lib/log.h b/lib/log.h
index 8948ea0..defe064 100644
--- a/lib/log.h
+++ b/lib/log.h
@@ -71,7 +71,7 @@
struct message
{
int key;
- char *str;
+ const char *str;
};
/* Default logging strucutre. */
diff --git a/lib/memory.c b/lib/memory.c
index faf3f26..8b311ae 100644
--- a/lib/memory.c
+++ b/lib/memory.c
@@ -101,7 +101,7 @@
/* String duplication. */
char *
-zstrdup (int type, char *str)
+zstrdup (int type, const char *str)
{
void *dup;
@@ -115,7 +115,7 @@
#ifdef MEMORY_LOG
struct
{
- char *name;
+ const char *name;
unsigned long alloc;
unsigned long t_malloc;
unsigned long c_malloc;
@@ -187,7 +187,7 @@
}
char *
-mtype_zstrdup (const char *file, int line, int type, char *str)
+mtype_zstrdup (const char *file, int line, int type, const char *str)
{
char *memory;
@@ -230,7 +230,7 @@
struct memory_list
{
int index;
- char *format;
+ const char *format;
};
struct memory_list memory_list_lib[] =
diff --git a/lib/memory.h b/lib/memory.h
index 40081fe..c2d595a 100644
--- a/lib/memory.h
+++ b/lib/memory.h
@@ -236,7 +236,7 @@
void *zcalloc (int type, size_t size);
void *zrealloc (int type, void *ptr, size_t size);
void zfree (int type, void *ptr);
-char *zstrdup (int type, char *str);
+char *zstrdup (int type, const char *str);
void *mtype_zmalloc (const char *file,
int line,
@@ -263,7 +263,7 @@
char *mtype_zstrdup (const char *file,
int line,
int type,
- char *str);
+ const char *str);
void memory_init ();
#endif /* _ZEBRA_MEMORY_H */
diff --git a/lib/network.c b/lib/network.c
index d105db4..e1f733c 100644
--- a/lib/network.c
+++ b/lib/network.c
@@ -50,7 +50,7 @@
/* Write nbytes from ptr to fd. */
int
-writen(int fd, u_char *ptr, int nbytes)
+writen(int fd, const u_char *ptr, int nbytes)
{
int nleft;
int nwritten;
diff --git a/lib/network.h b/lib/network.h
index 0544361..f0a7d4d 100644
--- a/lib/network.h
+++ b/lib/network.h
@@ -24,6 +24,6 @@
#define _ZEBRA_NETWORK_H
int readn (int, u_char *, int);
-int writen (int, u_char *, int);
+int writen (int, const u_char *, int);
#endif /* _ZEBRA_NETWORK_H */
diff --git a/lib/prefix.c b/lib/prefix.c
index 5a3e71b..d9751e3 100644
--- a/lib/prefix.c
+++ b/lib/prefix.c
@@ -66,7 +66,7 @@
/* If n includes p prefix then return 1 else return 0. */
int
-prefix_match (struct prefix *n, struct prefix *p)
+prefix_match (const struct prefix *n, const struct prefix *p)
{
int offset;
int shift;
@@ -94,7 +94,7 @@
/* Copy prefix from src to dest. */
void
-prefix_copy (struct prefix *dest, struct prefix *src)
+prefix_copy (struct prefix *dest, const struct prefix *src)
{
dest->family = src->family;
dest->prefixlen = src->prefixlen;
@@ -127,7 +127,7 @@
* as '==' (which is different from prefix_cmp).
*/
int
-prefix_same (struct prefix *p1, struct prefix *p2)
+prefix_same (const struct prefix *p1, const struct prefix *p2)
{
if (p1->family == p2->family && p1->prefixlen == p2->prefixlen)
{
@@ -154,7 +154,7 @@
* from prefix_same).
*/
int
-prefix_cmp (struct prefix *p1, struct prefix *p2)
+prefix_cmp (const struct prefix *p1, const struct prefix *p2)
{
int offset;
int shift;
@@ -181,8 +181,8 @@
}
/* Return prefix family type string. */
-char *
-prefix_family_str (struct prefix *p)
+const char *
+prefix_family_str (const struct prefix *p)
{
if (p->family == AF_INET)
return "inet";
@@ -213,7 +213,7 @@
/* When string format is invalid return 0. */
int
-str2prefix_ipv4 (char *str, struct prefix_ipv4 *p)
+str2prefix_ipv4 (const char *str, struct prefix_ipv4 *p)
{
int ret;
int plen;
@@ -335,7 +335,7 @@
/* If prefix is 0.0.0.0/0 then return 1 else return 0. */
int
-prefix_ipv4_any (struct prefix_ipv4 *p)
+prefix_ipv4_any (const struct prefix_ipv4 *p)
{
return (p->prefix.s_addr == 0 && p->prefixlen == 0);
}
@@ -362,7 +362,7 @@
/* If given string is valid return pin6 else return NULL */
int
-str2prefix_ipv6 (char *str, struct prefix_ipv6 *p)
+str2prefix_ipv6 (const char *str, struct prefix_ipv6 *p)
{
char *pnt;
char *cp;
@@ -399,7 +399,8 @@
return ret;
}
-/* Convert struct in6_addr netmask into integer. */
+/* Convert struct in6_addr netmask into integer.
+ * FIXME return u_char as ip_maskleni() does. */
int
ip6_masklen (struct in6_addr netmask)
{
@@ -470,7 +471,7 @@
}
void
-str2in6_addr (char *str, struct in6_addr *addr)
+str2in6_addr (const char *str, struct in6_addr *addr)
{
int i;
unsigned int x;
@@ -503,10 +504,11 @@
return;
}
-/* Utility function of convert between struct prefix <=> union sockunion */
+/* Utility function of convert between struct prefix <=> union sockunion.
+ * FIXME This function isn't used anywhere. */
struct prefix *
-sockunion2prefix (union sockunion *dest,
- union sockunion *mask)
+sockunion2prefix (const union sockunion *dest,
+ const union sockunion *mask)
{
if (dest->sa.sa_family == AF_INET)
{
@@ -533,9 +535,9 @@
return NULL;
}
-/* Utility function of convert between struct prefix <=> union sockunion */
+/* Utility function of convert between struct prefix <=> union sockunion. */
struct prefix *
-sockunion2hostprefix (union sockunion *su)
+sockunion2hostprefix (const union sockunion *su)
{
if (su->sa.sa_family == AF_INET)
{
@@ -563,7 +565,7 @@
}
int
-prefix_blen (struct prefix *p)
+prefix_blen (const struct prefix *p)
{
switch (p->family)
{
@@ -581,7 +583,7 @@
/* Generic function for conversion string to struct prefix. */
int
-str2prefix (char *str, struct prefix *p)
+str2prefix (const char *str, struct prefix *p)
{
int ret;
@@ -601,7 +603,7 @@
}
int
-prefix2str (struct prefix *p, char *str, int size)
+prefix2str (const struct prefix *p, char *str, int size)
{
char buf[BUFSIZ];
@@ -627,9 +629,10 @@
}
/* Utility function. Check the string only contains digit
- character. */
+ * character.
+ * FIXME str.[c|h] would be better place for this function. */
int
-all_digit (char *str)
+all_digit (const char *str)
{
for (; *str != '\0'; str++)
if (!isdigit ((int) *str))
@@ -668,7 +671,8 @@
ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
int
-netmask_str2prefix_str (char *net_str, char *mask_str, char *prefix_str)
+netmask_str2prefix_str (const char *net_str, const char *mask_str,
+ char *prefix_str)
{
struct in_addr network;
struct in_addr mask;
diff --git a/lib/prefix.h b/lib/prefix.h
index 5884640..e4f17ab 100644
--- a/lib/prefix.h
+++ b/lib/prefix.h
@@ -128,43 +128,45 @@
int afi2family (int);
int family2afi (int);
-int prefix2str (struct prefix *, char *, int);
-int str2prefix (char *, struct prefix *);
struct prefix *prefix_new ();
-void prefix_free (struct prefix *p);
+void prefix_free (struct prefix *);
+const char *prefix_family_str (const struct prefix *);
+int prefix_blen (const struct prefix *);
+int str2prefix (const char *, struct prefix *);
+int prefix2str (const struct prefix *, char *, int);
+int prefix_match (const struct prefix *, const struct prefix *);
+int prefix_same (const struct prefix *, const struct prefix *);
+int prefix_cmp (const struct prefix *, const struct prefix *);
+void prefix_copy (struct prefix *dest, const struct prefix *src);
+void apply_mask (struct prefix *);
-struct prefix_ipv4 *prefix_ipv4_new ();
-void prefix_ipv4_free ();
-int str2prefix_ipv4 (char *, struct prefix_ipv4 *);
-void apply_mask_ipv4 (struct prefix_ipv4 *);
-int prefix_blen (struct prefix *);
-u_char ip_masklen (struct in_addr);
-int prefix_ipv4_any (struct prefix_ipv4 *);
-void masklen2ip (int, struct in_addr *);
-void apply_classful_mask_ipv4 (struct prefix_ipv4 *);
-
-char *prefix_family_str (struct prefix *p);
struct prefix *sockunion2prefix ();
struct prefix *sockunion2hostprefix ();
+struct prefix_ipv4 *prefix_ipv4_new ();
+void prefix_ipv4_free (struct prefix_ipv4 *);
+int str2prefix_ipv4 (const char *, struct prefix_ipv4 *);
+void apply_mask_ipv4 (struct prefix_ipv4 *);
+
+int prefix_ipv4_any (const struct prefix_ipv4 *);
+void apply_classful_mask_ipv4 (struct prefix_ipv4 *);
+
+u_char ip_masklen (struct in_addr);
+void masklen2ip (int, struct in_addr *);
+int netmask_str2prefix_str (const char *, const char *, char *);
+
#ifdef HAVE_IPV6
struct prefix_ipv6 *prefix_ipv6_new ();
-void prefix_ipv6_free ();
-struct prefix *str2routev6 (char *);
-int str2prefix_ipv6 (char *str, struct prefix_ipv6 *p);
-void apply_mask_ipv6 (struct prefix_ipv6 *p);
-void str2in6_addr (char *str, struct in6_addr *addr);
-void masklen2ip6 (int masklen, struct in6_addr *netmask);
-int ip6_masklen (struct in6_addr netmask);
+void prefix_ipv6_free (struct prefix_ipv6 *);
+int str2prefix_ipv6 (const char *, struct prefix_ipv6 *);
+void apply_mask_ipv6 (struct prefix_ipv6 *);
+
+int ip6_masklen (struct in6_addr);
+void masklen2ip6 (int, struct in6_addr *);
+
+void str2in6_addr (const char *, struct in6_addr *);
#endif /* HAVE_IPV6 */
-void apply_mask (struct prefix *);
-int prefix_match (struct prefix *n, struct prefix *p);
-int prefix_same (struct prefix *, struct prefix *);
-int prefix_cmp (struct prefix *, struct prefix *);
-void prefix_copy (struct prefix *, struct prefix *);
-
-int all_digit (char *);
-int netmask_str2prefix_str (char *, char *, char *);
+int all_digit (const char *);
#endif /* _ZEBRA_PREFIX_H */