last commit i forgot to save changes in x11.c ; this fixes that file
[monky] / src / user.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-2010 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 <pwd.h>
32 #include <grp.h>
33 #include <errno.h>
34 #include "conky.h"
35 #include "logging.h"
36
37 void print_uid_name(struct text_object *obj, char *p, int p_max_size) {
38         struct passwd *pw;
39         uid_t uid;
40         char* firstinvalid;
41
42         errno = 0;
43         uid = strtol(obj->data.s, &firstinvalid, 10);
44         if (errno == 0 && obj->data.s != firstinvalid) {
45                 pw = getpwuid(uid);
46                 if(pw != NULL) {
47                         snprintf(p, p_max_size, "%s", pw->pw_name);
48                 } else {
49                         NORM_ERR("The uid %d doesn't exist", uid)
50                 }
51         } else {
52                 NORM_ERR("$uid_name didn't receive a uid as argument")
53         }
54 }
55
56 void print_gid_name(struct text_object *obj, char *p, int p_max_size) {
57         struct group *grp;
58         gid_t gid;
59         char* firstinvalid;
60
61         errno = 0;
62         gid = strtol(obj->data.s, &firstinvalid, 10);
63         if (errno == 0 && obj->data.s != firstinvalid) {
64                 grp = getgrgid(gid);
65                 if(grp != NULL) {
66                         snprintf(p, p_max_size, "%s", grp->gr_name);
67                 } else {
68                         NORM_ERR("The gid %d doesn't exist", gid)
69                 }
70         } else {
71                 NORM_ERR("$gid_name didn't receive a gid as argument")
72         }
73 }