add common alloc functions and handle cli argument

This commit is contained in:
Night Kaly 2024-04-01 21:23:10 +00:00
parent 3969ddc7dc
commit 4fdeb8c56b
Signed by: night0721
GPG key ID: 957D67B8DB7A119B
4 changed files with 52 additions and 42 deletions

20
ccc.c
View file

@ -44,6 +44,7 @@ bool to_open_file = false;
bool dirs_size = DIRS_SIZE; bool dirs_size = DIRS_SIZE;
bool show_hidden = SHOW_HIDDEN; bool show_hidden = SHOW_HIDDEN;
bool file_details = SHOW_DETAILS; bool file_details = SHOW_DETAILS;
char *argv_cp;
char *trash_dir; char *trash_dir;
char *cwd; char *cwd;
char *p_cwd; /* previous cwd */ char *p_cwd; /* previous cwd */
@ -73,9 +74,14 @@ int main(int argc, char** argv)
if (S_ISDIR(st.st_mode) && chdir(argv[1])) { if (S_ISDIR(st.st_mode) && chdir(argv[1])) {
perror("ccc"); perror("ccc");
die("Error from chdir"); die("Error from chdir");
} else if (S_ISREG(st.st_mode)) {
argv_cp = estrdup(argv[1]);
char *last_slash = strrchr(argv_cp, '/');
*last_slash = '\0';
if (chdir(argv[1])) {
perror("ccc");
die("Error from chdir");
} }
if (S_ISREG(st.st_mode)) {
// char *rm_f_name = strrchr(argv[1], '/');
to_open_file = true; to_open_file = true;
} }
} }
@ -125,7 +131,7 @@ int main(int argc, char** argv)
populate_files(cwd, 0); populate_files(cwd, 0);
if (to_open_file) { if (to_open_file) {
current_selection = arraylist_search(files, argv[1], true); current_selection = arraylist_search(files, argv_cp, true);
highlight_current_line(); highlight_current_line();
} }
@ -439,10 +445,7 @@ void change_dir(const char *buf, int selection, int ftype)
{ {
char *buf_dup; char *buf_dup;
if (buf == p_cwd) { if (buf == p_cwd) {
buf_dup = strdup(p_cwd); buf_dup = estrdup(p_cwd);
if (buf_dup == NULL) {
return;
}
} else { } else {
buf_dup = (char *) buf; buf_dup = (char *) buf;
} }
@ -656,6 +659,9 @@ void add_file_stat(char *filename, char *path, int ftype)
} }
/* get file mode string */ /* get file mode string */
char *mode_str = get_file_mode(file_stat.st_mode); char *mode_str = get_file_mode(file_stat.st_mode);
if (mode_str[0] == '-' && (mode_str[3] == 'x' || mode_str[6] == 'x' || mode_str[9] == 'x')) {
}
/* mode_str(11) + time(17) + size_size + 2 spaces + 1 null */ /* mode_str(11) + time(17) + size_size + 2 spaces + 1 null */
size_t stat_size = 11 * sizeof(char) + time_size + size_size + 3 * sizeof(char); size_t stat_size = 11 * sizeof(char) + time_size + size_size + 3 * sizeof(char);

45
file.c
View file

@ -83,31 +83,16 @@ void arraylist_add(ArrayList *list, char *name, char *path, char *stats, char *t
char *stats_cp = NULL; char *stats_cp = NULL;
wchar_t *icon_cp = NULL; wchar_t *icon_cp = NULL;
if (name != NULL) { if (name != NULL)
name_cp = strdup(name); name_cp = estrdup(name);
if (name_cp == NULL) if (path != NULL)
perror("can't add name to arraylist"); path_cp = estrdup(path);
} if (type != NULL)
if (path != NULL) { type_cp = estrdup(type);
path_cp = strdup(path); if (stats != NULL)
if (path_cp == NULL) stats_cp = estrdup(stats);
perror("can't add path to arraylist"); if (icon != NULL)
} icon_cp = ewcsdup(icon);
if (type != NULL) {
type_cp = strdup(type);
if (type_cp == NULL)
perror("can't add type to arraylist");
}
if (stats != NULL) {
stats_cp = strdup(stats);
if (stats_cp == NULL)
perror("can't add stats to arraylist");
}
if (icon != NULL) {
icon_cp = wcsdup(icon);
if (icon_cp == NULL)
perror("can't add icon to arraylist");
}
/* name, path, stats, type, icon, color */ /* name, path, stats, type, icon, color */
file new_file = { name_cp, path_cp, type_cp, stats_cp, icon_cp, color }; file new_file = { name_cp, path_cp, type_cp, stats_cp, icon_cp, color };
@ -145,18 +130,14 @@ void arraylist_add(ArrayList *list, char *name, char *path, char *stats, char *t
char *get_line(ArrayList *list, long index, bool detail) char *get_line(ArrayList *list, long index, bool detail)
{ {
file file = list->items[index]; file file = list->items[index];
char *name = strdup(file.name); char *name = estrdup(file.name);
wchar_t *icon = wcsdup(file.icon); wchar_t *icon = ewcsdup(file.icon);
if (name == NULL || icon == NULL)
perror("ccc");
size_t name_len = strlen(name); size_t name_len = strlen(name);
char *stats = NULL; char *stats = NULL;
size_t length; size_t length;
if (detail) { if (detail) {
stats = strdup(file.stats); stats = estrdup(file.stats);
if (stats == NULL)
perror("ccc");
length = name_len + strlen(stats) + 7; /* 4 for icon, 2 for space and 1 for null */ length = name_len + strlen(stats) + 7; /* 4 for icon, 2 for space and 1 for null */
} else { } else {
length = name_len + 6; /* 4 for icon, 1 for space and 1 for null */ length = name_len + 6; /* 4 for icon, 1 for space and 1 for null */

25
util.c
View file

@ -1,5 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <wchar.h>
void die(char *reason) void die(char *reason)
{ {
@ -11,18 +13,37 @@ void *memalloc(size_t size)
{ {
void *ptr = malloc(size); void *ptr = malloc(size);
if (!ptr) { if (!ptr) {
fprintf(stderr, "ccc: Error allocating memory\n"); perror("ccc");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return ptr; return ptr;
} }
void *estrdup(void *ptr)
{
void *duped = strdup(ptr);
if (!duped) {
perror("ccc");
exit(EXIT_FAILURE);
}
return duped;
}
void *ewcsdup(void *ptr)
{
void *duped = wcsdup(ptr);
if (!duped) {
perror("ccc");
exit(EXIT_FAILURE);
}
return duped;
}
void *rememalloc(void *ptr, size_t size) void *rememalloc(void *ptr, size_t size)
{ {
ptr = realloc(ptr, size); ptr = realloc(ptr, size);
if (!ptr) { if (!ptr) {
perror("ccc"); perror("ccc");
fprintf(stderr, "ccc: Error allocating memory\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return ptr; return ptr;

2
util.h
View file

@ -5,6 +5,8 @@
void die(char *reason); void die(char *reason);
void *memalloc(size_t size); void *memalloc(size_t size);
void *estrdup(void *ptr);
void *ewcsdup(void *ptr);
void *rememalloc(void *ptr, size_t size); void *rememalloc(void *ptr, size_t size);
#endif #endif