Subversion Repositories Kolibri OS

Rev

Rev 3247 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3247 Rev 7808
Line -... Line 1...
-
 
1
int newline_to_file(char *fn, unsigned long long pos)
-
 
2
{
-
 
3
	char *newline = "\n\r";
-
 
4
 
-
 
5
	kol_struct70 k70_out;
-
 
6
 
-
 
7
	k70_out.p00 = 3;
-
 
8
	k70_out.p04 = pos; // offset 
-
 
9
    //k70_out.p08 = 0;
-
 
10
    k70_out.p12 = 2;
-
 
11
    k70_out.p16 = (unsigned)newline;
-
 
12
    k70_out.p20 = 0;
-
 
13
    k70_out.p21 = fn;
-
 
14
 
-
 
15
    return kol_file_70(&k70_out); // write
-
 
16
}
-
 
17
 
-
 
18
 
-
 
19
int cmd_echo(char text[])
1
int cmd_echo(char text[])
20
{ // added output redirection by rgimad 2020.
-
 
21
 
-
 
22
    int text_len = strlen(text);
-
 
23
    int out_len = text_len;
-
 
24
    int i, redirect = 0, redirect_mode = 0; // if redirect = 0 echo to screen, if 1 to file. If redirect_mode = 0 rewrite the file, if 1 append to the file
-
 
25
    char *filename; // pointer to name of the file redirect to
-
 
26
    int ignore_redir_char = 0;
-
 
27
 
-
 
28
    for (i = 0; i < text_len; i++)
-
 
29
    {
-
 
30
    	if (text[i] == '"')
-
 
31
    	{
-
 
32
    		ignore_redir_char = !ignore_redir_char;
-
 
33
    		continue;
-
 
34
    	}
-
 
35
    	if (!ignore_redir_char && text[i] == '>')
-
 
36
    	{
-
 
37
    		if (i + 1 < text_len && text[i + 1] == '>')
-
 
38
    		{
-
 
39
    			redirect = 1;
-
 
40
    			redirect_mode = 1;
-
 
41
    			filename = text + i + 2;
-
 
42
    			out_len = i;
-
 
43
    			break;
-
 
44
    		} else
-
 
45
    		{
2
{
46
    			redirect = 1;
-
 
47
    			redirect_mode = 0;
-
 
48
    			filename = text + i + 1;
-
 
49
    			out_len = i;
-
 
50
    			break;
-
 
51
    		}
-
 
52
    	}
-
 
53
    }
-
 
54
 
-
 
55
    // remove leading spaces in filename
-
 
56
    while (*filename == ' ') { filename++; }
-
 
57
 
-
 
58
    // remove spaces at the end of out_len
-
 
59
    while (out_len > 0 && text[out_len - 1] == ' ') { out_len--; }
-
 
60
 
-
 
61
    // delete quotes if has
-
 
62
    if (text[out_len - 1] == '"') { out_len--; }
-
 
63
    if (text[0] == '"') { text++; out_len--; }
-
 
64
 
-
 
65
 
-
 
66
    if (redirect == 0) // echo to screen
-
 
67
    {
-
 
68
    	text[out_len] = '\0';
-
 
69
    	printf("%s\n\r", text);
3
printf("%s\n\r", text);
70
    } else
-
 
71
    {
-
 
72
	    char *filename_out = (char*) malloc(FILENAME_MAX); // abs path
-
 
73
 
-
 
74
	    if (filename[0] != '/')
-
 
75
	    {
-
 
76
	        strcpy(filename_out, cur_dir);
-
 
77
	        if (filename_out[strlen(filename_out)-1] != '/')
-
 
78
	        {
-
 
79
	            strcat(filename_out, "/"); // add slash
-
 
80
	        }
-
 
81
	        strcat(filename_out, filename);
-
 
82
	    } else
-
 
83
	    {
-
 
84
	        strcpy(filename_out, filename);
-
 
85
	    }
-
 
86
 
-
 
87
	    kol_struct70 k70_out, k70_in;
-
 
88
	    int result;
-
 
89
 
-
 
90
    	if (redirect_mode == 0) // rewrite the output file
-
 
91
    	{
-
 
92
 
-
 
93
    		k70_out.p00 = 2;
-
 
94
    		k70_out.p04 = 0; // offset
-
 
95
		    //k70_out.p08 = 0;
-
 
96
		    k70_out.p12 = out_len;
-
 
97
		    k70_out.p16 = (unsigned)text;
-
 
98
		    k70_out.p20 = 0;
-
 
99
		    k70_out.p21 = filename_out;
-
 
100
 
-
 
101
		    result = kol_file_70(&k70_out); // write
-
 
102
 
-
 
103
	        if (result != 0) // unable to write
-
 
104
	        {
-
 
105
	        	free(filename_out);
-
 
106
	        	return FALSE;
-
 
107
	        }
-
 
108
	        newline_to_file(filename_out, out_len);
-
 
109
 
-
 
110
	        free(filename_out);
-
 
111
	        return TRUE;
-
 
112
    	} else // append to the output file
-
 
113
    	{
-
 
114
    		kol_struct_BDVK	bdvk;
-
 
115
 
-
 
116
    		k70_in.p00 = 5;
-
 
117
		    k70_in.p04 = 0LL;
-
 
118
		    k70_in.p12 = 0;
-
 
119
		    k70_in.p16 = (unsigned) &bdvk;
-
 
120
		    k70_in.p20 = 0;
-
 
121
		    k70_in.p21 = filename_out;
-
 
122
 
-
 
123
		    result = kol_file_70(&k70_in); // get information about file
-
 
124
		    if ( 0 != result ) // file doesnt exist, then rewrite
-
 
125
		    {
-
 
126
		    	k70_out.p00 = 2;
-
 
127
	    		k70_out.p04 = 0; // offset
-
 
128
			    //k70_out.p08 = 0;
-
 
129
			    k70_out.p12 = out_len;
-
 
130
			    k70_out.p16 = (unsigned)text;
-
 
131
			    k70_out.p20 = 0;
-
 
132
			    k70_out.p21 = filename_out;
-
 
133
 
-
 
134
			    result = kol_file_70(&k70_out); // write
-
 
135
		        if (result != 0) // unable to write
-
 
136
		        {
-
 
137
		        	free(filename_out);
-
 
138
		        	return FALSE;
-
 
139
		        }
-
 
140
		        newline_to_file(filename_out, out_len);
-
 
141
 
-
 
142
		        free(filename_out);
-
 
143
		        return TRUE;
-
 
144
		    }
-
 
145
		    // if exists, append
-
 
146
		    unsigned long long filesize = bdvk.p32;
-
 
147
 
-
 
148
		    k70_out.p00 = 3;
-
 
149
    		k70_out.p04 = filesize; // offset 
-
 
150
		    //k70_out.p08 = 0;
-
 
151
		    k70_out.p12 = out_len;
-
 
152
		    k70_out.p16 = (unsigned)text;
-
 
153
		    k70_out.p20 = 0;
-
 
154
		    k70_out.p21 = filename_out;
-
 
155
 
-
 
156
		    result = kol_file_70(&k70_out); // write
-
 
157
	        if (result != 0) // unable to write
-
 
158
	        {
-
 
159
	        	free(filename_out);
-
 
160
	        	return FALSE;
-
 
161
	        }
-
 
162
 
-
 
163
	        newline_to_file(filename_out, filesize + out_len);
-
 
164
	        free(filename_out);
-
 
165
	        return TRUE;
-
 
166
    	}
-
 
167
    }
-
 
168
    //free(filename_out);
-
 
169
    return TRUE;
4
return TRUE;
170
}
5
}
171