tarina

git clone https://git.tarina.org/tarina
Log | Files | Refs | README | LICENSE

utils.c (3920B)


      1 /*
      2  *  Advanced Linux Sound Architecture Control Program - Support routines
      3  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
      4  *
      5  *
      6  *   This program is free software; you can redistribute it and/or modify
      7  *   it under the terms of the GNU General Public License as published by
      8  *   the Free Software Foundation; either version 2 of the License, or
      9  *   (at your option) any later version.
     10  *
     11  *   This program is distributed in the hope that it will be useful,
     12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14  *   GNU General Public License for more details.
     15  *
     16  *   You should have received a copy of the GNU General Public License
     17  *   along with this program; if not, write to the Free Software
     18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
     19  *
     20  */
     21 
     22 #include <stdlib.h>
     23 #include <stdio.h>
     24 #include <stddef.h>
     25 #include <unistd.h>
     26 #include <fcntl.h>
     27 #include <errno.h>
     28 #include <ctype.h>
     29 #include <dirent.h>
     30 #include <syslog.h>
     31 #include <sys/stat.h>
     32 #include <sys/mman.h>
     33 
     34 #include <alsa/asoundlib.h>
     35 #include "alsactl.h"
     36 
     37 int file_map(const char *filename, char **buf, size_t *bufsize)
     38 {
     39 	struct stat stats;
     40 	int fd;
     41 
     42 	fd = open(filename, O_RDONLY);
     43 	if (fd < 0) {
     44 		return -1;
     45 	}
     46 
     47 	if (fstat(fd, &stats) < 0) {
     48 		close(fd);
     49 		return -1;
     50 	}
     51 
     52 	*buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
     53 	if (*buf == MAP_FAILED) {
     54 		close(fd);
     55 		return -1;
     56 	}
     57 	*bufsize = stats.st_size;
     58 
     59 	close(fd);
     60 
     61 	return 0;
     62 }
     63 
     64 void file_unmap(void *buf, size_t bufsize)
     65 {
     66 	munmap(buf, bufsize);
     67 }
     68 
     69 size_t line_width(const char *buf, size_t bufsize, size_t pos)
     70 {
     71 	int esc = 0;
     72 	size_t count;
     73 	
     74 	for (count = pos; count < bufsize; count++) {
     75 		if (!esc && buf[count] == '\n')
     76 			break;
     77 		esc = buf[count] == '\\';
     78 	}
     79 
     80 	return count - pos;
     81 }
     82 
     83 void initfailed(int cardnumber, const char *reason, int exitcode)
     84 {
     85 	int fp;
     86 	char *str;
     87 	char sexitcode[16];
     88 
     89 	if (statefile == NULL)
     90 		return;
     91 	if (snd_card_get_name(cardnumber, &str) < 0)
     92 		return;
     93 	sprintf(sexitcode, "%i", exitcode);
     94 	fp = open(statefile, O_WRONLY|O_CREAT|O_APPEND, 0644);
     95 	write(fp, str, strlen(str));
     96 	write(fp, ":", 1);
     97 	write(fp, reason, strlen(reason));
     98 	write(fp, ":", 1);
     99 	write(fp, sexitcode, strlen(sexitcode));
    100 	write(fp, "\n", 1);
    101 	close(fp);
    102 	free(str);
    103 }
    104 
    105 static void syslog_(int prio, const char *fcn, long line,
    106 		    const char *fmt, va_list ap)
    107 {
    108 	char buf[1024];
    109 
    110 	snprintf(buf, sizeof(buf), "%s: %s:%ld", command, fcn, line);
    111 	buf[sizeof(buf)-1] = '\0';
    112 	vsnprintf(buf + strlen(buf), sizeof(buf)-strlen(buf), fmt, ap);
    113 	buf[sizeof(buf)-1] = '\0';
    114 	syslog(LOG_INFO, "%s", buf);
    115 }
    116 
    117 void info_(const char *fcn, long line, const char *fmt, ...)
    118 {
    119 	va_list ap;
    120 
    121 	va_start(ap, fmt);
    122 	if (use_syslog) {
    123 		syslog_(LOG_INFO, fcn, line, fmt, ap);
    124 	} else {
    125 		fprintf(stdout, "%s: %s:%ld: ", command, fcn, line);
    126 		vfprintf(stdout, fmt, ap);
    127 		putc('\n', stdout);
    128 	}
    129 	va_end(ap);
    130 }
    131 
    132 void error_(const char *fcn, long line, const char *fmt, ...)
    133 {
    134 	va_list ap;
    135 
    136 	va_start(ap, fmt);
    137 	if (use_syslog) {
    138 		syslog_(LOG_ERR, fcn, line, fmt, ap);
    139 	} else {
    140 		fprintf(stderr, "%s: %s:%ld: ", command, fcn, line);
    141 		vfprintf(stderr, fmt, ap);
    142 		putc('\n', stderr);
    143 	}
    144 	va_end(ap);
    145 }
    146 
    147 void cerror_(const char *fcn, long line, int cond, const char *fmt, ...)
    148 {
    149 	va_list ap;
    150 
    151 	if (!cond && !debugflag)
    152 		return;
    153 	va_start(ap, fmt);
    154 	if (use_syslog) {
    155 		syslog_(LOG_ERR, fcn, line, fmt, ap);
    156 	} else {
    157 		fprintf(stderr, "%s: %s:%ld: ", command, fcn, line);
    158 		vfprintf(stderr, fmt, ap);
    159 		putc('\n', stderr);
    160 	}
    161 	va_end(ap);
    162 }
    163 
    164 void dbg_(const char *fcn, long line, const char *fmt, ...)
    165 {
    166 	va_list ap;
    167 
    168 	if (!debugflag)
    169 		return;
    170 	va_start(ap, fmt);
    171 	if (use_syslog) {
    172 		syslog_(LOG_DEBUG, fcn, line, fmt, ap);
    173 	} else {
    174 		fprintf(stderr, "%s: %s:%ld: ", command, fcn, line);
    175 		vfprintf(stderr, fmt, ap);
    176 		putc('\n', stderr);
    177 	}
    178 	va_end(ap);
    179 }