[vhffs-dev] [1338] Added getopt() support |
[ Thread Index |
Date Index
| More vhffs.org/vhffs-dev Archives
]
Revision: 1338
Author: gradator
Date: 2009-02-17 00:09:47 +0100 (Tue, 17 Feb 2009)
Log Message:
-----------
Added getopt() support
Modified Paths:
--------------
trunk/vhffs-fssync/vhffsfssync_master.c
trunk/vhffs-fssync/vhffsfssync_slave.c
Modified: trunk/vhffs-fssync/vhffsfssync_master.c
===================================================================
--- trunk/vhffs-fssync/vhffsfssync_master.c 2009-02-16 18:44:31 UTC (rev 1337)
+++ trunk/vhffs-fssync/vhffsfssync_master.c 2009-02-16 23:09:47 UTC (rev 1338)
@@ -47,6 +47,7 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/sendfile.h>
+#include <getopt.h>
/* -- inotify stuff -- */
@@ -221,6 +222,9 @@
void vhffsfssync_net_fullview_alarmsignal(int signo);
+// misc
+static void usage_exit(int ret_code, char *progname);
+
/* ----------------------------------------- */
/* -- network stuff -- */
@@ -1527,19 +1531,87 @@
}
+static void usage_exit(int ret_code, char *progname) {
+ printf ("Usage: %s [OPTION]... DIRECTORY\n"
+ "Remote synchronous file-copying tool, this is the client (the slave)\n\n"
+ " -f, --foreground\tDon't daemonise the server, display errors on the console\n"
+ " -b, --bind\t\tListen to the specified IP address\n"
+ " -p, --port\t\tListen to this port\n"
+ " -h, --help\t\tDisplay this help and exit\n"
+ " -v, --version\t\tOutput version information and exit\n",
+ progname);
+ exit(ret_code);
+}
+
+
int main(int argc, char *argv[]) {
- char *root;
int inotifyfd, flags;
int wd;
int listenfd, opt;
- uint32_t bindaddr;
- uint16_t bindport;
struct sockaddr_in src;
+ int foreground = 0;
+ uint32_t bindaddr = INADDR_ANY;
+ uint16_t bindport = 4567;
+ char *root = NULL;
+
+ struct option long_options[] = {
+ { "foreground", no_argument, NULL, 'f' },
+ { "bind", required_argument, NULL, 'b' },
+ { "port", required_argument, NULL, 'p' },
+ { "help", no_argument, NULL, 'h' },
+ { "version", no_argument, NULL, 'v' },
+ { 0, 0, 0, 0 }
+ };
+
+ while(1) {
+ int option_index = 0, c;
+ c = getopt_long(argc, argv, "fb:p:hv", long_options, &option_index);
+ if(c == -1)
+ break;
+
+ switch(c) {
+ case 'f':
+ foreground = 1;
+ break;
+
+ case 'b':
+ bindaddr = inet_addr(optarg);
+ break;
+
+ case 'p':
+ bindport = atoi(optarg);
+ break;
+
+ case 'h':
+ usage_exit(0, argv[0]);
+
+ case 'v':
+#ifdef VERSION
+ fputs("vhffsfssync_master " VERSION "\n", stdout);
+#else
+ fputs("vhffsfssync_master\n", stdout);
+#endif
+ exit(0);
+
+ case '?':
+ /* `getopt_long' already printed an error message. */
+ fprintf(stderr,"Try `%s --help' for more information.\n", argv[0]);
+ exit(1);
+
+ default:
+ abort();
+ }
+ }
+
+ if(optind != argc-1)
+ usage_exit(1, argv[0]);
+
+ root = argv[optind++];
+
/* chdir() to the filesystem to monitor */
- root = argv[1];
if(!root) return -1;
if( root[strlen(root)-1] == '/' ) root[strlen(root)-1] = '\0';
#if DEBUG_INOTIFY
@@ -1603,8 +1675,6 @@
fprintf(stderr, "setsockopt() failed on socket %d: %s\n", listenfd, strerror(errno));
}
- bindaddr = INADDR_ANY;
- bindport = 4567;
src.sin_addr.s_addr = bindaddr;
src.sin_family = AF_INET;
src.sin_port = htons(bindport);
Modified: trunk/vhffs-fssync/vhffsfssync_slave.c
===================================================================
--- trunk/vhffs-fssync/vhffsfssync_slave.c 2009-02-16 18:44:31 UTC (rev 1337)
+++ trunk/vhffs-fssync/vhffsfssync_slave.c 2009-02-16 23:09:47 UTC (rev 1338)
@@ -45,6 +45,7 @@
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
+#include <getopt.h>
/* -- network stuff -- */
@@ -88,6 +89,9 @@
int vhffsfssync_event(vhffsfssync_conn *conn, char *event);
int vhffsfssync_parse(vhffsfssync_conn *conn);
+// misc
+static void usage_exit(int ret_code, char *progname);
+
/* ------------------------------------------------------------ */
/* our events use \0 as a delimiter, a double \0 is the end of the event */
@@ -618,14 +622,82 @@
}
+static void usage_exit(int ret_code, char *progname) {
+ printf ("Usage: %s [OPTION]... HOST[:PORT] DIRECTORY\n"
+ "Remote synchronous file-copying tool, this is the client (the slave)\n\n"
+ " -f, --foreground\tDon't daemonise the client, display errors on the console\n"
+ " -h, --help\t\tDisplay this help and exit\n"
+ " -v, --version\t\tOutput version information and exit\n",
+ progname);
+ exit(ret_code);
+}
+
+
int main(int argc, char *argv[]) {
- char *root, *host;
- int flags, port;
+ int flags;
vhffsfssync_conn *conn;
+ char *cur;
+ int foreground = 0;
+ char *host = NULL;
+ int port = 4567;
+ char *root = NULL;
+
+ struct option long_options[] = {
+ { "foreground", no_argument, NULL, 'f' },
+ { "help", no_argument, NULL, 'h' },
+ { "version", no_argument, NULL, 'v' },
+ { 0, 0, 0, 0 }
+ };
+
+ while(1) {
+ int option_index = 0, c;
+ c = getopt_long(argc, argv, "fhv", long_options, &option_index);
+ if(c == -1)
+ break;
+
+ switch(c) {
+ case 'f':
+ foreground = 1;
+ break;
+
+ case 'h':
+ usage_exit(0, argv[0]);
+
+ case 'v':
+#ifdef VERSION
+ fputs("vhffsfssync_slave " VERSION "\n", stdout);
+#else
+ fputs("vhffsfssync_slave\n", stdout);
+#endif
+ exit(0);
+
+ case '?':
+ /* `getopt_long' already printed an error message. */
+ fprintf(stderr,"Try `%s --help' for more information.\n", argv[0]);
+ exit(1);
+
+ default:
+ abort();
+ }
+ }
+
+ if(optind != argc-2)
+ usage_exit(1, argv[0]);
+
+ host = argv[optind++];
+ for(cur=host ; *cur ; cur++) {
+ if(*cur == ':') {
+ *cur = '\0';
+ port = atoi(++cur);
+ break;
+ }
+ }
+
+ root = argv[optind++];
+
/* chdir() to the filesystem to write the data */
- root = argv[1];
if( chdir(root) < 0 ) {
fprintf(stderr, "cannot chdir() to %s: %s\n", root, strerror(errno));
return -1;
@@ -637,8 +709,6 @@
root = ".";
/* -- network stuff -- */
- host = argv[2];
- port = atoi(argv[3]);
signal(SIGPIPE, SIG_IGN);
conn = malloc(sizeof(vhffsfssync_conn));
conn->fd = -1;