Upload Tizen:Main source
[pkgs/xorg/util/xorg-x11-xauth.git] / xauth.c
1 /*
2  * $Xorg: xauth.c,v 1.4 2001/02/09 02:05:38 xorgcvs Exp $
3  * $XdotOrg: $
4  *
5  * xauth - manipulate authorization file
6  *
7  * 
8 Copyright 1989,1998  The Open Group
9
10 Permission to use, copy, modify, distribute, and sell this software and its
11 documentation for any purpose is hereby granted without fee, provided that
12 the above copyright notice appear in all copies and that both that
13 copyright notice and this permission notice appear in supporting
14 documentation.
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
22 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 Except as contained in this notice, the name of The Open Group shall not be
27 used in advertising or otherwise to promote the sale, use or other dealings
28 in this Software without prior written authorization from The Open Group.
29  * *
30  * Author:  Jim Fulton, MIT X Consortium
31  */
32 /* $XFree86: xc/programs/xauth/xauth.c,v 1.5tsi Exp $ */
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include "xauth.h"
39
40
41 /*
42  * global data
43  */
44 char *ProgramName;                      /* argv[0], set at top of main() */
45 int verbose = -1;                       /* print certain messages */
46 Bool ignore_locks = False;              /* for error recovery */
47 Bool break_locks = False;               /* for error recovery */
48 Bool no_name_lookups = False;           /* show addresses instead of names */
49
50 /*
51  * local data
52  */
53
54 static char *authfilename = NULL;       /* filename of cookie file */
55 static char *defcmds[] = { "source", "-", NULL };  /* default command */
56 static int ndefcmds = 2;
57 static char *defsource = "(stdin)";
58
59 /*
60  * utility routines
61  */
62 static void 
63 usage(void)
64 {
65     static char *prefixmsg[] = {
66 "",
67 "where options include:",
68 "    -f authfilename                name of authority file to use",
69 "    -v                             turn on extra messages",
70 "    -q                             turn off extra messages",
71 "    -i                             ignore locks on authority file",
72 "    -b                             break locks on authority file",
73 "",
74 "and commands have the following syntax:",
75 "",
76 NULL };
77     static char *suffixmsg[] = {
78 "A dash may be used with the \"merge\" and \"source\" to read from the",
79 "standard input.  Commands beginning with \"n\" use numeric format.",
80 "",
81 NULL };
82     char **msg;
83
84     fprintf (stderr, "usage:  %s [-options ...] [command arg ...]\n",
85              ProgramName);
86     for (msg = prefixmsg; *msg; msg++) {
87         fprintf (stderr, "%s\n", *msg);
88     }
89     print_help (stderr, NULL, "    ");  /* match prefix indentation */
90     fprintf (stderr, "\n");
91     for (msg = suffixmsg; *msg; msg++) {
92         fprintf (stderr, "%s\n", *msg);
93     }
94     exit (1);
95 }
96
97
98 /*
99  * The main routine - parses command line and calls action procedures
100  */
101 int
102 main(int argc, char *argv[])
103 {
104     int i;
105     char *sourcename = defsource;
106     char **arglist = defcmds;
107     int nargs = ndefcmds;
108     int status;
109
110     ProgramName = argv[0];
111
112     for (i = 1; i < argc; i++) {
113         char *arg = argv[i];
114
115         if (arg[0] == '-') {
116             char *flag;
117
118             for (flag = (arg + 1); *flag; flag++) {
119                 switch (*flag) {
120                   case 'f':             /* -f authfilename */
121                     if (++i >= argc) usage ();
122                     authfilename = argv[i];
123                     continue;
124                   case 'v':             /* -v */
125                     verbose = 1;
126                     continue;
127                   case 'q':             /* -q */
128                     verbose = 0;
129                     continue;
130                   case 'b':             /* -b */
131                     break_locks = True;
132                     continue;
133                   case 'i':             /* -i */
134                     ignore_locks = True;
135                     continue;
136                   case 'n':             /* -n */
137                     no_name_lookups = True;
138                     continue;
139                   default:
140                     usage ();
141                 }
142             }
143         } else {
144             sourcename = "(argv)";
145             nargs = argc - i;
146             arglist = argv + i;
147             if (verbose == -1) verbose = 0;
148             break;
149         }
150     }
151
152     if (verbose == -1) {                /* set default, don't junk stdout */
153         verbose = (isatty(fileno(stdout)) != 0);
154     }
155
156     if (!authfilename) {
157         authfilename = XauFileName ();  /* static name, do not free */
158         if (!authfilename) {
159             fprintf (stderr,
160                      "%s:  unable to generate an authority file name\n",
161                      ProgramName);
162             exit (1);
163         }
164     }
165     if (auth_initialize (authfilename) != 0) {
166         /* error message printed in auth_initialize */
167         exit (1);
168     }
169
170     status = process_command (sourcename, 1, nargs, arglist);
171
172     (void) auth_finalize ();
173     exit ((status != 0) ? 1 : 0);
174 }
175
176