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