a51ac4bc8eeb0a23d2f10315d0738398f90ca004
[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
14 #include <sys/types.h>
15 #include <string.h>
16 #include <stdarg.h>
17 #include <time.h>
18
19 #ifdef AFS_NT40_ENV
20 #include <winsock2.h>
21 #else
22 #include <sys/file.h>
23 #include <netinet/in.h>
24 #include <sys/param.h>
25 #include <netdb.h>
26 #endif
27 #include <lock.h>
28 #include <rx/xdr.h>
29 #include <rx/rx.h>
30
31 #define UBIK_INTERNALS
32 #include "ubik.h"
33
34 /*! \file
35  * This file contain useful subroutines for parsing command line args for ubik
36  * applications.
37  */
38 int
39 ubik_ParseServerList(int argc, char **argv, afs_int32 *ahost, 
40                      afs_int32 *aothers)
41 {
42     register afs_int32 i;
43     register char *tp;
44     register struct hostent *th;
45     char hostname[64];
46     afs_int32 myHost, temp, counter;
47     int inServer, sawServer;
48
49     gethostname(hostname, sizeof(hostname));
50     th = gethostbyname(hostname);
51     if (!th)
52         return UBADHOST;
53     memcpy(&myHost, th->h_addr, sizeof(afs_int32));
54     *ahost = myHost;
55
56     inServer = 0;               /* haven't seen -servers yet */
57     sawServer = 0;
58     counter = 0;
59     for (i = 1; i < argc; i++) {
60         /* look for -servers argument */
61         tp = argv[i];
62
63         if (inServer) {
64             if (*tp == '-') {
65                 inServer = 0;
66             } else {
67                 /* otherwise this is a new host name */
68                 th = gethostbyname(tp);
69                 if (!th)
70                     return UBADHOST;
71                 memcpy(&temp, th->h_addr, sizeof(afs_int32));
72                 if (temp != myHost) {
73                     if (counter++ >= MAXSERVERS)
74                         return UNHOSTS;
75                     *aothers++ = temp;
76                 }
77             }
78         }
79         /* haven't seen a -server yet */
80         if (!strcmp(tp, "-servers")) {
81             inServer = 1;
82             sawServer = 1;
83         } else if (!strcmp(tp, "-dubik")) {
84             ubik_debugFlag = 1;
85         }
86     }
87     if (!sawServer) {
88         /* never saw a -server */
89         return UNOENT;
90     }
91     if (counter < MAXSERVERS)
92         *aothers++ = 0;         /* null terminate if there's room */
93     return 0;
94 }