diff src/syslogconfig.cpp @ 48:ba0259c9e411 stable-1-0-11

Fixes to compile on Fedora 9 and for const correctness
author Carl Byington <carl@five-ten-sg.com>
date Thu, 29 May 2008 11:38:42 -0700
parents d9ae11033b4b
children 206448c00b55
line wrap: on
line diff
--- a/src/syslogconfig.cpp	Sat Mar 22 11:34:56 2008 -0700
+++ b/src/syslogconfig.cpp	Thu May 29 11:38:42 2008 -0700
@@ -14,20 +14,20 @@
 #include <netdb.h>
 #include <limits.h>
 
-char *token_add;
-char *token_bucket;
-char *token_file;
-char *token_ignore;
-char *token_include;
-char *token_index;
-char *token_lbrace;
-char *token_message;
-char *token_pattern;
-char *token_rbrace;
-char *token_remove;
-char *token_semi;
-char *token_slash;
-char *token_threshold;
+const char *token_add;
+const char *token_bucket;
+const char *token_file;
+const char *token_ignore;
+const char *token_include;
+const char *token_index;
+const char *token_lbrace;
+const char *token_message;
+const char *token_pattern;
+const char *token_rbrace;
+const char *token_remove;
+const char *token_semi;
+const char *token_slash;
+const char *token_threshold;
 
 struct ltint
 {
@@ -49,10 +49,10 @@
 class IPR {
     ip_buckets  violations;
 public:
-    void add(int ip, int amount, CONFIG &con, char *file_name, int pattern_index, char *message);
+    void add(int ip, int amount, CONFIG &con, const char *file_name, int pattern_index, const char *message);
     void leak(int amount, CONFIG &con);
     void free_all(CONFIG &con);
-    void update(int ip, bool added, char *file_name, int pattern_index, char *message);
+    void update(int ip, bool added, const char *file_name, int pattern_index, const char *message);
     void changed(CONFIG &con, int ip, bool added);
 };
 
@@ -61,7 +61,7 @@
 
 ////////////////////////////////////////////////
 //
-void IPR::add(int ip, int amount, CONFIG &con, char *file_name, int pattern_index, char *message) {
+void IPR::add(int ip, int amount, CONFIG &con, const char *file_name, int pattern_index, const char *message) {
     if (con.looking(ip)) {
         ip_buckets::iterator i = violations.find(ip);
         if (i == violations.end()) {
@@ -126,7 +126,7 @@
 }
 
 
-void IPR::update(int ip, bool added, char *file_name, int pattern_index, char *message) {
+void IPR::update(int ip, bool added, const char *file_name, int pattern_index, const char *message) {
     if (debug_syslog > 2) {
         char buf[maxlen];
         in_addr ad;
@@ -164,8 +164,8 @@
 
 ////////////////////////////////////////////////
 //
-int ip_address(char *have);
-int ip_address(char *have) {
+int ip_address(const char *have);
+int ip_address(const char *have) {
     int ipaddr = 0;
     in_addr ip;
     if (inet_aton(have, &ip)) ipaddr = ip.s_addr;
@@ -179,7 +179,7 @@
 
 ////////////////////////////////////////////////
 //
-PATTERN::PATTERN(TOKEN &tok, char *pattern_, int index_, int amount_, char *msg_) {
+PATTERN::PATTERN(TOKEN &tok, const char *pattern_, int index_, int amount_, const char *msg_) {
     pattern = pattern_;
     index   = index_;
     amount  = amount_;
@@ -203,7 +203,7 @@
 }
 
 
-bool PATTERN::process(char *buf, CONFIG &con, char *file_name, int pattern_index) {
+bool PATTERN::process(char *buf, CONFIG &con, const char *file_name, int pattern_index) {
     if (pattern) {
         const int nmatch = index+1;
         regmatch_t match[nmatch];
@@ -327,7 +327,7 @@
 
 ////////////////////////////////////////////////
 //
-SYSLOGCONFIG::SYSLOGCONFIG(TOKEN &tok, char *file_name_) {
+SYSLOGCONFIG::SYSLOGCONFIG(TOKEN &tok, const char *file_name_) {
     tokp      = &tok;
     file_name = file_name_;
     open(true);
@@ -358,7 +358,7 @@
             snprintf(buf, sizeof(buf), "syslog file %s opened", file_name);
             my_syslog(buf);
         }
-        lseek(fd, 0, SEEK_END);
+        if (msg) lseek(fd, 0, SEEK_END);
         if (fstat(fd, &openfdstat)) {
             close();
             snprintf(buf, sizeof(buf), "syslog file %s cannot stat after open", file_name);
@@ -443,7 +443,6 @@
     int i = min(maxlen-1, level*4);
     memset(indent, ' ', i);
     indent[i] = '\0';
-    char buf[maxlen];
     printf("%s file \"%s\" {\n", indent, file_name);
     for (pattern_list::iterator i=patterns.begin(); i!=patterns.end(); i++) {
         PATTERN *p = *i;
@@ -458,7 +457,7 @@
 //
 void discard(string_set &s) {
     for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
-        free(*i);
+        free((void*)*i);
     }
     s.clear();
 }
@@ -467,8 +466,8 @@
 ////////////////////////////////////////////////
 // helper to register a string in a string set
 //
-char* register_string(string_set &s, char *name) {
-    string_set::iterator i = s.find(name);
+const char* register_string(string_set &s, const char *name) {
+    string_set::const_iterator i = s.find(name);
     if (i != s.end()) return *i;
     char *x = strdup(name);
     s.insert(x);
@@ -479,7 +478,7 @@
 ////////////////////////////////////////////////
 // register a global string
 //
-char* register_string(char *name) {
+const char* register_string(const char *name) {
     return register_string(all_strings, name);
 }
 
@@ -494,9 +493,9 @@
 
 ////////////////////////////////////////////////
 //
-bool tsa(TOKEN &tok, char *token);
-bool tsa(TOKEN &tok, char *token) {
-    char *have = tok.next();
+bool tsa(TOKEN &tok, const char *token);
+bool tsa(TOKEN &tok, const char *token) {
+    const char *have = tok.next();
     if (have == token) return true;
     tok.token_error(token, have);
     return false;
@@ -507,12 +506,13 @@
 //
 bool parse_pattern(TOKEN &tok, SYSLOGCONFIG &con);
 bool parse_pattern(TOKEN &tok, SYSLOGCONFIG &con) {
-    char *pat = tok.next();
-    int  ind, buc;
-    char *msg = NULL;
+    const char *pat = tok.next();
+    int  ind = 0;
+    int  buc = 0;
+    const char *msg = NULL;
     if (!tsa(tok, token_lbrace)) return false;
     while (true) {
-        char *have = tok.next();
+        const char *have = tok.next();
         if (!have) break;
         if (have == token_rbrace) break;
         if (have == token_index) {
@@ -547,7 +547,7 @@
 bool parse_ignore(TOKEN &tok, CONFIG &dc) {
     if (!tsa(tok, token_lbrace)) return false;
     while (true) {
-        char *have = tok.next();
+        const char *have = tok.next();
         if (!have) break;
         if (have == token_rbrace) break;
         int ipaddr = ip_address(have);
@@ -611,7 +611,7 @@
 //
 bool parse_syslogconfig(TOKEN &tok, CONFIG &dc);
 bool parse_syslogconfig(TOKEN &tok, CONFIG &dc) {
-    char *name = tok.next();
+    const char *name = tok.next();
     if (!tsa(tok, token_lbrace)) return false;
     SYSLOGCONFIGP con = new SYSLOGCONFIG(tok, name);
     if (con->failed()) {
@@ -620,7 +620,7 @@
     }
     dc.add_syslogconfig(con);
     while (true) {
-        char *have = tok.next();
+        const char *have = tok.next();
         if (!have) break;
         if (have == token_rbrace) break;
         if (have == token_pattern) {
@@ -639,11 +639,11 @@
 ////////////////////////////////////////////////
 // parse a config file
 //
-bool load_conf(CONFIG &dc, char *fn) {
+bool load_conf(CONFIG &dc, const char *fn) {
     int count = 0;
     TOKEN tok(fn, &dc.config_files);
     while (true) {
-        char *have = tok.next();
+        const char *have = tok.next();
         if (!have) break;
         if (have == token_threshold) {
             have = tok.next();