enable -fno-strict-aliasing until the code base gets a hefty clean up to fix all...
[platform/upstream/net-tools.git] / lib / util.c
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>
7
8 #include "util.h"
9
10
11 static void oom(void)
12 {
13     fprintf(stderr, "out of virtual memory\n");
14     exit(2);
15 }
16
17 void *xmalloc(size_t sz)
18 {
19     void *p = calloc(sz, 1);
20     if (!p)
21         oom();
22     return p;
23 }
24
25 void *xrealloc(void *oldp, size_t sz)
26 {
27     void *p = realloc(oldp, sz);
28     if (!p)
29         oom();
30     return p;
31 }
32
33 int kernel_version(void)
34 {
35     struct utsname uts;
36     int major, minor, patch;
37
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);
43 }
44
45
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)
48 {   
49     dst[size-1] = '\0';
50     return strncpy(dst,src,size-1);   
51 }