XMMS2
utils_unix.c
Go to the documentation of this file.
1/* XMMS2 - X Music Multiplexer System
2 * Copyright (C) 2003-2011 XMMS2 Team
3 *
4 * PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 */
16
17/** @file
18 * Miscellaneous internal utility functions.
19 */
20
21#include <stdlib.h>
22#include <unistd.h>
23#include <pwd.h>
24#include <time.h>
25#include <errno.h>
26
27#include "xmms_configuration.h"
28#include "xmmsc/xmmsc_util.h"
29
30/**
31 * internal function used for the function below.
32 * @internal
33**/
34static const char *
35xdg_dir_get (const char *env, const char *default_dir, char *buf, int len)
36{
37 struct passwd *pw;
38 char *home;
39
40 if (!buf || len <= 0)
41 return NULL;
42
43 home = getenv (env);
44
45 if (home && *home) {
46 snprintf (buf, len, "%s/xmms2", home);
47
48 return buf;
49 }
50
51 pw = getpwuid (getuid ());
52 if (!pw)
53 return NULL;
54
55 snprintf (buf, len, "%s/%s", pw->pw_dir, default_dir);
56
57 return buf;
58}
59
60/**
61 * Get the absolute path to the user cache dir.
62 * @param buf a char buffer
63 * @param len the lenght of buf (XMMS_PATH_MAX is a good choice)
64 * @return A pointer to buf, or NULL if an error occurred.
65**/
66const char *
67xmms_usercachedir_get (char *buf, int len)
68{
69 return xdg_dir_get ("XDG_CACHE_HOME", USERCACHEDIR, buf, len);
70}
71
72/**
73 * Get the absolute path to the user config dir.
74 *
75 * @param buf A char buffer
76 * @param len The length of buf (XMMS_PATH_MAX is a good choice)
77 * @return A pointer to buf, or NULL if an error occurred.
78 */
79const char *
80xmms_userconfdir_get (char *buf, int len)
81{
82 return xdg_dir_get ("XDG_CONFIG_HOME", USERCONFDIR, buf, len);
83}
84
85/**
86 * Get the fallback connection path (if XMMS_PATH is not accessible)
87 *
88 * @param buf A char buffer
89 * @param len The length of buf (XMMS_PATH_MAX is a good choice)
90 * @return A pointer to buf, or NULL if an error occured.
91 */
92const char *
93xmms_fallback_ipcpath_get (char *buf, int len)
94{
95 struct passwd *pw;
96
97 pw = getpwuid (getuid ());
98 if (!pw || !pw->pw_name) {
99 return NULL;
100 }
101
102 snprintf (buf, len, "unix:///tmp/xmms-ipc-%s", pw->pw_name);
103
104 return buf;
105}
106
107/**
108 * Sleep for n milliseconds.
109 *
110 * @param n The number of milliseconds to sleep.
111 * @return true when we waited the full time, false otherwise.
112 */
113bool
115{
116 struct timespec sleeptime;
117
118 sleeptime.tv_sec = (time_t) (n / 1000);
119 sleeptime.tv_nsec = (n % 1000) * 1000000;
120
121 while (nanosleep (&sleeptime, &sleeptime) == -1) {
122 if (errno != EINTR) {
123 return false;
124 }
125 }
126
127 return true;
128}
const char * xmms_fallback_ipcpath_get(char *buf, int len)
Get the fallback connection path (if XMMS_PATH is not accessible)
Definition: utils_unix.c:93
const char * xmms_userconfdir_get(char *buf, int len)
Get the absolute path to the user config dir.
Definition: utils_unix.c:80
bool xmms_sleep_ms(int n)
Sleep for n milliseconds.
Definition: utils_unix.c:114
const char * xmms_usercachedir_get(char *buf, int len)
Get the absolute path to the user cache dir.
Definition: utils_unix.c:67