source: roaraudio/libroarpulse/util.c @ 4896:0cd0dc3bc104

Last change on this file since 4896:0cd0dc3bc104 was 4896:0cd0dc3bc104, checked in by phi, 13 years ago

removed useage of usleep() in flavor to nanosleep() if supported.

File size: 4.1 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
76/** Return the current hostname in the specified buffer. */
77char *pa_get_host_name(char *s, size_t l);
78
79/** Return the fully qualified domain name in s */
80char *pa_get_fqdn(char *s, size_t l);
81
82/** Return the home directory of the current user */
83char *pa_get_home_dir(char *s, size_t l) {
84 char * home = getenv("HOME");
85
86 if ( home == NULL )
87  return NULL;
88
89 strncpy(s, home, l);
90
91 s[l-1] = 0;
92
93 return s;
94}
95
96/** Wait t milliseconds */
97int pa_msleep(unsigned long t) {
98 roar_usleep((uint_least32_t)1000*(uint_least32_t)t);
99 return 0;
100}
101
102/* Format the specified data as a hexademical string */
103char *pa_hexstr(const uint8_t* d, size_t dlength, char *s, size_t slength) {
104 const char table[] = "0123456789abcdef";
105 char * p = s;
106 size_t i;
107
108 for (i = 0; i < dlength && slength > 2; i++, slength -= 2, d++) {
109  *(p++) = table[*d >>    4];
110  *(p++) = table[*d &  0x0F];
111 }
112
113 *p = 0;
114
115 return s;
116}
117
118/* Parse a hexadecimal string as created by pa_hexstr() to a BLOB */
119size_t pa_parsehex(const char *p, uint8_t *d, size_t dlength) {
120 size_t have = 0;
121 register int low_nibble = 0;
122 register char c;
123
124 if ( dlength == 0 )
125  return 0;
126
127 for (; (c = *p); p++) {
128  if ( c >= '0' && c <= '9' ) {
129   c -= '0';
130  } else if ( c >= 'a' && c <= 'f' ) {
131   c -= 'a' - 10;
132  } else if ( c >= 'A' && c <= 'F' ) {
133   c -= 'A' - 10;
134  } else {
135   return (size_t)-1;
136  }
137
138  if ( low_nibble ) {
139   *d |= c & 0x0F;
140   low_nibble = 0;
141
142   d++;
143   have++;
144   dlength--;
145
146   if ( dlength == 0 )
147    return have;
148  } else {
149   *d = (c & 0x0F) << 4;
150   low_nibble = 1;
151  }
152 }
153
154 return have;
155}
156
157
158//ll
Note: See TracBrowser for help on using the repository browser.