Exemple #1
0
/* Copies the src file to the dst location.  Returns zero on success. */
static int
copy_file(const char src[], const char dst[])
{
	FILE *const src_fp = os_fopen(src, "rb");
	FILE *const dst_fp = os_fopen(dst, "wb");
	int result;

	result = copy_file_internal(src_fp, dst_fp);

	if(dst_fp != NULL)
	{
		(void)fclose(dst_fp);
	}
	if(src_fp != NULL)
	{
		(void)fclose(src_fp);
	}

	if(result != 0)
	{
		(void)remove(dst);
	}

	return result;
}
Exemple #2
0
static int config_parse(struct darray *sections, const char *file,
		bool always_open)
{
	char *file_data;
	struct lexer lex;
	struct base_token token;
	struct strref section_name;
	FILE *f;

	f = os_fopen(file, "rb");
	if (always_open && !f)
		f = os_fopen(file, "w+");
	if (!f)
		return CONFIG_FILENOTFOUND;

	os_fread_utf8(f, &file_data);
	fclose(f);

	if (!file_data)
		return CONFIG_SUCCESS;

	lexer_init(&lex);
	lexer_start_move(&lex, file_data);

	base_token_clear(&token);

	while (lexer_getbasetoken(&lex, &token, PARSE_WHITESPACE)) {
		struct config_section *section;

		while (token.type == BASETOKEN_WHITESPACE) {
			if (!lexer_getbasetoken(&lex, &token, PARSE_WHITESPACE))
				goto complete;
		}

		if (*token.text.array != '[') {
			while (!is_newline(*token.text.array)) {
				if (!lexer_getbasetoken(&lex, &token,
							PARSE_WHITESPACE))
					goto complete;
			}

			continue;
		}

		strref_clear(&section_name);
		config_parse_string(&lex, &section_name, ']');
		if (!section_name.len)
			break;

		section = darray_push_back_new(sizeof(struct config_section),
				sections);
		section->name = bstrdup_n(section_name.array,
				section_name.len);
		config_parse_section(section, &lex);
	}

complete:
	lexer_free(&lex);
	return CONFIG_SUCCESS;
}
Exemple #3
0
static int config_parse_file(struct darray *sections, const char *file,
		bool always_open)
{
	char *file_data;
	struct lexer lex;
	FILE *f;

	f = os_fopen(file, "rb");
	if (always_open && !f)
		f = os_fopen(file, "w+");
	if (!f)
		return CONFIG_FILENOTFOUND;

	os_fread_utf8(f, &file_data);
	fclose(f);

	if (!file_data)
		return CONFIG_SUCCESS;

	lexer_init(&lex);
	lexer_start_move(&lex, file_data);

	parse_config_data(sections, &lex);

	lexer_free(&lex);
	return CONFIG_SUCCESS;
}
Exemple #4
0
void
vim_write_dir(const char path[])
{
	/* TODO: move this and other non-Vim related code to extern.c unit. */

	FILE *fp;
	const char *const dir_out = curr_stats.chosen_dir_out;

	if(is_null_or_empty(dir_out))
	{
		return;
	}

	if(strcmp(dir_out, "-") == 0)
	{
		fputs(path, curr_stats.original_stdout);
		return;
	}

	fp = os_fopen(dir_out, "w");
	if(fp == NULL)
	{
		LOG_SERROR_MSG(errno, "Can't open file for writing: \"%s\"", dir_out);
		return;
	}

	fputs(path, fp);
	fclose(fp);
}
Exemple #5
0
int
vim_write_file_list(const FileView *view, int nfiles, char *files[])
{
	FILE *fp;
	const char *const files_out = curr_stats.chosen_files_out;

	if(is_null_or_empty(files_out))
	{
		return 0;
	}

	if(strcmp(files_out, "-") == 0)
	{
		dump_filenames(view, curr_stats.original_stdout, nfiles, files);
		return 0;
	}

	fp = os_fopen(files_out, "w");
	if(fp == NULL)
	{
		LOG_SERROR_MSG(errno, "Can't open file for writing: \"%s\"", files_out);
		return 1;
	}

	dump_filenames(view, fp, nfiles, files);
	fclose(fp);
	return 0;
}
Exemple #6
0
int
iop_mkfile(io_args_t *const args)
{
	FILE *f;
	const char *const path = args->arg1.path;

	if(path_exists(path, DEREF))
	{
		(void)ioe_errlst_append(&args->result.errors, path, EEXIST,
				strerror(EEXIST));
		return -1;
	}

	f = os_fopen(path, "wb");
	if(f == NULL)
	{
		(void)ioe_errlst_append(&args->result.errors, path, errno, strerror(errno));
		return -1;
	}

	if(fclose(f) != 0)
	{
		(void)ioe_errlst_append(&args->result.errors, path, errno, strerror(errno));
		return -1;
	}

	return 0;
}
static bool flv_output_start(void *data)
{
	struct flv_output *stream = data;
	obs_data_t *settings;
	const char *path;

	if (!obs_output_can_begin_data_capture(stream->output, 0))
		return false;
	if (!obs_output_initialize_encoders(stream->output, 0))
		return false;

	/* get path */
	settings = obs_output_get_settings(stream->output);
	path = obs_data_get_string(settings, "path");
	dstr_copy(&stream->path, path);
	obs_data_release(settings);

	stream->file = os_fopen(stream->path.array, "wb");
	if (!stream->file) {
		warn("Unable to open FLV file '%s'", stream->path.array);
		return false;
	}

	/* write headers and start capture */
	stream->active = true;
	obs_output_begin_data_capture(stream->output, 0);

	info("Writing FLV file '%s'...", stream->path.array);
	return true;
}
Exemple #8
0
static void
config_file_create(const char *buf)
{
	/* the test script will take care of removing this file for us */
	FILE *f = os_fopen(testconfig_path, "w+");
	fwrite(buf, sizeof(char), strlen(buf), f);
	fclose(f);
}
Exemple #9
0
int config_save(config_t *config)
{
	FILE *f;
	struct dstr str, tmp;
	size_t i, j;

	if (!config)
		return CONFIG_ERROR;
	if (!config->file)
		return CONFIG_ERROR;

	dstr_init(&str);
	dstr_init(&tmp);

	f = os_fopen(config->file, "wb");
	if (!f)
		return CONFIG_FILENOTFOUND;

	for (i = 0; i < config->sections.num; i++) {
		struct config_section *section = darray_item(
				sizeof(struct config_section),
				&config->sections, i);

		if (i) dstr_cat(&str, "\n");

		dstr_cat(&str, "[");
		dstr_cat(&str, section->name);
		dstr_cat(&str, "]\n");

		for (j = 0; j < section->items.num; j++) {
			struct config_item *item = darray_item(
					sizeof(struct config_item),
					&section->items, j);

			dstr_copy(&tmp, item->value ? item->value : "");
			dstr_replace(&tmp, "\\", "\\\\");
			dstr_replace(&tmp, "\r", "\\r");
			dstr_replace(&tmp, "\n", "\\n");

			dstr_cat(&str, item->name);
			dstr_cat(&str, "=");
			dstr_cat(&str, tmp.array);
			dstr_cat(&str, "\n");
		}
	}

#ifdef _WIN32
	fwrite("\xEF\xBB\xBF", 1, 3, f);
#endif
	fwrite(str.array, 1, str.len, f);
	fclose(f);

	dstr_free(&tmp);
	dstr_free(&str);

	return CONFIG_SUCCESS;
}
void load_text_from_file(struct ft2_source *srcdata, const char *filename)
{
	FILE *tmp_file = NULL;
	uint32_t filesize = 0;
	char *tmp_read = NULL;
	uint16_t header = 0;
	size_t bytes_read;

	tmp_file = os_fopen(filename, "rb");
	if (tmp_file == NULL) {
		if (!srcdata->file_load_failed) {
			blog(LOG_WARNING, "Failed to open file %s", filename);
			srcdata->file_load_failed = true;
		}
		return;
	}
	fseek(tmp_file, 0, SEEK_END);
	filesize = (uint32_t)ftell(tmp_file);
	fseek(tmp_file, 0, SEEK_SET);
	bytes_read = fread(&header, 2, 1, tmp_file);

	if (bytes_read == 2 && header == 0xFEFF) {
		// File is already in UTF-16 format
		if (srcdata->text != NULL) {
			bfree(srcdata->text);
			srcdata->text = NULL;
		}
		srcdata->text = bzalloc(filesize);
		bytes_read = fread(srcdata->text, filesize - 2, 1, tmp_file);

		srcdata->m_timestamp =
			get_modified_timestamp(srcdata->text_file);
		bfree(tmp_read);
		fclose(tmp_file);

		return;
	}

	fseek(tmp_file, 0, SEEK_SET);
	srcdata->m_timestamp = get_modified_timestamp(srcdata->text_file);

	tmp_read = bzalloc(filesize + 1);
	bytes_read = fread(tmp_read, filesize, 1, tmp_file);
	fclose(tmp_file);

	if (srcdata->text != NULL) {
		bfree(srcdata->text);
		srcdata->text = NULL;
	}
	srcdata->text = bzalloc((strlen(tmp_read) + 1)*sizeof(wchar_t));
	os_utf8_to_wcs(tmp_read, strlen(tmp_read),
		srcdata->text, (strlen(tmp_read) + 1));

	remove_cr(srcdata->text);
	bfree(tmp_read);
}
Exemple #11
0
/* Copies help file from shared files to the ~/.vifm directory if it's not
 * already there. */
