fix resource leak and replace deprecated usleep

This commit is contained in:
rexy712 2018-05-10 15:41:33 -07:00
parent cb71aa57ea
commit 9d55a23e82

View File

@ -25,6 +25,7 @@
#include <unistd.h>
#include <dirent.h>
#include <sys/time.h>
#include <time.h>
#include "common.h"
#include "cmd.h"
@ -91,6 +92,7 @@ struct string_array get_device_sources(void){
arr.list = malloc(sizeof(const char*) * list_size);
if(!arr.list){
closedir(fd);
mem_error();
return_value = RETVAL_MEM_ERROR;
return arr;
@ -160,7 +162,7 @@ float get_brightness(const char* file){
void fade_out(const char* device_name, int iv, int fv, int ms_duration, int steps){
FILE* fd;
struct timeval start, end;
double tdelta = 0; //amount of time that has passed
double tdelta = 0; //amount of time that has passed in ms
float value = iv; //current value to write to file
int step_delta = ms_duration / steps;
int step_inc = step_delta;
@ -187,7 +189,10 @@ void fade_out(const char* device_name, int iv, int fv, int ms_duration, int step
//waste excess time until next step
if(step_delta > tdelta){
usleep((step_delta - tdelta) * 1000);
struct timespec ts;
ts.tv_sec = (long)((step_delta - tdelta)/1000);
ts.tv_nsec = ((step_delta - tdelta) - ts.tv_sec*1000) * 1000*1000;
nanosleep(&ts, NULL);
tdelta = step_delta;
}
//calc end of next step
@ -361,11 +366,10 @@ int main(int argc, char** argv){
if(chdir(starting_dir)){
CLEANUP();
fprintf(stderr, "Could not return to starting directory!\nWas the directory moved/deleted?\n");
if(!chdir(getenv("HOME")) || !chdir("/")){
return RETVAL_INVALID_DIR;
}else{
return RETVAL_INVALID_DIR;
if(chdir(getenv("HOME"))){
chdir("/");
}
return RETVAL_INVALID_DIR;
}
CLEANUP();