add common alloc functions and handle cli argument
This commit is contained in:
parent
3969ddc7dc
commit
4fdeb8c56b
4 changed files with 52 additions and 42 deletions
20
ccc.c
20
ccc.c
|
@ -44,6 +44,7 @@ bool to_open_file = false;
|
|||
bool dirs_size = DIRS_SIZE;
|
||||
bool show_hidden = SHOW_HIDDEN;
|
||||
bool file_details = SHOW_DETAILS;
|
||||
char *argv_cp;
|
||||
char *trash_dir;
|
||||
char *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])) {
|
||||
perror("ccc");
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -125,7 +131,7 @@ int main(int argc, char** argv)
|
|||
|
||||
populate_files(cwd, 0);
|
||||
if (to_open_file) {
|
||||
current_selection = arraylist_search(files, argv[1], true);
|
||||
current_selection = arraylist_search(files, argv_cp, true);
|
||||
highlight_current_line();
|
||||
}
|
||||
|
||||
|
@ -439,10 +445,7 @@ void change_dir(const char *buf, int selection, int ftype)
|
|||
{
|
||||
char *buf_dup;
|
||||
if (buf == p_cwd) {
|
||||
buf_dup = strdup(p_cwd);
|
||||
if (buf_dup == NULL) {
|
||||
return;
|
||||
}
|
||||
buf_dup = estrdup(p_cwd);
|
||||
} else {
|
||||
buf_dup = (char *) buf;
|
||||
}
|
||||
|
@ -656,6 +659,9 @@ void add_file_stat(char *filename, char *path, int ftype)
|
|||
}
|
||||
/* get file mode string */
|
||||
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 */
|
||||
size_t stat_size = 11 * sizeof(char) + time_size + size_size + 3 * sizeof(char);
|
||||
|
|
45
file.c
45
file.c
|
@ -83,31 +83,16 @@ void arraylist_add(ArrayList *list, char *name, char *path, char *stats, char *t
|
|||
char *stats_cp = NULL;
|
||||
wchar_t *icon_cp = NULL;
|
||||
|
||||
if (name != NULL) {
|
||||
name_cp = strdup(name);
|
||||
if (name_cp == NULL)
|
||||
perror("can't add name to arraylist");
|
||||
}
|
||||
if (path != NULL) {
|
||||
path_cp = strdup(path);
|
||||
if (path_cp == NULL)
|
||||
perror("can't add path to arraylist");
|
||||
}
|
||||
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");
|
||||
}
|
||||
if (name != NULL)
|
||||
name_cp = estrdup(name);
|
||||
if (path != NULL)
|
||||
path_cp = estrdup(path);
|
||||
if (type != NULL)
|
||||
type_cp = estrdup(type);
|
||||
if (stats != NULL)
|
||||
stats_cp = estrdup(stats);
|
||||
if (icon != NULL)
|
||||
icon_cp = ewcsdup(icon);
|
||||
|
||||
/* name, path, stats, type, icon, 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)
|
||||
{
|
||||
file file = list->items[index];
|
||||
char *name = strdup(file.name);
|
||||
wchar_t *icon = wcsdup(file.icon);
|
||||
if (name == NULL || icon == NULL)
|
||||
perror("ccc");
|
||||
char *name = estrdup(file.name);
|
||||
wchar_t *icon = ewcsdup(file.icon);
|
||||
|
||||
size_t name_len = strlen(name);
|
||||
char *stats = NULL;
|
||||
size_t length;
|
||||
if (detail) {
|
||||
stats = strdup(file.stats);
|
||||
if (stats == NULL)
|
||||
perror("ccc");
|
||||
stats = estrdup(file.stats);
|
||||
length = name_len + strlen(stats) + 7; /* 4 for icon, 2 for space and 1 for null */
|
||||
} else {
|
||||
length = name_len + 6; /* 4 for icon, 1 for space and 1 for null */
|
||||
|
|
25
util.c
25
util.c
|
@ -1,5 +1,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <wchar.h>
|
||||
|
||||
void die(char *reason)
|
||||
{
|
||||
|
@ -11,18 +13,37 @@ void *memalloc(size_t size)
|
|||
{
|
||||
void *ptr = malloc(size);
|
||||
if (!ptr) {
|
||||
fprintf(stderr, "ccc: Error allocating memory\n");
|
||||
perror("ccc");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
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)
|
||||
{
|
||||
ptr = realloc(ptr, size);
|
||||
if (!ptr) {
|
||||
perror("ccc");
|
||||
fprintf(stderr, "ccc: Error allocating memory\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return ptr;
|
||||
|
|
2
util.h
2
util.h
|
@ -5,6 +5,8 @@
|
|||
|
||||
void die(char *reason);
|
||||
void *memalloc(size_t size);
|
||||
void *estrdup(void *ptr);
|
||||
void *ewcsdup(void *ptr);
|
||||
void *rememalloc(void *ptr, size_t size);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue