2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* Make the AFS_component_version_number.c file. Do it in C since there's no
11 * guarantee of perl on an NT platform.
17 #include <sys/types.h>
24 #define VERINFO_STRING_CHARS_MAX 950 /* max chars in verinfo string */
25 #define CFILE_STRING_CHARS_MAX 2000 /* max chars in C file string */
27 #define DEPTH_MAX 6 /* maximum depth of src dir search (>= 3) */
29 #define CMLDIR_DFLT "../../src/CML" /* default starting point for search */
30 #define CMLDIR_BUFSZ ((DEPTH_MAX * (sizeof("../") - 1)) + sizeof("src/CML"))
31 static char cmldir[CMLDIR_BUFSZ];
34 #define STAMPS "stamps"
36 #define CML_STRING "cml_version_number[]=\"@(#)"
37 #define CML_VER_DECL "char " CML_STRING
38 char *cml_string = CML_VER_DECL;
40 #define VERINFO_BUILD_STRING "#define AFS_VERINFO_BUILD "
42 #define AFS_STRING "char* AFSVersion = \""
43 #define VERS_FILE "AFS_component_version_number"
51 deltaNames stateDeltas[MAXDELTAS], stampDeltas[MAXDELTAS];
62 } cfgFormat = CF_DEFAULT;
66 void PrintStamps(void);
72 ("Usage: %s [-d directory] [-o output file name] [-c component] [-v | -t]\n",
74 printf("%s creates the AFS_component_version_number.c file.\n",
77 printf("-d directory - path of the CML directory, default is %s.\n",
79 printf("-o file name - alternate output file name.\n");
81 ("-c component - if not \"afs\" prefix for cml_version_number variable.\n");
82 printf("-v generate NT versioninfo style declarations.\n");
83 printf("-t generate text file style information.\n");
87 main(int argc, char **argv)
89 char stampsFile[1024];
95 char *outputFile = NULL;
96 char outputFileBuf[sizeof(VERS_FILE) + 2];
101 char *cml_prefix = NULL;
103 /* initialize cmldir buffer and set default starting directory */
104 for (i = 0; i < DEPTH_MAX; i++) {
105 strcat(cmldir, "../");
107 strcat(cmldir, "src/CML");
109 baseDir = strstr(cmldir, CMLDIR_DFLT);
111 programName = argv[0];
113 for (i = 1; i < argc; i++) {
114 if (!strcmp("-d", argv[i])) {
116 if (i >= argc || *argv[i] == '-') {
117 printf("Missing directory name for -d option.\n");
122 } else if (!strcmp("-o", argv[i])) {
124 if (i >= argc || *argv[i] == '-') {
125 printf("Missing output file name for -o option.\n");
128 outputFile = argv[i];
129 } else if (!strcmp("-c", argv[i])) {
131 if (i >= argc || *argv[i] == '-') {
132 printf("Missing component for -c option.\n");
135 cml_prefix = argv[i];
136 } else if (!strcmp("-v", argv[i])) {
137 if (cfgFormat == CF_DEFAULT || cfgFormat == CF_VERINFO) {
138 cfgFormat = CF_VERINFO;
140 printf("Specify only one alternative output format\n");
143 } else if (!strcmp("-t", argv[i])) {
144 if (cfgFormat == CF_DEFAULT || cfgFormat == CF_TEXT) {
147 printf("Specify only one alternative output format\n");
151 printf("%s: Unknown argument.\n", argv[i]);
156 /* set outputFile if not specified */
158 if (outputFile == NULL) {
159 strcpy(outputFileBuf, VERS_FILE);
160 if (cfgFormat == CF_VERINFO) {
161 strcat(outputFileBuf, ".h");
162 } else if (cfgFormat == CF_TEXT) {
163 strcat(outputFileBuf, ".txt");
165 strcat(outputFileBuf, ".c");
167 outputFile = outputFileBuf;
170 /* Determine if we need to create the output file. */
172 if ((code = stat(outputFile, &sbuf)) < 0) {
174 versTime = (time_t) 0; /* inidicates no output file. */
176 versTime = sbuf.st_mtime;
179 sprintf(stampsFile, "%s/%s", baseDir, STAMPS);
180 code = stat(stampsFile, &sbuf);
182 while (code < 0 && errno == ENOENT && !argDir && baseDir > cmldir) {
183 /* Try path at next level of depth. */
184 baseDir -= sizeof("../") - 1;
185 sprintf(stampsFile, "%s/%s", baseDir, STAMPS);
186 code = stat(stampsFile, &sbuf);
188 if (code == 0 && versTime <= sbuf.st_mtime) {
192 sprintf(stateFile, "%s/%s", baseDir, STATE);
195 code = stat(stateFile, &sbuf);
196 /* dont' check alternate base dir, since it would be reset above */
197 if (code == 0 && versTime <= sbuf.st_mtime)
202 printf("Not rebuilding %s since it is up to date.\n", outputFile);
208 malloc(strlen("char ") + strlen(cml_prefix) + strlen(CML_STRING) +
211 printf("No space to use prefix in cml string, ignoring it.\n");
212 cml_string = CML_VER_DECL;
214 (void)sprintf(cml_string, "%s%s%s", "char ", cml_prefix,
219 fpState = fopen(stateFile, "r");
220 fpStamps = fopen(stampsFile, "r");
221 fpVers = fopen(outputFile, "w");
223 if (fpStamps == NULL || fpState == NULL || fpVers == NULL) {
225 if (cfgFormat == CF_VERINFO) {
227 "%s \"No configuration information available\"\n",
228 VERINFO_BUILD_STRING);
229 } else if (cfgFormat == CF_TEXT) {
230 fprintf(fpVers, "No configuration information available.\n");
233 "%sNo configuration information available\";\n",
235 fprintf(fpVers, "%safs??\";\n", AFS_STRING);
239 fprintf(stderr, "Can't write version information to %s.\n",
243 "No configuration information available, continuing...\n");
249 while (fgets(s, sizeof(s), fpState)) {
250 if (*s == 'I' || *s == 'N' || *s == 'C' || *s == 'O') {
251 stateDeltas[nStates].type = *s;
252 s[strlen(s) - 1] = '\0';
253 (void)strcpy(stateDeltas[nStates].name, s + 2);
273 size_t outMax, outCount = 0;
275 if (cfgFormat == CF_VERINFO) {
276 outMax = VERINFO_STRING_CHARS_MAX;
277 } else if (cfgFormat == CF_TEXT) {
278 outMax = 0; /* signifies that there is no maximum */
280 outMax = CFILE_STRING_CHARS_MAX;
283 for (i = 0; i < nStates; i++) {
284 if (stateDeltas[i].type == 'C') {
285 if (cfgFormat == CF_VERINFO) {
286 fprintf(fpVers, "%s \"Base configuration %s",
287 VERINFO_BUILD_STRING, stateDeltas[i].name);
288 } else if (cfgFormat == CF_TEXT) {
289 fprintf(fpVers, "Base configuration %s\n",
290 stateDeltas[i].name);
292 fprintf(fpVers, "%sBase configuration %s", cml_string,
293 stateDeltas[i].name);
295 c = stateDeltas[i].name;
300 for (i = 0; i < nStates; i++) {
301 if (stateDeltas[i].type == 'I' || stateDeltas[i].type == 'N') {
302 outCount += strlen(stateDeltas[i].name) + 2;
303 if (outMax && outCount > outMax) {
306 if (cfgFormat == CF_TEXT) {
307 fprintf(fpVers, "%c%s\n", stateDeltas[i].type,
308 stateDeltas[i].name);
310 fprintf(fpVers, ";%c%s", stateDeltas[i].type,
311 stateDeltas[i].name);
316 for (i = 0; i < nStates; i++) {
317 if (stateDeltas[i].type == 'O') {
318 outCount += strlen(stateDeltas[i].name) + 2;
319 if (outMax && outCount > outMax) {
322 if (cfgFormat == CF_TEXT) {
323 fprintf(fpVers, "%c%s\n", stateDeltas[i].type,
324 stateDeltas[i].name);
326 fprintf(fpVers, ";%c%s", stateDeltas[i].type,
327 stateDeltas[i].name);
332 if (outMax && outCount > outMax) {
333 fprintf(fpVers, ";[LIST TRUNCATED]");
336 if (cfgFormat == CF_VERINFO) {
337 fprintf(fpVers, "\"\n");
338 } else if (cfgFormat == CF_DEFAULT) {
339 fprintf(fpVers, "\";\n");
343 s = (char *)strchr(c, ' ');
346 fprintf(fpVers, "%s%s\";\n", AFS_STRING, c ? c : "Unknown");