2 * Advanced Linux Sound Architecture Control Program - Support routines
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
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.
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.
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
34 #include <alsa/asoundlib.h>
37 int file_map(const char *filename, char **buf, size_t *bufsize)
42 fd = open(filename, O_RDONLY);
47 if (fstat(fd, &stats) < 0) {
52 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
53 if (*buf == MAP_FAILED) {
57 *bufsize = stats.st_size;
64 void file_unmap(void *buf, size_t bufsize)
69 size_t line_width(const char *buf, size_t bufsize, size_t pos)
74 for (count = pos; count < bufsize; count++) {
75 if (!esc && buf[count] == '\n')
77 esc = buf[count] == '\\';
83 void initfailed(int cardnumber, const char *reason, int exitcode)
89 if (statefile == NULL)
91 if (snd_card_get_name(cardnumber, &str) < 0)
93 sprintf(sexitcode, "%i", exitcode);
94 fp = open(statefile, O_WRONLY|O_CREAT|O_APPEND, 0644);
95 write(fp, str, strlen(str));
97 write(fp, reason, strlen(reason));
99 write(fp, sexitcode, strlen(sexitcode));
105 static void syslog_(int prio, const char *fcn, long line,
106 const char *fmt, va_list ap)
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);
117 void info_(const char *fcn, long line, const char *fmt, ...)
123 syslog_(LOG_INFO, fcn, line, fmt, ap);
125 fprintf(stdout, "%s: %s:%ld: ", command, fcn, line);
126 vfprintf(stdout, fmt, ap);
132 void error_(const char *fcn, long line, const char *fmt, ...)
138 syslog_(LOG_ERR, fcn, line, fmt, ap);
140 fprintf(stderr, "%s: %s:%ld: ", command, fcn, line);
141 vfprintf(stderr, fmt, ap);
147 void cerror_(const char *fcn, long line, int cond, const char *fmt, ...)
151 if (!cond && !debugflag)
155 syslog_(LOG_ERR, fcn, line, fmt, ap);
157 fprintf(stderr, "%s: %s:%ld: ", command, fcn, line);
158 vfprintf(stderr, fmt, ap);
164 void dbg_(const char *fcn, long line, const char *fmt, ...)
172 syslog_(LOG_DEBUG, fcn, line, fmt, ap);
174 fprintf(stderr, "%s: %s:%ld: ", command, fcn, line);
175 vfprintf(stderr, fmt, ap);