59c2321e7c5fea9ba3751727fd8dabae4a10986f
[platform/upstream/bash.git] / support / bashversion.c
1 /* bashversion.c -- Display bash version information. */
2
3 /* Copyright (C) 2001 Free Software Foundation, Inc.
4
5    This file is part of GNU Bash, the Bourne Again SHell.
6
7    Bash is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11
12    Bash is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Bash.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include "stdc.h"
24
25 #include <stdio.h>
26
27 #if defined (HAVE_UNISTD_H)
28 #  include <unistd.h>
29 #endif
30
31 #include "bashansi.h"
32
33 #include "version.h"
34 #include "conftypes.h"
35
36 #define RFLAG   0x0001
37 #define VFLAG   0x0002
38 #define MFLAG   0x0004
39 #define PFLAG   0x0008
40 #define SFLAG   0x0010
41 #define LFLAG   0x0020
42 #define XFLAG   0x0040
43
44 extern int optind;
45 extern char *optarg;
46
47 extern char *dist_version;
48 extern int patch_level;
49
50 extern char *shell_version_string __P((void));
51 extern void show_shell_version __P((int));
52
53 char *shell_name = "bash";
54 char *progname;
55
56 static void
57 usage()
58 {
59   fprintf(stderr, "%s: usage: %s [-hrvpmlsx]\n", progname, progname);
60 }
61
62 int
63 main (argc, argv)
64      int argc;
65      char **argv;
66 {
67   int opt, oflags;
68   char dv[128], *rv;
69
70   if (progname = strrchr (argv[0], '/'))
71     progname++;
72   else
73     progname = argv[0];
74
75   oflags = 0;
76   while ((opt = getopt(argc, argv, "hrvmpslx")) != EOF)
77     {
78       switch (opt)
79         {
80         case 'h':
81           usage ();
82           exit (0);
83         case 'r':
84           oflags |= RFLAG;      /* release */
85           break;
86         case 'v':
87           oflags |= VFLAG;      /* version */
88           break;
89         case 'm':
90           oflags |= MFLAG;      /* machtype */
91           break;
92         case 'p':
93           oflags |= PFLAG;      /* patchlevel */
94           break;
95         case 's':               /* short version string */
96           oflags |= SFLAG;
97           break;
98         case 'l':               /* long version string */
99           oflags |= LFLAG;
100           break;
101         case 'x':               /* extended version information */
102           oflags |= XFLAG;
103           break;
104         default:
105           usage ();
106           exit (2);
107         }
108     }
109
110   argc -= optind;
111   argv += optind;
112
113   if (argc > 0)
114     {
115       usage ();
116       exit (2);
117     }
118
119   /* default behavior */  
120   if (oflags == 0)
121     oflags = SFLAG;
122
123   if (oflags & (RFLAG|VFLAG))
124     {
125       strcpy (dv, dist_version);
126       rv = strchr (dv, '.');
127       if (rv)
128         *rv++ = '\0';
129       else
130         rv = "00";
131     }
132   if (oflags & RFLAG)
133     printf ("%s\n", dv);
134   else if (oflags & VFLAG)
135     printf ("%s\n", rv);
136   else if (oflags & MFLAG)
137     printf ("%s\n", MACHTYPE);
138   else if (oflags & PFLAG)
139     printf ("%d\n", patch_level);
140   else if (oflags & SFLAG)
141     printf ("%s\n", shell_version_string ());
142   else if (oflags & LFLAG)
143     show_shell_version (0);
144   else if (oflags & XFLAG)
145     show_shell_version (1);
146
147   exit (0);
148 }