Remove stdbool.h and fix calling on files

This commit is contained in:
Night Kaly 2024-11-17 14:49:24 +00:00
parent 7a98095d0b
commit 65c06ff53f
Signed by: night0721
SSH key fingerprint: SHA256:B/hgVwUoBpx5vdNsXl9w8XwZljA9766uk6T4ubZp5HM
6 changed files with 69 additions and 66 deletions

73
ccc.c
View file

@ -13,11 +13,35 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include "config.h"
#include "file.h" #include "file.h"
#include "icons.h" #include "icons.h"
#include "util.h" #include "util.h"
/* Keybindings */
enum keys {
CTRLD = 0x04,
ENTER = 0xD,
CTRLU = 0x15,
SPACE = 0x20,
TILDE = 0x7E,
BACKSPACE = 127,
ARROW_LEFT = 1000,
ARROW_RIGHT,
ARROW_UP,
ARROW_DOWN,
DEL_KEY,
HOME_KEY,
END_KEY,
PAGE_UP,
PAGE_DOWN,
};
typedef struct {
int key;
} key;
#define PATH_MAX 4096 /* Max length of path */
#include "config.h"
void handle_sigwinch(int ignore); void handle_sigwinch(int ignore);
void cleanup(void); void cleanup(void);
void show_help(void); void show_help(void);
@ -51,8 +75,8 @@ void bprintf(const char *fmt, ...);
/* global variables */ /* global variables */
unsigned int focus = 0; unsigned int focus = 0;
long sel_file = 0; long sel_file = 0;
bool file_picker = false; int file_picker = 1;
bool to_open_file = false; int to_open_file = 0;
char *argv_cp; char *argv_cp;
char *cwd; char *cwd;
char *p_cwd; /* previous cwd */ char *p_cwd; /* previous cwd */
@ -70,7 +94,7 @@ int main(int argc, char **argv)
{ {
if (argc == 3) { if (argc == 3) {
if (strncmp(argv[2], "-p", 2) == 0) if (strncmp(argv[2], "-p", 2) == 0)
file_picker = true; file_picker = 1;
} }
if (argc <= 3 && argc != 1) { if (argc <= 3 && argc != 1) {
if (strncmp(argv[1], "-h", 2) == 0) if (strncmp(argv[1], "-h", 2) == 0)
@ -88,12 +112,14 @@ int main(int argc, char **argv)
} else if (S_ISREG(st.st_mode)) { } else if (S_ISREG(st.st_mode)) {
argv_cp = estrdup(argv[1]); argv_cp = estrdup(argv[1]);
char *last_slash = strrchr(argv_cp, '/'); char *last_slash = strrchr(argv_cp, '/');
if (last_slash) {
*last_slash = '\0'; *last_slash = '\0';
if (chdir(argv[1])) { if (chdir(argv[1])) {
perror("ccc"); perror("ccc");
die("Error from chdir"); die("Error from chdir");
} }
to_open_file = true; }
to_open_file = 1;
} }
} }
@ -138,7 +164,7 @@ int main(int argc, char **argv)
handle_sigwinch(-1); handle_sigwinch(-1);
if (to_open_file) { if (to_open_file) {
sel_file = arraylist_search(files, argv_cp, true); sel_file = arraylist_search(files, argv_cp, 1);
list_files(); list_files();
} }
@ -597,8 +623,7 @@ void add_file_stat(char *filename, char *path, int ftype)
if (stat(path, &file_stat) == -1) { if (stat(path, &file_stat) == -1) {
/* can't be triggered? */ /* can't be triggered? */
if (errno == EACCES) if (errno == EACCES)
arraylist_add(files, filename, path, NULL, REG, NULL, DEF_COLOR, false, arraylist_add(files, filename, path, NULL, REG, NULL, DEF_COLOR, 0, 0);
false);
} }
int type; int type;
@ -648,8 +673,8 @@ void add_file_stat(char *filename, char *path, int ftype)
/* If file is to be marked */ /* If file is to be marked */
if (ftype == 1 || ftype == 2) { if (ftype == 1 || ftype == 2) {
/* Force if user is marking all files */ /* Force if user is marking all files */
bool force = ftype == 2 ? true : false; int force = ftype == 2 ? 1 : 0;
arraylist_add(marked, filename, path, NULL, type, icon_str, DEF_COLOR, true, arraylist_add(marked, filename, path, NULL, type, icon_str, DEF_COLOR, 1,
force); force);
/* free type and return without allocating more stuff */ /* free type and return without allocating more stuff */
return; return;
@ -665,7 +690,7 @@ void add_file_stat(char *filename, char *path, int ftype)
double bytes = file_stat.st_size; double bytes = file_stat.st_size;
if (dirs_size) { if (dirs_size) {
/* dirs_size is true, so calculate disk usage */ /* dirs_size is 1, so calculate disk usage */
if (S_ISDIR(file_stat.st_mode)) { if (S_ISDIR(file_stat.st_mode)) {
/* at most 15 fd opened */ /* at most 15 fd opened */
total_dir_size = 0; total_dir_size = 0;
@ -702,9 +727,9 @@ void add_file_stat(char *filename, char *path, int ftype)
/* DIR if color is blue */ /* DIR if color is blue */
if (color == 34) if (color == 34)
arraylist_add(tmp1, filename, path, total_stat, type, icon_str, color, false, false); arraylist_add(tmp1, filename, path, total_stat, type, icon_str, color, 0, 0);
else else
arraylist_add(tmp2, filename, path, total_stat, type, icon_str, color, false, false); arraylist_add(tmp2, filename, path, total_stat, type, icon_str, color, 0, 0);
free(time); free(time);
free(size); free(size);
@ -788,7 +813,7 @@ void list_files(void)
char *line = get_line(files, i, show_details, show_icons); char *line = get_line(files, i, show_details, show_icons);
int color = files->items[i].color; int color = files->items[i].color;
/* check is file marked for action */ /* check is file marked for action */
bool is_marked = arraylist_search(marked, files->items[i].path, false) != -1; int is_marked = arraylist_search(marked, files->items[i].path, 0) != -1;
move_cursor(i + 1, 1); move_cursor(i + 1, 1);
if (is_marked) color = 32; if (is_marked) color = 32;
bprintf("\033[30m\033[%dm%s\033[0m\n", bprintf("\033[30m\033[%dm%s\033[0m\n",
@ -804,13 +829,13 @@ void list_files(void)
size_t get_effective_length(const char *str) size_t get_effective_length(const char *str)
{ {
size_t length = 0; size_t length = 0;
bool in_ansi_sequence = false; int in_ansi_sequence = 0;
while (*str) { while (*str) {
if (*str == '\033') { if (*str == '\033') {
in_ansi_sequence = true; in_ansi_sequence = 1;
} else if (in_ansi_sequence && *str == 'm') { } else if (in_ansi_sequence && *str == 'm') {
in_ansi_sequence = false; in_ansi_sequence = 0;
} else if (!in_ansi_sequence) { } else if (!in_ansi_sequence) {
length++; length++;
} }
@ -822,17 +847,17 @@ size_t get_effective_length(const char *str)
void print_chunk(const char *str, size_t start, size_t end) void print_chunk(const char *str, size_t start, size_t end)
{ {
bool in_ansi_sequence = false; int in_ansi_sequence = 0;
size_t printed_length = 0; size_t printed_length = 0;
for (size_t i = start; i < end; i++) { for (size_t i = start; i < end; i++) {
if (str[i] == '\033') { if (str[i] == '\033') {
in_ansi_sequence = true; in_ansi_sequence = 1;
} }
if (in_ansi_sequence) { if (in_ansi_sequence) {
putchar(str[i]); putchar(str[i]);
if (str[i] == 'm') { if (str[i] == 'm') {
in_ansi_sequence = false; in_ansi_sequence = 0;
} }
} else { } else {
putchar(str[i]); putchar(str[i]);
@ -856,7 +881,7 @@ void show_file_content(void)
ArrayList *files_visit; ArrayList *files_visit;
populate_files(current_file.name, 0, &files_visit); populate_files(current_file.name, 0, &files_visit);
for (long i = 0; i < files_visit->length; i++) { for (long i = 0; i < files_visit->length; i++) {
char *line = get_line(files_visit, i, false, show_icons); char *line = get_line(files_visit, i, 0, show_icons);
int color = files_visit->items[i].color; int color = files_visit->items[i].color;
move_cursor(i + 1, half_width); move_cursor(i + 1, half_width);
bprintf("\033[K\033[%dm%s\033[m\n", color, line); bprintf("\033[K\033[%dm%s\033[m\n", color, line);
@ -919,17 +944,17 @@ void show_file_content(void)
/* Find the actual end position in the string to avoid cutting ANSI sequences */ /* Find the actual end position in the string to avoid cutting ANSI sequences */
size_t actual_end = offset; size_t actual_end = offset;
size_t visible_count = 0; size_t visible_count = 0;
bool in_ansi_sequence = false; int in_ansi_sequence = 0;
while (visible_count < chunk_size && buffer[actual_end] != '\0') { while (visible_count < chunk_size && buffer[actual_end] != '\0') {
if (buffer[actual_end] == '\033') { if (buffer[actual_end] == '\033') {
in_ansi_sequence = true; in_ansi_sequence = 1;
} }
if (!in_ansi_sequence) { if (!in_ansi_sequence) {
visible_count++; visible_count++;
} }
if (in_ansi_sequence && buffer[actual_end] == 'm') { if (in_ansi_sequence && buffer[actual_end] == 'm') {
in_ansi_sequence = false; in_ansi_sequence = 0;
} }
actual_end++; actual_end++;
} }

View file

@ -1,6 +1,3 @@
#define PATH_MAX 4096 /* Max length of path */
/* Settings */
static int panel_height = 1; /* Panel height */ static int panel_height = 1; /* Panel height */
static int jump_num = 14; /* Length of ctrl + u/d jump */ static int jump_num = 14; /* Length of ctrl + u/d jump */
static int decimal_place = 1; /* Number of decimal places size can be shown */ static int decimal_place = 1; /* Number of decimal places size can be shown */
@ -51,22 +48,8 @@ static char last_d[PATH_MAX] = "~/.cache/ccc/.ccc_d";
/* Will create this directory if doesn't exist! */ /* Will create this directory if doesn't exist! */
static char trash_dir[PATH_MAX] = "~/.cache/ccc/trash/"; static char trash_dir[PATH_MAX] = "~/.cache/ccc/trash/";
/* Keybindings */ /*
#define CTRLD 0x04 key keybindings[] = {
#define ENTER 0xD
#define CTRLU 0x15
#define SPACE 0x20
#define TILDE 0x7E
enum keys { }
BACKSPACE = 127, */
ARROW_LEFT = 1000,
ARROW_RIGHT,
ARROW_UP,
ARROW_DOWN,
DEL_KEY,
HOME_KEY,
END_KEY,
PAGE_UP,
PAGE_DOWN
};

9
file.c
View file

@ -1,6 +1,5 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include "util.h" #include "util.h"
#include "file.h" #include "file.h"
@ -34,9 +33,9 @@ void arraylist_free(ArrayList *list)
/* /*
* Check if the file is in the arraylist * Check if the file is in the arraylist
* Treat filepath as base name if bname is true * Treat filepath as base name if bname is 1
*/ */
long arraylist_search(ArrayList *list, char *filepath, bool bname) long arraylist_search(ArrayList *list, char *filepath, int bname)
{ {
for (long i = 0; i < list->length; i++) { for (long i = 0; i < list->length; i++) {
if (!bname && strcmp(list->items[i].path, filepath) == 0) { if (!bname && strcmp(list->items[i].path, filepath) == 0) {
@ -71,7 +70,7 @@ void arraylist_remove(ArrayList *list, long index)
/* /*
* Force will not remove duplicate marked files, instead it just skip adding * Force will not remove duplicate marked files, instead it just skip adding
*/ */
void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int type, char *icon, int color, bool marked, bool force) void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int type, char *icon, int color, int marked, int force)
{ {
file new_file = { name, path, type, stats, icon, color }; file new_file = { name, path, type, stats, icon, color };
@ -105,7 +104,7 @@ void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int typ
/* /*
* Construct a formatted line for display * Construct a formatted line for display
*/ */
char *get_line(ArrayList *list, long index, bool detail, bool icons) char *get_line(ArrayList *list, long index, int detail, int icons)
{ {
file file = list->items[index]; file file = list->items[index];

7
file.h
View file

@ -2,7 +2,6 @@
#define FILE_H_ #define FILE_H_
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
enum ftypes { enum ftypes {
REG, REG,
@ -31,9 +30,9 @@ typedef struct ArrayList {
ArrayList *arraylist_init(size_t capacity); ArrayList *arraylist_init(size_t capacity);
void arraylist_free(ArrayList *list); void arraylist_free(ArrayList *list);
long arraylist_search(ArrayList *list, char *filepath, bool bname); long arraylist_search(ArrayList *list, char *filepath, int bname);
void arraylist_remove(ArrayList *list, long index); void arraylist_remove(ArrayList *list, long index);
void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int type, char *icon, int color, bool marked, bool force); void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int type, char *icon, int color, int marked, int force);
char *get_line(ArrayList *list, long index, bool detail, bool icons); char *get_line(ArrayList *list, long index, int detail, int icons);
#endif #endif

View file

@ -1,7 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdbool.h>
#include "icons.h" #include "icons.h"
#include "util.h" #include "util.h"
@ -153,9 +152,9 @@ void hashtable_print(void)
} }
/* Gets hashed name and tries to store the icon struct in that place */ /* Gets hashed name and tries to store the icon struct in that place */
bool hashtable_add(icon *p) int hashtable_add(icon *p)
{ {
if (p == NULL) return false; if (p == NULL) return 0;
int index = hash(p->name); int index = hash(p->name);
int initial_index = index; int initial_index = index;
@ -163,11 +162,11 @@ bool hashtable_add(icon *p)
while (hash_table[index] != NULL) { while (hash_table[index] != NULL) {
index = (index + 1) % TABLE_SIZE; /* move to next item */ index = (index + 1) % TABLE_SIZE; /* move to next item */
/* the hash table is full as no available index back to initial index, cannot fit new item */ /* the hash table is full as no available index back to initial index, cannot fit new item */
if (index == initial_index) return false; if (index == initial_index) return 0;
} }
hash_table[index] = p; hash_table[index] = p;
return true; return 1;
} }
/* Rehashes the name and then looks in this spot, if found returns icon */ /* Rehashes the name and then looks in this spot, if found returns icon */

View file

@ -1,8 +1,6 @@
#ifndef ICONS_H_ #ifndef ICONS_H_
#define ICONS_H_ #define ICONS_H_
#include <stdbool.h>
#define MAX_NAME 30 #define MAX_NAME 30
#define TABLE_SIZE 100 #define TABLE_SIZE 100
@ -14,7 +12,7 @@ typedef struct {
unsigned int hash(char *name); unsigned int hash(char *name);
void hashtable_init(void); void hashtable_init(void);
void hashtable_print(void); void hashtable_print(void);
bool hashtable_add(icon *p); int hashtable_add(icon *p);
icon *hashtable_search(char *name); icon *hashtable_search(char *name);
#endif #endif