2010-10-17 21:50:55 +02:00
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
int cmd_ls(char dir[]) {
|
2010-10-17 21:50:55 +02:00
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
kol_struct70 k70;
|
|
|
|
unsigned *n;
|
|
|
|
unsigned num_of_file; // number of files in directory
|
|
|
|
unsigned *t;
|
|
|
|
unsigned type_of_file; // check is this a file or a folder
|
|
|
|
int i, result;
|
|
|
|
char tmp[FILENAME_MAX];
|
2012-04-16 12:40:07 +02:00
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
bool single_column_mode = FALSE;
|
2017-09-27 20:44:15 +02:00
|
|
|
|
2010-10-17 21:50:55 +02:00
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
k70.p00 = 1;
|
|
|
|
k70.p04 = 0;
|
|
|
|
//k70.p08 = 0;
|
|
|
|
k70.p12 = 2; // just for test exist & read number of entries
|
|
|
|
k70.p16 = (unsigned) malloc(32+k70.p12*560);
|
|
|
|
k70.p20 = 0;
|
2010-10-17 21:50:55 +02:00
|
|
|
|
2017-09-27 20:44:15 +02:00
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
if (!strnicmp(dir,"-1",1))
|
|
|
|
{
|
|
|
|
single_column_mode = TRUE;
|
|
|
|
dir += 3;
|
|
|
|
}
|
2010-10-17 21:50:55 +02:00
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
if ( !strlen(dir) ) // if argument is empty, list current directory
|
|
|
|
k70.p21 = cur_dir;
|
|
|
|
else
|
2017-02-06 13:35:58 +01:00
|
|
|
{
|
2021-06-12 23:34:41 +02:00
|
|
|
if (dir[0] != '/') // if given directory is relative path, then append cur_dir on left side
|
|
|
|
{
|
|
|
|
strcpy(tmp, cur_dir);
|
|
|
|
if (tmp[strlen(tmp)-1] != '/')
|
|
|
|
{
|
|
|
|
strcat(tmp, "/"); // add slash
|
|
|
|
}
|
|
|
|
strcat(tmp, dir);
|
|
|
|
k70.p21 = tmp;
|
|
|
|
} else // if given directory is an absolute path
|
|
|
|
{
|
|
|
|
k70.p21 = dir;
|
|
|
|
}
|
2017-02-06 13:35:58 +01:00
|
|
|
}
|
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
result = kol_file_70(&k70);
|
|
|
|
if ( !((result==0) || (result==6)) ) // check does the directory exists
|
2017-09-27 20:44:15 +02:00
|
|
|
{
|
2021-06-12 23:34:41 +02:00
|
|
|
free( (void*) k70.p16);
|
|
|
|
return FALSE;
|
2017-09-27 20:44:15 +02:00
|
|
|
}
|
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
n = (unsigned*) (k70.p16+8);
|
|
|
|
num_of_file = *n;
|
2017-09-27 20:44:15 +02:00
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
// now read full directory
|
|
|
|
k70.p12 = num_of_file;
|
|
|
|
free( (void*) k70.p16);
|
|
|
|
k70.p16 = (unsigned) malloc(32+k70.p12*560);
|
|
|
|
if ( !k70.p16 )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
result = kol_file_70(&k70);
|
|
|
|
if ( !((result==0) || (result==6)) )
|
2017-09-27 20:44:15 +02:00
|
|
|
{
|
2021-06-12 23:34:41 +02:00
|
|
|
free( (void*) k70.p16);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2017-09-27 20:44:15 +02:00
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
// if there was '-1' param, then show single column mode
|
|
|
|
// otherwise show files in several columns
|
|
|
|
if (single_column_mode == TRUE)
|
|
|
|
{
|
|
|
|
_SINGLE_COLUMN_MODE:
|
|
|
|
for (i = 0; i < num_of_file; i++)
|
|
|
|
{
|
|
|
|
printf (" %s", k70.p16+32+40+(264+40)*i);
|
|
|
|
t = (unsigned*) (k70.p16+32+(264+40)*i);
|
2017-09-27 20:44:15 +02:00
|
|
|
type_of_file = *t;
|
2021-06-12 23:34:41 +02:00
|
|
|
if ( (0x10 == (type_of_file&0x10)) || (8 == (type_of_file&8)) )
|
|
|
|
printf ("/");
|
|
|
|
printf ("\n\r");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int longest_name_len = 0;
|
|
|
|
int console_window_width = 78; //need to get this value from console.obj if it's possible
|
|
|
|
for (i = 0; i < num_of_file; i++)
|
|
|
|
{
|
|
|
|
int current_name_len;
|
|
|
|
current_name_len = strlen( (char*)k70.p16+32+40+(264+40)*i);
|
|
|
|
if (current_name_len > longest_name_len) longest_name_len = current_name_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
longest_name_len+=2; //consider space separator and '/' symbol for folders
|
|
|
|
int columns_max = console_window_width / longest_name_len;
|
|
|
|
|
|
|
|
if (longest_name_len >= console_window_width) goto _SINGLE_COLUMN_MODE; //there was too long filename
|
|
|
|
|
|
|
|
for (i = 0; i < num_of_file; i++)
|
|
|
|
{
|
|
|
|
char cur_file[2048];
|
|
|
|
strncpy(cur_file, (char*)k70.p16+32+40+(304)*i, sizeof(cur_file)-2);
|
|
|
|
|
|
|
|
t = (unsigned*) (k70.p16+32+(304)*i);
|
|
|
|
type_of_file = *t;
|
|
|
|
|
|
|
|
int is_folder = 0;
|
|
|
|
if ( (0x10 == (type_of_file&0x10)) || (8 == (type_of_file&8)) ) { is_folder = 1; strcat(cur_file, "/"); }
|
|
|
|
|
|
|
|
if (is_folder) { printf("\033[0;36m"); } // set cyan for folder
|
|
|
|
printf ("%*s", -longest_name_len, cur_file);
|
|
|
|
if (is_folder) { printf("\033[0m"); } // is had been set, reset
|
|
|
|
|
|
|
|
if ((i>0) && ((i+1)%columns_max == 0)) printf ("\n\r");
|
|
|
|
}
|
|
|
|
if ((i)%columns_max != 0) printf("\n\r");
|
|
|
|
}
|
2010-10-17 21:50:55 +02:00
|
|
|
|
2021-06-12 23:34:41 +02:00
|
|
|
free((void*)k70.p16);
|
|
|
|
return TRUE;
|
2010-10-17 21:50:55 +02:00
|
|
|
}
|
2013-02-17 16:22:51 +01:00
|
|
|
|