Subversion Repositories Kolibri OS

Rev

Rev 7802 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7802 Rev 8827
Line 1... Line 1...
1
int cmd_ls(char dir[])
1
int cmd_ls(char dir[]) {
2
{
-
 
Line 3... Line 2...
3
 
2
 
4
kol_struct70	k70;
3
	kol_struct70	k70;
5
unsigned	*n;
4
	unsigned	*n;
6
unsigned	num_of_file; // number of files in directory
5
	unsigned	num_of_file; // number of files in directory
7
unsigned	*t;
6
	unsigned	*t;
8
unsigned	type_of_file; // check is this a file or a folder
7
	unsigned	type_of_file; // check is this a file or a folder
9
int		i, result;
8
	int		i, result;
Line 10... Line 9...
10
char tmp[FILENAME_MAX];
9
	char tmp[FILENAME_MAX];
Line 11... Line 10...
11
 
10
 
12
bool single_column_mode = FALSE;
11
	bool single_column_mode = FALSE;
13
 
12
 
14
 
13
 
15
k70.p00 = 1;
14
	k70.p00 = 1;
16
k70.p04 = 0;
15
	k70.p04 = 0;
Line 17... Line 16...
17
//k70.p08 = 0;
16
	//k70.p08 = 0;
18
k70.p12 = 2;  // just for test exist & read number of entries
17
	k70.p12 = 2;  // just for test exist & read number of entries
19
k70.p16 =  (unsigned) malloc(32+k70.p12*560);
18
	k70.p16 =  (unsigned) malloc(32+k70.p12*560);
20
k70.p20 = 0;
19
	k70.p20 = 0;
21
 
20
 
Line 22... Line 21...
22
 
21
 
23
if (!strnicmp(dir,"-1",1)) 
22
	if (!strnicmp(dir,"-1",1)) 
24
	{
23
		{
25
	single_column_mode = TRUE;
-
 
26
	dir += 3;
-
 
27
	}
24
		single_column_mode = TRUE;
-
 
25
		dir += 3;
-
 
26
		}
28
 
27
 
29
if ( !strlen(dir) ) // if argument is empty, list current directory
28
	if ( !strlen(dir) ) // if argument is empty, list current directory
30
	k70.p21 = cur_dir;
29
		k70.p21 = cur_dir;
31
else
30
	else
32
{
31
	{
33
	if (dir[0] != '/') // if given directory is relative path, then append cur_dir on left side
32
		if (dir[0] != '/') // if given directory is relative path, then append cur_dir on left side
34
	{
33
		{
35
		strcpy(tmp, cur_dir);
34
			strcpy(tmp, cur_dir);
36
	    if (tmp[strlen(tmp)-1] != '/')
35
			if (tmp[strlen(tmp)-1] != '/')
37
	    {
36
			{
-
 
37
				strcat(tmp, "/"); // add slash
38
	        strcat(tmp, "/"); // add slash
38
			}
39
	    }
-
 
Line 40... Line 39...
40
	    strcat(tmp, dir);
39
			strcat(tmp, dir);
41
	    k70.p21 = tmp;
40
			k70.p21 = tmp;
42
	} else // if given directory is an absolute path
41
		} else // if given directory is an absolute path
43
	{
42
		{
44
		k70.p21 = dir;
43
			k70.p21 = dir;
45
	}
44
		}
Line 46... Line 45...
46
}
45
	}
47
 
46
 
Line 48... Line 47...
48
result = kol_file_70(&k70);
47
	result = kol_file_70(&k70);
49
if ( !((result==0) || (result==6)) ) // check does the directory exists
48
	if ( !((result==0) || (result==6)) ) // check does the directory exists
50
	{
-
 
51
	free( (void*) k70.p16);
-
 
52
	return FALSE;
-
 
53
	}
-
 
54
 
-
 
55
n =  (unsigned*) (k70.p16+8);
-
 
56
num_of_file = *n;
-
 
57
 
-
 
58
// now read full directory
49
		{
59
k70.p12 = num_of_file;  
-
 
60
free( (void*) k70.p16);
-
 
61
k70.p16 =  (unsigned) malloc(32+k70.p12*560);
-
 
62
if ( !k70.p16 )
-
 
63
	return FALSE;
-
 
64
	
-
 
65
result = kol_file_70(&k70);
-
 
66
if ( !((result==0) || (result==6)) ) 
-
 
67
	{
-
 
68
	free( (void*) k70.p16);
-
 
69
	return FALSE;
-
 
70
	}
50
		free( (void*) k70.p16);
71
 
51
		return FALSE;
72
// if there was '-1' param, then show single column mode
-
 
73
// otherwise show files in several columns
52
		}
74
if (single_column_mode == TRUE)
-
 
75
{
-
 
76
_SINGLE_COLUMN_MODE:
-
 
77
	for (i = 0; i < num_of_file; i++)
-
 
78
	{
53
 
79
	printf ("  %s", k70.p16+32+40+(264+40)*i);
54
	n =  (unsigned*) (k70.p16+8);
80
	t =  (unsigned*) (k70.p16+32+(264+40)*i);
-
 
81
	type_of_file = *t;
55
	num_of_file = *n;
82
	if ( (0x10 == (type_of_file&0x10)) || (8 == (type_of_file&8)) )
56
 
83
		printf ("/");
57
	// now read full directory
84
	printf ("\n\r");
58
	k70.p12 = num_of_file;  
85
	}
-
 
86
}
59
	free( (void*) k70.p16);
Line 87... Line 60...
87
else
60
	k70.p16 =  (unsigned) malloc(32+k70.p12*560);
88
{
61
	if ( !k70.p16 )
89
	int longest_name_len = 0;
-
 
90
	int console_window_width = 78; //need to get this value from console.obj if it's possible
62
		return FALSE;
91
	for (i = 0; i < num_of_file; i++)
63
		
-
 
64
	result = kol_file_70(&k70);
92
		{
65
	if ( !((result==0) || (result==6)) ) 
93
		int current_name_len;
66
		{
94
		current_name_len = strlen( (char*)k70.p16+32+40+(264+40)*i);
-
 
95
		if (current_name_len > longest_name_len) longest_name_len = current_name_len;
67
		free( (void*) k70.p16);
96
		}
-
 
97
 
68
		return FALSE;
98
	longest_name_len+=2; //consider space separator and '/' symbol for folders
69
		}
-
 
70
 
-
 
71
	// if there was '-1' param, then show single column mode
-
 
72
	// otherwise show files in several columns
-
 
73
	if (single_column_mode == TRUE)
-
 
74
	{
-
 
75
	_SINGLE_COLUMN_MODE:
-
 
76
		for (i = 0; i < num_of_file; i++)
-
 
77
		{
-
 
78
		printf ("  %s", k70.p16+32+40+(264+40)*i);
-
 
79
		t =  (unsigned*) (k70.p16+32+(264+40)*i);
-
 
80
		type_of_file = *t;
-
 
81
		if ( (0x10 == (type_of_file&0x10)) || (8 == (type_of_file&8)) )
-
 
82
			printf ("/");
-
 
83
		printf ("\n\r");
-
 
84
		}
99
	int columns_max = console_window_width / longest_name_len;
85
	}
-
 
86
	else
-
 
87
	{
-
 
88
		int longest_name_len = 0;
-
 
89
		int console_window_width = 78; //need to get this value from console.obj if it's possible
-
 
90
		for (i = 0; i < num_of_file; i++)
-
 
91
			{
-
 
92
			int current_name_len;
-
 
93
			current_name_len = strlen( (char*)k70.p16+32+40+(264+40)*i);
-
 
94
			if (current_name_len > longest_name_len) longest_name_len = current_name_len;
-
 
95
			}
-
 
96
 
-
 
97
		longest_name_len+=2; //consider space separator and '/' symbol for folders
-
 
98
		int columns_max = console_window_width / longest_name_len;
100
 
99
 
101
	if (longest_name_len >= console_window_width) goto _SINGLE_COLUMN_MODE; //there was too long filename
100
		if (longest_name_len >= console_window_width) goto _SINGLE_COLUMN_MODE; //there was too long filename
102
 
101
 
103
	for (i = 0; i < num_of_file; i++)
102
		for (i = 0; i < num_of_file; i++)
104
		{
103
			{
105
		char cur_file[2048];
104
			char cur_file[2048];
106
		strncpy(cur_file, (char*)k70.p16+32+40+(304)*i, sizeof(cur_file)-2);
105
			strncpy(cur_file, (char*)k70.p16+32+40+(304)*i, sizeof(cur_file)-2);
107
 
106
 
108
		t =  (unsigned*) (k70.p16+32+(304)*i);
107
			t =  (unsigned*) (k70.p16+32+(304)*i);
109
		type_of_file = *t;
108
			type_of_file = *t;
110
        
109
			
Line 111... Line 110...
111
        int is_folder = 0;
110
			int is_folder = 0;
112
		if ( (0x10 == (type_of_file&0x10)) || (8 == (type_of_file&8)) ) { is_folder = 1; strcat(cur_file, "/"); }
111
			if ( (0x10 == (type_of_file&0x10)) || (8 == (type_of_file&8)) ) { is_folder = 1; strcat(cur_file, "/"); }
113
 
112