]> granicus.if.org Git - vnstat/commitdiff
refactor vnstatd.c
authorTeemu Toivola <git@humdi.net>
Mon, 19 Aug 2019 17:31:55 +0000 (20:31 +0300)
committerTeemu Toivola <git@humdi.net>
Mon, 19 Aug 2019 17:31:55 +0000 (20:31 +0300)
src/vnstat.c
src/vnstatd.c
src/vnstatd.h

index 951e41f92e240e1b90ee4642b34c4b151325ef76..6ee8353ea35a1d8519094a04875ee1411af11f0c 100644 (file)
@@ -20,7 +20,6 @@ vnStat - Copyright (c) 2002-2019 Teemu Toivola <tst@iki.fi>
 #include "dbsql.h"
 #include "misc.h"
 #include "cfg.h"
-#include "cfgoutput.h"
 #include "ibw.h"
 #include "vnstat_func.h"
 
index 1ec9ad23c424cc55451a5c8c644d995cde9248a9..b72f29e7567369bed8e62418f6e51fab9563b36c 100644 (file)
@@ -70,87 +70,7 @@ int main(int argc, char *argv[])
        s.updateinterval = cfg.updateinterval;
        s.saveinterval = cfg.saveinterval * 60;
 
-       /* parse parameters, maybe not the best way but... */
-       for (currentarg = 1; currentarg < argc; currentarg++) {
-               if (debug)
-                       printf("arg %d: \"%s\"\n", currentarg, argv[currentarg]);
-               if ((strcmp(argv[currentarg], "-?") == 0) || (strcmp(argv[currentarg], "--help") == 0)) {
-                       break;
-               } else if (strcmp(argv[currentarg], "--config") == 0) {
-                       /* config has already been parsed earlier so nothing to do here */
-                       currentarg++;
-                       continue;
-               } else if ((strcmp(argv[currentarg], "-D") == 0) || (strcmp(argv[currentarg], "--debug") == 0)) {
-                       debug = 1;
-               } else if ((strcmp(argv[currentarg], "-d") == 0) || (strcmp(argv[currentarg], "--daemon") == 0)) {
-                       s.rundaemon = 1;
-                       s.showhelp = 0;
-               } else if ((strcmp(argv[currentarg], "-n") == 0) || (strcmp(argv[currentarg], "--nodaemon") == 0)) {
-                       s.showhelp = 0;
-               } else if ((strcmp(argv[currentarg], "-s") == 0) || (strcmp(argv[currentarg], "--sync") == 0)) {
-                       s.sync = 1;
-               } else if ((strcmp(argv[currentarg], "-u") == 0) || (strcmp(argv[currentarg], "--user") == 0)) {
-                       if (currentarg + 1 < argc) {
-                               strncpy_nt(s.user, argv[currentarg + 1], 33);
-                               if (debug)
-                                       printf("Requested user: \"%s\"\n", s.user);
-                               currentarg++;
-                               continue;
-                       } else {
-                               printf("Error: User for --user missing.\n");
-                               return 1;
-                       }
-               } else if ((strcmp(argv[currentarg], "-g") == 0) || (strcmp(argv[currentarg], "--group") == 0)) {
-                       if (currentarg + 1 < argc) {
-                               strncpy_nt(s.group, argv[currentarg + 1], 33);
-                               if (debug)
-                                       printf("Requested group: \"%s\"\n", s.group);
-                               currentarg++;
-                               continue;
-                       } else {
-                               printf("Error: Group for --group missing.\n");
-                               return 1;
-                       }
-               } else if (strcmp(argv[currentarg], "--noadd") == 0) {
-                       s.noadd = 1;
-               } else if (strcmp(argv[currentarg], "--alwaysadd") == 0) {
-                       s.alwaysadd = 1;
-               } else if ((strcmp(argv[currentarg], "-v") == 0) || (strcmp(argv[currentarg], "--version") == 0)) {
-                       printf("vnStat daemon %s by Teemu Toivola <tst at iki dot fi>\n", getversion());
-                       return 0;
-               } else if ((strcmp(argv[currentarg], "-p") == 0) || (strcmp(argv[currentarg], "--pidfile") == 0)) {
-                       if (currentarg + 1 < argc) {
-                               strncpy_nt(cfg.pidfile, argv[currentarg + 1], 512);
-                               cfg.pidfile[511] = '\0';
-                               if (debug)
-                                       printf("Used pid file: %s\n", cfg.pidfile);
-                               currentarg++;
-                               continue;
-                       } else {
-                               printf("Error: File for --pidfile missing.\n");
-                               return 1;
-                       }
-               } else {
-                       printf("Unknown arg \"%s\". Use --help for help.\n", argv[currentarg]);
-                       return 1;
-               }
-       }
-
-       if (s.noadd && s.alwaysadd) {
-               printf("Error: --noadd and --alwaysadd can't both be used at the same time.\n");
-               return 1;
-       }
-
-       if (s.rundaemon && debug) {
-               printf("Error: --daemon and --debug can't both be used at the same time.\n");
-               return 1;
-       }
-
-       /* show help if nothing else was asked to be done */
-       if (s.showhelp) {
-               showhelp();
-               return 0;
-       }
+       parseargs(&s, argc, argv);
 
        preparedirs(&s);
 
