Blame envy24control/profiles.c

Packit 427e91
/*
Packit 427e91
 *  Advanced Linux Sound Architecture part of envy24control
Packit 427e91
 *  handle profiles for envy24control using alsactl
Packit 427e91
 * 
Packit 427e91
 *  Copyright (c) by Dirk Kalis <dirk.kalis@t-online.de>
Packit 427e91
 *
Packit 427e91
 *
Packit 427e91
 *   This program is free software; you can redistribute it and/or modify
Packit 427e91
 *   it under the terms of the GNU General Public License as published by
Packit 427e91
 *   the Free Software Foundation; either version 2 of the License, or
Packit 427e91
 *   (at your option) any later version.
Packit 427e91
 *
Packit 427e91
 *   This program is distributed in the hope that it will be useful,
Packit 427e91
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 427e91
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 427e91
 *   GNU General Public License for more details.
Packit 427e91
 *
Packit 427e91
 *   You should have received a copy of the GNU General Public License
Packit 427e91
 *   along with this program; if not, write to the Free Software
Packit 427e91
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 427e91
 *
Packit 427e91
 */
Packit 427e91
Packit 427e91
#define __PROFILES_C__
Packit 427e91
#include "envy24control.h"
Packit 427e91
#undef __PROFILES_C__
Packit 427e91
Packit 427e91
/* include string search function */
Packit 427e91
#include "strstr_icase_blank.c"
Packit 427e91
Packit 427e91
/* include forking process function */
Packit 427e91
#include "new_process.c"
Packit 427e91
Packit 427e91
/* replace tilde with home directory */
Packit 427e91
static char filename_without_tilde[MAX_FILE_NAME_LENGTH];
Packit 427e91
Packit 427e91
static char profile_name[PROFILE_NAME_FIELD_LENGTH];
Packit 427e91
Packit 427e91
/*
Packit 427e91
 * check the environment and use the set variables
Packit 427e91
 */
Packit 427e91
char *check_environment(const char * const env_var, char * const var)
Packit 427e91
{
Packit 427e91
	if ( (char *)getenv(env_var) == NULL )
Packit 427e91
		return var;
Packit 427e91
	return (char *)getenv(env_var);
Packit 427e91
}
Packit 427e91
Packit 427e91
/* replace tilde with home directory by checking HOME environment variable */
Packit 427e91
void subst_tilde_in_filename(char * const filename)
Packit 427e91
{
Packit 427e91
	char new_filename[MAX_FILE_NAME_LENGTH];
Packit 427e91
	char *pos_after_tilde;
Packit 427e91
Packit 427e91
	if ((pos_after_tilde = strchr(filename, '~')) != NULL) {
Packit 427e91
		pos_after_tilde++;
Packit 427e91
		strncpy(new_filename, getenv("HOME"), MAX_FILE_NAME_LENGTH);
Packit 427e91
		strncpy(new_filename + strlen(new_filename), pos_after_tilde, MAX_FILE_NAME_LENGTH - strlen(new_filename));
Packit 427e91
		new_filename[MAX_FILE_NAME_LENGTH - 1] = '\0';
Packit 427e91
		strncpy(filename, new_filename, MAX_FILE_NAME_LENGTH);
Packit 427e91
	}
Packit 427e91
}
Packit 427e91
Packit 427e91
Packit 427e91
int which_cfgfile(char ** const cfgfile)
Packit 427e91
{
Packit 427e91
	int res;
Packit 427e91
	FILE *inputFile;
Packit 427e91
Packit 427e91
	strncpy(filename_without_tilde, *cfgfile, MAX_FILE_NAME_LENGTH);
Packit 427e91
	filename_without_tilde[MAX_FILE_NAME_LENGTH - 1] = '\0';
Packit 427e91
	subst_tilde_in_filename(filename_without_tilde);
Packit 427e91
	if ((inputFile = fopen(filename_without_tilde, "r")) == NULL) {
Packit 427e91
		if (strcmp(*cfgfile, DEFAULT_PROFILERC) &&
Packit 427e91
		    strcmp(*cfgfile, SYS_PROFILERC)) {
Packit 427e91
			res = -ENOENT;
Packit 427e91
		} else if (!strcmp(*cfgfile, DEFAULT_PROFILERC) &&
Packit 427e91
			   (inputFile = fopen(SYS_PROFILERC, "r")) == NULL) {
Packit 427e91
			res = -ENOENT;
Packit 427e91
		} else {
Packit 427e91
			fclose(inputFile);
Packit 427e91
			*cfgfile = SYS_PROFILERC;
Packit 427e91
			res = EXIT_SUCCESS;
Packit 427e91
		}
Packit 427e91
	} else {
Packit 427e91
		fclose(inputFile);
Packit 427e91
		*cfgfile = filename_without_tilde;
Packit 427e91
		res = EXIT_SUCCESS;
Packit 427e91
	}
Packit 427e91
	return res;
Packit 427e91
}
Packit 427e91
Packit 427e91
int get_file_size(const char * const filename)
Packit 427e91
{
Packit 427e91
	struct stat file_status;
Packit 427e91
Packit 427e91
	strncpy(filename_without_tilde, filename, MAX_FILE_NAME_LENGTH);
Packit 427e91
	filename_without_tilde[MAX_FILE_NAME_LENGTH - 1] = '\0';
Packit 427e91
	subst_tilde_in_filename(filename_without_tilde);
Packit 427e91
	if (stat(filename_without_tilde, &file_status) < 0) {
Packit 427e91
		fprintf(stderr, "Cannot find file '%s'.\n", filename);
Packit 427e91
		return -errno;
Packit 427e91
	}
Packit 427e91
Packit 427e91
	return (int)file_status.st_size;
Packit 427e91
}
Packit 427e91
Packit 427e91
int read_profiles_in_buffer(const char * const cfgfile, void * const buffer, const size_t max_length)
Packit 427e91
{
Packit 427e91
	int res;
Packit 427e91
	int inputFile;
Packit 427e91
Packit 427e91
	if ((inputFile = open(cfgfile, O_RDONLY)) < 0) {
Packit 427e91
		fprintf(stderr, "warning: can't open profiles file '%s' for reading.\n", cfgfile);
Packit 427e91
		return -errno;
Packit 427e91
	}
Packit 427e91
	res = read(inputFile, buffer, max_length);
Packit 427e91
	close(inputFile);
Packit 427e91
	*(char *)(buffer + max_length - 1) = '\0';
Packit 427e91
	if (res == max_length) {
Packit 427e91
		fprintf(stderr, "warning: profiles file '%s' has maximum size reached.\n", cfgfile);
Packit 427e91
		fprintf(stderr, "\tThe defined value for MAX_PROFILE_SIZE must be increased in 'profiles.h'.\n");
Packit 427e91
		fprintf(stderr, "\tThen you must recompile the '%s' tool.\n", PROGRAM_NAME);
Packit 427e91
		fprintf(stderr, "\tThe current value is %d.\n", MAX_PROFILE_SIZE);
Packit 427e91
	}
Packit 427e91
Packit 427e91
	return res;
Packit 427e91
}
Packit 427e91
Packit 427e91
/*
Packit 427e91
 * write buffer with max_length to cfgfile
Packit 427e91
 * with introduction if with_intro != 0
Packit 427e91
 */
