source: trunk/packs/update/upvers.c @ 11159

Revision 11159, 3.0 KB checked in by cfields, 26 years ago (diff)
Make sure that none of the dotted triples have trailing characters.
Line 
1/* Copyright 1996 by the Massachusetts Institute of Technology.
2 *
3 * Permission to use, copy, modify, and distribute this
4 * software and its documentation for any purpose and without
5 * fee is hereby granted, provided that the above copyright
6 * notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting
8 * documentation, and that the name of M.I.T. not be used in
9 * advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.
11 * M.I.T. makes no representations about the suitability of
12 * this software for any purpose.  It is provided "as is"
13 * without express or implied warranty.
14 */
15
16/* This is the source for the upvers program, which runs the
17 * appropriate scripts during the update process.
18 */
19
20static char rcsid[] = "$Id: upvers.c,v 1.14 1998-02-24 22:32:17 cfields Exp $";
21
22#include <sys/types.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <dirent.h>
27
28struct verfile {
29  int major;
30  int minor;
31  int patch;
32};
33
34static int version_compare(const void *arg1, const void *arg2);
35
36int main(int argc, char **argv)
37{
38  struct verfile vf[1024], oldv, newv;
39  DIR *dp;
40  struct dirent *dirp;
41  int n = 0, i, start, end;
42  char filename[1024], scratch;
43
44  if (argc < 4)
45    {
46      fprintf(stderr, "Usage: upvers <old-vers> <new-vers> <libdir>\n");
47      return 1;
48    }
49
50  /* Parse old-vers and new-vers. */
51  if (sscanf(argv[1], "%d.%d.%d%c", &oldv.major, &oldv.minor, &oldv.patch,
52             &scratch) != 3
53      || sscanf(argv[2], "%d.%d.%d%c", &newv.major, &newv.minor,
54                &newv.patch, &scratch) != 3)
55    {
56      fprintf(stderr, "First two arguments must be dotted triplets.\n");
57      return 1;
58    }
59
60  /* Get the names of all the version files in libdir. */
61  dp = opendir(argv[3]);
62  if (!dp)
63    {
64      perror("opendir");
65      return 1;
66    }
67  while (dirp = readdir(dp))
68    {
69      if (sscanf(dirp->d_name, "%d.%d.%d%c", &vf[n].major, &vf[n].minor,
70                 &vf[n].patch, &scratch) == 3)
71        n++;
72    }
73  closedir(dp);
74
75  /* Sort the version files names and decide where to start and end at. */
76  qsort(vf, n, sizeof(struct verfile), version_compare);
77  start = n + 1;
78  end = n;
79  for (i = 0; i < n; i++)
80    {
81      if (version_compare(&vf[i], &oldv) > 0 && start == n + 1)
82        start = i;
83      if (version_compare(&vf[i], &newv) > 0 && end == n)
84        end = i;
85    }
86
87  /* Now run the appropriate files. */
88  for (i = start; i < end; i++)
89    {
90      sprintf(filename, "%s/%d.%d.%d", argv[3], vf[i].major, vf[i].minor,
91              vf[i].patch);
92      printf("Running %s\n", filename);
93      system(filename);
94    }
95
96  return 0;
97}
98               
99static int version_compare(const void *arg1, const void *arg2)
100{
101  const struct verfile *v1 = (const struct verfile *) arg1;
102  const struct verfile *v2 = (const struct verfile *) arg2;
103
104  /* Compare major version, then minor version, then patchlevel. */
105  return (v1->major != v2->major) ? v1->major - v2->major
106    : (v1->minor != v2->minor) ? v1->minor - v2->minor
107    : v1->patch - v2->patch;
108}
Note: See TracBrowser for help on using the repository browser.