cf16f1081ab8ba949eef577e4e56ad8f71ef6d36
[openafs.git] / src / ubik / ubikcmd.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <sys/types.h>
17 #ifdef AFS_NT40_ENV
18 #include <winsock2.h>
19 #else
20 #include <sys/file.h>
21 #include <netinet/in.h>
22 #include <sys/param.h>
23 #include <netdb.h>
24 #endif
25 #include <time.h>
26 #include <lock.h>
27 #ifdef HAVE_STRING_H
28 #include <string.h>
29 #else
30 #ifdef HAVE_STRINGS_H
31 #include <strings.h>
32 #endif
33 #endif
34 #include <rx/xdr.h>
35 #include <rx/rx.h>
36
37 #define UBIK_INTERNALS
38 #include "ubik.h"
39
40 /* This file contain useful subroutines for parsing command line args for ubik
41  *   applications.
42  */
43
44 ubik_ParseServerList(argc, argv, ahost, aothers)
45      int argc;
46      char **argv;
47      afs_int32 *ahost;
48      afs_int32 *aothers;
49 {
50     register afs_int32 i;
51     register char *tp;
52     register struct hostent *th;
53     char hostname[64];
54     afs_int32 myHost, temp, counter;
55     int inServer, sawServer;
56
57     gethostname(hostname, sizeof(hostname));
58     th = gethostbyname(hostname);
59     if (!th)
60         return UBADHOST;
61     memcpy(&myHost, th->h_addr, sizeof(afs_int32));
62     *ahost = myHost;
63
64     inServer = 0;               /* haven't seen -servers yet */
65     sawServer = 0;
66     counter = 0;
67     for (i = 1; i < argc; i++) {
68         /* look for -servers argument */
69         tp = argv[i];
70
71         if (inServer) {
72             if (*tp == '-') {
73                 inServer = 0;
74             } else {
75                 /* otherwise this is a new host name */
76                 th = gethostbyname(tp);
77                 if (!th)
78                     return UBADHOST;
79                 memcpy(&temp, th->h_addr, sizeof(afs_int32));
80                 if (temp != myHost) {
81                     if (counter++ >= MAXSERVERS)
82                         return UNHOSTS;
83                     *aothers++ = temp;
84                 }
85             }
86         }
87         /* haven't seen a -server yet */
88         if (!strcmp(tp, "-servers")) {
89             inServer = 1;
90             sawServer = 1;
91         } else if (!strcmp(tp, "-dubik")) {
92             ubik_debugFlag = 1;
93         }
94     }
95     if (!sawServer) {
96         /* never saw a -server */
97         return UNOENT;
98     }
99     if (counter < MAXSERVERS)
100         *aothers++ = 0;         /* null terminate if there's room */
101     return 0;
102 }