"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.4/src/version.c" (20 Nov 2019, 5032 Bytes) of package /linux/misc/tin-2.4.4.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "version.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.4.3_vs_2.4.4.

    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-2020 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  */
   64 struct t_version *
   65 check_upgrade(
   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 
  120 void
  121 upgrade_prompt_quit(
  122     struct t_version *upgrade,
  123     const char *file)
  124 {
  125     switch (upgrade->state) {
  126         case RC_UPGRADE:
  127             error_message(2, _(txt_warn_update), VERSION, file);
  128             break;
  129 
  130         case RC_DOWNGRADE:
  131             error_message(2, _(txt_warn_downgrade), VERSION, file);
  132             break;
  133 
  134         case RC_ERROR: /* can't parse internal version string, should not happen */
  135             error_message(2, txt_warn_unrecognized_version);
  136             free(upgrade);
  137             free(tin_progname);
  138             giveup();
  139             /* NOTREACHED */
  140             break;
  141 
  142         default:    /* should no happen */
  143             return;
  144     }
  145 
  146     error_message(2, _(txt_return_key));
  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 }