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");
88 main(int argc, char **argv)
90 char stampsFile[1024];
96 char *outputFile = NULL;
97 char outputFileBuf[sizeof(VERS_FILE) + 2];
102 char *cml_prefix = NULL;
104 /* initialize cmldir buffer and set default starting directory */
105 for (i = 0; i < DEPTH_MAX; i++) {
106 strcat(cmldir, "../");
108 strcat(cmldir, "src/CML");
110 baseDir = strstr(cmldir, CMLDIR_DFLT);
112 programName = argv[0];
114 for (i = 1; i < argc; i++) {
115 if (!strcmp("-d", argv[i])) {
117 if (i >= argc || *argv[i] == '-') {
118 printf("Missing directory name for -d option.\n");
123 } else if (!strcmp("-o", argv[i])) {
125 if (i >= argc || *argv[i] == '-') {
126 printf("Missing output file name for -o option.\n");
129 outputFile = argv[i];
130 } else if (!strcmp("-c", argv[i])) {
132 if (i >= argc || *argv[i] == '-') {
133 printf("Missing component for -c option.\n");
136 cml_prefix = argv[i];
137 } else if (!strcmp("-v", argv[i])) {
138 if (cfgFormat == CF_DEFAULT || cfgFormat == CF_VERINFO) {
139 cfgFormat = CF_VERINFO;
141 printf("Specify only one alternative output format\n");
144 } else if (!strcmp("-t", argv[i])) {
145 if (cfgFormat == CF_DEFAULT || cfgFormat == CF_TEXT) {
148 printf("Specify only one alternative output format\n");
152 printf("%s: Unknown argument.\n", argv[i]);
157 /* set outputFile if not specified */
159 if (outputFile == NULL) {
160 strcpy(outputFileBuf, VERS_FILE);
161 if (cfgFormat == CF_VERINFO) {
162 strcat(outputFileBuf, ".h");
163 } else if (cfgFormat == CF_TEXT) {
164 strcat(outputFileBuf, ".txt");
166 strcat(outputFileBuf, ".c");
168 outputFile = outputFileBuf;
171 /* Determine if we need to create the output file. */
173 if ((code = stat(outputFile, &sbuf)) < 0) {
175 versTime = (time_t) 0; /* inidicates no output file. */
177 versTime = sbuf.st_mtime;
180 sprintf(stampsFile, "%s/%s", baseDir, STAMPS);
181 code = stat(stampsFile, &sbuf);
183 while (code < 0 && errno == ENOENT && !argDir && baseDir > cmldir) {
184 /* Try path at next level of depth. */
185 baseDir -= sizeof("../") - 1;
186 sprintf(stampsFile, "%s/%s", baseDir, STAMPS);
187 code = stat(stampsFile, &sbuf);
189 if (code == 0 && versTime <= sbuf.st_mtime) {
193 sprintf(stateFile, "%s/%s", baseDir, STATE);
196 code = stat(stateFile, &sbuf);
197 /* dont' check alternate base dir, since it would be reset above */
198 if (code == 0 && versTime <= sbuf.st_mtime)
203 printf("Not rebuilding %s since it is up to date.\n", outputFile);
209 (char *)malloc(strlen("char ") + strlen(cml_prefix) + strlen(CML_STRING) +
212 printf("No space to use prefix in cml string, ignoring it.\n");
213 cml_string = CML_VER_DECL;
215 (void)sprintf(cml_string, "%s%s%s", "char ", cml_prefix,
220 fpState = fopen(stateFile, "r");
221 fpStamps = fopen(stampsFile, "r");
222 fpVers = fopen(outputFile, "w");
224 if (fpStamps == NULL || fpState == NULL || fpVers == NULL) {
226 if (cfgFormat == CF_VERINFO) {
228 "%s \"No configuration information available\"\n",
229 VERINFO_BUILD_STRING);
230 } else if (cfgFormat == CF_TEXT) {
231 fprintf(fpVers, "No configuration information available.\n");
234 "%sNo configuration information available\";\n",
236 fprintf(fpVers, "%safs??\";\n", AFS_STRING);
240 fprintf(stderr, "Can't write version information to %s.\n",
244 "No configuration information available, continuing...\n");
250 while (fgets(s, sizeof(s), fpState)) {
251 if (*s == 'I' || *s == 'N' || *s == 'C' || *s == 'O') {
252 stateDeltas[nStates].type = *s;
253 s[strlen(s) - 1] = '\0';
254 (void)strcpy(stateDeltas[nStates].name, s + 2);
274 size_t outMax, outCount = 0;
276 if (cfgFormat == CF_VERINFO) {
277 outMax = VERINFO_STRING_CHARS_MAX;
278 } else if (cfgFormat == CF_TEXT) {
279 outMax = 0; /* signifies that there is no maximum */
281 outMax = CFILE_STRING_CHARS_MAX;
284 for (i = 0; i < nStates; i++) {
285 if (stateDeltas[i].type == 'C') {
286 if (cfgFormat == CF_VERINFO) {
287 fprintf(fpVers, "%s \"Base configuration %s",
288 VERINFO_BUILD_STRING, stateDeltas[i].name);
289 } else if (cfgFormat == CF_TEXT) {
290 fprintf(fpVers, "Base configuration %s\n",
291 stateDeltas[i].name);
293 fprintf(fpVers, "%sBase configuration %s", cml_string,
294 stateDeltas[i].name);
296 c = stateDeltas[i].name;
301 for (i = 0; i < nStates; i++) {
302 if (stateDeltas[i].type == 'I' || stateDeltas[i].type == 'N') {
303 outCount += strlen(stateDeltas[i].name) + 2;
304 if (outMax && outCount > outMax) {
307 if (cfgFormat == CF_TEXT) {
308 fprintf(fpVers, "%c%s\n", stateDeltas[i].type,
309 stateDeltas[i].name);
311 fprintf(fpVers, ";%c%s", stateDeltas[i].type,
312 stateDeltas[i].name);
317 for (i = 0; i < nStates; i++) {
318 if (stateDeltas[i].type == 'O') {
319 outCount += strlen(stateDeltas[i].name) + 2;
320 if (outMax && outCount > outMax) {
323 if (cfgFormat == CF_TEXT) {
324 fprintf(fpVers, "%c%s\n", stateDeltas[i].type,
325 stateDeltas[i].name);
327 fprintf(fpVers, ";%c%s", stateDeltas[i].type,
328 stateDeltas[i].name);
333 if (outMax && outCount > outMax) {
334 fprintf(fpVers, ";[LIST TRUNCATED]");
337 if (cfgFormat == CF_VERINFO) {
338 fprintf(fpVers, "\"\n");
339 } else if (cfgFormat == CF_DEFAULT) {
340 fprintf(fpVers, "\";\n");
344 s = (char *)strchr(c, ' ');
347 fprintf(fpVers, "%s%s\";\n", AFS_STRING, c ? c : "Unknown");