Blame envy24control/profiles.c

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