Skip to content

Commit 28c03e6

Browse files
committed
Move hardcoded parameters
Put MPD connection settings to the top of the file and eliminate hardcoded user home dir and retrieve it from passwd.
1 parent 079365e commit 28c03e6

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

sysstat.c

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
/* Refresh interval in seconds */
2525
const unsigned int refresh = 5;
26+
const char *mpd_host = "/home/helm/Music/.mpd/socket";
27+
const unsigned int mpd_port = 0; /* Unnecessary when used with UNIX socket. */
2628

2729
#define CUC(var) (const unsigned char *)var
2830

@@ -64,7 +66,9 @@ static int stdin_end_map(void *context);
6466
static inline int round_float_to_int(float f);
6567
static char *size_to_human_readable(double n);
6668
static char *get_kernel_string(void);
69+
static struct passwd *get_passwd(void);
6770
static char *get_user(void);
71+
static char *get_user_home(void);
6872
static char *get_ram_usage(void);
6973
static char *get_datetime(bool fuzzy);
7074
static char *get_disk_free(const char *mount);
@@ -212,21 +216,39 @@ get_kernel_string(void)
212216
return strdup(u.release);
213217
}
214218

219+
static struct passwd *
220+
get_passwd(void)
221+
{
222+
uid_t uid;
223+
224+
uid = geteuid();
225+
return getpwuid(uid);
226+
}
227+
215228
static char *
216229
get_user(void)
217230
{
218-
uid_t uid;
219231
struct passwd *pw;
220232

221-
uid = geteuid();
222-
pw = getpwuid(uid);
223-
if (pw == NULL) {
233+
if (!(pw = get_passwd())) {
224234
return NULL;
225235
}
226236

227237
return strdup(pw->pw_name);
228238
}
229239

240+
static char *
241+
get_user_home(void)
242+
{
243+
struct passwd *pw;
244+
245+
if (!(pw = get_passwd())) {
246+
return NULL;
247+
}
248+
249+
return strdup(pw->pw_dir);
250+
}
251+
230252
static char *
231253
get_ram_usage(void)
232254
{
@@ -297,14 +319,13 @@ get_datetime(bool fuzzy)
297319
return NULL;
298320
}
299321
if (!strftime(secondarytimestring, sizeof(datestring), "%H:%M", now)) {
300-
printf("foo\n");
301322
free(secondarytimestring);
302323
return NULL;
303324
}
304325
}
305326

306327

307-
size_t len = strnlen(datestring, sizeof(datestring)-1 + strlen(secondarytimestring) + strlen(" ") + 1;
328+
size_t len = strnlen(datestring, sizeof(datestring)-1) + strlen(secondarytimestring) + strlen(" ") + 1;
308329
timestring = malloc(len);
309330
if(!timestring) {
310331
free(secondarytimestring);
@@ -370,7 +391,7 @@ get_uptime(void)
370391
static struct mpd_connection *
371392
mpd_connect(void)
372393
{
373-
struct mpd_connection *connection = mpd_connection_new("/home/helm/Music/.mpd/socket", 0, 0);
394+
struct mpd_connection *connection = mpd_connection_new(mpd_host, mpd_port, 0);
374395
if (connection == NULL) {
375396
return NULL;
376397
}
@@ -479,10 +500,17 @@ static char *
479500
get_free_storage(void)
480501
{
481502
char *freestring;
503+
char *home_directory;
482504
char *home, *root;
483505

484-
home = get_disk_free("/home/helm");
506+
home_directory = get_user_home();
507+
if (home_directory == NULL) {
508+
return NULL;
509+
}
510+
511+
home = get_disk_free(home_directory);
485512
if (home == NULL) {
513+
free(home_directory);
486514
return NULL;
487515
}
488516
root = get_disk_free("/");
@@ -495,6 +523,7 @@ get_free_storage(void)
495523
snprintf(freestring, 64, "~/:%s /:%s", home, root);
496524
}
497525

526+
free(home_directory);
498527
free(home);
499528
free(root);
500529
return freestring;
@@ -665,7 +694,7 @@ main(void)
665694
char input_event_buf[256];
666695

667696
ret = read(fds[0].fd, input_event_buf, sizeof(input_event_buf));
668-
parse_click_event(buf, ret);
697+
parse_click_event(input_event_buf, ret);
669698
}
670699
}
671700

0 commit comments

Comments
 (0)