Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics (kill unused var, and kill/improve debug outputs)
[simgrid.git] / tools / tesh2 / w32 / src / Cat.c
1
2
3 /* must be defined before */
4 #ifdef _MSC_VER
5 #define _CRT_SECURE_NO_DEPRECATE 
6 #endif
7
8 #include <windows.h>
9
10 #include <sys/stat.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <locale.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <io.h>
17
18 #ifdef _MSC_VER
19 #define _CRT_SECURE_NO_DEPRECATE 
20 #define strdup  _strdup
21 #define fileno  _fileno
22 #define creat   _creat
23 #define open    _open
24 #define read    _read
25 #define write   _write
26 #endif
27
28 #ifdef max
29         #undef max
30         #define max(h,i) ((h) > (i) ? (h) : (i))
31 #endif
32         
33 #ifndef S_ISREG
34         #define S_ISREG(__mode) (((__mode) & S_IFMT) == S_IFREG)
35 #endif
36
37 #ifndef STDIN_FILENO
38 #define STDIN_FILENO    0
39 #endif
40
41 #ifndef STDOUT_FILENO
42 #define STDOUT_FILENO   1
43 #endif
44
45
46 static 
47 int exit_code = EXIT_SUCCESS;
48
49 static char*
50 rfname;
51
52 static char*
53 wfname = NULL;
54
55 static
56 int wfd;
57
58 #ifndef _MSC_VER
59 static size_t 
60 #else
61 static int
62 #endif
63 bsize;
64
65 static char *
66 buf = NULL;
67
68 static int 
69 fnumber = 0;
70
71 int
72 cat_files(char** fnames);
73
74 int
75 cat(int);
76
77 char** 
78 fnames = NULL;
79
80 void
81 exit0(int errcode)
82 {
83         if(fnames)
84         {
85                 int i;
86                 
87                 for(i = 0; i < fnumber; i++)
88                                 free(fnames[i]);
89                                 
90                 free(fnames);
91         }
92         
93         if(buf)         
94                 free(buf);
95                 
96         if(wfname)
97                 free(wfname);
98                 
99         
100         exit(errcode);
101 }
102
103 int
104 main(int argc, char *argv[])
105 {
106         int i, j;
107         int success = 1;
108         int redirect = 0;
109         int exists = 0;
110         
111         struct stat stat_buf = {0};
112         
113         setlocale(LC_ALL, "");
114         
115         bsize = BUFSIZ; 
116                 
117         if(!(buf = (char*)malloc(bsize)))
118         {
119                 fprintf(stderr, "[ERROR/cat] not enough memory\n");
120                 exit0(EXIT_FAILURE);
121         }
122         
123         if(argc > 1)
124         {
125                 if(!(fnames = (char**)calloc(argc, sizeof(char*))))
126                 {
127                         fprintf(stderr, "[ERROR/cat]  not enough memory\n");
128                         exit0(EXIT_FAILURE);
129                 }
130                 
131                 for(i = 1, j= 0; i < argc; i++)
132                 {
133                         if(!strcmp(argv[i], ">"))
134                         {
135                                 redirect = 1;
136                                 break;
137                         }
138                         else if(!strcmp(argv[i], ">>"))
139                         {
140                                 redirect = 1;
141                                 exists = 1;
142                                 break;
143                         }
144                         else
145                         {
146                                 if(stat(argv[i], &stat_buf))
147                                 {
148                                         fprintf(stderr, "[ERROR/cat (1)] could not get information about %s\n",argv[i]);
149                                         success = 0;
150                                         break;
151                                 }
152                                 else
153                                 {
154                                         if(S_ISREG(stat_buf.st_mode))
155                                         {
156                                                 fnames[j++] = strdup(argv[i]);
157                                                 fnumber++;      
158                                         }
159                                         else
160                                         {
161                                                 fprintf(stderr, "[ERROR/cat] %s is not a file\n",argv[i]);
162                                                 success = 0;
163                                                 break;
164                                         }
165                                 }       
166                         }               
167                 }
168                 
169                 if(!success)
170                         exit0(EXIT_FAILURE);
171         }
172         
173         if(redirect)
174         {
175                 if(i != argc - 2)
176                 {
177                         if(exists)
178                                 fprintf(stderr, "[ERROR/cat] syntax error near `>>' (i : %d - argc : %d\n", i , argc);
179                         else
180                                 fprintf(stderr, "[ERROR/cat] syntax error near `>' (i : %d - argc : %d\n", i , argc);
181                                         
182                         exit0(EXIT_FAILURE);
183                 }
184                 else
185                 {
186                         wfname = strdup(argv[i + 1]);
187                         
188                         if(!exists)
189                         {
190                                 if((wfd = creat(wfname, _S_IREAD | _S_IWRITE)) < 0)
191                                 {
192                                         fprintf(stderr, "[ERROR/cat] could not create %s file\n",wfname);
193                                         
194                                         exit0(EXIT_FAILURE);
195                                 }       
196                         }
197                         else
198                         {
199                                 if((wfd = open(wfname, O_WRONLY | O_APPEND, 0)) < 0)
200                                 {
201                                         fprintf(stderr, "[ERROR/cat] could not open %s file\n",wfname);
202                                         
203                                         exit0(EXIT_FAILURE);
204                                 }
205                         }
206                 }                       
207         }
208         else
209         {
210                 wfd = STDOUT_FILENO;
211                 
212                 if(fstat(wfd, &stat_buf))
213                 {       
214                         fprintf(stderr, "[ERROR/cat (3)] could not get information about stdout\n");
215                         exit0(EXIT_FAILURE);
216                 }
217         }
218         
219         exit_code = cat_files(fnames);
220         
221         if(wfd == STDOUT_FILENO)
222         {
223                 if(fclose(stdout))
224                 {
225                         fprintf(stderr, "[ERROR/cat] could not close stdout\n");
226                         exit_code = EXIT_FAILURE;
227                 }
228         }
229         else
230         {
231                 if(close(wfd))
232                 {
233                         fprintf(stderr, "[ERROR/cat] could not close %s\n", wfname);
234                         exit_code = EXIT_FAILURE;
235                 }       
236         }
237         
238         exit0(exit_code);
239 }
240
241 int
242 cat_files(char **fnames)
243 {
244         int rfd, i;
245         int failure = 0;
246         
247         rfd = STDIN_FILENO;
248         
249         rfname = "stdin";
250         
251         if(fnumber)
252         {
253                 
254                 for(i = 0; i < fnumber && !failure; i++) 
255                 {
256                         if (!strcmp(fnames[i], "-"))
257                                 rfd = fileno(stdin);
258                         else if ((rfd = open(fnames[i], O_RDONLY, 0)) < 0) 
259                         {
260                                 fprintf(stderr, "[WARN/cat] could not open %s file", fnames[i]);
261                                 exit_code = EXIT_FAILURE;
262                                 continue;
263                         }
264                         
265                         rfname = fnames[i];
266                         
267                         failure = cat(rfd);
268                         
269                         close(rfd);
270                                 
271                 }
272         }
273         else
274                 failure = cat(rfd);
275         
276         return failure ? 1 : 0;
277 }
278
279 int
280 cat(int rfd)
281 {
282
283         #ifndef _MSC_VER 
284         size_t bytes_readed_nb;
285         #else
286         int bytes_readed_nb;
287         #endif
288         int bytes_written_nb, pos;
289         
290         while ((bytes_readed_nb = read(rfd, buf, bsize)) != -1 && bytes_readed_nb != 0)
291         {
292                 for (pos = 0; bytes_readed_nb; bytes_readed_nb -= bytes_written_nb, pos += bytes_written_nb)
293                 {
294                         if ((bytes_written_nb = write(wfd, buf + pos, bytes_readed_nb)) == 0 || bytes_written_nb == -1)
295                         {
296                                 fprintf(stderr, "[ERROR/cat] could not write to %s\n", wfd == fileno(stdout) ? "stdout" : wfname);
297                                 return 1;
298                         }
299                 }
300         }
301                 
302         if(bytes_readed_nb < 0)
303         {
304                 fprintf(stderr, "[WARN/cat] could not read %s file", rfname);
305                 return 1;
306         }
307         
308         return 0;
309 }