source: roaraudio/libroarpulse/util.c @ 3810:9366443fd83f

Last change on this file since 3810:9366443fd83f was 3517:1a3218a3fc5b, checked in by phi, 14 years ago

updated license headers, FSF moved office

File size: 4.1 KB
Line 
1//util.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
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 (const char *) r+1;
67 } else {
68  return 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#ifdef ROAR_TARGET_WIN32
99 Sleep(t);
100#else
101 usleep(1000*t);
102#endif
103 return 0;
104}
105
106/* Format the specified data as a hexademical string */
107char *pa_hexstr(const uint8_t* d, size_t dlength, char *s, size_t slength) {
108 const char table[] = "0123456789abcdef";
109 char * p = s;
110 size_t i;
111
112 for (i = 0; i < dlength && slength > 2; i++, slength -= 2, d++) {
113  *(p++) = table[*d >>    4];
114  *(p++) = table[*d &  0x0F];
115 }
116
117 *p = 0;
118
119 return s;
120}
121
122/* Parse a hexadecimal string as created by pa_hexstr() to a BLOB */
123size_t pa_parsehex(const char *p, uint8_t *d, size_t dlength) {
124 size_t have = 0;
125 register int low_nibble = 0;
126 register char c;
127
128 if ( dlength == 0 )
129  return 0;
130
131 for (; (c = *p); p++) {
132  if ( c >= '0' && c <= '9' ) {
133   c -= '0';
134  } else if ( c >= 'a' && c <= 'f' ) {
135   c -= 'a' - 10;
136  } else if ( c >= 'A' && c <= 'F' ) {
137   c -= 'A' - 10;
138  } else {
139   return (size_t)-1;
140  }
141
142  if ( low_nibble ) {
143   *d |= c & 0x0F;
144   low_nibble = 0;
145
146   d++;
147   have++;
148   dlength--;
149
150   if ( dlength == 0 )
151    return have;
152  } else {
153   *d = (c & 0x0F) << 4;
154   low_nibble = 1;
155  }
156 }
157
158 return have;
159}
160
161
162//ll
Note: See TracBrowser for help on using the repository browser.