static void
copy_help_file(void)
{
	LOG_FUNC_ENTER;

	char src[PATH_MAX + 16];
	char dst[PATH_MAX + 16];

	io_args_t args = {
		.arg1.src = src,
		.arg2.dst = dst,
		.arg3.crs = IO_CRS_FAIL,
	};

	snprintf(src, sizeof(src), "%s/" VIFM_HELP, get_installed_data_dir());
	snprintf(dst, sizeof(dst), "%s/" VIFM_HELP, cfg.config_dir);

	/* Don't care if it fails, also don't overwrite if file exists. */
	(void)iop_cp(&args);
}

/* Responsible for creation of scripts/ directory if it doesn't exist (in which
 * case a README file is created as well). */
static void
create_scripts_dir(void)
{
	char scripts[PATH_MAX + 16];
	char readme[PATH_MAX + 1];
	FILE *fp;

	snprintf(scripts, sizeof(scripts), "%s/" SCRIPTS_DIR, cfg.config_dir);

	//mod by sim1
	//if(create_path(scripts, S_IRWXU) != 0)
	if(create_path(scripts, S_IRWXU | S_IRWXG | S_IROTH) != 0)
	{
		return;
	}

	snprintf(readme, sizeof(readme), "%s/README", scripts);
	fp = os_fopen(readme, "w");
	if(fp == NULL)
	{
		return;
	}

	fputs("This directory is dedicated for user-supplied scripts/executables.\n"
				"vifm modifies its PATH environment variable to let user run those\n"
				"scripts without specifying full path.  All subdirectories are added\n"
				"as well.  File in a subdirectory overrules file with the same name\n"
				"in parent directories.  Restart might be needed to recognize files\n"
				"in newly created or renamed subdirectories.", fp);

	fclose(fp);
}
Exemple #12
0
void HTMLOutput( void )
{
#define HTMLREADBUFSIZE 512
	static char buf[HTMLREADBUFSIZE];
	FILE *tpl;
	char *buftemp;
	char *bufptr;
	htmlfunc* htmlfuncptr;

	tpl = os_fopen( html_template, "rt" );
	if( !tpl ) {
		nlog( LOG_WARNING, "Failed to open StatServ HTML template %s.", html_template );
		irc_chanalert( statbot, "Failed to open StatServ HTML template %s.", html_template );
		return;
	}
	opf = os_fopen( StatServ.htmlpath, "wt" );
	if( !opf ) {
		nlog( LOG_WARNING, "Failed to open HTML output file %s. Check file permissions.", StatServ.htmlpath );
		irc_chanalert( statbot, "Failed to open HTML output file %s. Check file permissions.", StatServ.htmlpath );
		return;
	}
	while( os_fgets( buf, HTMLREADBUFSIZE, tpl ) != NULL )
	{
		bufptr = buf;
		htmlfuncptr = htmlfuncs;
		while( htmlfuncptr->directive != NULL )
		{
			buftemp = strstr( bufptr, htmlfuncptr->directive );
			if( buftemp ) {
				os_fwrite( bufptr, ( int )buftemp -( int )bufptr, 1, opf );
				htmlfuncptr->handler();
				bufptr = buftemp + strlen( htmlfuncptr->directive );
			}		
			htmlfuncptr++;
		}
		os_fputs( bufptr, opf );
	}
	os_fclose( tpl );
	os_fclose( opf );
    /* update the umode so others can read it and owner can overwrite it */
    os_chmod( StatServ.htmlpath, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
}
int64_t os_get_file_size(const char *path)
{
	FILE* f = os_fopen(path, "rb");
	if (!f)
		return -1;

	int64_t sz = os_fgetsize(f);
	fclose(f);

	return sz;
}
Exemple #14
0
/* Reads the first line of the file specified by the path.  Returns NULL on
 * error or an empty file, otherwise a newly allocated string, which should be
 * freed by the caller, is returned. */
static char *
get_file_first_line(const char path[])
{
	FILE *const fp = os_fopen(path, "rb");
	char *result = NULL;
	if(fp != NULL)
	{
		result = read_line(fp, NULL);
		fclose(fp);
	}
	return result;
}
Exemple #15
0
/* Create and fill file for external command prompt.  Returns zero on success,
 * otherwise non-zero is returned and errno contains valid value. */
static int
setup_extcmd_file(const char path[], const char beginning[], CmdInputType type)
{
	FILE *const fp = os_fopen(path, "wt");
	if(fp == NULL)
	{
		return 1;
	}
	prepare_extcmd_file(fp, beginning, type);
	fclose(fp);
	return 0;
}
Exemple #16
0
static void
init(void)
{
	log = os_fopen(cfg.log_file, "a");
	if(log == NULL)
		return;
	setvbuf(log, NULL, _IONBF, 0);

	fprintf(log, "\n");
	log_time();
	fprintf(log, " ===== Started vifm =====\n");
}
Exemple #17
0
char *os_quick_read_utf8_file(const char *path)
{
	FILE *f = os_fopen(path, "rb");
	char *file_string = NULL;

	if (!f)
		return NULL;

	os_fread_utf8(f, &file_string);
	fclose(f);

	return file_string;
}
Exemple #18
0
config_t *config_create(const char *file)
{
	struct config_data *config;
	FILE *f;

	f = os_fopen(file, "wb");
	if (!f)
		return NULL;
	fclose(f);

	config = bzalloc(sizeof(struct config_data));
	return config;
}
Exemple #19
0
bool os_quick_write_utf8_file(const char *path, const char *str, size_t len,
		bool marker)
{
	FILE *f = os_fopen(path, "wb");
	if (!f)
		return false;

	if (marker)
		fwrite("\xEF\xBB\xBF", 1, 3, f);
	if (len)
		fwrite(str, 1, len, f);
	fclose(f);

	return true;
}
Exemple #20
0
bool os_quick_write_mbs_file(const char *path, const char *str, size_t len)
{
	FILE *f = os_fopen(path, "wb");
	char *mbs = NULL;
	size_t mbs_len = 0;
	if (!f)
		return false;

	mbs_len = os_utf8_to_mbs(str, len, &mbs);
	if (mbs_len)
		fwrite(mbs, 1, mbs_len, f);
	bfree(mbs);
	fclose(f);

	return true;
}
Exemple #21
0
/* Reads file specified by filepath into null terminated string.  Returns
 * string of length *read to be freed by caller on success, otherwise NULL is
 * returned. */
static char *
read_whole_file(const char filepath[], size_t *read)
{
	char *content = NULL;
	FILE *fp;

	*read = 0U;

	if((fp = os_fopen(filepath, "rb")) != NULL)
	{
		content = read_seekable_stream(fp, read);
		fclose(fp);
	}

	return content;
}
Exemple #22
0
static void cf_include_file(struct cf_preprocessor *pp,
		const struct cf_token *file_token)
{
	struct cf_lexer new_lex;
	struct dstr str_file;
	FILE *file;
	char *file_data;
	struct cf_token *tokens;
	size_t i;

	dstr_init(&str_file);
	dstr_copy_strref(&str_file, &file_token->str);
	dstr_mid(&str_file, &str_file, 1, str_file.len-2);

	/* if dependency already exists, run preprocessor on it */
	for (i = 0; i < pp->dependencies.num; i++) {
		struct cf_lexer *dep = pp->dependencies.array+i;

		if (strcmp(dep->file, str_file.array) == 0) {
			tokens = cf_lexer_gettokens(dep);
			cf_preprocess_tokens(pp, false, &tokens);
			goto exit;
		}
	}

	file = os_fopen(str_file.array, "rb");
	if (!file) {
		cf_adderror(pp, file_token, "Could not open file '$1'",
				file_token->str.array, NULL, NULL);
		goto exit;
	}

	os_fread_utf8(file, &file_data);
	fclose(file);

	cf_lexer_init(&new_lex);
	cf_lexer_lex(&new_lex, file_data, str_file.array);
	tokens = cf_lexer_gettokens(&new_lex);
	cf_preprocess_tokens(pp, false, &tokens);
	bfree(file_data);

	da_push_back(pp->dependencies, &new_lex);

exit:
	dstr_free(&str_file);
}
Exemple #23
0
static int ss_set_htmlpath_cb( const CmdParams *cmdparams, SET_REASON reason )
{
	FILE *opf;

	if( reason == SET_CHANGE )
	{
		opf = os_fopen( StatServ.htmlpath, "wt" );
		if( !opf )
		{
			irc_prefmsg( statbot, cmdparams->source, 
				"Failed to open HTML output file %s. Check file permissions. HTML output disabled.", StatServ.htmlpath );
			return NS_SUCCESS;
		}
		os_fclose( opf );
		HTMLOutput();
	}
	return NS_SUCCESS;
}
Exemple #24
0
void romfs_extract_datafile(romfs_context* ctx, u64 offset, u64 size, const oschar_t* path)
{
	FILE* outfile = 0;
	u32 max;
	u8 buffer[4096];


	if (path == NULL || os_strlen(path) == 0)
		goto clean;

	offset += ctx->datablockoffset;

	romfs_fseek(ctx, offset);
	outfile = os_fopen(path, OS_MODE_WRITE);
	if (outfile == NULL)
	{
		fprintf(stderr, "Error opening file for writing\n");
		goto clean;
	}

	while(size)
	{
		max = sizeof(buffer);
		if (max > size)
			max = (u32) size;

		if (max != romfs_fread(ctx, buffer, 1, max))
		{
			fprintf(stderr, "Error reading file\n");
			goto clean;
		}

		if (max != fwrite(buffer, 1, max, outfile))
		{
			fprintf(stderr, "Error writing file\n");
			goto clean;
		}

		size -= max;
	}
clean:
	if (outfile)
		fclose(outfile);
}
Exemple #25
0
/* Makes fingerprint of file contents (all or part of it of fixed size).
 * Returns the fingerprint as a string, which is empty or NULL on error. */
static char *
get_contents_fingerprint(const char path[], const dir_entry_t *entry)
{
#if INTPTR_MAX == INT64_MAX
#define XX_BITS 64
#else
#define XX_BITS 32
#endif
#define XX__(name, bits) XXH ## bits ## _ ## name
#define XX_(name, bits) XX__(name, bits)
#define XX(name) XX_(name, XX_BITS)

	XX(state_t) st;
	char block[BLOCK_SIZE];
	size_t to_read = PREFIX_SIZE;
	FILE *in = os_fopen(path, "rb");
	if(in == NULL)
	{
		return strdup("");
	}

	XX(reset)(&st, 0U);
	while(to_read != 0U)
	{
		const size_t portion = MIN(sizeof(block), to_read);
		const size_t nread = fread(&block, 1, portion, in);
		if(nread == 0U)
		{
			break;
		}

		XX(update)(&st, block, nread);
		to_read -= nread;
	}
	fclose(in);

	return format_str("%" PRINTF_ULL "|%" PRINTF_ULL,
			(unsigned long long)entry->size, (unsigned long long)XX(digest)(&st));

#undef XX_BITS
#undef XX__
#undef XX_
#undef XX
}
Exemple #26
0
static void send_audio_header(struct rtmp_stream *stream)
{
	obs_output_t  context  = stream->output;
	obs_encoder_t aencoder = obs_output_get_audio_encoder(context);
	uint8_t       *header;

	struct encoder_packet packet   = {
		.type         = OBS_ENCODER_AUDIO,
		.timebase_den = 1
	};

	obs_encoder_get_extra_data(aencoder, &header, &packet.size);
	packet.data = bmemdup(header, packet.size);
	send_packet(stream, &packet, true);
}

static void send_video_header(struct rtmp_stream *stream)
{
	obs_output_t  context  = stream->output;
	obs_encoder_t vencoder = obs_output_get_video_encoder(context);
	uint8_t       *header;
	size_t        size;

	struct encoder_packet packet   = {
		.type         = OBS_ENCODER_VIDEO,
		.timebase_den = 1,
		.keyframe     = true
	};

	obs_encoder_get_extra_data(vencoder, &header, &size);
	packet.size = obs_parse_avc_header(&packet.data, header, size);
	send_packet(stream, &packet, true);
}

static void send_headers(struct rtmp_stream *stream)
{
#ifdef FILE_TEST
	stream->test = os_fopen("D:\\bla.flv", "wb");
#endif
	send_meta_data(stream);
	send_audio_header(stream);
	send_video_header(stream);
}
Exemple #27
0
int
write_file_of_lines(const char filepath[], char *strs[], size_t nstrs)
{
	FILE *fp;
	size_t i;

	if((fp = os_fopen(filepath, "w")) == NULL)
	{
		return 1;
	}

	for(i = 0U; i < nstrs; i++)
	{
		fprintf(fp, "%s\n", strs[i]);
	}

	fclose(fp);
	return 0;
}
Exemple #28
0
void
vim_write_empty_file_list(void)
{
	FILE *fp;
	const char *const files_out = curr_stats.chosen_files_out;

	if(is_null_or_empty(files_out) || strcmp(files_out, "-") == 0)
	{
		return;
	}

	fp = os_fopen(files_out, "w");
	if(fp != NULL)
	{
		fclose(fp);
	}
	else
	{
		LOG_SERROR_MSG(errno, "Can't truncate file: \"%s\"", files_out);
	}
}
Exemple #29
0
/*!
*@brief        通过outpath、outname和提供的构造后缀名称创建文件
*@author       zhaohm3
*@param[in]    szPostfix    文件后缀名称
*@param[out]   szFileName   文件名,用于输出
*@retval       创建$(outpath)/$(outname)_pid_$(pid).$(szPostfix)文件
*@note
* 
*@since    2014-9-25 14:27
*@attention
* 
*/
GPrivate FILE* file_open_fpostfix(String_t szPostfix, String_t szFileName)
{
    FILE *pFile = NULL;
    String_t szOutpath = agentargs_get_outpath();
    String_t szOutname = agentargs_get_outname();

    os_sprintf(szFileName, "%s%s%s_pid_%d.%s",
        (szOutpath != NULL) ? szOutpath : "",
        (szOutpath != NULL) ? "/" : "",
        (szOutname != NULL) ? szOutname : "gcmon",
        os_getpid(),
        (szPostfix != NULL) ? szPostfix : "unknown");

    pFile = os_fopen(szFileName, "w+");

    if (NULL == pFile)
    {
        szFileName[0] = '\0';
    }

    return pFile;
}
Exemple #30
0
static int ls_open_log( ChannelLog *cl )
{
	static char fname[MAXPATH];

	/* first, make sure the logdir dir exists */
	if( os_create_dir( LogServ.logdir ) != NS_SUCCESS )
	{
		return NS_FAILURE;
	}
	/* copy name to the filename holder( in case of invalid paths ) */
	strlcpy( cl->filename, cl->channame, MAXPATH );
	ircsnprintf( fname, MAXPATH, "%s/%s.log", LogServ.logdir, make_safe_filename( cl->filename ) );
	/* open the file */
	cl->logfile = os_fopen( fname, "a" );
	if( cl->logfile == NULL )
	{
		nlog( LOG_CRITICAL, "Could not open %s for appending: %s", cl->filename, os_strerror() );
		return NS_FAILURE;
	}
	dlog( DEBUG1, "Opened %s for appending", cl->filename );
	cl->ts_open = me.now;
	logging_funcs[LogServ.logtype][LGSMSG_START]( cl, NULL );
	return NS_SUCCESS;
}