]>
code.delx.au - pulseaudio/blob - polyp/util.c
70766a0685645984df6bee8847d89930ac808875
4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
35 #include <sys/types.h>
43 void pa_make_nonblock_fd(int fd
) {
46 if ((v
= fcntl(fd
, F_GETFL
)) >= 0)
47 if (!(v
& O_NONBLOCK
))
48 fcntl(fd
, F_SETFL
, v
|O_NONBLOCK
);
51 int pa_make_secure_dir(const char* dir
) {
54 if (mkdir(dir
, 0700) < 0)
58 if (lstat(dir
, &st
) < 0)
61 if (!S_ISDIR(st
.st_mode
) || (st
.st_uid
!= getuid()) || ((st
.st_mode
& 0777) != 0700))
71 ssize_t
pa_loop_read(int fd
, void*data
, size_t size
) {
73 assert(fd
>= 0 && data
&& size
);
78 if ((r
= read(fd
, data
, size
)) < 0)
92 ssize_t
pa_loop_write(int fd
, const void*data
, size_t size
) {
94 assert(fd
>= 0 && data
&& size
);
99 if ((r
= write(fd
, data
, size
)) < 0)
113 void pa_check_for_sigpipe(void) {
117 if (pthread_sigmask(SIG_SETMASK
, NULL
, &set
) < 0) {
118 if (sigprocmask(SIG_SETMASK
, NULL
, &set
) < 0) {
119 fprintf(stderr
, __FILE__
": sigprocmask() failed: %s\n", strerror(errno
));
124 if (sigismember(&set
, SIGPIPE
))
127 if (sigaction(SIGPIPE
, NULL
, &sa
) < 0) {
128 fprintf(stderr
, __FILE__
": sigaction() failed: %s\n", strerror(errno
));
132 if (sa
.sa_handler
!= SIG_DFL
)
135 fprintf(stderr
, "polypaudio: WARNING: SIGPIPE is not trapped. This might cause malfunction!\n");
138 /* The following is based on an example from the GNU libc documentation */
139 char *pa_sprintf_malloc(const char *format
, ...) {
149 c
= pa_xrealloc(c
, size
);
151 va_start(ap
, format
);
152 r
= vsnprintf(c
, size
, format
, ap
);
155 if (r
> -1 && r
< size
)
158 if (r
> -1) /* glibc 2.1 */
165 char *pa_get_user_name(char *s
, size_t l
) {
166 struct passwd pw
, *r
;
170 if (!(p
= getenv("USER")))
171 if (!(p
= getenv("LOGNAME")))
172 if (!(p
= getenv("USERNAME"))) {
174 if (getpwuid_r(getuid(), &pw
, buf
, sizeof(buf
), &r
) != 0 || !r
) {
175 snprintf(s
, l
, "%lu", (unsigned long) getuid());
182 snprintf(s
, l
, "%s", p
);
186 char *pa_get_host_name(char *s
, size_t l
) {