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