abf1aa8dfcf8514413073fdab3758b5197baa090
[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 it under
8    the terms of the GNU General Public License as published by the Free
9    Software Foundation; either version 2, or (at your option) any later
10    version.
11
12    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13    WARRANTY; without even the implied warranty of MERCHANTABILITY or
14    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15    for more details.
16
17    You should have received a copy of the GNU General Public License along
18    with Bash; see the file COPYING.  If not, write to the Free Software
19    Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
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 char *shell_name = "bash";
51 char *progname;
52
53 static void
54 usage()
55 {
56   fprintf(stderr, "%s: usage: %s [-hrvpmlsx]\n", progname, progname);
57 }
58
59 int
60 main (argc, argv)
61      int argc;
62      char **argv;
63 {
64   int opt, oflags;
65   char dv[128], *rv;
66
67   if (progname = strrchr (argv[0], '/'))
68     progname++;
69   else
70     progname = argv[0];
71
72   oflags = 0;
73   while ((opt = getopt(argc, argv, "hrvmpslx")) != EOF)
74     {
75       switch (opt)
76         {
77         case 'h':
78           usage ();
79           exit (0);
80         case 'r':
81           oflags |= RFLAG;      /* release */
82           break;
83         case 'v':
84           oflags |= VFLAG;      /* version */
85           break;
86         case 'm':
87           oflags |= MFLAG;      /* machtype */
88           break;
89         case 'p':
90           oflags |= PFLAG;      /* patchlevel */
91           break;
92         case 's':               /* short version string */
93           oflags |= SFLAG;
94           break;
95         case 'l':               /* long version string */
96           oflags |= LFLAG;
97           break;
98         case 'x':               /* extended version information */
99           oflags |= XFLAG;
100           break;
101         default:
102           usage ();
103           exit (2);
104         }
105     }
106
107   argc -= optind;
108   argv += optind;
109
110   if (argc > 0)
111     {
112       usage ();
113       exit (2);
114     }
115
116   /* default behavior */  
117   if (oflags == 0)
118     oflags = SFLAG;
119
120   if (oflags & (RFLAG|VFLAG))
121     {
122       strcpy (dv, dist_version);
123       rv = strchr (dv, '.');
124       if (rv)
125         *rv++ = '\0';
126       else
127         rv = "00";
128     }
129   if (oflags & RFLAG)
130     printf ("%s\n", dv);
131   else if (oflags & VFLAG)
132     printf ("%s\n", rv);
133   else if (oflags & MFLAG)
134     printf ("%s\n", MACHTYPE);
135   else if (oflags & PFLAG)
136     printf ("%d\n", patch_level);
137   else if (oflags & SFLAG)
138     printf ("%s\n", shell_version_string ());
139   else if (oflags & LFLAG)
140     show_shell_version (0);
141   else if (oflags & XFLAG)
142     show_shell_version (1);
143
144   exit (0);
145 }