openafs-string-header-cleanup-20071030
[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 #include <string.h>
28 #include <rx/xdr.h>
29 #include <rx/rx.h>
30
31 #define UBIK_INTERNALS
32 #include "ubik.h"
33
34 /* This file contain useful subroutines for parsing command line args for ubik
35  *   applications.
36  */
37
38 ubik_ParseServerList(argc, argv, ahost, aothers)
39      int argc;
40      char **argv;
41      afs_int32 *ahost;
42      afs_int32 *aothers;
43 {
44     register afs_int32 i;
45     register char *tp;
46     register struct hostent *th;
47     char hostname[64];
48     afs_int32 myHost, temp, 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_int32));
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_int32));
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 }