@@ -323,3 +243,90 @@ void showhelp(void)
 
        printf("See also \"man vnstatd\".\n");
 }
+
+void parseargs(DSTATE *s, int argc, char **argv)
+{
+       int currentarg;
+
+       /* parse parameters, maybe not the best way but... */
+       for (currentarg = 1; currentarg < argc; currentarg++) {
+               if (debug)
+                       printf("arg %d: \"%s\"\n", currentarg, argv[currentarg]);
+               if ((strcmp(argv[currentarg], "-?") == 0) || (strcmp(argv[currentarg], "--help") == 0)) {
+                       break;
+               } else if (strcmp(argv[currentarg], "--config") == 0) {
+                       /* config has already been parsed earlier so nothing to do here */
+                       currentarg++;
+                       continue;
+               } else if ((strcmp(argv[currentarg], "-D") == 0) || (strcmp(argv[currentarg], "--debug") == 0)) {
+                       debug = 1;
+               } else if ((strcmp(argv[currentarg], "-d") == 0) || (strcmp(argv[currentarg], "--daemon") == 0)) {
+                       s->rundaemon = 1;
+                       s->showhelp = 0;
+               } else if ((strcmp(argv[currentarg], "-n") == 0) || (strcmp(argv[currentarg], "--nodaemon") == 0)) {
+                       s->showhelp = 0;
+               } else if ((strcmp(argv[currentarg], "-s") == 0) || (strcmp(argv[currentarg], "--sync") == 0)) {
+                       s->sync = 1;
+               } else if ((strcmp(argv[currentarg], "-u") == 0) || (strcmp(argv[currentarg], "--user") == 0)) {
+                       if (currentarg + 1 < argc) {
+                               strncpy_nt(s->user, argv[currentarg + 1], 33);
+                               if (debug)
+                                       printf("Requested user: \"%s\"\n", s->user);
+                               currentarg++;
+                               continue;
+                       } else {
+                               printf("Error: User for --user missing.\n");
+                               exit(EXIT_FAILURE);
+                       }
+               } else if ((strcmp(argv[currentarg], "-g") == 0) || (strcmp(argv[currentarg], "--group") == 0)) {
+                       if (currentarg + 1 < argc) {
+                               strncpy_nt(s->group, argv[currentarg + 1], 33);
+                               if (debug)
+                                       printf("Requested group: \"%s\"\n", s->group);
+                               currentarg++;
+                               continue;
+                       } else {
+                               printf("Error: Group for --group missing.\n");
+                               exit(EXIT_FAILURE);
+                       }
+               } else if (strcmp(argv[currentarg], "--noadd") == 0) {
+                       s->noadd = 1;
+               } else if (strcmp(argv[currentarg], "--alwaysadd") == 0) {
+                       s->alwaysadd = 1;
+               } else if ((strcmp(argv[currentarg], "-v") == 0) || (strcmp(argv[currentarg], "--version") == 0)) {
+                       printf("vnStat daemon %s by Teemu Toivola <tst at iki dot fi>\n", getversion());
+                       exit(EXIT_SUCCESS);
+               } else if ((strcmp(argv[currentarg], "-p") == 0) || (strcmp(argv[currentarg], "--pidfile") == 0)) {
+                       if (currentarg + 1 < argc) {
+                               strncpy_nt(cfg.pidfile, argv[currentarg + 1], 512);
+                               cfg.pidfile[511] = '\0';
+                               if (debug)
+                                       printf("Used pid file: %s\n", cfg.pidfile);
+                               currentarg++;
+                               continue;
+                       } else {
+                               printf("Error: File for --pidfile missing.\n");
+                               exit(EXIT_FAILURE);
+                       }
+               } else {
+                       printf("Unknown arg \"%s\". Use --help for help.\n", argv[currentarg]);
+                       exit(EXIT_FAILURE);
+               }
+       }
+
+       if (s->noadd && s->alwaysadd) {
+               printf("Error: --noadd and --alwaysadd can't both be used at the same time.\n");
+               exit(EXIT_FAILURE);
+       }
+
+       if (s->rundaemon && debug) {
+               printf("Error: --daemon and --debug can't both be used at the same time.\n");
+               exit(EXIT_FAILURE);
+       }
+
+       /* show help if nothing else was asked to be done */
+       if (s->showhelp) {
+               showhelp();
+               exit(EXIT_SUCCESS);
+       }
+}
index 5be57d761482b07215311324125944d31577e43d..08110e532be4e8323b768e035f5c235e5396a155 100644 (file)
@@ -2,5 +2,6 @@
 #define VNSTATD_H
 
 void showhelp(void);
+void parseargs(DSTATE *s, int argv, char **argc);
 
 #endif