Compare commits

...

2 Commits

Author SHA1 Message Date
nova
3fa16fd8b2 changes too small for proper commit message 2025-10-09 01:01:52 +02:00
nova
ac6d0e8408 avoiding useless memory read/writes & fixing race condition in btm_status 2025-10-09 00:13:25 +02:00
4 changed files with 32 additions and 18 deletions

View File

@@ -69,8 +69,11 @@ static const binding key_binding[] = {
{ "G", jump_bottom, NULL, "jump to last file in dir" }, { "G", jump_bottom, NULL, "jump to last file in dir" },
{ "gg", jump_top, NULL, "jump to first file in dir" }, { "gg", jump_top, NULL, "jump to first file in dir" },
{ "gh", jump_to_dir, "$HOME", "jump to $HOME" }, { "gh", jump_to_dir, "$HOME", "jump to $HOME" },
{ "gs", jump_to_dir, "$START_PATH", "jump to $START_PATH" }, { "gs", jump_to_dir, "$START_PATH", "jump to $START_PATH" }, /* the path you started th in */
{ "gD", jump_to_dir, "$HOME/Downloads", "jump to $HOME/Downloads" }, { "gD", jump_to_dir, "$HOME/Downloads", "jump to $HOME/Downloads" },
{ "gC", jump_to_dir, "$HOME/Documents", "jump to $HOME/Documents" },
{ "gP", jump_to_dir, "$HOME/Pictures", "jump to $HOME/Pictures" },
{ "gM", jump_to_dir, "$HOME/Music", "jump to $HOME/Music" },
{ "gd", jump_to_dir, "/dev", "jump to /dev" }, { "gd", jump_to_dir, "/dev", "jump to /dev" },
{ "ge", jump_to_dir, "/etc", "jump to /etc" }, { "ge", jump_to_dir, "/etc", "jump to /etc" },
{ "gp", jump_to_dir, "/etc/portage", "jump to /etc/portage" }, { "gp", jump_to_dir, "/etc/portage", "jump to /etc/portage" },
@@ -87,11 +90,10 @@ static const binding key_binding[] = {
{ "uz", cmd_on_selected, "unzip ", "unzip zip" }, { "uz", cmd_on_selected, "unzip ", "unzip zip" },
{ "on", order_by, sort_natural, "order natural" }, { "on", order_by, sort_natural, "order natural" },
{ "or", not_implemented, "", "order reverse" }, { "oe", not_implemented, NULL, "order extension" },
{ "oe", not_implemented, "", "order extension" },
{ "os", order_by, sort_size, "order size" }, { "os", order_by, sort_size, "order size" },
{ "ot", order_by, sort_type, "order type" }, { "ot", order_by, sort_type, "order type" },
{ "oz", order_by, sort_random, "order random" }, { "or", order_by, sort_random, "order random" },
{ "oa", order_by, sort_alpha, "order alphabetically" }, { "oa", order_by, sort_alpha, "order alphabetically" },
{ "mk", makedir, NULL, "create directory" }, { "mk", makedir, NULL, "create directory" },

View File

@@ -642,6 +642,7 @@ void order_by(int passes, int index){
(void)passes; (void)passes;
free(seed); free(seed);
seed = NULL;
seed = malloc(sizeof(time_t)); seed = malloc(sizeof(time_t));
*seed = time(NULL); *seed = time(NULL);

View File

@@ -119,15 +119,15 @@ int sort_random(const void *file0, const void *file1){
return 1; return 1;
} }
time_t num = (time_t)&seed; time_t num = (time_t)&seed;
unsigned long i; time_t i;
for (i = 0; i < 6; i++){ for (i = num%2; i < 6+(((time_t)&seed)%2); i++){
num ^= *seed; num ^= *seed;
if (num%2) { if (num%2) {
num ^= (time_t)&num; num ^= (time_t)&num;
num ^= num << i; num ^= num << (i + (((time_t)&seed)%5));
} else { } else {
num ^= (time_t)&seed; num ^= (time_t)&seed;
num ^= num >> i; num ^= num >> (i + (((time_t)&num)%5));
} }
} }
num ^= getpid(); num ^= getpid();

View File

@@ -48,6 +48,8 @@ volatile unsigned long selected_file_last = 0;
extern unsigned int terminal_width; extern unsigned int terminal_width;
extern unsigned int status; extern unsigned int status;
unsigned int btm_status;
void *thread_mid(){ void *thread_mid(){
@@ -93,6 +95,7 @@ void *thread_mid(){
pthread_mutex_unlock(&mutex_selection); pthread_mutex_unlock(&mutex_selection);
} }
btm_status = local_status;
pthread_cond_signal(&cond_rgt); pthread_cond_signal(&cond_rgt);
pthread_cond_signal(&cond_btm); pthread_cond_signal(&cond_btm);
@@ -233,7 +236,8 @@ void *thread_top(){
top_width = sizeof("cannot open directory"); top_width = sizeof("cannot open directory");
top_buffer = "cannot open directory"; top_buffer = "cannot open directory";
} else { } else {
top_buffer = getcwd(NULL, 0); top_buffer = malloc(strlen(path)+1);
memcpy(top_buffer, path, strlen(path)+1);
top_width = strlen(top_buffer); top_width = strlen(top_buffer);
} }
@@ -246,21 +250,21 @@ void *thread_btm(){
char *path = malloc(sizeof(char)); char *path = malloc(sizeof(char));
char *ui_btm_right_block = malloc(sizeof(char)); char *ui_btm_right_block = malloc(sizeof(char));
unsigned int ui_btm_right_block_size = 0; unsigned int ui_btm_right_block_size = 0;
unsigned int buffer_width = 0;
while(!(status & STATUS_QUIT_PROGRAM)){ while(!(status & STATUS_QUIT_PROGRAM)){
pthread_mutex_lock(&mutex_btm); pthread_mutex_lock(&mutex_btm);
pthread_cond_wait(&cond_btm, &mutex_btm); pthread_cond_wait(&cond_btm, &mutex_btm);
unsigned int local_status = status; unsigned int local_status = btm_status;
int buffer_width = terminal_width;
if (local_status & (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY)) { if (local_status & (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY)) {
/*{{{ parse storage info */ /*{{{ parse storage info; the fold of shame*/
pthread_mutex_lock(&mutex_mid); pthread_mutex_lock(&mutex_mid);
unsigned long i; unsigned long i;
float total_dir_size = 0; float total_dir_size = 0;
for(i = 0; i < mid_file_count; i++) { for(i = 0; i < mid_file_count; i++) {
if (!(mid_content[i].file_type & (FILE_TYPE_DIR))) { if ((mid_content[i].file_type & (FILE_TYPE_DIR)) != FILE_TYPE_DIR) {
total_dir_size += mid_content[i].file_size; total_dir_size += mid_content[i].file_size;
} }
} }
@@ -290,6 +294,7 @@ void *thread_btm(){
} }
} else { } else {
size_index[0] = 0; size_index[0] = 0;
parsed_number[0] = 0;
} }
if (disk_size_free > 1) { if (disk_size_free > 1) {
@@ -314,7 +319,7 @@ void *thread_btm(){
ui_btm_right_block_size = snprintf(NULL, 0, "%0.2lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left)+1; ui_btm_right_block_size = snprintf(NULL, 0, "%0.2lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left)+1;
ui_btm_right_block = malloc(ui_btm_right_block_size); ui_btm_right_block = malloc(ui_btm_right_block_size);
sprintf(ui_btm_right_block, "%0.2lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left); sprintf(ui_btm_right_block, "%0.2lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left);
} else if (size_index[0] <= 0 && size_index[1] <= 0) { } else {
ui_btm_right_block_size = snprintf(NULL, 0, "%0.0lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left)+1; ui_btm_right_block_size = snprintf(NULL, 0, "%0.0lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left)+1;
ui_btm_right_block = malloc(ui_btm_right_block_size); ui_btm_right_block = malloc(ui_btm_right_block_size);
sprintf(ui_btm_right_block, "%0.0lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left); sprintf(ui_btm_right_block, "%0.0lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left);
@@ -322,10 +327,17 @@ void *thread_btm(){
/*}}}*/ /*}}}*/
} }
free(btm_buffer); if (buffer_width != terminal_width) {
btm_buffer = malloc(buffer_width); buffer_width = terminal_width;
memset(btm_buffer, ' ', buffer_width); free(btm_buffer);
btm_buffer = malloc(buffer_width);
memset(btm_buffer, ' ', buffer_width/2);
}
memset(btm_buffer + (buffer_width/2), ' ', buffer_width/2);
btm_buffer[buffer_width] = '\0'; btm_buffer[buffer_width] = '\0';
memcpy(btm_buffer + buffer_width - ui_btm_right_block_size, ui_btm_right_block, ui_btm_right_block_size);
btm_buffer[0] = (S_ISDIR(mid_content[selected_file_current].permissions)) ? 'd' : '-'; btm_buffer[0] = (S_ISDIR(mid_content[selected_file_current].permissions)) ? 'd' : '-';
btm_buffer[1] = (mid_content[selected_file_current].permissions & S_IRUSR) ? 'r' : '-'; btm_buffer[1] = (mid_content[selected_file_current].permissions & S_IRUSR) ? 'r' : '-';
btm_buffer[2] = (mid_content[selected_file_current].permissions & S_IWUSR) ? 'w' : '-'; btm_buffer[2] = (mid_content[selected_file_current].permissions & S_IWUSR) ? 'w' : '-';
@@ -338,7 +350,6 @@ void *thread_btm(){
btm_buffer[9] = (mid_content[selected_file_current].permissions & S_IXOTH) ? 'x' : '-'; btm_buffer[9] = (mid_content[selected_file_current].permissions & S_IXOTH) ? 'x' : '-';
memcpy(btm_buffer + buffer_width - ui_btm_right_block_size, ui_btm_right_block, ui_btm_right_block_size);
pthread_mutex_unlock(&mutex_btm); pthread_mutex_unlock(&mutex_btm);
} }