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