add type field in file and calculate real folder size
This commit is contained in:
parent
bf3cbe71d1
commit
6757faf9a6
3 changed files with 123 additions and 26 deletions
106
ccc.c
106
ccc.c
|
@ -24,7 +24,8 @@ typedef struct {
|
||||||
|
|
||||||
/* functions' definitions */
|
/* functions' definitions */
|
||||||
void list_files(const char *path);
|
void list_files(const char *path);
|
||||||
char *get_file_stat(char *filename);
|
long long get_directory_size(const char *path);
|
||||||
|
long add_file_stat(char *filename);
|
||||||
void highlight_current_line();
|
void highlight_current_line();
|
||||||
void show_file_content();
|
void show_file_content();
|
||||||
void edit_file();
|
void edit_file();
|
||||||
|
@ -64,11 +65,12 @@ int main(int argc, char** argv)
|
||||||
endwin();
|
endwin();
|
||||||
die("ccc: Color is not supported in your terminal.\n");
|
die("ccc: Color is not supported in your terminal.\n");
|
||||||
} else {
|
} else {
|
||||||
|
use_default_colors();
|
||||||
start_color();
|
start_color();
|
||||||
}
|
}
|
||||||
|
|
||||||
init_pair(1, COLOR_BLUE, COLOR_BLACK); /* foreground, background */
|
init_pair(1, COLOR_BLUE, -1); /* foreground, background */
|
||||||
init_pair(2, COLOR_CYAN, COLOR_BLACK); /* active color */
|
init_pair(2, COLOR_CYAN, -1); /* active color */
|
||||||
|
|
||||||
half_width = COLS / 2;
|
half_width = COLS / 2;
|
||||||
init_windows();
|
init_windows();
|
||||||
|
@ -119,17 +121,14 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
/* enter directory/open a file */
|
/* enter directory/open a file */
|
||||||
case 'l':;
|
case 'l':;
|
||||||
char *filename = get_filepath(current_selection);
|
file *file = get_file(current_selection);
|
||||||
if (filename != NULL) {
|
if (file != NULL) {
|
||||||
struct stat file_stat;
|
|
||||||
stat(filename, &file_stat);
|
|
||||||
|
|
||||||
/* check if it is directory or regular file */
|
/* check if it is directory or regular file */
|
||||||
if (S_ISDIR(file_stat.st_mode)) {
|
if (strncmp(file->type, "DIR", 3) == 0) {
|
||||||
/* change cwd to directory */
|
/* change cwd to directory */
|
||||||
strcpy(cwd, filename);
|
strcpy(cwd, file->path);
|
||||||
clear_files();
|
clear_files();
|
||||||
list_files(filename);
|
list_files(cwd);
|
||||||
current_selection = 0;
|
current_selection = 0;
|
||||||
highlight_current_line();
|
highlight_current_line();
|
||||||
}
|
}
|
||||||
|
@ -200,12 +199,15 @@ int main(int argc, char** argv)
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
delwin(windows[i].window);
|
delwin(windows[i].window);
|
||||||
}
|
}
|
||||||
|
endwin();
|
||||||
init_windows();
|
init_windows();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
clear_files();
|
||||||
|
clear_marked();
|
||||||
endwin();
|
endwin();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -220,8 +222,7 @@ void list_files(const char *path)
|
||||||
struct dirent *ep;
|
struct dirent *ep;
|
||||||
|
|
||||||
draw_border_title(directory_border, true);
|
draw_border_title(directory_border, true);
|
||||||
dp = opendir(path);
|
if ((dp = opendir(path)) != NULL) {
|
||||||
if (dp != NULL) {
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
/* clear directory window to ready for printing */
|
/* clear directory window to ready for printing */
|
||||||
wclear(directory_content);
|
wclear(directory_content);
|
||||||
|
@ -234,19 +235,16 @@ void list_files(const char *path)
|
||||||
|
|
||||||
/* can't be strncmp as that would filter out the dotfiles */
|
/* can't be strncmp as that would filter out the dotfiles */
|
||||||
if (strcmp(filename, ".") && strcmp(filename, "..")) {
|
if (strcmp(filename, ".") && strcmp(filename, "..")) {
|
||||||
|
|
||||||
/* construct full file path */
|
/* construct full file path */
|
||||||
filename[0] = '\0';
|
filename[0] = '\0';
|
||||||
strcat(filename, cwd);
|
strcat(filename, cwd);
|
||||||
strcat(filename, "/");
|
strcat(filename, "/");
|
||||||
strcat(filename, ep->d_name); /* add file name */
|
strcat(filename, ep->d_name); /* add file name */
|
||||||
|
|
||||||
char *stats = get_file_stat(filename);
|
long index = add_file_stat(filename);
|
||||||
long index = add_file(filename, stats);
|
|
||||||
char *line = get_line(index);
|
char *line = get_line(index);
|
||||||
|
|
||||||
mvwprintw(directory_content, count, 0, "%s", line);
|
mvwprintw(directory_content, count, 0, "%s", line);
|
||||||
free(stats);
|
|
||||||
free(line);
|
free(line);
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
@ -259,20 +257,69 @@ void list_files(const char *path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long long get_directory_size(const char *path)
|
||||||
|
{
|
||||||
|
DIR *dp;
|
||||||
|
struct dirent *ep;
|
||||||
|
struct stat statbuf;
|
||||||
|
long long total_size = 0;
|
||||||
|
|
||||||
|
if ((dp = opendir(path)) != NULL) {
|
||||||
|
while ((ep = readdir(dp)) != NULL) {
|
||||||
|
if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// build full path of entry
|
||||||
|
char full_path[1024];
|
||||||
|
snprintf(full_path, sizeof(full_path), "%s/%s", path, ep->d_name);
|
||||||
|
|
||||||
|
if (lstat(full_path, &statbuf) == -1) {
|
||||||
|
perror("lstat");
|
||||||
|
closedir(dp);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// recursively calculate its size if it is directory
|
||||||
|
if (S_ISDIR(statbuf.st_mode)) {
|
||||||
|
total_size += get_directory_size(full_path);
|
||||||
|
} else {
|
||||||
|
// else add the size of the file to the total
|
||||||
|
total_size += statbuf.st_size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(dp);
|
||||||
|
} else {
|
||||||
|
perror("ccc");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return total_size;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get file's last modified time and size
|
* Get file's last modified time, size, type
|
||||||
|
* Add that file into list
|
||||||
*/
|
*/
|
||||||
char *get_file_stat(char *filename)
|
long add_file_stat(char *filepath)
|
||||||
{
|
{
|
||||||
struct stat file_stat;
|
struct stat file_stat;
|
||||||
stat(filename, &file_stat);
|
stat(filepath, &file_stat);
|
||||||
|
|
||||||
|
/* get last modified time */
|
||||||
char *time = memalloc(20 * sizeof(char));
|
char *time = memalloc(20 * sizeof(char));
|
||||||
/* format last modified time to a string */
|
/* format last modified time to a string */
|
||||||
strftime(time, 20, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime));
|
strftime(time, 20, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime));
|
||||||
|
|
||||||
|
|
||||||
|
/* get file size */
|
||||||
double bytes = file_stat.st_size;
|
double bytes = file_stat.st_size;
|
||||||
char *size = memalloc(25 * sizeof(char)); /* max 25 chars due to long, space, suffix and nul */
|
if (S_ISDIR(file_stat.st_mode)) {
|
||||||
|
bytes = (double) get_directory_size(filepath);
|
||||||
|
}
|
||||||
|
/* max 25 chars due to long, space, suffix and nul */
|
||||||
|
char *size = memalloc(25 * sizeof(char));
|
||||||
int unit = 0;
|
int unit = 0;
|
||||||
const char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"};
|
const char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"};
|
||||||
while (bytes > 1024) {
|
while (bytes > 1024) {
|
||||||
|
@ -281,13 +328,28 @@ char *get_file_stat(char *filename)
|
||||||
}
|
}
|
||||||
sprintf(size, "%.*f%s", unit, bytes, units[unit]);
|
sprintf(size, "%.*f%s", unit, bytes, units[unit]);
|
||||||
|
|
||||||
|
/* get file type */
|
||||||
|
char *type = memalloc(4 * sizeof(char)); /* 3 chars for type */
|
||||||
|
if (S_ISDIR(file_stat.st_mode)) {
|
||||||
|
strcpy(type, "DIR"); /* directory */
|
||||||
|
} else if (S_ISREG(file_stat.st_mode)) {
|
||||||
|
strcpy(type, "REG"); /* regular file */
|
||||||
|
} else if (S_ISLNK(file_stat.st_mode)) {
|
||||||
|
strcpy(type, "LNK"); /* symbolic link */
|
||||||
|
} // socket, block device, character device and FIFO not yet implemented
|
||||||
|
|
||||||
char *total_stat = memalloc(45 * sizeof(char));
|
char *total_stat = memalloc(45 * sizeof(char));
|
||||||
snprintf(total_stat, 45, "%-18s %-10s", time, size);
|
snprintf(total_stat, 45, "%-18s %-10s", time, size);
|
||||||
total_stat[strlen(total_stat)] = '\0';
|
total_stat[strlen(total_stat)] = '\0';
|
||||||
|
|
||||||
free(time);
|
free(time);
|
||||||
free(size);
|
free(size);
|
||||||
return total_stat;
|
|
||||||
|
long index = add_file(filepath, total_stat, type);
|
||||||
|
|
||||||
|
free(total_stat);
|
||||||
|
free(type);
|
||||||
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
38
file.c
38
file.c
|
@ -8,14 +8,16 @@
|
||||||
typedef struct file {
|
typedef struct file {
|
||||||
char *path;
|
char *path;
|
||||||
char *stats;
|
char *stats;
|
||||||
|
char *type;
|
||||||
/* put some more useful stat here */
|
/* put some more useful stat here */
|
||||||
struct file *next;
|
struct file *next;
|
||||||
} file;
|
} file;
|
||||||
|
|
||||||
file *files = NULL;
|
file *files = NULL;
|
||||||
|
file *marked = NULL;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* get length of files linked list
|
* Get length of files linked list
|
||||||
*/
|
*/
|
||||||
long files_len()
|
long files_len()
|
||||||
{
|
{
|
||||||
|
@ -28,6 +30,21 @@ long files_len()
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get length of marked files
|
||||||
|
*/
|
||||||
|
long marked_len()
|
||||||
|
{
|
||||||
|
file *current = marked;
|
||||||
|
int count = 0;
|
||||||
|
while (current != NULL) {
|
||||||
|
count++;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void clear_files()
|
void clear_files()
|
||||||
{
|
{
|
||||||
file *tmp;
|
file *tmp;
|
||||||
|
@ -38,17 +55,29 @@ void clear_files()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
long add_file(char *filepath, char *stats)
|
void clear_marked()
|
||||||
|
{
|
||||||
|
file *tmp;
|
||||||
|
while (marked != NULL) {
|
||||||
|
tmp = marked;
|
||||||
|
files = marked->next;
|
||||||
|
free(tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
long add_file(char *filepath, char *stats, char *type)
|
||||||
{
|
{
|
||||||
file *current = files;
|
file *current = files;
|
||||||
file *new_file = memalloc(sizeof(file));
|
file *new_file = memalloc(sizeof(file));
|
||||||
char *buf = strdup(filepath);
|
char *buf = strdup(filepath);
|
||||||
char *buf2 = strdup(stats);
|
char *buf2 = strdup(stats);
|
||||||
if (buf == NULL || buf2 == NULL) {
|
char *buf3 = strdup(type);
|
||||||
|
if (buf == NULL || buf2 == NULL || buf3 == NULL) {
|
||||||
perror("ccc");
|
perror("ccc");
|
||||||
}
|
}
|
||||||
new_file->path = buf;
|
new_file->path = buf;
|
||||||
new_file->stats = buf2;
|
new_file->stats = buf2;
|
||||||
|
new_file->type = buf3;
|
||||||
new_file->next = NULL;
|
new_file->next = NULL;
|
||||||
if (current == NULL) {
|
if (current == NULL) {
|
||||||
files = new_file;
|
files = new_file;
|
||||||
|
@ -92,6 +121,9 @@ char *get_filepath(long index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Construct a formatted line for display
|
||||||
|
*/
|
||||||
char *get_line(long index)
|
char *get_line(long index)
|
||||||
{
|
{
|
||||||
file *file = get_file(index);
|
file *file = get_file(index);
|
||||||
|
|
5
file.h
5
file.h
|
@ -4,13 +4,16 @@
|
||||||
typedef struct file {
|
typedef struct file {
|
||||||
char *path;
|
char *path;
|
||||||
char *stats;
|
char *stats;
|
||||||
|
char *type;
|
||||||
// put some more useful stat here
|
// put some more useful stat here
|
||||||
struct file *next;
|
struct file *next;
|
||||||
} file;
|
} file;
|
||||||
|
|
||||||
long files_len();
|
long files_len();
|
||||||
|
long marked_len();
|
||||||
void clear_files();
|
void clear_files();
|
||||||
long add_file(char *filename, char *time);
|
void clear_marked();
|
||||||
|
long add_file(char *filename, char *time, char *type);
|
||||||
file *get_file(long index);
|
file *get_file(long index);
|
||||||
char *get_filepath(long index);
|
char *get_filepath(long index);
|
||||||
char *get_line(long index);
|
char *get_line(long index);
|
||||||
|
|
Loading…
Reference in a new issue