Back to index

lshw  02.16
version.cc
Go to the documentation of this file.
00001 /*
00002  * version.cc
00003  *
00004  * The sole purpose of this module is to report package version information:
00005  * keyword expansion should be allowed for the 'URL' tag (svn ps svn:keywords URL)
00006  * Reported version is computed using this file's location in the source archive;
00007  * development versions return 'development' whereas released versions return
00008  * version numbers like A.01.04
00009  *
00010  */
00011 #include "version.h"
00012 #include "config.h"
00013 #include <stdlib.h>
00014 #include <string.h>
00015 #include <ctype.h>
00016 #include <netinet/in.h>
00017 #include <arpa/nameser.h>
00018 #include <resolv.h>
00019 #include <sys/types.h>
00020 
00021 #ifndef PACKETSZ
00022 #define PACKETSZ 512
00023 #endif
00024 
00025 
00026 static char versiontag[] = "@(#) $URL: https://ezix.org/source/packages/lshw/releases/B.02.16/src/core/version.cc $ >";
00027 
00028 const char *getpackageversion()
00029 {
00030   static char * result = NULL;
00031   char * lastslash = NULL;
00032 
00033   if(result)
00034     return result;
00035 
00036   lastslash = strrchr(versiontag, '/');
00037   if(lastslash)
00038     *lastslash = '\0';      // cut the basename off
00039 
00040   lastslash = strrchr(versiontag, '/');
00041   if(lastslash)
00042     *lastslash = '\0';  // cut the basename off
00043 
00044   lastslash = strrchr(versiontag, '/');
00045   if(lastslash)
00046     *lastslash = '\0';  // cut the basename off
00047 
00048   lastslash = strrchr(versiontag, '/');
00049   if(lastslash)
00050     result = lastslash+1;
00051   else
00052     return "unknown";
00053 
00054   return result;
00055 }
00056 
00057 static char *txtquery(const char *name, const char *domain, unsigned int *ttl)
00058 {
00059   unsigned char answer[PACKETSZ], *pt;
00060   char host[128], *txt;
00061   int len, exp, cttl, size, txtlen, type;
00062 
00063   if(res_init() < 0)
00064     return NULL;
00065 
00066   memset(answer, 0, PACKETSZ);
00067   if((len = res_querydomain(name, domain, C_IN, T_TXT, answer, PACKETSZ)) < 0)
00068     return NULL;
00069 
00070   pt = answer + sizeof(HEADER);
00071 
00072   if((exp = dn_expand(answer, answer + len, pt, host, sizeof(host))) < 0)
00073     return NULL;
00074 
00075   pt += exp;
00076 
00077   GETSHORT(type, pt);
00078   if(type != T_TXT)
00079     return NULL;
00080 
00081   pt += INT16SZ; /* class */
00082 
00083   if((exp = dn_expand(answer, answer + len, pt, host, sizeof(host))) < 0)
00084     return NULL;
00085 
00086   pt += exp;
00087   GETSHORT(type, pt);
00088   if(type != T_TXT)
00089     return NULL;
00090 
00091   pt += INT16SZ; /* class */
00092   GETLONG(cttl, pt);
00093   if(ttl)
00094     *ttl = cttl;
00095   GETSHORT(size, pt);
00096   txtlen = *pt;
00097 
00098   if(txtlen >= size || !txtlen)
00099     return NULL;
00100 
00101   if(!(txt = (char*)malloc(txtlen + 1)))
00102     return NULL;
00103 
00104   pt++;
00105   strncpy(txt, (char*)pt, txtlen);
00106   txt[txtlen] = 0;
00107 
00108   return txt;
00109 }
00110 
00111 const char * checkupdates()
00112 {
00113   static char *latest = NULL;
00114 
00115   if(!latest)
00116     latest = txtquery(PACKAGE, "ezix.org", NULL);
00117 
00118   return latest;
00119 }