1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 void scan_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash, const char *file)
41 if(sscanf(arg, "%d", &pid) == 1) {
42 asprintf(&obj->data.s, PROCDIR "/%d/%s", pid, file);
44 CRIT_ERR(obj, free_at_crash, "syntax error: ${pid_%s pid}", file);
48 void scan_pid_cmdline_arg(struct text_object *obj, const char *arg, void* free_at_crash)
50 scan_pid_arg(obj, arg, free_at_crash, "cmdline");
53 void print_pid_cmdline(struct text_object *obj, char *p, int p_max_size)
59 infofile = fopen(obj->data.s, "r");
61 bytes_read = fread(buf, 1, p_max_size, infofile);
62 for(i = 0; i < bytes_read-1; i++) {
67 snprintf(p, p_max_size, "%s", buf);
70 NORM_ERR(READERR, obj->data.s);
74 void scan_pid_cwd_arg(struct text_object *obj, const char *arg, void* free_at_crash)
76 scan_pid_arg(obj, arg, free_at_crash, "cwd");
79 void print_pid_cwd(struct text_object *obj, char *p, int p_max_size)
84 memset(buf, 0, p_max_size);
85 bytes_read = readlink(obj->data.s, buf, p_max_size);
86 if(bytes_read != -1) {
87 snprintf(p, p_max_size, "%s", buf);
89 NORM_ERR(READERR, obj->data.s);
93 void scan_pid_environ_arg(struct text_object *obj, const char *arg, void* free_at_crash)
96 struct environ_data* ed = malloc(sizeof(struct environ_data));
97 ed->var = malloc(strlen(arg));
99 if(sscanf(arg, "%d %s", &pid, ed->var) == 2) {
100 asprintf(&ed->file, PROCDIR "/%d/environ", pid);
101 for(int i = 0; ed->var[i] != 0; i++) {
102 ed->var[i] = toupper(ed->var[i]);
104 obj->data.opaque = ed;
108 CRIT_ERR(obj, free_at_crash, "${pid_environ pid varname}");
112 void print_pid_environ(struct text_object *obj, char *p, int p_max_size)
117 int bytes_read, total_read = 0;
119 searchstring = malloc(strlen(((struct environ_data*) obj->data.opaque)->var) + strlen("=%[\1-\255]") + 1);
120 strcpy(searchstring, ((struct environ_data*) obj->data.opaque)->var);
121 strcat(searchstring, "=%[\1-\255]");
122 infofile = fopen(((struct environ_data*) obj->data.opaque)->file, "r");
125 buf = realloc(buf, total_read + p_max_size + 1);
126 bytes_read = fread(buf + total_read, 1, p_max_size, infofile);
127 total_read += bytes_read;
129 }while(bytes_read != 0);
130 for(bytes_read = 0; bytes_read < total_read; bytes_read += strlen(buf + bytes_read) + 1) {
131 if(sscanf(buf + bytes_read, searchstring, p) == 1) {
143 NORM_ERR(READERR, ((struct environ_data*) obj->data.opaque)->file);
147 void free_pid_environ(struct text_object *obj) {
148 free(((struct environ_data*) obj->data.opaque)->file);
149 free(((struct environ_data*) obj->data.opaque)->var);
150 free(obj->data.opaque);
153 void scan_pid_environ_list_arg(struct text_object *obj, const char *arg, void* free_at_crash)
157 if(sscanf(arg, "%d", &pid) == 1) {
158 asprintf(&obj->data.s, PROCDIR "/%d/environ", pid);
160 CRIT_ERR(obj, free_at_crash, "syntax error: ${pid_environ pid}");
164 void print_pid_environ_list(struct text_object *obj, char *p, int p_max_size)
169 int bytes_read, total_read = 0;
172 infofile = fopen(obj->data.s, "r");
175 buf = realloc(buf, total_read + p_max_size + 1);
176 bytes_read = fread(buf + total_read, 1, p_max_size, infofile);
177 total_read += bytes_read;
179 }while(bytes_read != 0);
180 while(bytes_read < total_read) {
181 buf2 = strdup(buf+bytes_read);
182 bytes_read += strlen(buf2)+1;
183 sscanf(buf2, "%[^=]", buf+i);
189 snprintf(p, p_max_size, buf);
193 NORM_ERR(READERR, obj->data.s);