Subversion Repositories Kolibri OS

Rev

Rev 3247 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1665 Nasarus 1
 
7808 rgimad 2
 
3
	char *newline = "\n\r";
1665 Nasarus 4
7808 rgimad 5
	kol_struct70 k70_out;
6
 
7
	k70_out.p00 = 3;
8
 
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
1665 Nasarus 18
3247 Albom 19
 
7808 rgimad 20
 
21
22
    int text_len = strlen(text);
23
 
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
    		{
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
 
57
58
    // remove spaces at the end of out_len
59
 
60
61
    // delete quotes if has
62
 
63
    if (text[0] == '"') { text++; out_len--; }
64
65
66
 
67
 
68
    	text[out_len] = '\0';
69
    	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
 
89
90
    	if (redirect_mode == 0) // rewrite the output file
91
 
92
93
    		k70_out.p00 = 2;
94
 
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
 
112
    	} else // append to the output file
113
    	{
114
    		kol_struct_BDVK	bdvk;
115
116
    		k70_in.p00 = 5;
117
 
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
 
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
 
136
		        {
137
		        	free(filename_out);
138
		        	return FALSE;
139
		        }
140
		        newline_to_file(filename_out, out_len);
141
142
		        free(filename_out);
143
 
144
		    }
145
		    // if exists, append
146
		    unsigned long long filesize = bdvk.p32;
147
148
		    k70_out.p00 = 3;
149
 
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
 
158
	        {
159
	        	free(filename_out);
160
	        	return FALSE;
161
	        }
162
163
	        newline_to_file(filename_out, filesize + out_len);
164
 
165
	        return TRUE;
166
    	}
167
    }
168
    //free(filename_out);
169
    return TRUE;
170
}
171