fix previous commit
[monky] / src / proc.c
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
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *   (see AUTHORS)
15  * All rights reserved.
16  *
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.
21  *
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/>.
28  *
29  */
30
31 #include <logging.h>
32 #include "conky.h"
33 #include "proc.h"
34
35 void scan_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash)
36 {
37         char* string = strdup(arg);
38         pid_t pid;
39
40         if(sscanf(arg, "%s %d", string, &pid) == 2 && strcasecmp(string, "cmdline") == 0) {
41                 asprintf(&obj->data.s, PROCDIR "/%d/%s",  pid, string);
42                 free(string);
43         } else {
44                 free(string);
45                 CRIT_ERR(obj, free_at_crash, PID_SYNTAXERR);
46         }
47 }
48
49 void print_pid(struct text_object *obj, char *p, int p_max_size)
50 {
51         char buf[p_max_size];
52         FILE* infofile;
53         int i, bytes_read;
54
55         infofile = fopen(obj->data.s, "r");
56         if(infofile) {
57                 bytes_read = fread(buf, 1, p_max_size, infofile);
58                 for(i = 0; i < bytes_read-1; i++) {
59                         if(buf[i] == 0) {
60                                 buf[i] = ' ';
61                         }
62                 }
63                 snprintf(p, p_max_size, "%s", buf);
64                 fclose(infofile);
65         } else {
66                 NORM_ERR(READERR, obj->data.s);
67         }
68 }