ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/tools.c
Revision: 1.13
Committed: Mon Nov 10 22:46:32 2003 UTC (20 years, 6 months ago) by ats
Content type: text/plain
Branch: MAIN
Changes since 1.12: +20 -9 lines
Log Message:
Provide atoll() on platforms that don't have it.

File Contents

# User Rev Content
1 tdb 1.5 /*
2     * i-scream central monitoring system
3 tdb 1.6 * http://www.i-scream.org
4     * Copyright (C) 2000-2003 i-scream
5 tdb 1.5 *
6     * This program is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU General Public License
8     * as published by the Free Software Foundation; either version 2
9     * of the License, or (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19     */
20    
21     #ifdef HAVE_CONFIG_H
22     #include "config.h"
23     #endif
24    
25 pajs 1.1 #include <stdio.h>
26     #include <string.h>
27 pajs 1.3 #include <stdlib.h>
28     #include <sys/types.h>
29     #include <regex.h>
30 ats 1.9 #ifdef ALLBSD
31 ats 1.8 #include <fcntl.h>
32     #include <kvm.h>
33     #endif
34 ats 1.9 #ifdef NETBSD
35     #include <uvm/uvm_extern.h>
36     #endif
37 pajs 1.1
38 ats 1.7 #include "tools.h"
39    
40 pajs 1.1 char *f_read_line(FILE *f, const char *string){
41     /* Max line length. 8k should be more than enough */
42     static char line[8192];
43    
44 pajs 1.2 while((fgets(line, sizeof(line), f))!=NULL){
45 pajs 1.1 if(strncmp(string, line, strlen(string))==0){
46     return line;
47     }
48     }
49    
50     return NULL;
51 pajs 1.3 }
52    
53     char *get_string_match(char *line, regmatch_t *match){
54     int len=match->rm_eo - match->rm_so;
55     char *match_string=malloc(len+1);
56    
57     match_string=strncpy(match_string, line+match->rm_so, len);
58     match_string[len]='\0';
59    
60     return match_string;
61     }
62 ats 1.7
63 ats 1.13 #ifndef HAVE_ATOLL
64     static long long atoll(const char *s) {
65     long long value = 0;
66     int isneg = 0;
67    
68     while (*s == ' ' || *s == '\t') {
69     s++;
70     }
71     if (*s == '-') {
72     isneg = 1;
73     s++;
74     }
75     while (*s >= '0' && *s <= '9') {
76     value = (10 * value) + (*s - '0');
77     s++;
78     }
79     return (isneg ? -value : value);
80     }
81     #endif
82    
83 pajs 1.3 long long get_ll_match(char *line, regmatch_t *match){
84     char *ptr;
85     long long num;
86    
87     ptr=line+match->rm_so;
88     num=atoll(ptr);
89    
90     return num;
91 ats 1.8 }
92    
93 ats 1.9 #ifdef ALLBSD
94 ats 1.8 kvm_t *get_kvm() {
95     static kvm_t *kvmd = NULL;
96    
97     if (kvmd != NULL) {
98     return kvmd;
99     }
100    
101     kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
102     return kvmd;
103 pajs 1.1 }
104 pajs 1.4 #endif
105 ats 1.9
106     #ifdef NETBSD
107     struct uvmexp *get_uvmexp() {
108     static u_long addr = 0;
109     static struct uvmexp uvm;
110     kvm_t *kvmd = get_kvm();
111    
112     if (kvmd == NULL) {
113     return NULL;
114     }
115    
116     if (addr == 0) {
117     struct nlist symbols[] = {
118     { "_uvmexp" },
119     { NULL }
120     };
121    
122     if (kvm_nlist(kvmd, symbols) != 0) {
123     return NULL;
124     }
125     addr = symbols[0].n_value;
126     }
127    
128     if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) {
129     return NULL;
130     }
131     return &uvm;
132     }
133     #endif
134    
135 pajs 1.10 int statgrab_init(){
136 pajs 1.11 #ifdef ALLBSD
137     {
138     kvm_t *kvmd = get_kvm();
139     if (kvmd == NULL) return 1;
140     }
141 pajs 1.10 #endif
142     #ifdef NETBSD
143 pajs 1.11 {
144     struct uvmexp *uvm = get_uvmexp();
145     if (uvm == NULL) return 1;
146     }
147 pajs 1.10 #endif
148     return 0;
149     }