[pisa-src] r1592 - in trunk: libpisa/cfg.c libpisa/cfg.h libpisa/global.h libpisa/packet.c libpisa/packet.h libpisa/util.c libpisa/util.h pisacd/cdctx.c pisacd/cdctx.h pisacd/cdmain.c pisasd/sdctx.c pisasd/sd...

  • From: Thomas Jansen <mithi@xxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Mon, 09 Nov 2009 16:04:16 +0100

Author: tjansen
Date: Mon Nov  9 16:04:15 2009
New Revision: 1592

Log:
Use (bool, true, false) instead of (int, TRUE, FALSE).

We switched to C99 (-std=gnu99) recently. C99 comes with a standard way to
handle boolean variables, so we switch from the handwritten macros to the
standard ones defined in stdbool.h.

Modified:
   trunk/libpisa/cfg.c
   trunk/libpisa/cfg.h
   trunk/libpisa/global.h
   trunk/libpisa/packet.c
   trunk/libpisa/packet.h
   trunk/libpisa/util.c
   trunk/libpisa/util.h
   trunk/pisacd/cdctx.c
   trunk/pisacd/cdctx.h
   trunk/pisacd/cdmain.c
   trunk/pisasd/sdctx.c
   trunk/pisasd/sdctx.h
   trunk/pisasd/sdmain.c

Modified: trunk/libpisa/cfg.c
==============================================================================
--- trunk/libpisa/cfg.c Mon Nov  9 15:55:49 2009        (r1591)
+++ trunk/libpisa/cfg.c Mon Nov  9 16:04:15 2009        (r1592)
@@ -163,36 +163,36 @@
  * Return an int value of the currently opened config file
  * @param name name of setting
  * @param result here the result will be stored. if the setting can't be red, 
it won't be altered. So you can use a default value als initial setting
- * @return TRUE on success and FALSE on failure
+ * @return true on success and false on failure
  */
-int pisa_cfg_get_int_value(const char* name, int* result)
+bool pisa_cfg_get_int_value(const char* name, int* result)
 {
        long value = 0;
        int success = config_lookup_int(&cfg, name, &value);
 
        if (success == CONFIG_TRUE) {
                *result = value;
-               return TRUE;
+               return true;
        } else
-               return FALSE;
+               return false;
 }
 
 /**
  * Return an int value of the currently opened config file
  * @param name name of setting
  * @param result here the result will be stored. if the setting can't be red, 
it won't be altered. So you can use a default value als initial setting
- * @return TRUE on success and FALSE on failure
+ * @return true on success and false on failure
  */
-int pisa_cfg_get_bool_value(const char* name, int* result)
+bool pisa_cfg_get_bool_value(const char* name, int* result)
 {
        int value =0;
        int success = config_lookup_bool(&cfg, name, &value);
 
        if (success == CONFIG_TRUE) {
                *result = value;
-               return TRUE;
+               return true;
        } else
-               return FALSE;
+               return false;
 }
 
 /**
@@ -200,9 +200,9 @@
  * @param name    name of setting
  * @param buffer  [out] result here the result will be stored. if the setting 
can't be red, it won't be altered. So you can use a default value as initial 
setting
  * @param bufflen maximum size of buffer
- * @return TRUE on success and FALSE on failure
+ * @return true on success and false on failure
  */
-int pisa_cfg_get_string_value(const char* name, char* buffer, size_t bufflen)
+bool pisa_cfg_get_string_value(const char* name, char* buffer, size_t bufflen)
 {
        char* value = NULL;
        int success = config_lookup_string(&cfg, name, (const char **)&value);
@@ -211,9 +211,9 @@
                strncpy(buffer, value, bufflen);
                if (bufflen > 0)
                        buffer[bufflen - 1] = 0;
-               return TRUE;
+               return true;
        } else
-               return FALSE;
+               return false;
 }
 
 /**
@@ -223,9 +223,10 @@
  * @param name name of the group member to be obtained
  * @param buffer [out] buffer where the output data will be stored
  * @param bufflen the maximum length of buffer
+ * @return true on success and false on failure
  */
