procps/proc/version.c

84 lines
2.7 KiB
C
Raw Normal View History

/*
* Suite version information for procps-ng utilities
2002-02-02 04:17:29 +05:30
* Copyright (c) 1995 Martin Schulze <joey@infodrom.north.de>
* Amended by cblake to only export the function symbol.
*
2003-02-09 12:57:16 +05:30
* Modified by Albert Cahalan, ????-2003
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2002-02-02 04:17:29 +05:30
*/
2002-02-02 04:17:29 +05:30
#include <stdio.h>
#include <stdlib.h>
2002-10-10 05:31:37 +05:30
#include "version.h"
2002-02-02 04:17:29 +05:30
const char procps_version[] = PACKAGE_NAME " version " PACKAGE_VERSION;
2002-02-02 04:17:29 +05:30
void display_version(void) {
fprintf(stdout, "%s\n", procps_version);
}
/* Linux kernel version information for procps-ng utilities
2002-02-02 04:17:29 +05:30
* Copyright (c) 1996 Charles Blake <cblake@bbn.com>
*/
#include <sys/utsname.h>
#define LINUX_VERSION(x,y,z) (0x10000*(x) + 0x100*(y) + z)
2003-02-09 12:57:16 +05:30
int linux_version_code;
2002-02-02 04:17:29 +05:30
void init_Linux_version(void) {
2002-02-02 04:17:29 +05:30
int x = 0, y = 0, z = 0; /* cleared in case sscanf() < 3 */
2011-06-20 21:24:06 +05:30
int version_string_depth;
#ifdef __linux__
static struct utsname uts;
2002-02-02 04:17:29 +05:30
if (uname(&uts) == -1) /* failure implies impending death */
exit(1);
2011-06-20 21:24:06 +05:30
version_string_depth = sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
#else
FILE *fp;
char buf[256];
if ( (fp=fopen("/proc/version","r")) == NULL) {
fprintf(stderr, "Cannot find /proc/version - is /proc mounted?\n");
exit(1);
}
if (fgets(buf, 256, fp) == NULL) {
fprintf(stderr, "Cannot read kernel version from /proc/version\n");
fclose(fp);
exit(1);
}
fclose(fp);
version_string_depth = sscanf(buf, "Linux version %d.%d.%d", &x, &y, &z);
#endif /* __linux__ */
2011-06-20 21:24:06 +05:30
if ((version_string_depth < 2) || /* Non-standard for all known kernels */
((version_string_depth < 3) && (x < 3))) /* Non-standard for 2.x.x kernels */
#ifdef __linux__
2002-02-02 04:17:29 +05:30
fprintf(stderr, /* *very* unlikely to happen by accident */
"Non-standard uts for running kernel:\n"
"release %s=%d.%d.%d gives version code %d\n",
uts.release, x, y, z, LINUX_VERSION(x,y,z));
#else
fprintf(stderr, /* *very* unlikely to happen by accident */
"%s=%d.%d.%d gives version code %d\n",
buf, x, y, z, LINUX_VERSION(x,y,z));
#endif /* __linux__ */
2002-02-02 04:17:29 +05:30
linux_version_code = LINUX_VERSION(x, y, z);
}