feature from the guys at RedHat for more selective "-n" option:
[oss-qm-packages.git] / lib / util.c
blobac932f4746118e450fb16f887cfc98ba35a772b7
1 /* Copyright 1998 by Andi Kleen. Subject to the GPL. */
2 /* $Id: util.c,v 1.4 1998/11/17 15:17:02 freitag Exp $ */
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/utsname.h>
8 #include "util.h"
11 static void oom(void)
13 fprintf(stderr, "out of virtual memory\n");
14 exit(2);
17 void *xmalloc(size_t sz)
19 void *p = calloc(sz, 1);
20 if (!p)
21 oom();
22 return p;
25 void *xrealloc(void *oldp, size_t sz)
27 void *p = realloc(oldp, sz);
28 if (!p)
29 oom();
30 return p;
33 int kernel_version(void)
35 struct utsname uts;
36 int major, minor, patch;
38 if (uname(&uts) < 0)
39 return -1;
40 if (sscanf(uts.release, "%d.%d.%d", &major, &minor, &patch) != 3)
41 return -1;
42 return KRELEASE(major, minor, patch);
46 /* Like strncpy but make sure the resulting string is always 0 terminated. */
47 char *safe_strncpy(char *dst, const char *src, size_t size)
49 dst[size-1] = '\0';
50 return strncpy(dst,src,size-1);