Back to index

lightning-sunbird  0.9+nobinonly
dirver.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*--------------------------------------------------------------------------
00039 /                                                                           
00040 /   Name: Netscape File Version Generator                                   
00041 /   Platforms: WIN32                                                        
00042 /   ......................................................................  
00043 /   This program generates an ascii format of the 64-bit FILEVERSION        
00044 /   resource identifier used by Windows executable binaries.                
00045 /                                                                           
00046 /   Usage Syntax:                                                           
00047 /   fversion <major.minor.patch> [mm/dd/yyyy] [outfile]                     
00048 /   If date is not specified, the current GMT date is used.  yyyy must be   
00049 /   greater than 1980                                                       
00050 /                                                                           
00051 /   Usage Example:                                                          
00052 /   fversion 3.0.0                                                          
00053 /   fversion 6.5.4 1/30/2001                                                
00054 /   fversion 6.5.4 1/30/2001 fileversion.h                                  
00055 /                                                                           
00056 /   see http://ntsbuild/sd/30ver.htm for specification                      
00057 /   ......................................................................  
00058 /   Revision History:                                                       
00059 /   01-30-97  Initial Version, Andy Hakim (ahakim@netscape.com)             
00060 / --------------------------------------------------------------------------*/
00061 #ifdef _WIN32
00062 #include <windows.h>
00063 #endif
00064 #ifdef macintosh
00065 #include <console.h>
00066 #endif
00067 #include <stdio.h>
00068 #include <string.h>
00069 #include <stdlib.h>
00070 #include <time.h>
00071 
00072 unsigned _CalcVersion(unsigned nMajor, unsigned nMinor, unsigned nPatch)
00073 {
00074     unsigned nVersion;
00075     nVersion = nMajor;
00076     nVersion <<= 5;
00077     nVersion += nMinor;
00078     nVersion <<= 7;
00079     nVersion += nPatch;
00080     nVersion &= 0xFFFF;
00081     return(nVersion);
00082 }
00083 
00084 
00085 static void _GetVersions(char *szVer, unsigned *nMajor, unsigned *nMinor,
00086                                            unsigned *nPatch)
00087 {
00088     char szVersion[128];
00089     unsigned nReturn = 0;
00090     char *szToken;
00091     *nMajor = 0;
00092     *nMinor = 0;
00093     *nPatch = 0;
00094 
00095     strcpy(szVersion, szVer);
00096     if(szToken = strtok(szVersion, ".\n"))
00097     {
00098         *nMajor = atoi(szToken);
00099         if(szToken = strtok(NULL, ".\n"))
00100         {
00101             *nMinor = atoi(szToken);
00102             if(szToken = strtok(NULL, ".\n"))
00103             {
00104                 *nPatch = atoi(szToken);
00105             }
00106         }
00107     }
00108 }
00109 
00110 
00111 
00112 unsigned _CalcBuildDate(unsigned nYear, unsigned nMonth, unsigned nDay)
00113 {
00114     unsigned nBuildDate = 0;
00115 
00116     if(nYear < 1900) /* they really mean 1900 + nYear */
00117         nYear += 1900;
00118 
00119     nYear -= 1980;
00120     nBuildDate = nYear;
00121     /*
00122     nBuildDate <<= 5;
00123     */
00124     nBuildDate <<= 4;
00125     nBuildDate += nMonth;
00126     /* nBuildDate <<= 4; */
00127     nBuildDate <<= 5; 
00128     nBuildDate += nDay;
00129     nBuildDate &= 0xFFFF;
00130     return(nBuildDate);
00131 }
00132 
00133 
00134 
00135 unsigned _GenBuildDate(char *szBuildDate)
00136 {
00137     unsigned nReturn = 0;
00138     char *szToken;
00139     unsigned nYear = 0;
00140     unsigned nMonth = 0;
00141     unsigned nDay = 0;
00142 
00143     if((szBuildDate) && (strchr(szBuildDate, '\\') || strchr(szBuildDate, '/')) && (szToken = strtok(szBuildDate, "\\/")))
00144     {
00145         nMonth = atoi(szToken);
00146        nMonth--;     /* use months in the range [0..11], as in struct tm */
00147         if(szToken = strtok(NULL, "\\/"))
00148         {
00149             nDay = atoi(szToken);
00150             if(szToken = strtok(NULL, "\\/"))
00151             {
00152                 nYear = atoi(szToken);
00153               if(nYear < 70) { /* handle 2 digit years like (20)00 */ 
00154                   nYear += 100;
00155               }
00156               else if (nYear < 100) {
00157               }
00158               else if (nYear > 1900){
00159                   nYear -= 1900;
00160               }
00161             }
00162         }
00163     }
00164     else
00165     {
00166               struct tm *newtime;
00167               time_t ltime;
00168 
00169               time( &ltime );
00170 
00171         /* Obtain coordinated universal time: */
00172               newtime = gmtime( &ltime );
00173         nYear = newtime->tm_year;
00174         nMonth = newtime->tm_mon;
00175         nDay = newtime->tm_mday;
00176     }
00177 
00178     nReturn = _CalcBuildDate(nYear, nMonth, nDay);
00179     return(nReturn);
00180 }
00181 
00182 
00183 
00184 static void ShowHelp(char *szFilename)
00185 {
00186     fprintf(stdout, "%s: Generates ascii format #define for FILEVERSION\n", szFilename);
00187     fprintf(stdout, "   resource identifier used by Windows executable binaries.\n");
00188     fprintf(stdout, "\n");
00189     fprintf(stdout, "Usage: %s <major.minor.patch> [mm/dd/yy] [outfile]\n", szFilename);
00190     fprintf(stdout, "\n");
00191     fprintf(stdout, "Examples:\n");
00192     fprintf(stdout, "%s 3.0.0\n", szFilename);
00193     fprintf(stdout, "%s 6.5.2 1/30/2001\n", szFilename);
00194     fprintf(stdout, "%s 6.5.2 1/30/2001 fileversion.h\n", szFilename);
00195 }
00196 
00197 
00198 
00199 main(int nArgc, char **lpArgv)
00200 {
00201     int nReturn = 0;
00202     unsigned nVersion = 0;
00203     unsigned nBuildDate = 0;
00204 
00205 #ifdef macintosh
00206        nArgc = ccommand( &lpArgv );
00207 #endif
00208 
00209     if(nArgc < 2)
00210     {
00211         ShowHelp(lpArgv[0]);
00212         nReturn = 1;
00213     }
00214     else
00215     {
00216         char *szVersion = NULL;
00217         char *szDate = NULL;
00218         char *szOutput = NULL;
00219         FILE *f = stdout;
00220               unsigned nMajor = 0;
00221               unsigned nMinor = 0;
00222               unsigned nPatch = 0;
00223 
00224         szVersion = (char *)lpArgv[1];
00225         szDate = (char *)lpArgv[2];
00226         szOutput = (char *)lpArgv[3];
00227               _GetVersions( szVersion, &nMajor, &nMinor, &nPatch );
00228               nVersion = _CalcVersion(nMajor, nMinor, nPatch);
00229         nBuildDate = _GenBuildDate(szDate);
00230 
00231         if(nArgc >= 4) {
00232             if (( f = fopen(szOutput, "w")) == NULL ) {
00233               perror( szOutput );
00234               exit( 1 );
00235            }
00236        }
00237 
00238         fprintf(f, "#define VI_PRODUCTVERSION %u.%u\n", nMajor, nMinor);
00239         fprintf(f, "#define PRODUCTTEXT \"%s\"\n", szVersion );
00240         fprintf(f, "#define VI_FILEVERSION %u, 0, 0,%u\n",
00241                             nVersion, nBuildDate);
00242         fprintf(f, "#define VI_FileVersion \"%s Build %u\\0\"\n",
00243                             szVersion, nBuildDate);
00244 
00245         if(nArgc >= 4)
00246             fclose(f);
00247         nReturn = (nVersion && !nBuildDate);
00248     }
00249     return(nReturn);
00250 }
00251