[dev] [sic] [patch] const-correctness and formalities

From: FRIGN <dev_AT_frign.de>
Date: Wed, 21 Aug 2013 21:11:17 +0200

#### util.c ####
--- util.c 2013-08-21 20:53:34.774661860 +0200
+++ util.c 2013-08-21 17:58:22.327780012 +0200
_AT_@ -17,7 +17,7 @@
 }

 static int
-dial(char *host, char *port) {
+dial(const char *host, const char *port) {
        static struct addrinfo hints;
        int srv;
        struct addrinfo *res, *r;
_AT_@ -42,20 +42,20 @@

 #define strlcpy _strlcpy
 static void
-strlcpy(char *to, const char *from, int l) {
+strlcpy(char *to, const char *from, size_t l) {
        memccpy(to, from, '\0', l);
        to[l-1] = '\0';
 }

-static char *
-eat(char *s, int (*p)(int), int r) {
+static char*
+eat(char *s, int (*p)(int), const int r) {
        while(*s != '\0' && p(*s) == r)
                s++;
        return s;
 }

 static char*
-skip(char *s, char c) {
+skip(char *s, const char c) {
        while(*s != c && *s != '\0')
                s++;
        if(*s != '\0')


#### sic.c ####
--- sic.c 2013-08-21 18:14:48.608768927 +0200
+++ sic.c 2013-08-21 20:02:40.947696183 +0200
_AT_@ -21,7 +21,7 @@
 #include "util.c"

 static void
-pout(char *channel, char *fmt, ...) {
+pout(const char *channel, const char *fmt, ...) {
        static char timestr[18];
        time_t t;
        va_list ap;
_AT_@ -35,7 +35,7 @@
 }

 static void
-sout(char *fmt, ...) {
+sout(const char *fmt, ...) {
        va_list ap;

        va_start(ap, fmt);
_AT_@ -45,7 +45,7 @@
 }

 static void
-privmsg(char *channel, char *msg) {
+privmsg(const char *channel, const char *msg) {
        if(channel[0] == '\0') {
                pout("", "No channel to send to");
                return;
_AT_@ -75,8 +75,8 @@
                                strlcpy(channel, p, sizeof channel);
                        return;
                case 'l':
- s = eat(p, isspace, 1);
- p = eat(s, isspace, 0);
+ s = eat(p, &isspace, 1);
+ p = eat(s, &isspace, 0);
                        if(!*s)
                                s = channel;
                        if(*p)
_AT_@ -86,8 +86,8 @@
                        sout("PART %s :%s", s, p);
                        return;
                case 'm':
- s = eat(p, isspace, 1);
- p = eat(s, isspace, 0);
+ s = eat(p, &isspace, 1);
+ p = eat(s, &isspace, 0);
                        if(*p)
                                *p++ = '\0';
                        privmsg(s, p);
_AT_@ -132,7 +132,7 @@
 }

 int
-main(int argc, char *argv[]) {
+main(const int argc, const char *argv[]) {
        int i, c;
        struct timeval tv;
        const char *user = getenv("USER");


Declaring immutable arguments makes reading the code easier.
Additionally, there was a small formal error how the pointer to the
isspace()-function was passed as an argument to the eat-function.
Please let me know what you think about my proposed changes.

    FRIGN
Received on Wed Aug 21 2013 - 21:11:17 CEST

This archive was generated by hypermail 2.3.0 : Wed Aug 21 2013 - 23:12:07 CEST