Packit 427e91
int write_profiles_from_buffer(const char * const cfgfile, const void * const buffer, const size_t max_length, const int with_intro)
Packit 427e91
{
Packit 427e91
	int res, length;
Packit 427e91
	time_t date_time[MAX_NUM_STR_LENGTH];
Packit 427e91
	char introduction_part[MAX_SEARCH_FIELD_LENGTH];
Packit 427e91
	int outputFile;
Packit 427e91
Packit 427e91
	if ((outputFile = open(cfgfile, O_WRONLY | O_CREAT | O_TRUNC | 0400000 /* O_NOFOLLOW */, FILE_CREA_MODE)) < 0) {
Packit 427e91
		fprintf(stderr, "warning: can't open profiles file '%s' for writing.\n", cfgfile);
Packit 427e91
		return -errno;
Packit 427e91
	}
Packit 427e91
	length = max_length > strlen(buffer) ? strlen(buffer) : max_length;
Packit 427e91
	time(date_time);
Packit 427e91
	snprintf(introduction_part, MAX_SEARCH_FIELD_LENGTH, "%s'%s'%s%s%s'%s'%s%s%s%s%s%s%s%s%s\n", \
Packit 427e91
							"#\n" \
Packit 427e91
				   			"# This file is automatically generated by ", PROGRAM_NAME, " at ", \
Packit 427e91
											(char *) asctime(localtime(date_time)), \
Packit 427e91
							"# Do not edit this file manually. This file will be permanently overwritten.\n" \
Packit 427e91
							"# Use ", PROGRAM_NAME, " to modify and store settings.\n" \
Packit 427e91
							"#\n" \
Packit 427e91
							"# File-Structure:\n" \
Packit 427e91
							"# ", PROFILE_HEADER_TEMPL, "\t\t- profile header\n" \
Packit 427e91
							"#\t", CARD_HEADER_TEMPL, "\t- card header\n" \
Packit 427e91
							"#\t", PROFILE_NAME_TEMPL, "\t\t- profile name - optional\n" \
Packit 427e91
							"#\t\t***********************************\n" \
Packit 427e91
							"#\t\t***** ALSA code for this card *****\n" \
Packit 427e91
							"#\t\t***********************************\n" \
Packit 427e91
							"#\t", CARD_FOOTER_TEMPL, "\t- card footer\n" \
Packit 427e91
							"#\n");
Packit 427e91
Packit 427e91
	introduction_part[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
	if (with_intro)
Packit 427e91
		res = write(outputFile, introduction_part, strlen(introduction_part));
Packit 427e91
	res = write(outputFile, buffer, length);
Packit 427e91
	close(outputFile);
Packit 427e91
	if (res < 0) {
Packit 427e91
		fprintf(stderr, "warning: can't write profiles file '%s'.\n", cfgfile);
Packit 427e91
	}
Packit 427e91
Packit 427e91
	return res;
Packit 427e91
}
Packit 427e91
Packit 427e91
int create_dir_from_filename(const char * const filename)
Packit 427e91
{
Packit 427e91
	int res;
Packit 427e91
	char pathname[MAX_FILE_NAME_LENGTH];
Packit 427e91
	char * parameter[MAX_PARAM];
Packit 427e91
	char *mkdir;
Packit 427e91
Packit 427e91
	mkdir = check_environment("MKDIR_PROG", MKDIR);
Packit 427e91
Packit 427e91
	strncpy(pathname, filename, MAX_FILE_NAME_LENGTH);
Packit 427e91
	pathname[MAX_FILE_NAME_LENGTH - 1] = '\0';
Packit 427e91
Packit 427e91
	*strrchr(pathname, '/') = '\0';
Packit 427e91
	subst_tilde_in_filename(pathname);
Packit 427e91
	
Packit 427e91
	parameter[0] = mkdir;
Packit 427e91
	parameter[1] = "-p";
Packit 427e91
	parameter[2] = "-m";
Packit 427e91
	parameter[3] = DIR_CREA_MODE;
Packit 427e91
	parameter[4] = pathname;
Packit 427e91
	parameter[5] = NULL;
Packit 427e91
	
Packit 427e91
	res = new_process(parameter);
Packit 427e91
	
Packit 427e91
	return res;
Packit 427e91
}
Packit 427e91
Packit 427e91
int compose_search_string(char * const search_string, const char * const templ, const char * const value, const char place_holder, const int max_length)
Packit 427e91
{
Packit 427e91
	int res;
Packit 427e91
	char *pos_place_holder;
Packit 427e91
Packit 427e91
	memset(search_string, '\0', max_length);
Packit 427e91
	strncpy(search_string, templ, max_length);
Packit 427e91
	search_string[max_length - 1] = '\0';
Packit 427e91
	if ((pos_place_holder = strchr(search_string, place_holder)) == NULL) {
Packit 427e91
		res = NOTFOUND;
Packit 427e91
		return res;
Packit 427e91
	}
Packit 427e91
	strncpy(pos_place_holder, value, max_length - strlen(search_string));
Packit 427e91
	strncpy(pos_place_holder + strlen(value), \
Packit 427e91
			strchr(templ, place_holder) \
Packit 427e91
				+ sizeof(char), \
Packit 427e91
			max_length - strlen(search_string));
Packit 427e91
	search_string[max_length - 1] = '\0';
Packit 427e91
	res = EXIT_SUCCESS;
Packit 427e91
	
Packit 427e91
	return res;
Packit 427e91
}
Packit 427e91
Packit 427e91
/* search backwards first newline - start of line is position after newline */
Packit 427e91
int get_start_of_line(const char * const buffer, const int offset)
Packit 427e91
{
Packit 427e91
	int begin_of_line;
Packit 427e91
Packit 427e91
	for (begin_of_line = offset; begin_of_line >= 0; begin_of_line--)
Packit 427e91
	{
Packit 427e91
		if (buffer[begin_of_line] == '\n')
Packit 427e91
			break;
Packit 427e91
	}
Packit 427e91
	begin_of_line++;
Packit 427e91
Packit 427e91
	return begin_of_line;
Packit 427e91
}
Packit 427e91
Packit 427e91
/*
Packit 427e91
 * search tokens sequential for the furthest token backwards from section_end
Packit 427e91
 * tokens should be specified from nearest token to the furthest
Packit 427e91
 */
Packit 427e91
int get_begin_of_section(const char * const buffer, char * const section_tokens, const char * const token_sep, const int section_end)
Packit 427e91
{
Packit 427e91
	char *search_token;
Packit 427e91
	int pos_start_of_section, pos_end_of_section, pos_end_of_search;
Packit 427e91
Packit 427e91
	search_token = strtok(section_tokens, token_sep);
Packit 427e91
Packit 427e91
	pos_end_of_search = section_end;
Packit 427e91
	do
Packit 427e91
	{
Packit 427e91
		pos_start_of_section = 0;
Packit 427e91
		pos_end_of_section = 0;
Packit 427e91
		while ((pos_start_of_section = strstr_icase_blank(buffer + pos_end_of_section, search_token)) < pos_end_of_search - pos_end_of_section)
Packit 427e91
		{
Packit 427e91
			if (pos_start_of_section < 0)
Packit 427e91
				break;
Packit 427e91
			pos_end_of_section += pos_start_of_section;
Packit 427e91
			pos_end_of_section++;
Packit 427e91
		}
Packit 427e91
		pos_end_of_section--;
Packit 427e91
		pos_start_of_section = pos_end_of_section;
Packit 427e91
		pos_end_of_search = pos_end_of_section;
Packit 427e91
		if (pos_start_of_section < 0) {
Packit 427e91
			pos_start_of_section = NOTFOUND;
Packit 427e91
			// fprintf(stderr, "Begin of section for '%s' can't be found\n", search_token);
Packit 427e91
			return pos_start_of_section;
Packit 427e91
		}
Packit 427e91
	} while ((search_token = strtok(NULL, token_sep)) != NULL);
Packit 427e91
Packit 427e91
	return pos_start_of_section;
Packit 427e91
}
Packit 427e91
Packit 427e91
/*
Packit 427e91
 * search tokens sequential for the nearest token from section_begin
Packit 427e91
 * tokens should be specified from furthest token to the nearest
Packit 427e91
 */
Packit 427e91
int get_end_of_section(const char * const buffer, char * const section_tokens, const char * const token_sep, const int section_begin)
Packit 427e91
{
Packit 427e91
	char *search_token;
Packit 427e91
	int pos_end_of_section, pos_end_of_search;
Packit 427e91
Packit 427e91
	search_token = strtok(section_tokens, token_sep);
Packit 427e91
Packit 427e91
	pos_end_of_section = strlen(buffer);
Packit 427e91
	do
Packit 427e91
	{
Packit 427e91
		pos_end_of_search = strstr_icase_blank(buffer + section_begin, search_token);
Packit 427e91
		if (!((pos_end_of_search < 0) || (pos_end_of_search > pos_end_of_section))) {
Packit 427e91
			pos_end_of_section = pos_end_of_search;
Packit 427e91
		}
Packit 427e91
	} while ((search_token = strtok(NULL, token_sep)) != NULL);
Packit 427e91
Packit 427e91
	return pos_end_of_section == strlen(buffer) ? pos_end_of_section : pos_end_of_section + section_begin;
Packit 427e91
}
Packit 427e91
Packit 427e91
int get_profile_begin(const char * const buffer, const int profile_number)
Packit 427e91
{
Packit 427e91
	char header[MAX_SEARCH_FIELD_LENGTH], profile_or_card_number_as_str[MAX_NUM_STR_LENGTH];
Packit 427e91
	char place_holder;
Packit 427e91
Packit 427e91
	/* compose profile header */
Packit 427e91
	place_holder = PLACE_HOLDER_NUM;
Packit 427e91
	snprintf(profile_or_card_number_as_str, MAX_NUM_STR_LENGTH, "%d", profile_number);
Packit 427e91
	profile_or_card_number_as_str[MAX_NUM_STR_LENGTH - 1] = '\0';
Packit 427e91
	compose_search_string(header, PROFILE_HEADER_TEMPL, profile_or_card_number_as_str, place_holder, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
	header[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
	return strstr_icase_blank(buffer, header);
Packit 427e91
}
Packit 427e91
Packit 427e91
int get_profile_end(const char * const buffer, const int profile_number)
Packit 427e91
{
Packit 427e91
	char header[MAX_SEARCH_FIELD_LENGTH];
Packit 427e91
	char place_holder;
Packit 427e91
	int pos_profile_begin;
Packit 427e91
Packit 427e91
	if ((pos_profile_begin = get_profile_begin(buffer, profile_number)) < 0)
Packit 427e91
		return pos_profile_begin;
Packit 427e91
	place_holder = PLACE_HOLDER_NUM;
Packit 427e91
	strncpy(header, PROFILE_HEADER_TEMPL, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
	header[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
	*strchr(header, place_holder) = '\0';
Packit 427e91
	return get_end_of_section(buffer, header, TOKEN_SEP, pos_profile_begin + sizeof(char));
Packit 427e91
}
Packit 427e91
Packit 427e91
int get_card_begin(const char * const buffer, const int profile_number, const int card_number)
Packit 427e91
{
Packit 427e91
	char header[MAX_SEARCH_FIELD_LENGTH], profile_or_card_number_as_str[MAX_NUM_STR_LENGTH];
Packit 427e91
	char place_holder;
Packit 427e91
	int pos_profile_begin, pos_profile_end, pos_card_begin;
Packit 427e91
Packit 427e91
	if ((pos_profile_begin = get_profile_begin(buffer, profile_number)) < 0)
Packit 427e91
		return pos_profile_begin;
Packit 427e91
	pos_profile_end = get_profile_end(buffer, profile_number);
Packit 427e91
Packit 427e91
	/* compose card header */
Packit 427e91
	place_holder = PLACE_HOLDER_NUM;
Packit 427e91
	snprintf(profile_or_card_number_as_str, MAX_NUM_STR_LENGTH, "%d", card_number);
Packit 427e91
	profile_or_card_number_as_str[MAX_NUM_STR_LENGTH - 1] = '\0';
Packit 427e91
	compose_search_string(header, CARD_HEADER_TEMPL, profile_or_card_number_as_str, place_holder, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
	header[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
	if ((pos_card_begin = strstr_icase_blank(buffer + pos_profile_begin, header)) < 0)
Packit 427e91
		return pos_card_begin;
Packit 427e91
	if ((pos_card_begin += pos_profile_begin) > pos_profile_end)
Packit 427e91
		return NOTFOUND;
Packit 427e91
	return pos_card_begin;
Packit 427e91
}
Packit 427e91
Packit 427e91
int get_card_end(const char * const buffer, const int profile_number, const int card_number)
Packit 427e91
{
Packit 427e91
	char header[MAX_SEARCH_FIELD_LENGTH], profile_or_card_number_as_str[MAX_NUM_STR_LENGTH];
Packit 427e91
	char place_holder;
Packit 427e91
	int pos_card_begin;
Packit 427e91
Packit 427e91
	if ((pos_card_begin = get_card_begin(buffer, profile_number, card_number)) < 0)
Packit 427e91
		return pos_card_begin;
Packit 427e91
	/* searching "[ profile | < card | < /card # >" */
Packit 427e91
	/* add "[ profile |" to search_field */
Packit 427e91
	place_holder = PLACE_HOLDER_NUM;
Packit 427e91
	snprintf(profile_or_card_number_as_str, MAX_NUM_STR_LENGTH, "%d", card_number);
Packit 427e91
	profile_or_card_number_as_str[MAX_NUM_STR_LENGTH - 1] = '\0';
Packit 427e91
	strncpy(header, PROFILE_HEADER_TEMPL, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
	*strchr(header, place_holder) = '\0';
Packit 427e91
	strncpy(header + strlen(header), TOKEN_SEP, MAX_SEARCH_FIELD_LENGTH - strlen(header));
Packit 427e91
	header[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
	/* add "< card |" to header */
Packit 427e91
	strncpy(header + strlen(header), CARD_HEADER_TEMPL, MAX_SEARCH_FIELD_LENGTH - strlen(header));
Packit 427e91
	*strchr(header, place_holder) = '\0';
Packit 427e91
	strncpy(header + strlen(header), TOKEN_SEP, MAX_SEARCH_FIELD_LENGTH - strlen(header));
Packit 427e91
	header[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
	/* add "< /card # >" to header */
Packit 427e91
	compose_search_string(header + strlen(header), CARD_FOOTER_TEMPL, profile_or_card_number_as_str, place_holder, MAX_SEARCH_FIELD_LENGTH - strlen(header));
Packit 427e91
	header[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
	return get_end_of_section(buffer, header, TOKEN_SEP, pos_card_begin + sizeof(char));
Packit 427e91
}
Packit 427e91
Packit 427e91
int get_pos_for_next_card(const char * const buffer, const int profile_number, const int card_number)
Packit 427e91
{
Packit 427e91
	char header[MAX_SEARCH_FIELD_LENGTH], profile_or_card_number_as_str[MAX_NUM_STR_LENGTH];
Packit 427e91
	char place_holder;
Packit 427e91
	int pos_next_card;
Packit 427e91
Packit 427e91
	if ((pos_next_card = get_card_end(buffer, profile_number, card_number)) < 0)
Packit 427e91
		return pos_next_card;
Packit 427e91
	/* add "< /card # >" to header */
Packit 427e91
	place_holder = PLACE_HOLDER_NUM;
Packit 427e91
	snprintf(profile_or_card_number_as_str, MAX_NUM_STR_LENGTH, "%d", card_number);
Packit 427e91
	profile_or_card_number_as_str[MAX_NUM_STR_LENGTH - 1] = '\0';
Packit 427e91
	compose_search_string(header, CARD_FOOTER_TEMPL, profile_or_card_number_as_str, place_holder, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
	header[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
	if (strstr_icase_blank(buffer + pos_next_card, header) == 0) {
Packit 427e91
		while((buffer[pos_next_card] != '\n') && (buffer[pos_next_card] != '\0'))
Packit 427e91
			pos_next_card++;
Packit 427e91
		if (buffer[pos_next_card] == '\n')
Packit 427e91
			pos_next_card++;
Packit 427e91
	}
Packit 427e91
	return pos_next_card;
Packit 427e91
}
Packit 427e91
Packit 427e91
int get_number_from_header(const char * const header_string)
Packit 427e91
{
Packit 427e91
	char string[MAX_SEARCH_FIELD_LENGTH];
Packit 427e91
	char number_string[MAX_NUM_STR_LENGTH];
Packit 427e91
	char *pos_number;
Packit 427e91
Packit 427e91
	strncpy(string, header_string, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
	string[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
Packit 427e91
	if ((pos_number = strpbrk(string, "0123456789")) == NULL) {
Packit 427e91
		return -EINVAL;
Packit 427e91
	} else {
Packit 427e91
		strncpy(number_string, pos_number, MAX_NUM_STR_LENGTH);
Packit 427e91
		number_string[MAX_NUM_STR_LENGTH - 1] = '\0';
Packit 427e91
	}
Packit 427e91
Packit 427e91
	return atoi(number_string);
Packit 427e91
}
Packit 427e91
Packit 427e91
char *get_profile_name_from_header(const char * const header_string)
Packit 427e91
{
Packit 427e91
	char string[MAX_SEARCH_FIELD_LENGTH];
Packit 427e91
	char header_templ[MAX_SEARCH_FIELD_LENGTH];
Packit 427e91
	char left_name_border;
Packit 427e91
Packit 427e91
	memset(profile_name, '\0', PROFILE_NAME_FIELD_LENGTH);
Packit 427e91
	strncpy(string, header_string, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
	string[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
	strncpy(header_templ, PROFILE_NAME_TEMPL, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
	header_templ[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
	string[strstr_icase_blank(string, strchr(header_templ, PLACE_HOLDER_STR) + sizeof(char))] = '\0';
Packit 427e91
	left_name_border = *(strchr(header_templ, PLACE_HOLDER_STR) - sizeof(char));
Packit 427e91
	strncpy(profile_name, strchr(string + sizeof(char), left_name_border) + sizeof(char), PROFILE_NAME_FIELD_LENGTH);
Packit 427e91
	profile_name[PROFILE_NAME_FIELD_LENGTH - 1] = '\0';
Packit 427e91
Packit 427e91
	return profile_name;
Packit 427e91
}
Packit 427e91
Packit 427e91
/* search max card number in profile */
Packit 427e91
int get_max_card_number_in_profile(const char * const buffer, const int profile_number)
Packit 427e91
{
Packit 427e91
	char header[MAX_SEARCH_FIELD_LENGTH];
Packit 427e91
	char place_holder;
Packit 427e91
	int pos_card_next, pos_profile_end, pos_card_begin, card_number, card_number_max;
Packit 427e91
Packit 427e91
	place_holder = PLACE_HOLDER_NUM;
Packit 427e91
	card_number_max = NOTFOUND;
Packit 427e91
	strncpy(header, CARD_HEADER_TEMPL, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
	header[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
	*strchr(header, place_holder) = '\0';
Packit 427e91
	pos_card_next = get_profile_begin(buffer, profile_number);
Packit 427e91
	pos_profile_end = get_profile_end(buffer, profile_number);
Packit 427e91
	while ((pos_card_begin = strstr_icase_blank(buffer + pos_card_next, header)) >= 0)
Packit 427e91
	{
Packit 427e91
		if ((pos_card_begin += pos_card_next) > pos_profile_end)
Packit 427e91
			break;
Packit 427e91
		pos_card_next = pos_card_begin + sizeof(char);
Packit 427e91
		card_number = get_number_from_header(buffer + pos_card_begin);
Packit 427e91
		if (card_number > card_number_max)
Packit 427e91
			card_number_max = card_number;
Packit 427e91
	}
Packit 427e91
	return card_number_max;
Packit 427e91
}
Packit 427e91
Packit 427e91
int get_pos_name_header_from_card(const char * const buffer, const int profile_number, const int card_number)
Packit 427e91
{
Packit 427e91
	char header[MAX_SEARCH_FIELD_LENGTH];
Packit 427e91
	char place_holder;
Packit 427e91
	int pos_card_begin, pos_card_end, pos_name_header;
Packit 427e91
Packit 427e91
	pos_card_begin = get_card_begin(buffer, profile_number, card_number);
Packit 427e91
	pos_card_end = get_card_end(buffer, profile_number, card_number);
Packit 427e91
	place_holder = PLACE_HOLDER_STR;
Packit 427e91
	strncpy(header, PROFILE_NAME_TEMPL, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
	*strchr(header, place_holder) = '\0';
Packit 427e91
	header[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
	if ((pos_name_header = strstr_icase_blank(buffer + pos_card_begin, header)) >= 0) {
Packit 427e91
		if ((pos_name_header += pos_card_begin) < pos_card_end) {
Packit 427e91
			return pos_name_header;
Packit 427e91
		}
Packit 427e91
	}
Packit 427e91
	return NOTFOUND;
Packit 427e91
}
Packit 427e91
Packit 427e91
int get_begin_of_alsa_section(const char * const buffer, const int profile_number, const int card_number)
Packit 427e91
{
Packit 427e91
	char search_string[MAX_SEARCH_FIELD_LENGTH];
Packit 427e91
	int card_section_begin, card_section_end;
Packit 427e91
	int begin_of_alsa_section, pos_after_alsa_section;
Packit 427e91
Packit 427e91
	if ((card_section_begin = get_card_begin(buffer, profile_number, card_number)) < 0)
Packit 427e91
		return card_section_begin;
Packit 427e91
	card_section_end = get_card_end(buffer, profile_number, card_number);
Packit 427e91
	strncpy(search_string, PROFILE_NAME_TEMPL, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
	search_string[MAX_SEARCH_FIELD_LENGTH -1] = '\0';
Packit 427e91
	*strchr(search_string, PLACE_HOLDER_STR) = '\0';
Packit 427e91
Packit 427e91
	pos_after_alsa_section = get_start_of_line(buffer, card_section_end);
Packit 427e91
Packit 427e91
	begin_of_alsa_section = strstr_icase_blank(buffer + card_section_begin, search_string);
Packit 427e91
	begin_of_alsa_section = begin_of_alsa_section < 0 ? card_section_begin : begin_of_alsa_section + card_section_begin;
Packit 427e91
	if (begin_of_alsa_section > pos_after_alsa_section)
Packit 427e91
		begin_of_alsa_section = card_section_begin;
Packit 427e91
	while (begin_of_alsa_section < pos_after_alsa_section)
Packit 427e91
	{
Packit 427e91
		if (buffer[begin_of_alsa_section] == '\n') {
Packit 427e91
			begin_of_alsa_section++;
Packit 427e91
			break;
Packit 427e91
		}
Packit 427e91
		begin_of_alsa_section++;
Packit 427e91
	}
Packit 427e91
Packit 427e91
	if (begin_of_alsa_section >= pos_after_alsa_section)
Packit 427e91
		begin_of_alsa_section = NOTFOUND;
Packit 427e91
Packit 427e91
	return begin_of_alsa_section;
Packit 427e91
}
Packit 427e91
Packit 427e91
int reorganize_profiles(char * const buffer, const int max_length)
Packit 427e91
{
Packit 427e91
	int profile_number, card_number, card_number_max;
Packit 427e91
	int res;
Packit 427e91
	int pos_profile_begin, pos_profile_end, pos_card_begin, pos_card_end, pos_name_header;
Packit 427e91
	int pos_alsa_section_begin, pos_after_alsa_section;
Packit 427e91
	char header[MAX_SEARCH_FIELD_LENGTH];
Packit 427e91
	void *buffer_copy = NULL;
Packit 427e91
	char profile_or_card_number_as_str[MAX_NUM_STR_LENGTH];
Packit 427e91
	char place_holder;
Packit 427e91
Packit 427e91
	if ((buffer_copy = malloc(max_length)) == NULL) {
Packit 427e91
		res = -ENOBUFS;
Packit 427e91
		profile_number = res;
Packit 427e91
		fprintf(stderr, "Cannot allocate memory for reorganize profiles.\n");
Packit 427e91
		return res;
Packit 427e91
	}
Packit 427e91
	memset(buffer_copy, '\0', max_length);
Packit 427e91
	for (profile_number = 1; profile_number <= MAX_PROFILES; profile_number++)
Packit 427e91
	{
Packit 427e91
		if ((pos_profile_begin = get_profile_begin(buffer, profile_number)) < 0)
Packit 427e91
			continue;
Packit 427e91
		/* write profile header */
Packit 427e91
		place_holder = PLACE_HOLDER_NUM;
Packit 427e91
		snprintf(profile_or_card_number_as_str, MAX_NUM_STR_LENGTH, "%d", profile_number);
Packit 427e91
		profile_or_card_number_as_str[MAX_NUM_STR_LENGTH - 1] = '\0';
Packit 427e91
		compose_search_string(header, PROFILE_HEADER_TEMPL, profile_or_card_number_as_str, place_holder, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
		header[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
		snprintf(buffer_copy + strlen(buffer_copy), max_length - strlen(buffer_copy), "%s\n", header);
Packit 427e91
		pos_profile_end = get_profile_end(buffer, profile_number);
Packit 427e91
		/* search max card number in profile */
Packit 427e91
		card_number_max = get_max_card_number_in_profile(buffer, profile_number);
Packit 427e91
		for (card_number = 0; card_number <= card_number_max; card_number++)
Packit 427e91
		{
Packit 427e91
			if ((pos_card_begin = get_card_begin(buffer, profile_number, card_number)) < 0)
Packit 427e91
				continue;
Packit 427e91
			/* write card header */
Packit 427e91
			place_holder = PLACE_HOLDER_NUM;
Packit 427e91
			snprintf(profile_or_card_number_as_str, MAX_NUM_STR_LENGTH, "%d", card_number);
Packit 427e91
			profile_or_card_number_as_str[MAX_NUM_STR_LENGTH - 1] = '\0';
Packit 427e91
			compose_search_string(header, CARD_HEADER_TEMPL, profile_or_card_number_as_str, place_holder, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
			header[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
			snprintf(buffer_copy + strlen(buffer_copy), max_length - strlen(buffer_copy), "%s\n", header);
Packit 427e91
			pos_card_end = get_card_end(buffer, profile_number, card_number);
Packit 427e91
			/* write profile name */
Packit 427e91
			place_holder = PLACE_HOLDER_STR;
Packit 427e91
			if ((pos_name_header = get_pos_name_header_from_card(buffer, profile_number, card_number)) >= 0) {
Packit 427e91
				compose_search_string(header, PROFILE_NAME_TEMPL, get_profile_name_from_header(buffer + pos_name_header), place_holder, \
Packit 427e91
						MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
				snprintf(buffer_copy + strlen(buffer_copy), max_length - strlen(buffer_copy), "%s\n", header);
Packit 427e91
			}
Packit 427e91
			/* copy alsa section if exists */
Packit 427e91
			if ((pos_alsa_section_begin = get_begin_of_alsa_section(buffer, profile_number, card_number)) >= 0) {
Packit 427e91
				pos_after_alsa_section = get_start_of_line(buffer, pos_card_end);
Packit 427e91
				strncpy(buffer_copy + strlen(buffer_copy), buffer + pos_alsa_section_begin, pos_after_alsa_section - pos_alsa_section_begin);
Packit 427e91
			}
Packit 427e91
			/* write card footer */
Packit 427e91
			place_holder = PLACE_HOLDER_NUM;
Packit 427e91
			snprintf(profile_or_card_number_as_str, MAX_NUM_STR_LENGTH, "%d", card_number);
Packit 427e91
			profile_or_card_number_as_str[MAX_NUM_STR_LENGTH - 1] = '\0';
Packit 427e91
			compose_search_string(header, CARD_FOOTER_TEMPL, profile_or_card_number_as_str, place_holder, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
			header[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
			snprintf(buffer_copy + strlen(buffer_copy), max_length - strlen(buffer_copy), "%s\n", header);
Packit 427e91
		}
Packit 427e91
	}
Packit 427e91
	memset(buffer, '\0', max_length);
Packit 427e91
	strncpy(buffer, buffer_copy, max_length);
Packit 427e91
	buffer[max_length - 1] = '\0';
Packit 427e91
	free(buffer_copy);
Packit 427e91
	buffer_copy = NULL;
Packit 427e91
	return EXIT_SUCCESS;
Packit 427e91
}
Packit 427e91
Packit 427e91
int delete_card_from_profile(char * const buffer, const int profile_number, const int card_number, const int max_length)
Packit 427e91
{
Packit 427e91
	int pos_card_begin, pos_next_card;
Packit 427e91
	char *buffer_copy = NULL;
Packit 427e91
Packit 427e91
	if ((pos_card_begin = get_card_begin(buffer, profile_number, card_number)) < 0)
Packit 427e91
		return pos_card_begin;
Packit 427e91
	if ((buffer_copy = malloc(max_length)) == NULL) {
Packit 427e91
		fprintf(stderr, "Cannot allocate memory for reading profiles.\n");
Packit 427e91
		fprintf(stderr, "Cannot delete card '%d' from profile '%d'.\n", card_number, profile_number);
Packit 427e91
		return -ENOBUFS;
Packit 427e91
	}
Packit 427e91
	memset(buffer_copy, '\0', max_length);
Packit 427e91
	pos_next_card = get_pos_for_next_card(buffer, profile_number, card_number);
Packit 427e91
	strncpy(buffer_copy, buffer, pos_card_begin);
Packit 427e91
	strncpy(buffer_copy + pos_card_begin, buffer + pos_next_card, max_length - pos_card_begin);
Packit 427e91
	buffer_copy[max_length - 1] = '\0';
Packit 427e91
	memset(buffer, '\0', max_length);
Packit 427e91
	strncpy(buffer, buffer_copy, max_length);
Packit 427e91
	buffer[max_length - 1] = '\0';
Packit 427e91
	free(buffer_copy);
Packit 427e91
	buffer_copy = NULL;
Packit 427e91
	return EXIT_SUCCESS;
Packit 427e91
}
Packit 427e91
Packit 427e91
int save_restore_alsactl_settings(char * const tmpfile, const int card_number, char * const operation)
Packit 427e91
{
Packit 427e91
	int res;
Packit 427e91
	char * parameter[MAX_PARAM];
Packit 427e91
	char *alsactl;
Packit 427e91
	char profile_number_or_card_number_as_str[MAX_NUM_STR_LENGTH];
Packit 427e91
Packit 427e91
	alsactl = check_environment("ALSACTL_PROG", ALSACTL);
Packit 427e91
	snprintf(profile_number_or_card_number_as_str, MAX_NUM_STR_LENGTH, "%d", card_number);
Packit 427e91
	profile_number_or_card_number_as_str[MAX_NUM_STR_LENGTH - 1] = '\0';
Packit 427e91
Packit 427e91
	parameter[0] = alsactl;
Packit 427e91
	parameter[1] = "-f";
Packit 427e91
	parameter[2] = tmpfile;
Packit 427e91
	parameter[3] = operation;
Packit 427e91
	parameter[4] = profile_number_or_card_number_as_str;
Packit 427e91
	parameter[5] = NULL;
Packit 427e91
	
Packit 427e91
	res = new_process(parameter);
Packit 427e91
	
Packit 427e91
	return res;
Packit 427e91
}
Packit 427e91
Packit 427e91
void compose_tmpfile_name(char * const tmpfile, const char * const cfgfile)
Packit 427e91
{
Packit 427e91
	strncpy(tmpfile, cfgfile, MAX_FILE_NAME_LENGTH);
Packit 427e91
	tmpfile[MAX_FILE_NAME_LENGTH - 1] = '\0';
Packit 427e91
	strncpy(tmpfile + strlen(tmpfile), "_alsactl_tmp", MAX_FILE_NAME_LENGTH - strlen(tmpfile));
Packit 427e91
	tmpfile[MAX_FILE_NAME_LENGTH - 1] = '\0';
Packit 427e91
}
Packit 427e91
Packit 427e91
/*
Packit 427e91
 * restore card settings
Packit 427e91
 * if profile_number < 0 profile_name must be given
Packit 427e91
 * if booth is given profile_number will be used profile_name will be ignored
Packit 427e91
 */
Packit 427e91
int restore_profile(const int profile_number, const int card_number, const char * profile_name, char * cfgfile)
Packit 427e91
{
Packit 427e91
	int res, max_length;
Packit 427e91
	int begin_of_alsa_section, pos_after_alsa_section, profile_nr;
Packit 427e91
	char *buffer = NULL;
Packit 427e91
	char tmpfile[MAX_FILE_NAME_LENGTH];
Packit 427e91
	int get_profile_number(const char * const profile_name_given, const int card_number, char * cfgfile);
Packit 427e91
Packit 427e91
	if ((profile_number < 0) && (profile_name == NULL)) {
Packit 427e91
		fprintf(stderr, "Without profile number - profile name for card '%d' must given.\n", card_number);
Packit 427e91
		return -EINVAL;
Packit 427e91
	}
Packit 427e91
	if ((max_length = get_file_size(cfgfile)) < 0) {
Packit 427e91
		fprintf(stderr, "Cannot get file size from '%s'.\n", cfgfile);
Packit 427e91
		return max_length;
Packit 427e91
	}
Packit 427e91
	max_length++;
Packit 427e91
	if ((buffer = malloc(max_length)) == NULL) {
Packit 427e91
		fprintf(stderr, "Cannot allocate memory for reading profiles.\n");
Packit 427e91
		fprintf(stderr, "Cannot read settings for card '%d' in profile '%d'.\n", card_number, profile_number);
Packit 427e91
		return -ENOBUFS;
Packit 427e91
	}
Packit 427e91
	memset(buffer, '\0', max_length);
Packit 427e91
	if ((res = read_profiles_in_buffer(cfgfile, buffer, max_length)) <= 0) {
Packit 427e91
		if (profile_number < 0) {
Packit 427e91
			fprintf(stderr, "Cannot read settings for card '%d' in profile '%s'.\n", card_number, profile_name);
Packit 427e91
		} else {
Packit 427e91
			fprintf(stderr, "Cannot read settings for card '%d' in profile '%d'.\n", card_number, profile_number);
Packit 427e91
		}
Packit 427e91
		free(buffer);
Packit 427e91
		buffer = NULL;
Packit 427e91
		return -EINVAL;
Packit 427e91
	}
Packit 427e91
	profile_nr = profile_number;
Packit 427e91
	if (profile_number < 0) {
Packit 427e91
		if ((profile_nr = get_profile_number(profile_name, card_number, cfgfile)) < 0) {
Packit 427e91
			fprintf(stderr, "Cannot find profile '%s' for card '%d'.\n", profile_name, card_number);
Packit 427e91
			free(buffer);
Packit 427e91
			buffer = NULL;
Packit 427e91
			return profile_nr;
Packit 427e91
		}
Packit 427e91
	}
Packit 427e91
	if ((begin_of_alsa_section = get_begin_of_alsa_section(buffer, profile_nr, card_number)) < 0) {
Packit 427e91
		fprintf(stderr, "Cannot find alsa section for card '%d' in profile '%d'.\n", card_number, profile_nr);
Packit 427e91
		free(buffer);
Packit 427e91
		buffer = NULL;
Packit 427e91
		return begin_of_alsa_section;
Packit 427e91
	}
Packit 427e91
	pos_after_alsa_section = get_start_of_line(buffer, get_card_end(buffer, profile_nr, card_number));
Packit 427e91
	compose_tmpfile_name(tmpfile, cfgfile);
Packit 427e91
	if ((res = write_profiles_from_buffer(tmpfile, buffer + begin_of_alsa_section, pos_after_alsa_section - begin_of_alsa_section, 0)) >= 0) {
Packit 427e91
		res = save_restore_alsactl_settings(tmpfile, card_number, ALSACTL_OP_RESTORE);
Packit 427e91
		unlink(tmpfile);
Packit 427e91
	}
Packit 427e91
	free(buffer);
Packit 427e91
	buffer = NULL;
Packit 427e91
Packit 427e91
	if (res > 0)
Packit 427e91
		res = EXIT_SUCCESS;
Packit 427e91
Packit 427e91
	return res;
Packit 427e91
}
Packit 427e91
Packit 427e91
int append_alsactl_settings(const char * const tmpfile, char * const buffer_position, const int max_length)
Packit 427e91
{
Packit 427e91
	int res;
Packit 427e91
Packit 427e91
	res = read_profiles_in_buffer(tmpfile, buffer_position, max_length);
Packit 427e91
	if (res >= 0)
Packit 427e91
		return EXIT_SUCCESS;
Packit 427e91
	return res;
Packit 427e91
}
Packit 427e91
Packit 427e91
/*
Packit 427e91
 * insert entry for card in profile with alsactl settings
Packit 427e91
 * if profile_number < 0 no profile header is needed
Packit 427e91
 * if pos_end < 0 the new entry will be appended
Packit 427e91
 * if profile_name == NULL the profile name header will not be written
Packit 427e91
 */
Packit 427e91
int insert_card(char * const buffer, const int profile_number, const int card_number, const char * const profile_name, const int pos_begin, \
Packit 427e91
		const int pos_end, char * const tmpfile, const int max_length)
Packit 427e91
{
Packit 427e91
	int res;
Packit 427e91
	char *buffer_copy = NULL;
Packit 427e91
	char header[MAX_SEARCH_FIELD_LENGTH];
Packit 427e91
	char profile_number_or_card_number_as_str[MAX_NUM_STR_LENGTH];
Packit 427e91
	char profile_name_copy[PROFILE_NAME_FIELD_LENGTH];
Packit 427e91
	char place_holder;
Packit 427e91
Packit 427e91
	if ((res = save_restore_alsactl_settings(tmpfile, card_number, ALSACTL_OP_STORE)) < 0)
Packit 427e91
		return res;
Packit 427e91
	if (pos_end >= 0) {
Packit 427e91
		if ((buffer_copy = malloc(max_length)) == NULL) {
Packit 427e91
			fprintf(stderr, "Cannot allocate memory for reading profiles.\n");
Packit 427e91
			fprintf(stderr, "Cannot save settings for card '%d' in profile '%d'.\n", card_number, profile_number);
Packit 427e91
			unlink(tmpfile);
Packit 427e91
			return -ENOBUFS;
Packit 427e91
		}
Packit 427e91
		memset(buffer_copy, '\0', max_length);
Packit 427e91
		strncpy(buffer_copy, buffer, max_length);
Packit 427e91
		buffer_copy[max_length - 1] = '\0';
Packit 427e91
		memset(buffer + pos_begin, '\0', max_length - pos_begin);
Packit 427e91
	}
Packit 427e91
	if (profile_number > 0) {
Packit 427e91
		place_holder = PLACE_HOLDER_NUM;
Packit 427e91
		snprintf(profile_number_or_card_number_as_str, MAX_NUM_STR_LENGTH, "%d", profile_number);
Packit 427e91
		profile_number_or_card_number_as_str[MAX_NUM_STR_LENGTH - 1] = '\0';
Packit 427e91
		compose_search_string(header, PROFILE_HEADER_TEMPL, profile_number_or_card_number_as_str, place_holder, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
		header[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
		snprintf(buffer + strlen(buffer), max_length - strlen(buffer), "%s\n", header);
Packit 427e91
		buffer[max_length - 1] = '\0';
Packit 427e91
	}
Packit 427e91
	/* compose card header */
Packit 427e91
	place_holder = PLACE_HOLDER_NUM;
Packit 427e91
	snprintf(profile_number_or_card_number_as_str, MAX_NUM_STR_LENGTH, "%d", card_number);
Packit 427e91
	profile_number_or_card_number_as_str[MAX_NUM_STR_LENGTH - 1] = '\0';
Packit 427e91
	compose_search_string(header, CARD_HEADER_TEMPL, profile_number_or_card_number_as_str, place_holder, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
	header[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
	snprintf(buffer + strlen(buffer), max_length - strlen(buffer), "%s\n", header);
Packit 427e91
	buffer[max_length - 1] = '\0';
Packit 427e91
	/* compose profile name header if needed */
Packit 427e91
	if (profile_name != NULL) {
Packit 427e91
		strncpy(profile_name_copy, profile_name, PROFILE_NAME_FIELD_LENGTH);
Packit 427e91
		profile_name_copy[PROFILE_NAME_FIELD_LENGTH - 1] = '\0';
Packit 427e91
		place_holder = PLACE_HOLDER_STR;
Packit 427e91
		compose_search_string(header, PROFILE_NAME_TEMPL, profile_name_copy, place_holder, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
		header[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
		snprintf(buffer + strlen(buffer), max_length - strlen(buffer), "%s\n", header);
Packit 427e91
		buffer[max_length - 1] = '\0';
Packit 427e91
	}
Packit 427e91
	res = append_alsactl_settings(tmpfile, buffer + strlen(buffer), max_length - strlen(buffer));
Packit 427e91
	buffer[max_length - 1] = '\0';
Packit 427e91
	unlink(tmpfile);
Packit 427e91
	/* compose card footer */
Packit 427e91
	place_holder = PLACE_HOLDER_NUM;
Packit 427e91
	snprintf(profile_number_or_card_number_as_str, MAX_NUM_STR_LENGTH, "%d", card_number);
Packit 427e91
	profile_number_or_card_number_as_str[MAX_NUM_STR_LENGTH - 1] = '\0';
Packit 427e91
	compose_search_string(header, CARD_FOOTER_TEMPL, profile_number_or_card_number_as_str, place_holder, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
	header[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
	snprintf(buffer + strlen(buffer), max_length - strlen(buffer), "%s\n", header);
Packit 427e91
	buffer[max_length - 1] = '\0';
Packit 427e91
	if (pos_end >= 0) {
Packit 427e91
		strncpy(buffer + strlen(buffer), buffer_copy + pos_end, max_length - strlen(buffer));
Packit 427e91
		free(buffer_copy);
Packit 427e91
		buffer_copy = NULL;
Packit 427e91
	}
Packit 427e91
	if (res >= 0)
Packit 427e91
		res = EXIT_SUCCESS;
Packit 427e91
		
Packit 427e91
	return res;
Packit 427e91
}
Packit 427e91
Packit 427e91
int save_profile(const int profile_number, const int card_number, const char * const profile_name, char *cfgfile)
Packit 427e91
{
Packit 427e91
	int res, profile_begin, profile_end, profile_nr;
Packit 427e91
	int card_begin, pos_next_card, card_nr, card_number_max;
Packit 427e91
	const int no_profile_header = -1;
Packit 427e91
	const int append = -1;
Packit 427e91
	char *buffer = NULL;
Packit 427e91
	char tmpfile[MAX_FILE_NAME_LENGTH];
Packit 427e91
	int max_length;
Packit 427e91
Packit 427e91
	if ((max_length = get_file_size(cfgfile)) < 0) {
Packit 427e91
		fprintf(stderr, "This operation will create a new profiles file '%s'.\n", cfgfile);
Packit 427e91
		max_length = 0;
Packit 427e91
	}
Packit 427e91
	max_length += MAX_PROFILE_SIZE;
Packit 427e91
	if ((buffer = malloc(max_length)) == NULL) {
Packit 427e91
		fprintf(stderr, "Cannot allocate memory for reading profiles.\n");
Packit 427e91
		fprintf(stderr, "Cannot save settings for card '%d' in profile '%d'.\n", card_number, profile_number);
Packit 427e91
		return -ENOBUFS;
Packit 427e91
	}
Packit 427e91
	memset(buffer, '\0', max_length);
Packit 427e91
	compose_tmpfile_name(tmpfile, cfgfile);
Packit 427e91
	/* file found */
Packit 427e91
	if ((res = open(cfgfile, O_RDONLY | 0400000 /* NOFOLLOW */)) >= 0) {
Packit 427e91
		close(res);
Packit 427e91
		res = read_profiles_in_buffer(cfgfile, buffer, max_length);
Packit 427e91
		if (res > 0)
Packit 427e91
			res = reorganize_profiles(buffer, max_length);
Packit 427e91
	}
Packit 427e91
	res = strlen(buffer);
Packit 427e91
	if (res > 0) {
Packit 427e91
		if ((profile_begin = get_profile_begin(buffer, profile_number)) < 0) {
Packit 427e91
			if (profile_number < MAX_PROFILES) {
Packit 427e91
				for (profile_nr = 1; profile_nr <= MAX_PROFILES; profile_nr++)
Packit 427e91
				{
Packit 427e91
					if (profile_nr > profile_number) {
Packit 427e91
						if ((profile_begin = get_profile_begin(buffer, profile_nr)) >= 0)
Packit 427e91
							break;
Packit 427e91
					}
Packit 427e91
				}
Packit 427e91
				if (profile_begin < 0)
Packit 427e91
					profile_begin = strlen(buffer);
Packit 427e91
			} else {
Packit 427e91
				profile_begin = strlen(buffer);
Packit 427e91
			}
Packit 427e91
			if (profile_begin < strlen(buffer)) {
Packit 427e91
				res = insert_card(buffer, profile_number, card_number, profile_name, profile_begin, profile_begin, tmpfile, max_length);
Packit 427e91
			} else {
Packit 427e91
				res = insert_card(buffer, profile_number, card_number, profile_name, profile_begin, append, tmpfile, max_length);
Packit 427e91
			}
Packit 427e91
		} else {
Packit 427e91
			if ((card_begin = get_card_begin(buffer, profile_number, card_number)) < 0) {
Packit 427e91
				card_number_max = get_max_card_number_in_profile(buffer, profile_number);
Packit 427e91
				profile_end = get_profile_end(buffer, profile_number);
Packit 427e91
				if (card_number_max > card_number) {
Packit 427e91
					for (card_nr = 0; card_nr <= card_number_max; card_nr++)
Packit 427e91
					{
Packit 427e91
						if (card_nr > card_number) {
Packit 427e91
							if ((card_begin = get_card_begin(buffer, profile_number, card_number)) >= 0)
Packit 427e91
								break;
Packit 427e91
						}
Packit 427e91
					}
Packit 427e91
					if (card_begin < 0)
Packit 427e91
						card_begin = profile_end;
Packit 427e91
				} else {
Packit 427e91
					card_begin = profile_end;
Packit 427e91
				}
Packit 427e91
				if (card_begin < strlen(buffer)) {
Packit 427e91
					res = insert_card(buffer, no_profile_header, card_number, profile_name, card_begin, card_begin, tmpfile, max_length);
Packit 427e91
				} else {
Packit 427e91
					res = insert_card(buffer, no_profile_header, card_number, profile_name, strlen(buffer), append, tmpfile, max_length);
Packit 427e91
				}
Packit 427e91
			} else {
Packit 427e91
				pos_next_card = get_pos_for_next_card(buffer, profile_number, card_number);
Packit 427e91
				res = insert_card(buffer, no_profile_header, card_number, profile_name, card_begin, pos_next_card, tmpfile, max_length);
Packit 427e91
			}
Packit 427e91
		}
Packit 427e91
	} else {
Packit 427e91
		res = insert_card(buffer, profile_number, card_number, profile_name, 0, -1, tmpfile, max_length);
Packit 427e91
	}
Packit 427e91
	if (res < 0) {
Packit 427e91
		fprintf(stderr, "Cannot store profile '%d' for card '%d'.\n", profile_number, card_number);
Packit 427e91
	} else {
Packit 427e91
		res = write_profiles_from_buffer(cfgfile, buffer, max_length, 1);
Packit 427e91
	}
Packit 427e91
	free(buffer);
Packit 427e91
	buffer = NULL;
Packit 427e91
Packit 427e91
	if (res > 0)
Packit 427e91
		res = EXIT_SUCCESS;
Packit 427e91
Packit 427e91
	return res;
Packit 427e91
}
Packit 427e91
Packit 427e91
int delete_card(const int card_number, char * cfgfile)
Packit 427e91
{
Packit 427e91
	int res, profile_number, max_length;
Packit 427e91
	void *buffer = NULL;
Packit 427e91
Packit 427e91
	if (cfgfile == NULL)
Packit 427e91
		cfgfile = DEFAULT_PROFILERC;
Packit 427e91
	strncpy(filename_without_tilde, cfgfile, MAX_FILE_NAME_LENGTH);
Packit 427e91
	filename_without_tilde[MAX_FILE_NAME_LENGTH - 1] = '\0';
Packit 427e91
	subst_tilde_in_filename(filename_without_tilde);
Packit 427e91
	cfgfile = filename_without_tilde;
Packit 427e91
	if ((res = open(cfgfile, O_RDWR | 0400000 /* O_NOFOLLOW */, FILE_CREA_MODE)) < 0) {
Packit 427e91
		fprintf(stderr, "Cannot open configuration file '%s' for writing.\n", cfgfile);
Packit 427e91
		fprintf(stderr, "Cannot save settings for card '%d'.\n", card_number);
Packit 427e91
		return -errno;
Packit 427e91
	}
Packit 427e91
	close(res);
Packit 427e91
	if (res >= 0) {
Packit 427e91
		if ((max_length = get_file_size(cfgfile)) < 0) {
Packit 427e91
			fprintf(stderr, "Cannot get file size for '%s'.\n", cfgfile);
Packit 427e91
			return max_length;
Packit 427e91
		}
Packit 427e91
		max_length++;
Packit 427e91
		if ((buffer = malloc(max_length)) == NULL) {
Packit 427e91
			fprintf(stderr, "Cannot allocate memory for reading profiles.\n");
Packit 427e91
			fprintf(stderr, "Cannot delete card '%d'.\n", card_number);
Packit 427e91
			return -ENOBUFS;
Packit 427e91
		}
Packit 427e91
		memset(buffer, '\0', max_length);
Packit 427e91
		res = read_profiles_in_buffer(cfgfile, buffer, max_length);
Packit 427e91
		if (res > 0) {
Packit 427e91
			for (profile_number = 1; profile_number <= MAX_PROFILES; profile_number++)
Packit 427e91
				delete_card_from_profile(buffer, profile_number, card_number, max_length);
Packit 427e91
			res = EXIT_SUCCESS;
Packit 427e91
		} else {
Packit 427e91
			res = NOTFOUND;
Packit 427e91
		}
Packit 427e91
		res = write_profiles_from_buffer(cfgfile, buffer, max_length, 1);
Packit 427e91
		free(buffer);
Packit 427e91
		buffer = NULL;
Packit 427e91
	} else {
Packit 427e91
		res = NOTFOUND;
Packit 427e91
	}
Packit 427e91
	return res;
Packit 427e91
}
Packit 427e91
Packit 427e91
/*
Packit 427e91
 * First search profile name. if profile name is found look that this profile
Packit 427e91
 * name is from the specified card number.
Packit 427e91
 * if not search next occurence from given profile name.
Packit 427e91
 */
Packit 427e91
int get_profile_number(const char * const profile_name_given, const int card_number, char * cfgfile)
Packit 427e91
{
Packit 427e91
	int res, pos_name, pos_name_offset, pos_begin, pos_end, found;
Packit 427e91
	int profile_number, pos_profile;
Packit 427e91
	void *buffer = NULL;
Packit 427e91
	char search_field[MAX_SEARCH_FIELD_LENGTH];
Packit 427e91
	char header_templ[MAX_SEARCH_FIELD_LENGTH];
Packit 427e91
	char profile_or_card_number_as_str[MAX_NUM_STR_LENGTH];
Packit 427e91
	char place_holder;
Packit 427e91
	int max_length;
Packit 427e91
Packit 427e91
	if (strlen(profile_name_given) == 0) {
Packit 427e91
		fprintf(stderr, "Profile name for card '%d' must be given.\n", card_number);
Packit 427e91
		return -EINVAL;
Packit 427e91
	}
Packit 427e91
	/* cut profile name to MAX_PROFILE_NAME_LENGTH */
Packit 427e91
	strncpy(profile_name, profile_name_given, PROFILE_NAME_FIELD_LENGTH);
Packit 427e91
	profile_name[PROFILE_NAME_FIELD_LENGTH - 1] = '\0';
Packit 427e91
	if (cfgfile == NULL)
Packit 427e91
		cfgfile = DEFAULT_PROFILERC;
Packit 427e91
	res = which_cfgfile(&cfgfile);
Packit 427e91
	if (res < 0) {
Packit 427e91
		profile_number = res;
Packit 427e91
	} else {
Packit 427e91
		if ((max_length = get_file_size(cfgfile)) < 0) {
Packit 427e91
			fprintf(stderr, "Cannot get file size from '%s'.\n", cfgfile);
Packit 427e91
			return max_length;
Packit 427e91
		}
Packit 427e91
		max_length++;
Packit 427e91
		if ((buffer = malloc(max_length)) == NULL) {
Packit 427e91
			res = -ENOBUFS;
Packit 427e91
			profile_number = res;
Packit 427e91
			fprintf(stderr, "Cannot allocate memory for reading profiles.\n");
Packit 427e91
			return profile_number;
Packit 427e91
		}
Packit 427e91
		memset(buffer, '\0', max_length);
Packit 427e91
		res = read_profiles_in_buffer(cfgfile, buffer, max_length);
Packit 427e91
		if (res > 0) {
Packit 427e91
			/* insert profile name in PROFILE_NAME_TEMPL */
Packit 427e91
			place_holder = PLACE_HOLDER_STR;
Packit 427e91
			compose_search_string(search_field, PROFILE_NAME_TEMPL, profile_name, place_holder, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
			pos_name = 0;
Packit 427e91
			pos_name_offset = 0;
Packit 427e91
			pos_begin = NOTFOUND;
Packit 427e91
			found = 0;
Packit 427e91
			while ((pos_name = strstr_icase_blank(buffer + pos_name_offset, search_field)) >= 0)
Packit 427e91
			{
Packit 427e91
				pos_name += pos_name_offset;
Packit 427e91
				/* search begin of section for the given card
Packit 427e91
				   from profile name pointer backward */
Packit 427e91
				/* insert card number in CARD_HEADER_TEMPL */
Packit 427e91
				place_holder = PLACE_HOLDER_NUM;
Packit 427e91
				snprintf(profile_or_card_number_as_str, MAX_NUM_STR_LENGTH, "%d", card_number);
Packit 427e91
				profile_or_card_number_as_str[MAX_NUM_STR_LENGTH - 1] = '\0';
Packit 427e91
				compose_search_string(search_field, CARD_HEADER_TEMPL, profile_or_card_number_as_str, place_holder, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
				search_field[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
				if ((pos_begin = get_begin_of_section(buffer, search_field, TOKEN_SEP, pos_name)) < 0)
Packit 427e91
					break;
Packit 427e91
				/* searching "[ profile | < card | < /card # >" */
Packit 427e91
				/* add "[ profile |" to search_field */
Packit 427e91
				strncpy(header_templ, PROFILE_HEADER_TEMPL, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
				*strchr(header_templ, place_holder) = '\0';
Packit 427e91
				strncpy(search_field, header_templ, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
				strncpy(search_field + strlen(search_field), TOKEN_SEP, MAX_SEARCH_FIELD_LENGTH - strlen(search_field));
Packit 427e91
				/* add "< card |" to search_field */
Packit 427e91
				strncpy(header_templ, CARD_HEADER_TEMPL, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
				*strchr(header_templ, place_holder) = '\0';
Packit 427e91
				strncpy(search_field + strlen(search_field), header_templ, MAX_SEARCH_FIELD_LENGTH - strlen(search_field));
Packit 427e91
				strncpy(search_field + strlen(search_field), TOKEN_SEP, MAX_SEARCH_FIELD_LENGTH - strlen(search_field));
Packit 427e91
				search_field[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
				/* add "< card # >" to search_field */
Packit 427e91
				compose_search_string(header_templ, CARD_FOOTER_TEMPL, profile_or_card_number_as_str, place_holder, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
				header_templ[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
				strncpy(search_field + strlen(search_field), header_templ, MAX_SEARCH_FIELD_LENGTH - strlen(search_field));
Packit 427e91
				search_field[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
				pos_end = get_end_of_section(buffer, search_field, TOKEN_SEP, pos_begin + sizeof(char));
Packit 427e91
				if ((pos_name > pos_begin) && (pos_name < pos_end) && (pos_begin >= 0) && (pos_end >= 0)) {
Packit 427e91
					found = 1;
Packit 427e91
					break;
Packit 427e91
				}
Packit 427e91
				pos_name_offset = pos_name + sizeof(char);
Packit 427e91
				place_holder = PLACE_HOLDER_STR;
Packit 427e91
				compose_search_string(search_field, PROFILE_NAME_TEMPL, profile_name, place_holder, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
			}
Packit 427e91
			if (found) {
Packit 427e91
				place_holder = PLACE_HOLDER_NUM;
Packit 427e91
				strncpy(search_field, PROFILE_HEADER_TEMPL, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
				search_field[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
				*strchr(search_field, place_holder) = '\0';
Packit 427e91
				if ((pos_profile = get_begin_of_section(buffer, search_field, TOKEN_SEP, pos_begin)) < 0) {
Packit 427e91
					profile_number = NOTFOUND;
Packit 427e91
				} else {
Packit 427e91
					profile_number = get_number_from_header(buffer + pos_profile);
Packit 427e91
					/* check profile header syntax */
Packit 427e91
					if (get_profile_begin(buffer, profile_number) != pos_profile) {
Packit 427e91
						profile_number = -EINVAL;
Packit 427e91
						/* only the profile line */
Packit 427e91
						strncpy(search_field, buffer + pos_profile, MAX_SEARCH_FIELD_LENGTH);
Packit 427e91
						search_field[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
Packit 427e91
						*strchr(search_field, '\n') = '\0';
Packit 427e91
						fprintf(stderr, "profile header '%s' has incorrect syntax.\n", search_field);
Packit 427e91
						fprintf(stderr, "profile header syntax is '%s'\n" \
Packit 427e91
									"by replacing place holder '%c' with profile number.\n", \
Packit 427e91
									PROFILE_HEADER_TEMPL, PLACE_HOLDER_NUM);
Packit 427e91
						/* check profile number */
Packit 427e91
					} else if (profile_number < 1 || profile_number > MAX_PROFILES) {
Packit 427e91
						fprintf(stderr, "profile number '%d' is incorrect. the profile number will be in [1 ... %d].\n", \
Packit 427e91
									profile_number, MAX_PROFILES);
Packit 427e91
						profile_number = -EINVAL;
Packit 427e91
					}
Packit 427e91
				}
Packit 427e91
			} else {
Packit 427e91
				profile_number = NOTFOUND;
Packit 427e91
			}
Packit 427e91
		} else {
Packit 427e91
			profile_number = NOTFOUND;
Packit 427e91
		}
Packit 427e91
		free(buffer);
Packit 427e91
		buffer = NULL;
Packit 427e91
	}
Packit 427e91
	return profile_number;
Packit 427e91
}
Packit 427e91
Packit 427e91
char *get_profile_name(const int profile_number, const int card_number, char * cfgfile)
Packit 427e91
{
Packit 427e91
	int res, max_length;
Packit 427e91
	void *buffer = NULL;
Packit 427e91
Packit 427e91
	if (profile_number < 1 || profile_number > MAX_PROFILES) {
Packit 427e91
		fprintf(stderr, "profile number '%d' is incorrect. the profile number will be in [1 ... %d].\n", \
Packit 427e91
					profile_number, MAX_PROFILES);
Packit 427e91
		return NULL;
Packit 427e91
	}
Packit 427e91
	if (cfgfile == NULL)
Packit 427e91
		cfgfile = DEFAULT_PROFILERC;
Packit 427e91
	res = which_cfgfile(&cfgfile);
Packit 427e91
	if (res < 0) {
Packit 427e91
		snprintf(profile_name, PROFILE_NAME_FIELD_LENGTH, "%d", profile_number);
Packit 427e91
	} else {
Packit 427e91
		if ((max_length = get_file_size(cfgfile)) < 0) {
Packit 427e91
			fprintf(stderr, "Cannot get file size from '%s'.\n", cfgfile);
Packit 427e91
			snprintf(profile_name, PROFILE_NAME_FIELD_LENGTH, "%d", profile_number);
Packit 427e91
			return profile_name;
Packit 427e91
		}
Packit 427e91
		max_length++;
Packit 427e91
		if ((buffer = malloc(max_length)) == NULL) {
Packit 427e91
			res = -ENOBUFS;
Packit 427e91
			snprintf(profile_name, PROFILE_NAME_FIELD_LENGTH, "%d", profile_number);
Packit 427e91
			fprintf(stderr, "Cannot allocate memory for reading profiles.\n");
Packit 427e91
			return profile_name;
Packit 427e91
		}
Packit 427e91
		memset(buffer, '\0', max_length);
Packit 427e91
		memset(profile_name, '\0', PROFILE_NAME_FIELD_LENGTH);
Packit 427e91
		res = read_profiles_in_buffer(cfgfile, buffer, max_length);
Packit 427e91
		if (res > 0) {
Packit 427e91
			if ((res = get_pos_name_header_from_card(buffer, profile_number, card_number)) >= 0) {
Packit 427e91
				get_profile_name_from_header(buffer + (res * sizeof(char)));
Packit 427e91
				profile_name[PROFILE_NAME_FIELD_LENGTH - 1] = '\0';
Packit 427e91
			}
Packit 427e91
		}
Packit 427e91
		free(buffer);
Packit 427e91
		buffer = NULL;
Packit 427e91
		if (strlen(profile_name) == 0) {
Packit 427e91
			snprintf(profile_name, PROFILE_NAME_FIELD_LENGTH, "%d", profile_number);
Packit 427e91
		}
Packit 427e91
	}
Packit 427e91
	return profile_name;
Packit 427e91
}
Packit 427e91
Packit 427e91
int save_restore(const char * const operation, const int profile_number, const int card_number, char * cfgfile, const char * const profile_name)
Packit 427e91
{
Packit 427e91
	int res;
Packit 427e91
Packit 427e91
	if (profile_number < 1 || profile_number > MAX_PROFILES) {
Packit 427e91
		fprintf(stderr, "profile number '%d' is incorrect. the profile number will be in [1 ... %d].\n", \
Packit 427e91
					profile_number, MAX_PROFILES);
Packit 427e91
		return -EINVAL;
Packit 427e91
	}
Packit 427e91
	if (cfgfile == NULL)
Packit 427e91
		cfgfile = DEFAULT_PROFILERC;
Packit 427e91
	if (!strcmp(operation, ALSACTL_OP_STORE)) {
Packit 427e91
		strncpy(filename_without_tilde, cfgfile, MAX_FILE_NAME_LENGTH);
Packit 427e91
		filename_without_tilde[MAX_FILE_NAME_LENGTH - 1] = '\0';
Packit 427e91
		subst_tilde_in_filename(filename_without_tilde);
Packit 427e91
		cfgfile = filename_without_tilde;
Packit 427e91
		if ((res = open(cfgfile, O_RDONLY | 0400000 /* O_NOFOLLOW */)) < 0) {
Packit 427e91
			if ((res = create_dir_from_filename(cfgfile)) < 0) {
Packit 427e91
				fprintf(stderr, "Cannot open configuration file '%s' for writing.\n", cfgfile);
Packit 427e91
				fprintf(stderr, "Cannot save settings for card '%d' in profile '%d'.\n", card_number, profile_number);
Packit 427e91
				return -EACCES;
Packit 427e91
			}
Packit 427e91
			if ((res = open(cfgfile, O_RDWR | O_CREAT | 0400000 /* O_NOFOLLOW */, FILE_CREA_MODE)) < 0) {
Packit 427e91
				fprintf(stderr, "Cannot open configuration file '%s' for writing.\n", cfgfile);
Packit 427e91
				fprintf(stderr, "Cannot save settings for card '%d' in profile '%d'.\n", card_number, profile_number);
Packit 427e91
				return -errno;
Packit 427e91
			}
Packit 427e91
			unlink(cfgfile);
Packit 427e91
		} else {
Packit 427e91
			if ((res = open(cfgfile, O_RDWR | 0400000 /* O_NOFOLLOW */, FILE_CREA_MODE)) < 0) {
Packit 427e91
				fprintf(stderr, "Cannot open configuration file '%s' for writing.\n", cfgfile);
Packit 427e91
				fprintf(stderr, "Cannot save settings for card '%d' in profile '%d'.\n", card_number, profile_number);
Packit 427e91
				return -errno;
Packit 427e91
			}
Packit 427e91
		}
Packit 427e91
		res =  save_profile(profile_number, card_number, profile_name, cfgfile);
Packit 427e91
	} else if (!strcmp(operation, ALSACTL_OP_RESTORE)) {
Packit 427e91
		res = which_cfgfile(&cfgfile);
Packit 427e91
		if (res < 0) {
Packit 427e91
			fprintf(stderr, "Cannot open profiles file '%s' ...\n", cfgfile);
Packit 427e91
			fprintf(stderr, "Use current settings.\n");
Packit 427e91
			fprintf(stderr, "You can store this settings to profile no. %d in file '%s' by pressing save button.\n",
Packit 427e91
						profile_number, cfgfile);
Packit 427e91
		} else { 
Packit 427e91
			if ((res = restore_profile(profile_number, card_number, profile_name, cfgfile)) < 0) {
Packit 427e91
				fprintf(stderr, "Cannot restore settings for card '%d' in profile '%d'.\n", card_number, profile_number); 
Packit 427e91
				fprintf(stderr, "Use current settings.\n");
Packit 427e91
			}
Packit 427e91
		}
Packit 427e91
	} else {
Packit 427e91
		fprintf(stderr, "%s: Unknown command '%s'...\n", 
Packit 427e91
			PROGRAM_NAME, operation);
Packit 427e91
		res = -ENODEV;
Packit 427e91
	}
Packit 427e91
Packit 427e91
	return res < 0 ? -EXIT_FAILURE : EXIT_SUCCESS;
Packit 427e91
}