Blame alsactl/utils.c

Packit Service a9274b
/*
Packit Service a9274b
 *  Advanced Linux Sound Architecture Control Program - Support routines
Packit Service a9274b
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Packit Service a9274b
 *
Packit Service a9274b
 *
Packit Service a9274b
 *   This program is free software; you can redistribute it and/or modify
Packit Service a9274b
 *   it under the terms of the GNU General Public License as published by
Packit Service a9274b
 *   the Free Software Foundation; either version 2 of the License, or
Packit Service a9274b
 *   (at your option) any later version.
Packit Service a9274b
 *
Packit Service a9274b
 *   This program is distributed in the hope that it will be useful,
Packit Service a9274b
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a9274b
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service a9274b
 *   GNU General Public License for more details.
Packit Service a9274b
 *
Packit Service a9274b
 *   You should have received a copy of the GNU General Public License
Packit Service a9274b
 *   along with this program; if not, write to the Free Software
Packit Service a9274b
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit Service a9274b
 *
Packit Service a9274b
 */
Packit Service a9274b
Packit Service a9274b
#include <stdlib.h>
Packit Service a9274b
#include <stdio.h>
Packit Service a9274b
#include <stddef.h>
Packit Service a9274b
#include <unistd.h>
Packit Service a9274b
#include <fcntl.h>
Packit Service a9274b
#include <errno.h>
Packit Service a9274b
#include <ctype.h>
Packit Service a9274b
#include <dirent.h>
Packit Service a9274b
#include <syslog.h>
Packit Service a9274b
#include <sys/stat.h>
Packit Service a9274b
#include <sys/mman.h>
Packit Service a9274b
Packit Service a9274b
#include <alsa/asoundlib.h>
Packit Service a9274b
#include "alsactl.h"
Packit Service a9274b
Packit Service a9274b
int file_map(const char *filename, char **buf, size_t *bufsize)
Packit Service a9274b
{
Packit Service a9274b
	struct stat stats;
Packit Service a9274b
	int fd;
Packit Service a9274b
Packit Service a9274b
	fd = open(filename, O_RDONLY);
Packit Service a9274b
	if (fd < 0) {
Packit Service a9274b
		return -1;
Packit Service a9274b
	}
Packit Service a9274b
Packit Service a9274b
	if (fstat(fd, &stats) < 0) {
Packit Service a9274b
		close(fd);
Packit Service a9274b
		return -1;
Packit Service a9274b
	}
Packit Service a9274b
Packit Service a9274b
	*buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
Packit Service a9274b
	if (*buf == MAP_FAILED) {
Packit Service a9274b
		close(fd);
Packit Service a9274b
		return -1;
Packit Service a9274b
	}
Packit Service a9274b
	*bufsize = stats.st_size;
Packit Service a9274b
Packit Service a9274b
	close(fd);
Packit Service a9274b
Packit Service a9274b
	return 0;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
void file_unmap(void *buf, size_t bufsize)
Packit Service a9274b
{
Packit Service a9274b
	munmap(buf, bufsize);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
size_t line_width(const char *buf, size_t bufsize, size_t pos)
Packit Service a9274b
{
Packit Service a9274b
	int esc = 0;
Packit Service a9274b
	size_t count;
Packit Service a9274b
	
Packit Service a9274b
	for (count = pos; count < bufsize; count++) {
Packit Service a9274b
		if (!esc && buf[count] == '\n')
Packit Service a9274b
			break;
Packit Service a9274b
		esc = buf[count] == '\\';
Packit Service a9274b
	}
Packit Service a9274b
Packit Service a9274b
	return count - pos;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
void initfailed(int cardnumber, const char *reason, int exitcode)
Packit Service a9274b
{
Packit Service a9274b
	int fp;
Packit Service a9274b
	char *str;
Packit Service a9274b
	char sexitcode[16];
Packit Service a9274b
Packit Service a9274b
	if (statefile == NULL)
Packit Service a9274b
		return;
Packit Service a9274b
	if (snd_card_get_name(cardnumber, &str) < 0)
Packit Service a9274b
		return;
Packit Service a9274b
	sprintf(sexitcode, "%i", exitcode);
Packit Service a9274b
	fp = open(statefile, O_WRONLY|O_CREAT|O_APPEND, 0644);
Packit Service a9274b
	write(fp, str, strlen(str));
Packit Service a9274b
	write(fp, ":", 1);
Packit Service a9274b
	write(fp, reason, strlen(reason));
Packit Service a9274b
	write(fp, ":", 1);
Packit Service a9274b
	write(fp, sexitcode, strlen(sexitcode));
Packit Service a9274b
	write(fp, "\n", 1);
Packit Service a9274b
	close(fp);
Packit Service a9274b
	free(str);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static void syslog_(int prio, const char *fcn, long line,
Packit Service a9274b
		    const char *fmt, va_list ap)
Packit Service a9274b
{
Packit Service a9274b
	char buf[1024];
Packit Service a9274b
Packit Service a9274b
	snprintf(buf, sizeof(buf), "%s: %s:%ld: ", command, fcn, line);
Packit Service a9274b
	buf[sizeof(buf)-1] = '\0';
Packit Service a9274b
	vsnprintf(buf + strlen(buf), sizeof(buf)-strlen(buf), fmt, ap);
Packit Service a9274b
	buf[sizeof(buf)-1] = '\0';
Packit Service a9274b
	syslog(prio, "%s", buf);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
void info_(const char *fcn, long line, const char *fmt, ...)
Packit Service a9274b
{
Packit Service a9274b
	va_list ap;
Packit Service a9274b
Packit Service a9274b
	va_start(ap, fmt);
Packit Service a9274b
	if (use_syslog) {
Packit Service a9274b
		syslog_(LOG_INFO, fcn, line, fmt, ap);
Packit Service a9274b
	} else {
Packit Service a9274b
		fprintf(stdout, "%s: %s:%ld: ", command, fcn, line);
Packit Service a9274b
		vfprintf(stdout, fmt, ap);
Packit Service a9274b
		putc('\n', stdout);
Packit Service a9274b
	}
Packit Service a9274b
	va_end(ap);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
void error_(const char *fcn, long line, const char *fmt, ...)
Packit Service a9274b
{
Packit Service a9274b
	va_list ap;
Packit Service a9274b
Packit Service a9274b
	va_start(ap, fmt);
Packit Service a9274b
	if (use_syslog) {
Packit Service a9274b
		syslog_(LOG_ERR, fcn, line, fmt, ap);
Packit Service a9274b
	} else {
Packit Service a9274b
		fprintf(stderr, "%s: %s:%ld: ", command, fcn, line);
Packit Service a9274b
		vfprintf(stderr, fmt, ap);
Packit Service a9274b
		putc('\n', stderr);
Packit Service a9274b
	}
Packit Service a9274b
	va_end(ap);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
void cerror_(const char *fcn, long line, int cond, const char *fmt, ...)
Packit Service a9274b
{
Packit Service a9274b
	va_list ap;
Packit Service a9274b
Packit Service a9274b
	if (!cond && !debugflag)
Packit Service a9274b
		return;
Packit Service a9274b
	va_start(ap, fmt);
Packit Service a9274b
	if (use_syslog) {
Packit Service a9274b
		syslog_(LOG_ERR, fcn, line, fmt, ap);
Packit Service a9274b
	} else {
Packit Service a9274b
		fprintf(stderr, "%s: %s:%ld: ", command, fcn, line);
Packit Service a9274b
		vfprintf(stderr, fmt, ap);
Packit Service a9274b
		putc('\n', stderr);
Packit Service a9274b
	}
Packit Service a9274b
	va_end(ap);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
void dbg_(const char *fcn, long line, const char *fmt, ...)
Packit Service a9274b
{
Packit Service a9274b
	va_list ap;
Packit Service a9274b
Packit Service a9274b
	if (!debugflag)
Packit Service a9274b
		return;
Packit Service a9274b
	va_start(ap, fmt);
Packit Service a9274b
	if (use_syslog) {
Packit Service a9274b
		syslog_(LOG_DEBUG, fcn, line, fmt, ap);
Packit Service a9274b
	} else {
Packit Service a9274b
		fprintf(stderr, "%s: %s:%ld: ", command, fcn, line);
Packit Service a9274b
		vfprintf(stderr, fmt, ap);
Packit Service a9274b
		putc('\n', stderr);
Packit Service a9274b
	}
Packit Service a9274b
	va_end(ap);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
void error_handler(const char *file, int line, const char *function, int err, const char *fmt, ...)
Packit Service a9274b
{
Packit Service a9274b
	char buf[2048];
Packit Service a9274b
	va_list arg;
Packit Service a9274b
Packit Service a9274b
	va_start(arg, fmt);
Packit Service a9274b
	vsnprintf(buf, sizeof(buf), fmt, arg);
Packit Service a9274b
	va_end(arg);
Packit Service a9274b
	if (use_syslog)
Packit Service a9274b
		syslog(LOG_ERR, "alsa-lib %s:%i:(%s) %s%s%s\n", file, line, function,
Packit Service a9274b
				buf, err ? ": " : "", err ? snd_strerror(err) : "");
Packit Service a9274b
	else
Packit Service a9274b
		fprintf(stderr, "alsa-lib %s:%i:(%s) %s%s%s\n", file, line, function,
Packit Service a9274b
				buf, err ? ": " : "", err ? snd_strerror(err) : "");
Packit Service a9274b
}