xref: /haiku/src/bin/version.cpp (revision 3cb015b1ee509d69c643506e8ff573808c86dcfc)
1 // Author: Ryan Fleet
2 // Created: 9th October 2002
3 // Modified: 14th October 2002
4 
5 #include <cstdio>
6 #include <cstring>
7 #include <AppFileInfo.h>
8 
9 
10 void help(void)
11 {
12 	printf("usage: version [OPTION] FILENAME [FILENAME2, ...]\n");
13 	printf("Returns the version of a file.\n\n");
14 	printf("        -h, --help              this usage message\n");
15 	printf("        -l, --long              print long version information of FILENAME\n");
16 	printf("        -n, --numerical         print in numerical mode\n");
17 	printf("                                (Major miDdle miNor Variety Internal)\n");
18 	printf("        -s, --system            print system version instead of app version\n");
19 	printf("        --version               print version information for this command\n");
20 }
21 
22 
23 int getversion(const char *filename, version_kind kind, bool bLongFlag, bool bNumericalFlag)
24 {
25 	BFile file(filename, O_RDONLY);
26 	if(file.InitCheck() != B_OK)
27 	{
28 		printf("Couldn't get file info!\n");
29 		return 1;
30 	}
31 
32 	BAppFileInfo info(&file);
33 	if(info.InitCheck() != B_OK)
34 	{
35 		printf("Couldn't get file info!\n");
36 		return 1;
37 	}
38 
39 	version_info version;
40 	if(info.GetVersionInfo(&version, kind) != B_OK)
41 	{
42 		printf("Version unknown!\n");
43 		return 1;
44 	}
45 
46 	if(true == bLongFlag)
47 	{
48 		printf("%s\n", version.long_info);
49 		return 0;
50 	}
51 
52 	if(true == bNumericalFlag)
53 	{
54 		printf("%lu ", version.major);
55 		printf("%lu ", version.middle);
56 		printf("%lu ", version.minor);
57 		switch(version.variety)
58 		{
59 		case 0: printf("d "); break;
60 		case 1: printf("a "); break;
61 		case 2: printf("b "); break;
62 		case 3: printf("g "); break;
63 		case 4: printf("gm "); break;
64 		case 5: printf("f "); break;
65 		};
66 		printf("%lu\n", version.internal);
67 		return 0;
68 	}
69 
70 	printf("%s\n", version.short_info);
71 
72 	return 0;
73 }
74 
75 
76 /*
77 	strLessEqual(string1, string2)
78 	determines whether string1 contains at least one or more of the characters
79 	of string2 but none of which string2 doesn't contain.
80 
81 	true == ("hel" == "help"); true == ("help" == "help"); true == ("h" == "help");
82 	false == ("held" == "help"); false == ("helped" == "help");
83 */
84 bool strLessEqual(const char *str1, const char *str2)
85 {
86 	char *ptr1 = const_cast<char*>(str1);
87 	char *ptr2 = const_cast<char*>(str2);
88 
89 	while(*ptr1 != '\0')
90 	{
91 		if(*ptr1 != *ptr2)
92 			return false;
93 		++ptr1;
94 		++ptr2;
95 	}
96 	return true;
97 }
98 
99 
100 int main(int argc, char *argv[])
101 {
102 	version_kind kind = B_APP_VERSION_KIND;
103 	bool bLongFlag = false;
104 	bool bNumericalFlag = false;
105 	int i;
106 
107 	if(argc < 2)
108 		return 0;
109 
110 	for(i = 1; i < argc; ++i)
111 	{
112 		if(strncmp(argv[i], "-", 1) == 0)
113 		{
114 			char *ptr = argv[i];
115 			++ptr;
116 
117 			if(*ptr == '-')
118 			{
119 				bool lequal = false;
120 				++ptr;
121 
122 				if(*ptr == 'h')
123 				{
124 					lequal = strLessEqual(ptr, "help");
125 					if(lequal == true)
126 					{
127 						help();
128 						return 0;
129 					}
130 				}
131 
132 				else if(*ptr == 'l')
133 				{
134 					lequal = strLessEqual(ptr, "long");
135 					if(lequal == true)
136 						bLongFlag = true;
137 				}
138 
139 				else if(*ptr == 'n')
140 				{
141 					lequal = strLessEqual(ptr, "numerical");
142 					if(lequal == true)
143 						bNumericalFlag = true;
144 				}
145 
146 				else if(*ptr == 's')
147 				{
148 					lequal = strLessEqual(ptr, "system");
149 					if(lequal == true)
150 						kind = B_SYSTEM_VERSION_KIND;
151 				}
152 
153 				else if(*ptr == 'v')
154 				{
155 					lequal = strLessEqual(ptr, "version");
156 					if(lequal == true)
157 					{
158 						getversion(argv[0], B_APP_VERSION_KIND, false, false);
159 						return 0;
160 					}
161 				}
162 
163 				if(lequal == false)
164 					printf("%s unrecognized option `%s'\n", argv[0], argv[i]);
165 			}
166 
167 			else while(*ptr != '\0')
168 			{
169 				if(*ptr == 'h')
170 				{
171 					help();
172 					return 0;
173 				}
174 				else if(*ptr == 's')
175 					kind = B_SYSTEM_VERSION_KIND;
176 				else if(*ptr == 'l')
177 					bLongFlag = true;
178 				else if(*ptr == 'n')
179 					bNumericalFlag = true;
180 				else
181 					printf("%s: invalid option -- %c\n", argv[0], *ptr);
182 
183 				++ptr;
184 			}
185 		}
186 	}
187 
188 	int status = 0;
189 	int retval = 0;
190 	for(i = 1; i < argc; ++i)
191 	{
192 		if(strncmp(argv[i], "-", 1) != 0)
193 		{
194 			status = getversion(argv[i], kind, bLongFlag, bNumericalFlag);
195 			if(status != 0)
196 				retval = 1;
197 		}
198 	}
199 
200 	return retval;
201 }
202