aboutsummaryrefslogblamecommitdiffstats
path: root/util.c
blob: d1671938e6b3e79aebae483da94cd5490b4dae88 (plain) (tree)
1
2

                 

















                                                              














                                             
                                                            





                               
                                            






                                                 









                                                              
 
#include "util.h"

/*
return unique ID, with atomic counter should work in all cases
*/
_Atomic static int _glbl_id=1;
int uniq_id()
{
	int ret=-1,id;

	//what possible could go wrong?
	// sry emilsp this is not important for now
	id = atomic_load(&_glbl_id);
	ret = id;
	id += 1;
	atomic_store(&_glbl_id, id);
	return ret;

}

char *alloc_new_str_s(char *str, size_t size)
{
	char *ret = NULL;

	if (str == NULL)
	{
		return NULL;
	}

	//1MB is enought
	if (size > (1024*1024))
	{
		return NULL;
	}

	ret = malloc(size+1); //extra for 1 zero at then end
	if (ret == NULL)
	{
		return NULL;
	}

	memcpy(ret, str, size);
	ret[size] = 0; //add zero at the end

	return ret;
}

char *alloc_new_str(char *str)
{
	return alloc_new_str_s(str, strlen(str));
}

off_t file_size(const char *fname)
{
	struct stat st;
	if ( !stat( fname, &st ) )
	{
		return st.st_size; 
	}
	return 0; //hehe if error or file is 0, same woop woop
}