fix resource leak and replace deprecated usleep
This commit is contained in:
parent
cb71aa57ea
commit
9d55a23e82
@ -25,6 +25,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "cmd.h"
|
#include "cmd.h"
|
||||||
@ -91,6 +92,7 @@ struct string_array get_device_sources(void){
|
|||||||
|
|
||||||
arr.list = malloc(sizeof(const char*) * list_size);
|
arr.list = malloc(sizeof(const char*) * list_size);
|
||||||
if(!arr.list){
|
if(!arr.list){
|
||||||
|
closedir(fd);
|
||||||
mem_error();
|
mem_error();
|
||||||
return_value = RETVAL_MEM_ERROR;
|
return_value = RETVAL_MEM_ERROR;
|
||||||
return arr;
|
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){
|
void fade_out(const char* device_name, int iv, int fv, int ms_duration, int steps){
|
||||||
FILE* fd;
|
FILE* fd;
|
||||||
struct timeval start, end;
|
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
|
float value = iv; //current value to write to file
|
||||||
int step_delta = ms_duration / steps;
|
int step_delta = ms_duration / steps;
|
||||||
int step_inc = step_delta;
|
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
|
//waste excess time until next step
|
||||||
if(step_delta > tdelta){
|
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;
|
tdelta = step_delta;
|
||||||
}
|
}
|
||||||
//calc end of next step
|
//calc end of next step
|
||||||
@ -361,11 +366,10 @@ int main(int argc, char** argv){
|
|||||||
if(chdir(starting_dir)){
|
if(chdir(starting_dir)){
|
||||||
CLEANUP();
|
CLEANUP();
|
||||||
fprintf(stderr, "Could not return to starting directory!\nWas the directory moved/deleted?\n");
|
fprintf(stderr, "Could not return to starting directory!\nWas the directory moved/deleted?\n");
|
||||||
if(!chdir(getenv("HOME")) || !chdir("/")){
|
if(chdir(getenv("HOME"))){
|
||||||
return RETVAL_INVALID_DIR;
|
chdir("/");
|
||||||
}else{
|
|
||||||
return RETVAL_INVALID_DIR;
|
|
||||||
}
|
}
|
||||||
|
return RETVAL_INVALID_DIR;
|
||||||
}
|
}
|
||||||
|
|
||||||
CLEANUP();
|
CLEANUP();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user