2013-05-12 22:01:40 -04:00
|
|
|
/* Copyright 1998 by Andi Kleen. Subject to the GPL. */
|
1998-11-17 15:16:09 +00:00
|
|
|
/* $Id: util.c,v 1.4 1998/11/17 15:17:02 freitag Exp $ */
|
1998-10-23 06:15:04 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1998-11-17 15:16:09 +00:00
|
|
|
#include <string.h>
|
1998-10-30 14:15:46 +00:00
|
|
|
#include <sys/utsname.h>
|
2015-06-15 21:51:32 +02:00
|
|
|
#include <unistd.h>
|
1998-10-23 06:15:04 +00:00
|
|
|
|
1998-11-17 15:16:09 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
|
1998-10-23 06:15:04 +00:00
|
|
|
static void oom(void)
|
1998-11-15 20:07:31 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "out of virtual memory\n");
|
|
|
|
exit(2);
|
1998-10-23 06:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *xmalloc(size_t sz)
|
|
|
|
{
|
1998-11-15 20:07:31 +00:00
|
|
|
void *p = calloc(sz, 1);
|
|
|
|
if (!p)
|
|
|
|
oom();
|
|
|
|
return p;
|
1998-10-23 06:15:04 +00:00
|
|
|
}
|
|
|
|
|
2013-08-30 12:30:25 +00:00
|
|
|
/* Like strdup, but oom() instead of NULL */
|
2013-10-08 21:50:38 +00:00
|
|
|
char *xstrdup(const char *s)
|
2013-08-30 12:30:25 +00:00
|
|
|
{
|
|
|
|
char *d = strdup(s);
|
|
|
|
if (!d)
|
|
|
|
oom();
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
1998-10-23 06:15:04 +00:00
|
|
|
void *xrealloc(void *oldp, size_t sz)
|
|
|
|
{
|
1998-11-15 20:07:31 +00:00
|
|
|
void *p = realloc(oldp, sz);
|
|
|
|
if (!p)
|
|
|
|
oom();
|
|
|
|
return p;
|
1998-10-23 06:15:04 +00:00
|
|
|
}
|
1998-10-30 14:15:46 +00:00
|
|
|
|
|
|
|
int kernel_version(void)
|
|
|
|
{
|
1998-11-15 20:07:31 +00:00
|
|
|
struct utsname uts;
|
2013-04-25 21:01:56 +00:00
|
|
|
int major, minor, patch=0;
|
1998-10-30 14:15:46 +00:00
|
|
|
|
1998-11-15 20:07:31 +00:00
|
|
|
if (uname(&uts) < 0)
|
|
|
|
return -1;
|
2013-04-25 21:01:56 +00:00
|
|
|
if (sscanf(uts.release, "%d.%d.%d", &major, &minor, &patch) < 2)
|
1998-11-15 20:07:31 +00:00
|
|
|
return -1;
|
|
|
|
return KRELEASE(major, minor, patch);
|
1998-10-30 14:15:46 +00:00
|
|
|
}
|
1998-11-17 15:16:09 +00:00
|
|
|
|
2015-06-15 21:51:32 +02:00
|
|
|
long ticks_per_second(void)
|
|
|
|
{
|
|
|
|
return sysconf(_SC_CLK_TCK);
|
|
|
|
}
|
1998-11-17 15:16:09 +00:00
|
|
|
|
2013-05-12 22:01:40 -04:00
|
|
|
/* Like strncpy but make sure the resulting string is always 0 terminated. */
|
1998-11-17 15:16:09 +00:00
|
|
|
char *safe_strncpy(char *dst, const char *src, size_t size)
|
2013-05-12 22:01:40 -04:00
|
|
|
{
|
1998-11-17 15:16:09 +00:00
|
|
|
dst[size-1] = '\0';
|
2013-05-12 22:01:40 -04:00
|
|
|
return strncpy(dst,src,size-1);
|
1998-11-17 15:16:09 +00:00
|
|
|
}
|