tin  2.6.1
About: TIN is a threaded NNTP and spool based UseNet newsreader.
  Fossies Dox: tin-2.6.1.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

version.c
Go to the documentation of this file.
1/*
2 * Project : tin - a Usenet reader
3 * Module : version.c
4 * Author : U. Janssen
5 * Created : 2003-05-11
6 * Updated : 2019-02-04
7 * Notes :
8 *
9 * Copyright (c) 2003-2022 Urs Janssen <urs@tin.org>
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * 3. Neither the name of the copyright holder nor the names of its
24 * contributors may be used to endorse or promote products derived from
25 * this software without specific prior written permission.
26 *
27 * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40
41#ifndef TIN_H
42# include "tin.h"
43#endif /* !TIN_H */
44
45
46/*
47 * line is the entire line we should check
48 * skip must be the leading portion of the version line not including the
49 * version number (which must be a dotted triple)
50 * version is the version number (dotted triple) we expect to match
51 *
52 * returns struct t_version*
53 * ->state
54 * RC_IGNORE 1st args dotted triple matches 3rd arg
55 * RC_UPGRADE 1st args dotted triple is older than 3rd arg
56 * RC_DOWNGRADE 1st args dotted triple is newer than 3rd arg
57 * RC_ERROR 3rd arg is not a dotted triple (usage error)
58 * ->file_version version number in file as int
59 * (rc_majorv * 10000 + rc_minorv * 100 + rc_subv) or -1
60 *
61 * Don't make the arguments to sscanf() consts, as some old systems require
62 * them to writable (but do not change them)
63 */
64struct t_version *
66 char *line,
67 const char *skip,
68 const char *version)
69{
70 char *format;
71 char *lskip = my_strdup(skip);
72 char *lversion = my_strdup(version);
73 char fmt[10];
74 int rc_majorv, rc_minorv, rc_subv; /* version numbers in the file */
75 int current_version, c_majorv, c_minorv, c_subv; /* version numbers we require */
76 size_t len;
77 struct t_version *fversion = my_malloc(sizeof(struct t_version));
78
79 fversion->state = RC_ERROR;
80 fversion->file_version = -1;
81
82 rc_majorv = rc_minorv = rc_subv = c_majorv = c_minorv = c_subv = -1;
83 strcpy(fmt, "%d.%d.%d"); /* we are expecting dotted triples */
84 len = strlen(lskip) + strlen(fmt) + 1; /* format buffer len */
85 format = my_malloc(len + 1);
86 snprintf(format, len, "%s%s", lskip, fmt);
87 free(lskip);
88
89 if (sscanf(line, format, &rc_majorv, &rc_minorv, &rc_subv) != 3) {
90 free(format);
91 free(lversion);
92 return fversion;
93 }
94 free(format);
95
96 fversion->file_version = rc_majorv * 10000 + rc_minorv * 100 + rc_subv;
97
98 /* we can't parse our own version number - should never happen */
99 if (sscanf(lversion, fmt, &c_majorv, &c_minorv, &c_subv) != 3) {
100 free(lversion);
101 return fversion;
102 }
103 free(lversion);
104
105 current_version = c_majorv * 10000 + c_minorv * 100 + c_subv;
106
107 if (fversion->file_version == current_version)
108 fversion->state = RC_IGNORE;
109
110 if (fversion->file_version > current_version)
111 fversion->state = RC_DOWNGRADE;
112
113 if (fversion->file_version < current_version)
114 fversion->state = RC_UPGRADE;
115
116 return fversion;
117}
118
119
120void
122 struct t_version *upgrade,
123 const char *file)
124{
125 switch (upgrade->state) {
126 case RC_UPGRADE:
128 break;
129
130 case RC_DOWNGRADE:
132 break;
133
134 case RC_ERROR: /* can't parse internal version string, should not happen */
136 free(upgrade);
137 free(tin_progname);
138 giveup();
139 /* NOTREACHED */
140 break;
141
142 default: /* should no happen */
143 return;
144 }
145
147
148 /*
149 * TODO: document, use something unbuffered here
150 * NOTE: these keys can not be remapped
151 */
152 switch (getchar()) {
153 case 'q':
154 case 'Q':
155 case ESC:
156 free(upgrade);
157 free(tin_progname);
158 giveup();
159 /* NOTREACHED */
160 break;
161
162 default:
163 break;
164 }
165}
constext txt_warn_unrecognized_version[]
Definition: lang.c:984
constext txt_warn_downgrade[]
Definition: lang.c:982
constext txt_warn_update[]
Definition: lang.c:980
char * tin_progname
Definition: init.c:105
constext txt_return_key[]
Definition: lang.c:799
#define ESC
Definition: keymap.h:140
void error_message(unsigned int sdelay, const char *fmt,...)
Definition: screen.c:224
char * my_strdup(const char *str)
Definition: string.c:139
_Noreturn void giveup(void)
Definition: main.c:1058
int file_version
Definition: tin.h:2543
enum rc_state state
Definition: tin.h:2542
#define my_malloc(size)
Definition: tin.h:2245
@ RC_IGNORE
Definition: tin.h:110
@ RC_DOWNGRADE
Definition: tin.h:110
@ RC_ERROR
Definition: tin.h:110
@ RC_UPGRADE
Definition: tin.h:110
#define _(Text)
Definition: tin.h:94
#define snprintf
Definition: tin.h:2464
struct t_version * check_upgrade(char *line, const char *skip, const char *version)
Definition: version.c:65
void upgrade_prompt_quit(struct t_version *upgrade, const char *file)
Definition: version.c:121
#define VERSION
Definition: version.h:46