-int pisa_cfg_get_group_string_value(const struct cfg_group *g, const char* 
name,
-                                   char* buffer, size_t bufflen)
+bool pisa_cfg_get_group_string_value(const struct cfg_group *g, const char* 
name,
+                                    char* buffer, size_t bufflen)
 {
        const char* result;
        config_setting_t *s = NULL;
@@ -234,18 +235,18 @@
        *buffer = 0;
        if (!s) {
                fprintf(stderr, "Could find no such member in config.\n");
-               return FALSE;
+               return false;
        }
 
        result = config_setting_get_string(s);
        if (!result)
-               return FALSE;
+               return false;
 
        strncpy(buffer, result, bufflen);
        if (bufflen > 0)
                buffer[bufflen - 1] = 0;
 
-       return TRUE;
+       return true;
 }
 
 /**
@@ -253,16 +254,16 @@
  *
  * @param g the part of the configuration file that contains the value
  * @param name name of the group member to be obtained
- * @return 1 if true; 0 otherwise
+ * @return true on success and false on failure
  */
-int pisa_cfg_get_group_bool_value(const struct cfg_group *g, const char* name)
+bool pisa_cfg_get_group_bool_value(const struct cfg_group *g, const char* name)
 {
        config_setting_t *s = NULL;
        s = config_setting_get_member((const config_setting_t *)g, name);
 
        if (!s) {
                fprintf(stderr, "Could find no such member in config.\n");
-               return FALSE;
+               return false;
        }
 
        return config_setting_get_bool(s);

Modified: trunk/libpisa/cfg.h
==============================================================================
--- trunk/libpisa/cfg.h Mon Nov  9 15:55:49 2009        (r1591)
+++ trunk/libpisa/cfg.h Mon Nov  9 16:04:15 2009        (r1592)
@@ -14,6 +14,8 @@
 #ifndef PISA_CFG_H
 #define PISA_CFG_H
 
+#include <stdbool.h>
+
 #include <libconfig.h>
 struct cfg_group;
 
@@ -42,12 +44,12 @@
 void pisa_cfg_cleanup(void);
 int pisa_cfg_setup_file(const char *file);
 int pisa_cfg_setup(FILE *file);
-int pisa_cfg_get_int_value(const char* name,int* result);
-int pisa_cfg_get_bool_value(const char* name,int* result);
-int pisa_cfg_get_string_value(const char* name,char* buffer,size_t bufflen);
-int pisa_cfg_get_group_int_value(const struct cfg_group *,const char* 
name,int* result);
-int pisa_cfg_get_group_string_value(const struct cfg_group *,const char* 
name,char* buffer,size_t bufflen);
-int pisa_cfg_get_group_bool_value(const struct cfg_group *g, const char* name);
+bool pisa_cfg_get_int_value(const char* name,int* result);
+bool pisa_cfg_get_bool_value(const char* name,int* result);
+bool pisa_cfg_get_string_value(const char* name,char* buffer,size_t bufflen);
+bool pisa_cfg_get_group_int_value(const struct cfg_group *,const char* 
name,int* result);
+bool pisa_cfg_get_group_string_value(const struct cfg_group *,const char* 
name,char* buffer,size_t bufflen);
+bool pisa_cfg_get_group_bool_value(const struct cfg_group *g, const char* 
name);
 
 int pisa_cfg_authorized_hosts_setup_file(const char* file);
 int pisa_cfg_authorized_hosts_setup(FILE* file);

Modified: trunk/libpisa/global.h
==============================================================================
--- trunk/libpisa/global.h      Mon Nov  9 15:55:49 2009        (r1591)
+++ trunk/libpisa/global.h      Mon Nov  9 16:04:15 2009        (r1592)
@@ -106,14 +106,6 @@
  */
 #define MAX_PAYLOAD_BUFFER     1432
 
-#ifndef TRUE
-#define TRUE 1
-#endif
-
-#ifndef FALSE
-#define FALSE 0
-#endif
-
 /**
  * __BYTE_ORDER is not available on _APPLE_
  */

Modified: trunk/libpisa/packet.c
==============================================================================
--- trunk/libpisa/packet.c      Mon Nov  9 15:55:49 2009        (r1591)
+++ trunk/libpisa/packet.c      Mon Nov  9 16:04:15 2009        (r1592)
@@ -19,16 +19,16 @@
  * Check if the given buffer is a valid PISA tunnel control packet
  *
  * @param data   pointer to the data to be checked
- * @return TRUE if yes, FALSE if no
+ * @return true or false
  */
-int is_pisatun_packet(const char *data)
+bool is_pisatun_packet(const char *data)
 {
        const pisa_packet *pkt = (const pisa_packet *)data;
 
        if ((pisa_get_packet_type(pkt) & PISA_TYPE_MASK) == PISA_PKTTYPE_TUN)
-               return TRUE;
+               return true;
        else
-               return FALSE;
+               return false;
 }
 
 /**

Modified: trunk/libpisa/packet.h
==============================================================================
--- trunk/libpisa/packet.h      Mon Nov  9 15:55:49 2009        (r1591)
+++ trunk/libpisa/packet.h      Mon Nov  9 16:04:15 2009        (r1592)
@@ -17,6 +17,7 @@
 #include <unistd.h>
 #include <assert.h>
 #include <string.h>
+#include <stdbool.h>
 #include <stdlib.h>
 
 #include <sys/types.h>
@@ -124,7 +125,7 @@
        } body;
 } __attribute__ ((packed)) pisa_packet;
 
-int is_pisatun_packet(const char *data);
+bool is_pisatun_packet(const char *data);
 
 void pisa_send_control_packet_ipv4(int fd, struct sockaddr_in* addr, 
pisa_packet *p);
 void pisa_send_control_packet_ipv6(int fd, struct sockaddr_in6* addr, 
pisa_packet *p);
@@ -149,16 +150,16 @@
  * Check if the given buffer is a valid PISA packet
  *
  * @param data   pointer to the data to be checked
- * @return TRUE if yes, FALSE if no
+ * @return true or false
  */
-static inline int is_pisa_packet(char *data)
+static inline bool is_pisa_packet(char *data)
 {
        pisa_packet *pkt = (pisa_packet *)data;
 
        if (pisa_get_packet_version(pkt) == PISA_VERSION)
-               return TRUE;
+               return true;
        else
-               return FALSE;
+               return false;
 }
 
 #endif /* PISA_PACKET_H */

Modified: trunk/libpisa/util.c
==============================================================================
--- trunk/libpisa/util.c        Mon Nov  9 15:55:49 2009        (r1591)
+++ trunk/libpisa/util.c        Mon Nov  9 16:04:15 2009        (r1592)
@@ -173,26 +173,26 @@
  *
  * @param filename  the name of the file
  *
- * @return TRUE if exists, FALSE otherwise
+ * @return true or false
  */
-int pisa_check_if_hipd_runs(void){
+bool pisa_check_if_hipd_runs(void){
        struct stat file_stat;
        char filename[MAX_FILENAME+1], pidfilename[MAX_FILENAME+1];
        FILE *lock_file;
        char data[MAX_BUF];
-       int err = 0, pid = 0, ret, result = FALSE;
+       int err = 0, pid = 0, ret, result = false;
 
        strncpy(filename, HIPD_LOCKFILE, MAX_FILENAME);
 
        err = stat(filename, &file_stat);
        if (err && errno == ENOENT) {
                /* no such file or directory */
-               return FALSE;
+               return false;
        }
 
        if (!(lock_file = fopen(filename, "r+"))) {
                PISA_ERROR("Cannot open the lock file %s.\n", filename);
-               return FALSE;
+               return false;
        }
 
        ret = fread(data, sizeof(char), 16, lock_file);
@@ -208,7 +208,7 @@
                goto out_err;
        }
 
-       result = TRUE;
+       result = true;
 
 out_err:
        fclose(lock_file);

Modified: trunk/libpisa/util.h
==============================================================================
--- trunk/libpisa/util.h        Mon Nov  9 15:55:49 2009        (r1591)
+++ trunk/libpisa/util.h        Mon Nov  9 16:04:15 2009        (r1592)
@@ -14,6 +14,7 @@
 #ifndef PISA_UTIL_H
 #define PISA_UTIL_H
 
+#include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
 #include <netinet/in.h>
@@ -102,7 +103,7 @@
 
 int pisa_maxof(int num_args, ...);
 
-int pisa_check_if_hipd_runs(void);
+bool pisa_check_if_hipd_runs(void);
 
 int pisa_create_lock_file(const char *filename, const int killold);
 int pisa_remove_lock_file(const char *filename);

Modified: trunk/pisacd/cdctx.c
==============================================================================
--- trunk/pisacd/cdctx.c        Mon Nov  9 15:55:49 2009        (r1591)
+++ trunk/pisacd/cdctx.c        Mon Nov  9 16:04:15 2009        (r1592)
@@ -28,10 +28,10 @@
  */
 void cdctx_init(void)
 {
-       cd_ctx.flag_running = FALSE;
-       cd_ctx.flag_background = FALSE;
-       cd_ctx.flag_pending = 0;
-       cd_ctx.flag_kill_running = FALSE;
+       cd_ctx.flag_running = false;
+       cd_ctx.flag_background = false;
+       cd_ctx.flag_pending = false;
+       cd_ctx.flag_kill_running = false;
 
        cd_ctx.fd_tunnel = -1;
        cd_ctx.fd_control = -1;

Modified: trunk/pisacd/cdctx.h
==============================================================================
--- trunk/pisacd/cdctx.h        Mon Nov  9 15:55:49 2009        (r1591)
+++ trunk/pisacd/cdctx.h        Mon Nov  9 16:04:15 2009        (r1592)
@@ -13,6 +13,8 @@
 #ifndef PISA_CDCTX_H
 #define PISA_CDCTX_H
 
+#include <stdbool.h>
+
 #include "libpisa/global.h"
 #include "libpisa/conmgr.h"
 #include "libpisa/ctrlhandler.h"
@@ -26,12 +28,11 @@
 typedef struct {
        /**
         * Flags:
-        * 1: true, 0: false.
         */
-       int flag_running;               /* should the daemon continue to run */
-       int flag_background;    /* is the daemon running in background mode */
-       int flag_pending;               /* is there at least one pending event 
*/
-       int flag_kill_running;  /* should the daemon kill an already running 
instance */
+       bool flag_running;              /* should the daemon continue to run */
+       bool flag_background;   /* is the daemon running in background mode */
+       bool flag_pending;              /* is there at least one pending event 
*/
+       bool flag_kill_running; /* should the daemon kill an already running 
instance */
 
        /**
         * File descriptors and sockets:

Modified: trunk/pisacd/cdmain.c
==============================================================================
--- trunk/pisacd/cdmain.c       Mon Nov  9 15:55:49 2009        (r1591)
+++ trunk/pisacd/cdmain.c       Mon Nov  9 16:04:15 2009        (r1592)
@@ -102,7 +102,7 @@
        int i;
        struct pollfd pfds[nfds];
 
-       cd_ctx.flag_running = TRUE;
+       cd_ctx.flag_running = true;
 
        pisa_servers_add_all();
 
@@ -246,10 +246,10 @@
                        cd_cfg.port_data = atoi(optarg);
                        break;
                case 'b':
-                       cd_ctx.flag_background = TRUE;
+                       cd_ctx.flag_background = true;
                        break;
                case 'k':
-                       cd_ctx.flag_kill_running = TRUE;
+                       cd_ctx.flag_kill_running = true;
                        break;
                case 'd':
                        strncpy(cd_cfg.debuglevel, "all", 
sizeof(cd_cfg.debuglevel));
@@ -279,11 +279,11 @@
                switch (quitcode) {
                case SIGINT:
                        PISA_INFO("WARNING: Got a SIGINT signal.\n");
-                       cd_ctx.flag_running = FALSE;
+                       cd_ctx.flag_running = false;
                        break;
                case SIGQUIT:
                        PISA_INFO("WARNING: Got a SIGQUIT signal.\n");
-                       cd_ctx.flag_running = FALSE;
+                       cd_ctx.flag_running = false;
                        break;
                case SIGILL:
                        PISA_INFO("WARNING: Got a SIGILL signal.\n");
@@ -293,11 +293,11 @@
                        break;
                case SIGBUS:
                        PISA_INFO("WARNING: Got a SIGBUS signal.\n");
-                       cd_ctx.flag_running = FALSE;
+                       cd_ctx.flag_running = false;
                        break;
                case SIGTERM:
                        PISA_INFO("WARNING: Got a SIGTERM signal.\n");
-                       cd_ctx.flag_running = FALSE;
+                       cd_ctx.flag_running = false;
                        break;
                default:
                        PISA_INFO("WARNING: Got an unknown 
signal(signum=%d).\n",

Modified: trunk/pisasd/sdctx.c
==============================================================================
--- trunk/pisasd/sdctx.c        Mon Nov  9 15:55:49 2009        (r1591)
+++ trunk/pisasd/sdctx.c        Mon Nov  9 16:04:15 2009        (r1592)
@@ -25,9 +25,9 @@
  */
 void sdctx_init(void)
 {
-       sd_ctx.flag_running = FALSE;
-       sd_ctx.flag_background = FALSE;
-       sd_ctx.flag_kill_running = FALSE;
+       sd_ctx.flag_running = false;
+       sd_ctx.flag_background = false;
+       sd_ctx.flag_kill_running = false;
 
        sd_ctx.fd_control = -1;
        sd_ctx.fd_data = -1;

Modified: trunk/pisasd/sdctx.h
==============================================================================
--- trunk/pisasd/sdctx.h        Mon Nov  9 15:55:49 2009        (r1591)
+++ trunk/pisasd/sdctx.h        Mon Nov  9 16:04:15 2009        (r1592)
@@ -13,6 +13,8 @@
 #ifndef PISA_SDCTX_H
 #define PISA_SDCTX_H
 
+#include <stdbool.h>
+
 #include "libpisa/conmgr.h"
 #include "libpisa/ctrlhandler.h"
 #include "libpisa/global.h"
@@ -25,12 +27,10 @@
 typedef struct sd_context {
        /**
         * Flags:
-        * 1: true, 0: false.
         */
-       int flag_running;               /* should the daemon continue to run */
-       int flag_background;    /* is the daemon running in background mode */
-       int flag_pending;               /* is there at least one pending event 
*/
-       int flag_kill_running;  /* should the daemon kill an already running 
instance */
+       bool flag_running;              /* should the daemon continue to run */
+       bool flag_background;   /* is the daemon running in background mode */
+       bool flag_kill_running; /* should the daemon kill an already running 
instance */
 
        /**
         * File descriptors and sockets:

Modified: trunk/pisasd/sdmain.c
==============================================================================
--- trunk/pisasd/sdmain.c       Mon Nov  9 15:55:49 2009        (r1591)
+++ trunk/pisasd/sdmain.c       Mon Nov  9 16:04:15 2009        (r1592)
@@ -119,7 +119,7 @@
        for (i = 0; i < nfds; i++)
                pfds[i].events = POLLIN;
 
-       sd_ctx.flag_running = TRUE;
+       sd_ctx.flag_running = true;
        PISA_INFO("\nEntering main loop\n");
 
        while (sd_ctx.flag_running) {
@@ -253,11 +253,11 @@
                                break;
 
                        case 'b':
-                               sd_ctx.flag_background = TRUE;
+                               sd_ctx.flag_background = true;
                                break;
 
                        case 'k':
-                               sd_ctx.flag_kill_running = TRUE;
+                               sd_ctx.flag_kill_running = true;
                                break;
 
                        case 'd':
@@ -293,7 +293,7 @@
                case SIGQUIT:
                case SIGBUS:
                        PISA_DEBUG(PL_SHUTDOWN, "Quitting PISA server 
daemon...\n");
-                       sd_ctx.flag_running = FALSE;
+                       sd_ctx.flag_running = false;
                        break;
 
                case SIGILL:

Other related posts:

  • » [pisa-src] r1592 - in trunk: libpisa/cfg.c libpisa/cfg.h libpisa/global.h libpisa/packet.c libpisa/packet.h libpisa/util.c libpisa/util.h pisacd/cdctx.c pisacd/cdctx.h pisacd/cdmain.c pisasd/sdctx.c pisasd/sd... - Thomas Jansen