Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / kinfocenter / memory / memory_fbsd.cpp
blob6919cc62abf1e4cfc0a5b738637c29f51b8c4856
2 #include <sys/types.h>
3 #include <sys/sysctl.h>
4 #include <sys/vmmeter.h>
6 #include <vm/vm_param.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <unistd.h>
12 void KCMMemory::fetchValues()
14 char blah[10], buf[80], *used_str, *total_str;
15 /* Stuff for sysctl */
16 int memory;
17 size_t len;
18 /* Stuff for swap display */
19 int used, total, _free;
20 FILE *pipe;
22 len=sizeof(memory);
23 sysctlbyname("hw.physmem", &memory, &len, NULL, 0);
25 snprintf(blah, 10, "%d", memory);
26 // Numerical values
28 // total physical memory (without swap space)
29 memoryInfos[TOTAL_MEM] = MEMORY(memory);
31 // added by Brad Hughes bhughes@trolltech.com
32 struct vmtotal vmem;
33 #ifdef __GNUC__
34 #warning "FIXME: memoryInfos[CACHED_MEM]"
35 #endif
36 memoryInfos[CACHED_MEM] = NO_MEMORY_INFO;
38 // The sysctls don't work in a nice manner under FreeBSD v2.2.x
39 // so we assume that if sysctlbyname doesn't return what we
40 // prefer, assume it's the old data types. FreeBSD prior
41 // to 4.0-R isn't supported by the rest of KDE, so what is
42 // this code doing here.
44 len = sizeof(vmem);
45 if (sysctlbyname("vm.vmmeter", &vmem, &len, NULL, 0) == 0)
46 memoryInfos[SHARED_MEM] = MEMORY(vmem.t_armshr) * PAGE_SIZE;
47 else
48 memoryInfos[SHARED_MEM] = NO_MEMORY_INFO;
50 int buffers;
51 len = sizeof (buffers);
52 if ((sysctlbyname("vfs.bufspace", &buffers, &len, NULL, 0) == -1) || !len)
53 memoryInfos[BUFFER_MEM] = NO_MEMORY_INFO;
54 else
55 memoryInfos[BUFFER_MEM] = MEMORY(buffers);
57 // total free physical memory (without swap space)
58 int free;
59 len = sizeof (buffers);
60 if ((sysctlbyname("vm.stats.vm.v_free_count", &free, &len, NULL, 0) == -1) || !len)
61 memoryInfos[FREE_MEM] = NO_MEMORY_INFO;
62 else
63 memoryInfos[FREE_MEM] = MEMORY(free) * getpagesize();
65 // Q&D hack for swap display. Borrowed from xsysinfo-1.4
66 if ((pipe = popen("/usr/sbin/pstat -ks", "r")) == NULL) {
67 used = total = 1;
68 return;
71 fgets(buf, sizeof(buf), pipe);
72 fgets(buf, sizeof(buf), pipe);
73 fgets(buf, sizeof(buf), pipe);
74 fgets(buf, sizeof(buf), pipe);
75 pclose(pipe);
77 strtok(buf, " ");
78 total_str = strtok(NULL, " ");
79 used_str = strtok(NULL, " ");
80 used = atoi(used_str);
81 total = atoi(total_str);
83 _free=total-used;
85 // total size of all swap-partitions
86 memoryInfos[SWAP_MEM] = MEMORY(total) * 1024;
88 // free memory in swap-partitions
89 memoryInfos[FREESWAP_MEM] = MEMORY(_free) * 1024;