source: roaraudio/libroarpulse/util.c @ 4960:60cdebcb83ef

Last change on this file since 4960:60cdebcb83ef was 4960:60cdebcb83ef, checked in by phi, 8 years ago

some updates for pulseaudio emulation, converted libroarpulse-simple fully to VS API

File size: 4.8 KB
Line 
1//util.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
5 *  The code (may) include prototypes and comments (and maybe
6 *  other code fragements) from libpulse*. They are mostly copyrighted by:
7 *  Lennart Poettering <poettering@users.sourceforge.net> and
8 *  Pierre Ossman <drzeus@drzeus.cx>
9 *
10 *  This file is part of libroarpulse a part of RoarAudio,
11 *  a cross-platform sound system for both, home and professional use.
12 *  See README for details.
13 *
14 *  This file is free software; you can redistribute it and/or modify
15 *  it under the terms of the GNU General Public License version 3
16 *  as published by the Free Software Foundation.
17 *
18 *  RoarAudio is distributed in the hope that it will be useful,
19 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 *  GNU General Public License for more details.
22 *
23 *  You should have received a copy of the GNU General Public License
24 *  along with this software; see the file COPYING.  If not, write to
25 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
26 *  Boston, MA 02110-1301, USA.
27 *
28 *  NOTE for everyone want's to change something and send patches:
29 *  read README and HACKING! There a addition information on
30 *  the license of this document you need to read before you send
31 *  any patches.
32 *
33 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
34 *  or libpulse*:
35 *  The libs libroaresd, libroararts and libroarpulse link this libroar
36 *  and are therefore GPL. Because of this it may be illigal to use
37 *  them with any software that uses libesd, libartsc or libpulse*.
38 */
39
40#include <libroarpulse/libroarpulse.h>
41
42/** Return the binary file name of the current process. This is not
43 * supported on all architectures, in which case NULL is returned. */
44char *pa_get_binary_name(char *s, size_t l) {
45#ifdef __linux__
46 int ret;
47 char path[PATH_MAX];
48
49 if ( (ret = readlink("/proc/self/exe", path, PATH_MAX-1)) != -1 ) {
50  path[ret] = 0;
51  return strncpy(s, pa_path_get_filename(path), l);
52 }
53#endif
54
55 return NULL;
56}
57
58/** Return a pointer to the filename inside a path (which is the last
59 * component). */
60// some versions declare this as const char * f(...)
61// and newer(?) versions as char * f(...)...
62ROAR_HAVE_TYPE_PA_PATH_GET_FILENAME pa_path_get_filename(const char *p) {
63 char * r;
64
65 if ( (r = strrchr(p, '/')) ) {
66  return (ROAR_HAVE_TYPE_PA_PATH_GET_FILENAME) r+1;
67 } else {
68  return (ROAR_HAVE_TYPE_PA_PATH_GET_FILENAME) p;
69 }
70
71}
72
73/** Return the current username in the specified string buffer. */
74char *pa_get_user_name(char *s, size_t l) {
75 char * user = NULL;
76
77 if ( s == NULL ) {
78  roar_err_set(ROAR_ERROR_FAULT);
79  roar_err_to_errno();
80  return NULL;
81 }
82
83 if ( user == NULL )
84  user = getenv("USER");
85 if ( user == NULL )
86  user = getenv("LOGNAME");
87 if ( user == NULL )
88  user = getenv("USERNAME");
89
90#ifdef ROAR_HAVE_GETUID
91 if ( user == NULL )
92  if ( getuid() == 0 )
93   user = "root";
94#endif
95
96 if ( user == NULL ) {
97  roar_err_set(ROAR_ERROR_NOENT);
98  roar_err_to_errno();
99  return NULL;
100 }
101
102 strncpy(s, user, l);
103 s[l-1] = 0;
104
105 return s;
106}
107
108/** Return the current hostname in the specified buffer. */
109char *pa_get_host_name(char *s, size_t l) {
110#ifdef ROAR_HAVE_GETHOSTNAME
111 if (gethostname(s, l) == -1)
112  return NULL;
113
114 s[l-1] = 0;
115 return s;
116#else
117 roar_err_set(ROAR_ERROR_NOSYS);
118 roar_err_to_errno();
119 return NULL;
120#endif
121}
122
123/** Return the fully qualified domain name in s */
124char *pa_get_fqdn(char *s, size_t l);
125
126/** Return the home directory of the current user */
127char *pa_get_home_dir(char *s, size_t l) {
128 char * home = getenv("HOME");
129
130 if ( home == NULL )
131  return NULL;
132
133 strncpy(s, home, l);
134
135 s[l-1] = 0;
136
137 return s;
138}
139
140/** Wait t milliseconds */
141int pa_msleep(unsigned long t) {
142 roar_usleep((uint_least32_t)1000*(uint_least32_t)t);
143 return 0;
144}
145
146/* Format the specified data as a hexademical string */
147char *pa_hexstr(const uint8_t* d, size_t dlength, char *s, size_t slength) {
148 const char table[] = "0123456789abcdef";
149 char * p = s;
150 size_t i;
151
152 for (i = 0; i < dlength && slength > 2; i++, slength -= 2, d++) {
153  *(p++) = table[*d >>    4];
154  *(p++) = table[*d &  0x0F];
155 }
156
157 *p = 0;
158
159 return s;
160}
161
162/* Parse a hexadecimal string as created by pa_hexstr() to a BLOB */
163size_t pa_parsehex(const char *p, uint8_t *d, size_t dlength) {
164 size_t have = 0;
165 register int low_nibble = 0;
166 register char c;
167
168 if ( dlength == 0 )
169  return 0;
170
171 for (; (c = *p); p++) {
172  if ( c >= '0' && c <= '9' ) {
173   c -= '0';
174  } else if ( c >= 'a' && c <= 'f' ) {
175   c -= 'a' - 10;
176  } else if ( c >= 'A' && c <= 'F' ) {
177   c -= 'A' - 10;
178  } else {
179   return (size_t)-1;
180  }
181
182  if ( low_nibble ) {
183   *d |= c & 0x0F;
184   low_nibble = 0;
185
186   d++;
187   have++;
188   dlength--;
189
190   if ( dlength == 0 )
191    return have;
192  } else {
193   *d = (c & 0x0F) << 4;
194   low_nibble = 1;
195  }
196 }
197
198 return have;
199}
200
201
202//ll
Note: See TracBrowser for help on using the repository browser.