XMMS2

src/lib/xmmsutils/utils_unix.c

Go to the documentation of this file.
00001 /*  XMMS2 - X Music Multiplexer System
00002  *  Copyright (C) 2003-2009 XMMS2 Team
00003  *
00004  *  PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2.1 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Lesser General Public License for more details.
00015  */
00016 
00017 /** @file
00018  * Miscellaneous internal utility functions.
00019  */
00020 
00021 #include <stdlib.h>
00022 #include <unistd.h>
00023 #include <pwd.h>
00024 #include <time.h>
00025 #include <errno.h>
00026 
00027 #include "xmms_configuration.h"
00028 #include "xmmsc/xmmsc_util.h"
00029 
00030 /**
00031  * internal function used for the function below.
00032  * @internal
00033 **/
00034 static const char *
00035 xdg_dir_get (const char *env, const char *default_dir, char *buf, int len)
00036 {
00037     struct passwd *pw;
00038     char *home;
00039 
00040     if (!buf || len <= 0)
00041         return NULL;
00042 
00043     home = getenv (env);
00044 
00045     if (home && *home) {
00046         snprintf (buf, len, "%s/xmms2", home);
00047 
00048         return buf;
00049     }
00050 
00051     pw = getpwuid (getuid ());
00052     if (!pw)
00053         return NULL;
00054 
00055     snprintf (buf, len, "%s/%s", pw->pw_dir, default_dir);
00056 
00057     return buf;
00058 }
00059 
00060 /**
00061  * Get the absolute path to the user cache dir.
00062  * @param buf a char buffer
00063  * @param len the lenght of buf (PATH_MAX is a good choice)
00064  * @return A pointer to buf, or NULL if an error occurred.
00065 **/
00066 const char *
00067 xmms_usercachedir_get (char *buf, int len)
00068 {
00069     return xdg_dir_get ("XDG_CACHE_HOME", USERCACHEDIR, buf, len);
00070 }
00071 
00072 /**
00073  * Get the absolute path to the user config dir.
00074  *
00075  * @param buf A char buffer
00076  * @param len The length of buf (PATH_MAX is a good choice)
00077  * @return A pointer to buf, or NULL if an error occurred.
00078  */
00079 const char *
00080 xmms_userconfdir_get (char *buf, int len)
00081 {
00082     return xdg_dir_get ("XDG_CONFIG_HOME", USERCONFDIR, buf, len);
00083 }
00084 
00085 /**
00086  * Get the fallback connection path (if XMMS_PATH is not accessible)
00087  *
00088  * @param buf A char buffer
00089  * @param len The length of buf (PATH_MAX is a good choice)
00090  * @return A pointer to buf, or NULL if an error occured.
00091  */
00092 const char *
00093 xmms_fallback_ipcpath_get (char *buf, int len)
00094 {
00095     struct passwd *pw;
00096 
00097     pw = getpwuid (getuid ());
00098     if (!pw || !pw->pw_name) {
00099         return NULL;
00100     }
00101 
00102     snprintf (buf, len, "unix:///tmp/xmms-ipc-%s", pw->pw_name);
00103 
00104     return buf;
00105 }
00106 
00107 /**
00108  * Sleep for n milliseconds.
00109  *
00110  * @param n The number of milliseconds to sleep.
00111  * @return true when we waited the full time, false otherwise.
00112  */
00113 bool
00114 xmms_sleep_ms (int n)
00115 {
00116     struct timespec sleeptime;
00117 
00118     sleeptime.tv_sec = (time_t) (n / 1000);
00119     sleeptime.tv_nsec = (n % 1000) * 1000000;
00120 
00121     while (nanosleep (&sleeptime, &sleeptime) == -1) {
00122         if (errno != EINTR) {
00123             return false;
00124         }
00125     }
00126 
00127     return true;
00128 }