1
|
1 // file-io.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1993, 1994, 1995 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
|
24 // Written by John C. Campbell <jcc@che.utexas.edu>. |
1230
|
25 // |
|
26 // Thomas Baier <baier@ci.tuwien.ac.at> added the following functions: |
|
27 // |
|
28 // popen pclose execute sync_system async_system |
|
29 // waitpid mkfifo unlink |
|
30 |
1
|
31 |
240
|
32 #ifdef HAVE_CONFIG_H |
1230
|
33 #include <config.h> |
1
|
34 #endif |
|
35 |
1343
|
36 #include <cstring> |
|
37 #include <cstdio> |
|
38 #include <cerrno> |
|
39 #include <cstdlib> |
|
40 #include <cctype> |
|
41 |
1350
|
42 #include <strstream.h> |
|
43 |
|
44 #ifdef HAVE_UNISTD_H |
1230
|
45 #include <sys/types.h> |
1
|
46 #include <unistd.h> |
1343
|
47 #endif |
1350
|
48 |
1230
|
49 #include <fcntl.h> |
1343
|
50 |
1230
|
51 #include <DLList.h> |
|
52 |
456
|
53 #include "dMatrix.h" |
444
|
54 |
1352
|
55 #include "defun.h" |
1
|
56 #include "error.h" |
1352
|
57 #include "file-io.h" |
542
|
58 #include "help.h" |
1352
|
59 #include "input.h" |
|
60 #include "mappers.h" |
1377
|
61 #include "oct-map.h" |
1352
|
62 #include "octave-hist.h" |
1
|
63 #include "pager.h" |
1352
|
64 #include "statdefs.h" |
444
|
65 #include "sysdep.h" |
1352
|
66 #include "syswait.h" |
|
67 #include "tree-const.h" |
|
68 #include "utils.h" |
529
|
69 #include "variables.h" |
1
|
70 |
1377
|
71 extern "C" |
|
72 { |
|
73 #include <readline/tilde.h> |
1379
|
74 |
|
75 extern void mode_string (); |
1377
|
76 } |
|
77 |
1
|
78 // keeps a count of args sent to printf or scanf |
|
79 static int fmt_arg_count = 0; |
|
80 |
759
|
81 // double linked list containing relevant information about open files |
|
82 static DLList <file_info> file_list; |
1
|
83 |
240
|
84 file_info::file_info (void) |
1
|
85 { |
334
|
86 file_number = -1; |
529
|
87 file_name = 0; |
|
88 file_fptr = 0; |
|
89 file_mode = 0; |
1
|
90 } |
|
91 |
240
|
92 file_info::file_info (int n, const char *nm, FILE *t, const char *md) |
|
93 { |
334
|
94 file_number = n; |
|
95 file_name = strsave (nm); |
|
96 file_fptr = t; |
|
97 file_mode = strsave (md); |
240
|
98 } |
|
99 |
|
100 file_info::file_info (const file_info& f) |
1
|
101 { |
367
|
102 file_number = f.file_number; |
|
103 file_name = strsave (f.file_name); |
|
104 file_fptr = f.file_fptr; |
|
105 file_mode = strsave (f.file_mode); |
1
|
106 } |
|
107 |
240
|
108 file_info& |
|
109 file_info::operator = (const file_info& f) |
1
|
110 { |
240
|
111 if (this != & f) |
|
112 { |
367
|
113 file_number = f.file_number; |
334
|
114 delete [] file_name; |
|
115 file_name = strsave (f.file_name); |
|
116 file_fptr = f.file_fptr; |
|
117 delete [] file_mode; |
|
118 file_mode = strsave (f.file_mode); |
240
|
119 } |
1
|
120 return *this; |
|
121 } |
|
122 |
240
|
123 file_info::~file_info (void) |
1
|
124 { |
334
|
125 delete [] file_name; |
|
126 delete [] file_mode; |
1
|
127 } |
|
128 |
|
129 int |
240
|
130 file_info::number (void) const |
1
|
131 { |
334
|
132 return file_number; |
1
|
133 } |
|
134 |
240
|
135 const char * |
|
136 file_info::name (void) const |
1
|
137 { |
334
|
138 return file_name; |
1
|
139 } |
|
140 |
|
141 FILE * |
240
|
142 file_info::fptr (void) const |
1
|
143 { |
334
|
144 return file_fptr; |
1
|
145 } |
|
146 |
240
|
147 const char * |
|
148 file_info::mode (void) const |
1
|
149 { |
334
|
150 return file_mode; |
1
|
151 } |
|
152 |
|
153 void |
164
|
154 initialize_file_io (void) |
1
|
155 { |
334
|
156 file_info octave_stdin (0, "stdin", stdin, "r"); |
|
157 file_info octave_stdout (1, "stdout", stdout, "w"); |
|
158 file_info octave_stderr (2, "stderr", stderr, "w"); |
1
|
159 |
334
|
160 file_list.append (octave_stdin); |
|
161 file_list.append (octave_stdout); |
|
162 file_list.append (octave_stderr); |
1
|
163 } |
|
164 |
761
|
165 // Given a file name or number, return a pointer to the corresponding |
|
166 // open file. If the file has not already been opened, return NULL. |
|
167 |
1
|
168 Pix |
164
|
169 return_valid_file (const tree_constant& arg) |
1
|
170 { |
610
|
171 if (arg.is_string ()) |
1
|
172 { |
|
173 Pix p = file_list.first (); |
240
|
174 file_info file; |
896
|
175 int file_count = file_list.length (); |
1
|
176 for (int i = 0; i < file_count; i++) |
|
177 { |
1363
|
178 const char *file_name = arg.string_value (); |
1
|
179 file = file_list (p); |
|
180 if (strcmp (file.name (), file_name) == 0) |
|
181 return p; |
|
182 file_list.next (p); |
|
183 } |
|
184 } |
636
|
185 else |
1
|
186 { |
|
187 double file_num = arg.double_value (); |
636
|
188 |
|
189 if (! error_state) |
1
|
190 { |
1086
|
191 if (D_NINT (file_num) != file_num) |
636
|
192 error ("file number not an integer value"); |
|
193 else |
1
|
194 { |
636
|
195 Pix p = file_list.first (); |
|
196 file_info file; |
896
|
197 int file_count = file_list.length (); |
636
|
198 for (int i = 0; i < file_count; i++) |
|
199 { |
|
200 file = file_list (p); |
|
201 if (file.number () == file_num) |
|
202 return p; |
|
203 file_list.next (p); |
|
204 } |
|
205 error ("no file with that number"); |
1
|
206 } |
|
207 } |
636
|
208 else |
|
209 error ("inapproriate file specifier"); |
|
210 } |
1
|
211 |
529
|
212 return 0; |
1
|
213 } |
|
214 |
|
215 static Pix |
636
|
216 fopen_file_for_user (const char *name, const char *mode, |
370
|
217 const char *warn_for) |
1
|
218 { |
636
|
219 FILE *file_ptr = fopen (name, mode); |
529
|
220 if (file_ptr) |
1
|
221 { |
896
|
222 int file_number = file_list.length () + 1; |
|
223 |
|
224 file_info file (file_number, name, file_ptr, mode); |
1
|
225 file_list.append (file); |
|
226 |
|
227 Pix p = file_list.first (); |
240
|
228 file_info file_from_list; |
896
|
229 int file_count = file_list.length (); |
1
|
230 for (int i = 0; i < file_count; i++) |
|
231 { |
|
232 file_from_list = file_list (p); |
636
|
233 if (strcmp (file_from_list.name (), name) == 0) |
1
|
234 return p; |
|
235 file_list.next (p); |
|
236 } |
|
237 } |
|
238 |
636
|
239 error ("%s: unable to open file `%s'", warn_for, name); |
240
|
240 |
529
|
241 return 0; |
1
|
242 } |
|
243 |
368
|
244 static Pix |
636
|
245 file_io_get_file (const tree_constant& arg, const char *mode, |
368
|
246 const char *warn_for) |
|
247 { |
|
248 Pix p = return_valid_file (arg); |
|
249 |
529
|
250 if (! p) |
368
|
251 { |
610
|
252 if (arg.is_string ()) |
368
|
253 { |
1363
|
254 const char *name = arg.string_value (); |
368
|
255 |
|
256 struct stat buffer; |
|
257 int status = stat (name, &buffer); |
|
258 |
372
|
259 if (status == 0) |
368
|
260 { |
|
261 if ((buffer.st_mode & S_IFREG) == S_IFREG) |
636
|
262 p = fopen_file_for_user (name, mode, warn_for); |
368
|
263 else |
|
264 error ("%s: invalid file type", warn_for); |
|
265 } |
372
|
266 else if (status < 0 && *mode != 'r') |
636
|
267 p = fopen_file_for_user (name, mode, warn_for); |
368
|
268 else |
|
269 error ("%s: can't stat file `%s'", warn_for, name); |
|
270 } |
|
271 else |
|
272 error ("%s: invalid file specifier", warn_for); |
|
273 } |
|
274 |
|
275 return p; |
|
276 } |
1
|
277 |
1181
|
278 static Octave_object |
497
|
279 fclose_internal (const Octave_object& args) |
1
|
280 { |
497
|
281 Octave_object retval; |
1
|
282 |
712
|
283 Pix p = return_valid_file (args(0)); |
1
|
284 |
529
|
285 if (! p) |
1
|
286 return retval; |
|
287 |
240
|
288 file_info file = file_list (p); |
1
|
289 |
|
290 if (file.number () < 3) |
|
291 { |
|
292 warning ("fclose: can't close stdin, stdout, or stderr!"); |
|
293 return retval; |
|
294 } |
|
295 |
|
296 int success = fclose (file.fptr ()); |
|
297 file_list.del (p); |
|
298 |
|
299 if (success == 0) |
636
|
300 retval(0) = 1.0; // succeeded |
1
|
301 else |
|
302 { |
|
303 error ("fclose: error on closing file"); |
636
|
304 retval(0) = 0.0; // failed |
1
|
305 } |
|
306 |
|
307 return retval; |
|
308 } |
|
309 |
1181
|
310 DEFUN ("fclose", Ffclose, Sfclose, 1, 1, |
1230
|
311 "fclose (FILENAME or FILENUM): close a file") |
529
|
312 { |
|
313 Octave_object retval; |
|
314 |
|
315 int nargin = args.length (); |
|
316 |
712
|
317 if (nargin != 1) |
1181
|
318 print_usage ("fclose"); |
529
|
319 else |
1181
|
320 retval = fclose_internal (args); |
529
|
321 |
|
322 return retval; |
|
323 } |
|
324 |
1181
|
325 static Octave_object |
497
|
326 fflush_internal (const Octave_object& args) |
1
|
327 { |
497
|
328 Octave_object retval; |
1
|
329 |
712
|
330 Pix p = return_valid_file (args(0)); |
1
|
331 |
529
|
332 if (! p) |
1
|
333 return retval; |
|
334 |
240
|
335 file_info file = file_list (p); |
1
|
336 |
|
337 if (strcmp (file.mode (), "r") == 0) |
|
338 { |
|
339 warning ("can't flush an input stream"); |
|
340 return retval; |
|
341 } |
|
342 |
|
343 int success = 0; |
240
|
344 |
1
|
345 if (file.number () == 1) |
|
346 flush_output_to_pager (); |
|
347 else |
|
348 success = fflush (file.fptr ()); |
|
349 |
|
350 if (success == 0) |
636
|
351 retval(0) = 1.0; // succeeded |
1
|
352 else |
|
353 { |
|
354 error ("fflush: write error"); |
636
|
355 retval(0) = 0.0; // failed |
1
|
356 } |
|
357 |
|
358 return retval; |
|
359 } |
|
360 |
1181
|
361 DEFUN ("fflush", Ffflush, Sfflush, 1, 1, |
|
362 "fflush (FILENAME or FILENUM): flush buffered data to output file") |
|
363 { |
|
364 Octave_object retval; |
|
365 |
|
366 int nargin = args.length (); |
|
367 |
|
368 if (nargin != 1) |
|
369 print_usage ("fflush"); |
|
370 else |
|
371 retval = fflush_internal (args); |
|
372 |
|
373 return retval; |
|
374 } |
|
375 |
1
|
376 static int |
164
|
377 valid_mode (const char *mode) |
1
|
378 { |
529
|
379 if (mode) |
1
|
380 { |
|
381 char m = mode[0]; |
|
382 if (m == 'r' || m == 'w' || m == 'a') |
|
383 { |
|
384 m = mode[1]; |
|
385 return (m == '\0' || (m == '+' && mode[2] == '\0')); |
|
386 } |
|
387 } |
|
388 return 0; |
|
389 } |
|
390 |
1181
|
391 static Octave_object |
1339
|
392 fgets_internal (const Octave_object& args, int nargin, int nargout, |
|
393 int strip_final_newline = 0) |
1
|
394 { |
497
|
395 Octave_object retval; |
1
|
396 |
712
|
397 Pix p = file_io_get_file (args(0), "r", "fgets"); |
1
|
398 |
529
|
399 if (! p) |
368
|
400 return retval; |
|
401 |
1339
|
402 int length = 0; |
1338
|
403 |
|
404 if (nargin == 2) |
1
|
405 { |
1338
|
406 double dlen = args(1).double_value (); |
|
407 |
|
408 if (error_state) |
|
409 return retval; |
|
410 |
|
411 if (xisnan (dlen)) |
|
412 { |
|
413 error ("fgets: NaN invalid as length"); |
|
414 return retval; |
|
415 } |
|
416 |
1339
|
417 length = NINT (dlen); |
1338
|
418 |
|
419 if ((double) length != dlen) |
|
420 { |
|
421 error ("fgets: length not an integer value"); |
|
422 return retval; |
|
423 } |
|
424 |
1339
|
425 if (length < 0) |
|
426 { |
|
427 error ("fgets: length must be a nonnegative integer"); |
|
428 return retval; |
|
429 } |
1
|
430 } |
1339
|
431 |
|
432 file_info file = file_list (p); |
|
433 FILE *fileptr = file.fptr (); |
|
434 |
|
435 ostrstream buf; |
|
436 int c; |
|
437 int count = 0; |
|
438 int newline_stripped = 0; |
|
439 |
|
440 if (nargin == 1 || length > 0) |
1
|
441 { |
1339
|
442 while ((c = fgetc (fileptr)) != EOF) |
1338
|
443 { |
1339
|
444 count++; |
1338
|
445 if (c == '\n') |
|
446 { |
1339
|
447 if (! strip_final_newline) |
|
448 buf << (char) c; |
|
449 else |
|
450 newline_stripped = 1; |
|
451 |
1338
|
452 break; |
|
453 } |
1339
|
454 else |
|
455 buf << (char) c; |
|
456 |
|
457 if (nargin == 2 && count == length) |
|
458 break; |
1338
|
459 } |
1
|
460 } |
1339
|
461 |
|
462 buf << ends; |
|
463 char *string = buf.str (); |
|
464 |
|
465 if (count) |
1338
|
466 { |
|
467 if (nargout == 2) |
1339
|
468 retval(1) = (double) (count - newline_stripped); |
1338
|
469 |
|
470 retval(0) = string; |
|
471 } |
|
472 else |
|
473 retval(0) = -1.0; |
|
474 |
|
475 delete [] string; |
1
|
476 |
|
477 return retval; |
|
478 } |
|
479 |
1339
|
480 DEFUN ("fgetl", Ffgetl, Sfgetl, 2, 2, |
|
481 "[STRING, LENGTH] = fgetl (FILENAME or FILENUM [, LENGTH])\n\ |
|
482 \n\ |
|
483 read a string from a file") |
|
484 { |
|
485 Octave_object retval; |
|
486 |
|
487 int nargin = args.length (); |
|
488 |
|
489 if (nargin == 1 || nargin == 2) |
|
490 retval = fgets_internal (args, nargin, nargout, 1); |
|
491 else |
|
492 print_usage ("fgetl"); |
|
493 |
|
494 return retval; |
|
495 } |
|
496 |
1181
|
497 DEFUN ("fgets", Ffgets, Sfgets, 2, 2, |
1339
|
498 "[STRING, LENGTH] = fgets (FILENAME or FILENUM [, LENGTH])\n\ |
529
|
499 \n\ |
1181
|
500 read a string from a file") |
529
|
501 { |
|
502 Octave_object retval; |
|
503 |
|
504 int nargin = args.length (); |
|
505 |
1338
|
506 if (nargin == 1 || nargin == 2) |
|
507 retval = fgets_internal (args, nargin, nargout); |
|
508 else |
1181
|
509 print_usage ("fgets"); |
529
|
510 |
|
511 return retval; |
|
512 } |
|
513 |
1181
|
514 static Octave_object |
497
|
515 fopen_internal (const Octave_object& args) |
1
|
516 { |
497
|
517 Octave_object retval; |
1
|
518 Pix p; |
|
519 |
712
|
520 if (! args(0).is_string ()) |
1
|
521 { |
|
522 error ("fopen: file name must be a string"); |
|
523 return retval; |
|
524 } |
|
525 |
712
|
526 p = return_valid_file (args(0)); |
1
|
527 |
529
|
528 if (p) |
1
|
529 { |
240
|
530 file_info file = file_list (p); |
1
|
531 |
636
|
532 retval(0) = (double) file.number (); |
1
|
533 |
|
534 return retval; |
|
535 } |
|
536 |
712
|
537 if (! args(1).is_string ()) |
1
|
538 { |
370
|
539 error ("fopen: file mode must be a string"); |
1
|
540 return retval; |
|
541 } |
|
542 |
1363
|
543 const char *name = args(0).string_value (); |
|
544 const char *mode = args(1).string_value (); |
1
|
545 |
|
546 if (! valid_mode (mode)) |
|
547 { |
|
548 error ("fopen: invalid mode"); |
|
549 return retval; |
|
550 } |
|
551 |
|
552 struct stat buffer; |
|
553 if (stat (name, &buffer) == 0 && (buffer.st_mode & S_IFDIR) == S_IFDIR) |
|
554 { |
|
555 error ("fopen: can't open directory"); |
|
556 return retval; |
|
557 } |
|
558 |
|
559 FILE *file_ptr = fopen (name, mode); |
|
560 |
529
|
561 if (! file_ptr) |
1
|
562 { |
370
|
563 error ("fopen: unable to open file `%s'", name); |
1
|
564 return retval; |
|
565 } |
|
566 |
896
|
567 int file_number = file_list.length () + 1; |
1
|
568 |
896
|
569 file_info file (file_number, name, file_ptr, mode); |
1
|
570 file_list.append (file); |
|
571 |
896
|
572 retval(0) = (double) file_number; |
1
|
573 |
|
574 return retval; |
|
575 } |
|
576 |
1181
|
577 DEFUN ("fopen", Ffopen, Sfopen, 2, 1, |
|
578 "FILENUM = fopen (FILENAME, MODE): open a file\n\ |
|
579 \n\ |
|
580 Valid values for mode include:\n\ |
|
581 \n\ |
|
582 r : open text file for reading\n\ |
|
583 w : open text file for writing; discard previous contents if any\n\ |
|
584 a : append; open or create text file for writing at end of file\n\ |
|
585 r+ : open text file for update (i.e., reading and writing)\n\ |
|
586 w+ : create text file for update; discard previous contents if any\n\ |
|
587 a+ : append; open or create text file for update, writing at end\n\n\ |
|
588 Update mode permits reading from and writing to the same file.") |
529
|
589 { |
|
590 Octave_object retval; |
|
591 |
|
592 int nargin = args.length (); |
|
593 |
1181
|
594 if (nargin != 2) |
|
595 print_usage ("fopen"); |
|
596 else |
|
597 retval = fopen_internal (args); |
529
|
598 |
|
599 return retval; |
|
600 } |
|
601 |
1181
|
602 static Octave_object |
164
|
603 freport_internal (void) |
1
|
604 { |
497
|
605 Octave_object retval; |
1
|
606 Pix p = file_list.first (); |
|
607 |
|
608 ostrstream output_buf; |
|
609 |
|
610 output_buf << "\n number mode name\n\n"; |
896
|
611 |
|
612 int file_count = file_list.length (); |
1
|
613 for (int i = 0; i < file_count; i++) |
|
614 { |
240
|
615 file_info file = file_list (p); |
1
|
616 output_buf.form ("%7d%6s %s\n", file.number (), file.mode (), |
|
617 file.name ()); |
|
618 file_list.next (p); |
|
619 } |
|
620 |
|
621 output_buf << "\n" << ends; |
|
622 maybe_page_output (output_buf); |
|
623 |
|
624 return retval; |
|
625 } |
|
626 |
1181
|
627 DEFUN ("freport", Ffreport, Sfreport, 0, 1, |
|
628 "freport (): list open files and their status") |
|
629 { |
|
630 Octave_object retval; |
|
631 |
|
632 int nargin = args.length (); |
|
633 |
|
634 if (nargin > 0) |
|
635 warning ("freport: ignoring extra arguments"); |
|
636 |
|
637 retval = freport_internal (); |
|
638 |
|
639 return retval; |
|
640 } |
|
641 |
|
642 static Octave_object |
|
643 frewind_internal (const Octave_object& args) |
|
644 { |
|
645 Octave_object retval; |
|
646 |
|
647 Pix p = file_io_get_file (args(0), "a+", "frewind"); |
|
648 |
|
649 if (p) |
|
650 { |
|
651 file_info file = file_list (p); |
|
652 rewind (file.fptr ()); |
|
653 } |
|
654 |
|
655 return retval; |
|
656 } |
|
657 |
712
|
658 DEFUN ("frewind", Ffrewind, Sfrewind, 1, 1, |
529
|
659 "frewind (FILENAME or FILENUM): set file position at beginning of file") |
|
660 { |
|
661 Octave_object retval; |
|
662 |
|
663 int nargin = args.length (); |
|
664 |
712
|
665 if (nargin != 1) |
529
|
666 print_usage ("frewind"); |
|
667 else |
|
668 retval = frewind_internal (args); |
|
669 |
|
670 return retval; |
|
671 } |
|
672 |
1181
|
673 static Octave_object |
506
|
674 fseek_internal (const Octave_object& args) |
1
|
675 { |
497
|
676 Octave_object retval; |
1
|
677 |
506
|
678 int nargin = args.length (); |
|
679 |
712
|
680 Pix p = file_io_get_file (args(0), "a+", "fseek"); |
1
|
681 |
529
|
682 if (! p) |
368
|
683 return retval; |
1
|
684 |
|
685 long origin = SEEK_SET; |
636
|
686 |
712
|
687 double doff = args(1).double_value (); |
636
|
688 |
|
689 if (error_state) |
|
690 return retval; |
|
691 |
1086
|
692 if (xisnan (doff)) |
|
693 { |
|
694 error ("fseek: NaN invalid as offset"); |
|
695 return retval; |
|
696 } |
|
697 |
636
|
698 long offset = NINT (doff); |
|
699 |
|
700 if ((double) offset != doff) |
1
|
701 { |
636
|
702 error ("fseek: offset not an integer value"); |
|
703 return retval; |
|
704 } |
|
705 |
712
|
706 if (nargin == 3) |
636
|
707 { |
712
|
708 double dorig = args(2).double_value (); |
636
|
709 |
|
710 if (error_state) |
|
711 return retval; |
|
712 |
1086
|
713 if (xisnan (dorig)) |
|
714 { |
|
715 error ("fseek: NaN invalid as origin"); |
|
716 return retval; |
|
717 } |
|
718 |
636
|
719 origin = NINT (dorig); |
|
720 |
|
721 if ((double) dorig != origin) |
1
|
722 { |
636
|
723 error ("fseek: origin not an integer value"); |
1
|
724 return retval; |
|
725 } |
|
726 |
763
|
727 if (origin == 0) |
|
728 origin = SEEK_SET; |
|
729 else if (origin == 1) |
1
|
730 origin = SEEK_CUR; |
763
|
731 else if (origin == 2) |
1
|
732 origin = SEEK_END; |
|
733 else |
|
734 { |
636
|
735 error ("fseek: invalid value for origin"); |
|
736 return retval; |
1
|
737 } |
|
738 } |
|
739 |
240
|
740 file_info file = file_list (p); |
1
|
741 int success = fseek (file.fptr (), offset, origin); |
|
742 |
|
743 if (success == 0) |
636
|
744 retval(0) = 1.0; // succeeded |
1
|
745 else |
|
746 { |
|
747 error ("fseek: file error"); |
636
|
748 retval(0) = 0.0; // failed |
1
|
749 } |
|
750 |
|
751 return retval; |
|
752 } |
|
753 |
1181
|
754 DEFUN ("fseek", Ffseek, Sfseek, 3, 1, |
|
755 "fseek (FILENAME or FILENUM, OFFSET [, ORIGIN])\n\ |
|
756 \n\ |
|
757 set file position for reading or writing") |
529
|
758 { |
|
759 Octave_object retval; |
|
760 |
|
761 int nargin = args.length (); |
|
762 |
1181
|
763 if (nargin != 2 && nargin != 3) |
|
764 print_usage ("fseek"); |
529
|
765 else |
1181
|
766 retval = fseek_internal (args); |
529
|
767 |
|
768 return retval; |
|
769 } |
|
770 |
1181
|
771 // Tell current position of file. |
|
772 |
|
773 static Octave_object |
497
|
774 ftell_internal (const Octave_object& args) |
1
|
775 { |
497
|
776 Octave_object retval; |
1
|
777 |
712
|
778 Pix p = file_io_get_file (args(0), "a+", "ftell"); |
1
|
779 |
529
|
780 if (p) |
368
|
781 { |
|
782 file_info file = file_list (p); |
|
783 long offset = ftell (file.fptr ()); |
636
|
784 |
|
785 retval(0) = (double) offset; |
1
|
786 |
368
|
787 if (offset == -1L) |
|
788 error ("ftell: write error"); |
|
789 } |
1
|
790 |
|
791 return retval; |
|
792 } |
|
793 |
1181
|
794 DEFUN ("ftell", Fftell, Sftell, 1, 1, |
|
795 "POSITION = ftell (FILENAME or FILENUM): returns the current file position") |
|
796 { |
|
797 Octave_object retval; |
|
798 |
|
799 int nargin = args.length (); |
|
800 |
|
801 if (nargin != 1) |
|
802 print_usage ("ftell"); |
|
803 else |
|
804 retval = ftell_internal (args); |
|
805 |
|
806 return retval; |
|
807 } |
|
808 |
1
|
809 void |
164
|
810 close_files (void) |
1
|
811 { |
|
812 Pix p = file_list.first (); |
|
813 |
896
|
814 int file_count = file_list.length (); |
1
|
815 for (int i = 0; i < file_count; i++) |
|
816 { |
896
|
817 if (p) |
1
|
818 { |
896
|
819 file_info file = file_list (p); |
|
820 |
|
821 if (i > 2) // do not close stdin, stdout, stderr! |
|
822 { |
|
823 int success = fclose (file.fptr ()); |
|
824 if (success != 0) |
|
825 error ("closing %s", file.name ()); |
|
826 } |
|
827 |
|
828 file_list.del (p); |
1
|
829 } |
896
|
830 else |
|
831 { |
|
832 error ("inconsistent state for internal file list!"); |
|
833 break; |
|
834 } |
1
|
835 } |
|
836 } |
|
837 |
|
838 static int |
497
|
839 process_printf_format (const char *s, const Octave_object& args, |
506
|
840 ostrstream& sb, const char *type) |
1
|
841 { |
|
842 ostrstream fmt; |
|
843 |
506
|
844 int nargin = args.length (); |
|
845 |
1
|
846 fmt << "%"; // do_printf() already blew past this one... |
|
847 |
|
848 int chars_from_fmt_str = 0; |
|
849 |
|
850 again: |
|
851 switch (*s) |
|
852 { |
|
853 case '+': case '-': case ' ': case '0': case '#': |
|
854 chars_from_fmt_str++; |
|
855 fmt << *s++; |
|
856 goto again; |
|
857 |
|
858 case '\0': |
|
859 goto invalid_format; |
|
860 |
|
861 default: |
|
862 break; |
|
863 } |
|
864 |
|
865 if (*s == '*') |
|
866 { |
712
|
867 if (fmt_arg_count > nargin) |
1
|
868 { |
218
|
869 error ("%s: not enough arguments", type); |
1
|
870 return -1; |
|
871 } |
|
872 |
636
|
873 double tmp_len = args(fmt_arg_count++).double_value (); |
|
874 |
1086
|
875 if (error_state || xisnan (tmp_len)) |
1
|
876 { |
218
|
877 error ("%s: `*' must be replaced by an integer", type); |
1
|
878 return -1; |
|
879 } |
|
880 |
636
|
881 fmt << NINT (tmp_len); |
1
|
882 s++; |
|
883 chars_from_fmt_str++; |
|
884 } |
|
885 else |
|
886 { |
|
887 while (*s != '\0' && isdigit (*s)) |
|
888 { |
|
889 chars_from_fmt_str++; |
|
890 fmt << *s++; |
|
891 } |
|
892 } |
|
893 |
|
894 if (*s == '\0') |
|
895 goto invalid_format; |
|
896 |
|
897 if (*s == '.') |
|
898 { |
|
899 chars_from_fmt_str++; |
|
900 fmt << *s++; |
|
901 } |
|
902 |
|
903 if (*s == '*') |
|
904 { |
|
905 if (*(s-1) == '*') |
|
906 goto invalid_format; |
|
907 |
712
|
908 if (fmt_arg_count > nargin) |
1
|
909 { |
218
|
910 error ("%s: not enough arguments", type); |
1
|
911 return -1; |
|
912 } |
|
913 |
636
|
914 double tmp_len = args(fmt_arg_count++).double_value (); |
|
915 |
1086
|
916 if (error_state || xisnan (tmp_len)) |
1
|
917 { |
218
|
918 error ("%s: `*' must be replaced by an integer", type); |
1
|
919 return -1; |
|
920 } |
|
921 |
636
|
922 fmt << NINT (tmp_len); |
1
|
923 s++; |
|
924 chars_from_fmt_str++; |
|
925 } |
|
926 else |
|
927 { |
|
928 while (*s != '\0' && isdigit (*s)) |
|
929 { |
|
930 chars_from_fmt_str++; |
|
931 fmt << *s++; |
|
932 } |
|
933 } |
|
934 |
|
935 if (*s == '\0') |
|
936 goto invalid_format; |
|
937 |
|
938 if (*s != '\0' && (*s == 'h' || *s == 'l' || *s == 'L')) |
|
939 { |
|
940 chars_from_fmt_str++; |
|
941 fmt << *s++; |
|
942 } |
|
943 |
|
944 if (*s == '\0') |
|
945 goto invalid_format; |
|
946 |
712
|
947 if (fmt_arg_count > nargin) |
1
|
948 { |
218
|
949 error ("%s: not enough arguments", type); |
1
|
950 return -1; |
|
951 } |
|
952 |
|
953 switch (*s) |
|
954 { |
|
955 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': |
636
|
956 { |
|
957 double d = args(fmt_arg_count++).double_value (); |
1
|
958 |
1086
|
959 if (error_state || xisnan (d)) |
|
960 goto invalid_conversion; |
|
961 |
636
|
962 int val = NINT (d); |
|
963 |
1086
|
964 if ((double) val != d) |
636
|
965 goto invalid_conversion; |
|
966 else |
|
967 { |
|
968 chars_from_fmt_str++; |
|
969 fmt << *s << ends; |
|
970 char *tmp_fmt = fmt.str (); |
|
971 sb.form (tmp_fmt, val); |
|
972 delete [] tmp_fmt; |
|
973 return chars_from_fmt_str; |
|
974 } |
|
975 } |
1
|
976 |
|
977 case 'e': case 'E': case 'f': case 'g': case 'G': |
636
|
978 { |
|
979 double val = args(fmt_arg_count++).double_value (); |
1
|
980 |
636
|
981 if (error_state) |
|
982 goto invalid_conversion; |
|
983 else |
|
984 { |
|
985 chars_from_fmt_str++; |
|
986 fmt << *s << ends; |
|
987 char *tmp_fmt = fmt.str (); |
|
988 sb.form (tmp_fmt, val); |
|
989 delete [] tmp_fmt; |
|
990 return chars_from_fmt_str; |
|
991 } |
|
992 } |
1
|
993 |
|
994 case 's': |
636
|
995 { |
1363
|
996 const char *val = args(fmt_arg_count++).string_value (); |
1
|
997 |
636
|
998 if (error_state) |
|
999 goto invalid_conversion; |
|
1000 else |
|
1001 { |
|
1002 chars_from_fmt_str++; |
|
1003 fmt << *s << ends; |
|
1004 char *tmp_fmt = fmt.str (); |
|
1005 sb.form (tmp_fmt, val); |
|
1006 delete [] tmp_fmt; |
|
1007 return chars_from_fmt_str; |
|
1008 } |
|
1009 } |
1
|
1010 |
|
1011 case 'c': |
636
|
1012 { |
1363
|
1013 const char *val = args(fmt_arg_count++).string_value (); |
1
|
1014 |
636
|
1015 if (error_state || strlen (val) != 1) |
|
1016 goto invalid_conversion; |
|
1017 else |
|
1018 { |
|
1019 chars_from_fmt_str++; |
|
1020 fmt << *s << ends; |
|
1021 char *tmp_fmt = fmt.str (); |
|
1022 sb.form (tmp_fmt, *val); |
|
1023 delete [] tmp_fmt; |
|
1024 return chars_from_fmt_str; |
|
1025 } |
|
1026 } |
1
|
1027 |
|
1028 default: |
|
1029 goto invalid_format; |
|
1030 } |
|
1031 |
|
1032 invalid_conversion: |
218
|
1033 error ("%s: invalid conversion", type); |
1
|
1034 return -1; |
|
1035 |
|
1036 invalid_format: |
218
|
1037 error ("%s: invalid format", type); |
1
|
1038 return -1; |
|
1039 } |
|
1040 |
761
|
1041 // Formatted printing to a file. |
1
|
1042 |
1181
|
1043 static Octave_object |
506
|
1044 do_printf (const char *type, const Octave_object& args, int nargout) |
1
|
1045 { |
497
|
1046 Octave_object retval; |
712
|
1047 fmt_arg_count = 0; |
1363
|
1048 const char *fmt; |
240
|
1049 file_info file; |
1
|
1050 |
|
1051 if (strcmp (type, "fprintf") == 0) |
|
1052 { |
712
|
1053 Pix p = file_io_get_file (args(0), "a+", type); |
368
|
1054 |
529
|
1055 if (! p) |
368
|
1056 return retval; |
1
|
1057 |
|
1058 file = file_list (p); |
368
|
1059 |
1
|
1060 if (file.mode () == "r") |
|
1061 { |
|
1062 error ("%s: file is read only", type); |
|
1063 return retval; |
|
1064 } |
368
|
1065 |
712
|
1066 fmt = args(1).string_value (); |
368
|
1067 |
636
|
1068 if (error_state) |
|
1069 { |
|
1070 error ("%s: format must be a string", type); |
|
1071 return retval; |
|
1072 } |
|
1073 |
|
1074 fmt_arg_count += 2; |
1
|
1075 } |
|
1076 else |
|
1077 { |
712
|
1078 fmt = args(0).string_value (); |
636
|
1079 |
|
1080 if (error_state) |
|
1081 { |
|
1082 error ("%s: invalid format string", type); |
|
1083 return retval; |
|
1084 } |
|
1085 |
|
1086 fmt_arg_count++; |
1
|
1087 } |
|
1088 |
1358
|
1089 // Scan fmt for % escapes and print out the arguments. |
1
|
1090 |
|
1091 ostrstream output_buf; |
|
1092 |
1363
|
1093 const char *ptr = fmt; |
1
|
1094 |
|
1095 for (;;) |
|
1096 { |
|
1097 char c; |
|
1098 while ((c = *ptr++) != '\0' && c != '%') |
|
1099 output_buf << c; |
|
1100 |
|
1101 if (c == '\0') |
|
1102 break; |
|
1103 |
|
1104 if (*ptr == '%') |
|
1105 { |
|
1106 ptr++; |
|
1107 output_buf << c; |
|
1108 continue; |
|
1109 } |
|
1110 |
1358
|
1111 // We must be looking at a format specifier. Extract it or |
|
1112 // fail. |
1
|
1113 |
506
|
1114 int status = process_printf_format (ptr, args, output_buf, type); |
1
|
1115 |
|
1116 if (status < 0) |
|
1117 return retval; |
|
1118 |
|
1119 ptr += status; |
|
1120 } |
|
1121 |
|
1122 output_buf << ends; |
|
1123 if (strcmp (type, "printf") == 0 |
|
1124 || (strcmp (type, "fprintf") == 0 && file.number () == 1)) |
|
1125 { |
|
1126 maybe_page_output (output_buf); |
|
1127 } |
|
1128 else if (strcmp (type, "fprintf") == 0) |
|
1129 { |
|
1130 char *msg = output_buf.str (); |
|
1131 int success = fputs (msg, file.fptr ()); |
|
1132 if (success == EOF) |
218
|
1133 warning ("%s: unknown failure writing to file", type); |
1
|
1134 delete [] msg; |
|
1135 } |
|
1136 else if (strcmp (type, "sprintf") == 0) |
|
1137 { |
|
1138 char *msg = output_buf.str (); |
636
|
1139 retval(0) = msg; |
1
|
1140 delete [] msg; |
|
1141 } |
|
1142 |
|
1143 return retval; |
|
1144 } |
|
1145 |
1181
|
1146 DEFUN ("fprintf", Ffprintf, Sfprintf, -1, 1, |
|
1147 "fprintf (FILENAME or FILENUM, FORMAT, ...)") |
|
1148 { |
|
1149 Octave_object retval; |
|
1150 |
|
1151 int nargin = args.length (); |
|
1152 |
|
1153 if (nargin < 2) |
|
1154 print_usage ("fprintf"); |
|
1155 else |
|
1156 retval = do_printf ("fprintf", args, nargout); |
|
1157 |
|
1158 return retval; |
|
1159 } |
|
1160 |
|
1161 // Formatted printing. |
|
1162 |
|
1163 DEFUN ("printf", Fprintf, Sprintf, -1, 1, |
|
1164 "printf (FORMAT, ...)") |
|
1165 { |
|
1166 Octave_object retval; |
|
1167 |
|
1168 int nargin = args.length (); |
|
1169 |
|
1170 if (nargin < 1) |
|
1171 print_usage ("printf"); |
|
1172 else |
|
1173 retval = do_printf ("printf", args, nargout); |
|
1174 |
|
1175 return retval; |
|
1176 } |
|
1177 |
|
1178 // Formatted printing to a string. |
|
1179 |
|
1180 DEFUN ("sprintf", Fsprintf, Ssprintf, -1, 1, |
|
1181 "s = sprintf (FORMAT, ...)") |
|
1182 { |
|
1183 Octave_object retval; |
|
1184 |
|
1185 int nargin = args.length (); |
|
1186 |
|
1187 if (nargin < 1) |
|
1188 print_usage ("sprintf"); |
|
1189 else |
|
1190 retval = do_printf ("sprintf", args, nargout); |
|
1191 |
|
1192 return retval; |
|
1193 } |
|
1194 |
1
|
1195 static int |
368
|
1196 process_scanf_format (const char *s, ostrstream& fmt, |
|
1197 const char *type, int nargout, FILE* fptr, |
497
|
1198 Octave_object& values) |
1
|
1199 { |
|
1200 fmt << "%"; |
|
1201 |
|
1202 int chars_from_fmt_str = 0; |
|
1203 int store_value = 1; |
628
|
1204 int string_width = 0; |
1
|
1205 int success = 1; |
|
1206 |
|
1207 if (*s == '*') |
|
1208 { |
|
1209 store_value = 0; |
|
1210 s++; |
|
1211 chars_from_fmt_str++; |
|
1212 } |
|
1213 |
|
1214 if (isdigit (*s)) |
|
1215 { |
|
1216 ostrstream str_number; |
|
1217 while (*s != '\0' && isdigit (*s)) |
|
1218 { |
|
1219 chars_from_fmt_str++; |
|
1220 str_number << *s; |
|
1221 fmt << *s++; |
|
1222 } |
|
1223 str_number << ends; |
|
1224 char *number = str_number.str (); |
|
1225 string_width = atoi (number); |
|
1226 delete [] number; |
|
1227 } |
|
1228 |
|
1229 if (*s == '\0') |
|
1230 goto invalid_format; |
|
1231 |
|
1232 if (*s != '\0' && (*s == 'h' || *s == 'l' || *s == 'L')) |
|
1233 { |
|
1234 chars_from_fmt_str++; |
|
1235 s++; |
|
1236 } |
|
1237 |
|
1238 if (*s == '\0') |
|
1239 goto invalid_format; |
|
1240 |
1358
|
1241 // Even if we don't have a place to store them, attempt to convert |
|
1242 // everything specified by the format string. |
1
|
1243 |
712
|
1244 if (fmt_arg_count > (nargout ? nargout : 1)) |
368
|
1245 store_value = 0; |
1
|
1246 |
|
1247 switch (*s) |
|
1248 { |
|
1249 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': |
|
1250 { |
|
1251 chars_from_fmt_str++; |
|
1252 fmt << *s << ends; |
|
1253 int temp; |
|
1254 char *str = fmt.str (); |
|
1255 success = fscanf (fptr, str, &temp); |
|
1256 delete [] str; |
|
1257 if (success > 0 && store_value) |
636
|
1258 values(fmt_arg_count++) = (double) temp; |
1
|
1259 } |
|
1260 break; |
628
|
1261 |
1
|
1262 case 'e': case 'E': case 'f': case 'g': case 'G': |
|
1263 { |
|
1264 chars_from_fmt_str++; |
|
1265 fmt << 'l' << *s << ends; |
|
1266 double temp; |
|
1267 char *str = fmt.str (); |
|
1268 success = fscanf (fptr, str, &temp); |
|
1269 delete [] str; |
|
1270 if (success > 0 && store_value) |
636
|
1271 values(fmt_arg_count++) = temp; |
1
|
1272 } |
|
1273 break; |
628
|
1274 |
1
|
1275 case 's': |
|
1276 { |
|
1277 if (string_width < 1) |
|
1278 { |
1358
|
1279 // XXX FIXME XXX -- The code below is miscompiled on the |
|
1280 // Alpha with gcc 2.6.0, so that string_width is never |
|
1281 // incremented, even though reading the data works |
|
1282 // correctly. One fix is to use a fixed-size buffer... |
628
|
1283 // string_width = 8192; |
|
1284 |
1
|
1285 string_width = 0; |
|
1286 long original_position = ftell (fptr); |
628
|
1287 |
1
|
1288 int c; |
|
1289 |
636
|
1290 while ((c = getc (fptr)) != EOF && isspace (c)) |
1
|
1291 ; // Don't count leading whitespace. |
|
1292 |
|
1293 if (c != EOF) |
|
1294 string_width++; |
|
1295 |
|
1296 for (;;) |
|
1297 { |
|
1298 c = getc (fptr); |
636
|
1299 if (c != EOF && ! isspace (c)) |
1
|
1300 string_width++; |
|
1301 else |
|
1302 break; |
|
1303 } |
|
1304 |
|
1305 fseek (fptr, original_position, SEEK_SET); |
|
1306 } |
|
1307 chars_from_fmt_str++; |
636
|
1308 char temp [string_width+1]; |
1
|
1309 fmt << *s << ends; |
|
1310 char *str = fmt.str (); |
|
1311 success = fscanf (fptr, str, temp); |
|
1312 delete [] str; |
636
|
1313 temp[string_width] = '\0'; |
628
|
1314 if (success > 0 && store_value) |
636
|
1315 values(fmt_arg_count++) = temp; |
1
|
1316 } |
|
1317 break; |
628
|
1318 |
1
|
1319 case 'c': |
|
1320 { |
|
1321 if (string_width < 1) |
|
1322 string_width = 1; |
|
1323 chars_from_fmt_str++; |
636
|
1324 char temp [string_width+1]; |
1
|
1325 memset (temp, '\0', string_width+1); |
|
1326 fmt << *s << ends; |
|
1327 char *str = fmt.str (); |
|
1328 success = fscanf (fptr, str, temp); |
|
1329 delete [] str; |
|
1330 temp[string_width] = '\0'; |
|
1331 if (success > 0 && store_value) |
636
|
1332 values(fmt_arg_count++) = temp; |
1
|
1333 } |
|
1334 break; |
628
|
1335 |
1
|
1336 default: |
|
1337 goto invalid_format; |
|
1338 } |
|
1339 |
368
|
1340 if (success > 0) |
1
|
1341 return chars_from_fmt_str; |
368
|
1342 else if (success == 0) |
218
|
1343 warning ("%s: invalid conversion", type); |
1
|
1344 else if (success == EOF) |
|
1345 { |
|
1346 if (strcmp (type, "fscanf") == 0) |
218
|
1347 warning ("%s: end of file reached before final conversion", type); |
1
|
1348 else if (strcmp (type, "sscanf") == 0) |
218
|
1349 warning ("%s: end of string reached before final conversion", type); |
1
|
1350 else if (strcmp (type, "scanf") == 0) |
218
|
1351 warning ("%s: end of input reached before final conversion", type); |
1
|
1352 } |
|
1353 else |
|
1354 { |
|
1355 invalid_format: |
218
|
1356 warning ("%s: invalid format", type); |
1
|
1357 } |
|
1358 |
|
1359 return -1; |
|
1360 } |
|
1361 |
761
|
1362 // Formatted reading from a file. |
|
1363 |
1181
|
1364 static Octave_object |
506
|
1365 do_scanf (const char *type, const Octave_object& args, int nargout) |
1
|
1366 { |
497
|
1367 Octave_object retval; |
1363
|
1368 const char *scanf_fmt = 0; |
529
|
1369 char *tmp_file = 0; |
1
|
1370 int tmp_file_open = 0; |
529
|
1371 FILE *fptr = 0; |
240
|
1372 file_info file; |
1
|
1373 |
|
1374 fmt_arg_count = 0; |
|
1375 |
|
1376 if (strcmp (type, "scanf") != 0) |
|
1377 { |
712
|
1378 scanf_fmt = args(1).string_value (); |
636
|
1379 |
|
1380 if (error_state) |
1
|
1381 { |
|
1382 error ("%s: format must be a string", type); |
|
1383 return retval; |
|
1384 } |
|
1385 } |
|
1386 |
|
1387 int doing_fscanf = (strcmp (type, "fscanf") == 0); |
|
1388 |
|
1389 if (doing_fscanf) |
|
1390 { |
712
|
1391 Pix p = file_io_get_file (args(0), "r", type); |
368
|
1392 |
529
|
1393 if (! p) |
368
|
1394 return retval; |
1
|
1395 |
|
1396 file = file_list (p); |
|
1397 |
|
1398 if (strcmp (file.mode (), "w") == 0 || strcmp (file.mode (), "a") == 0) |
|
1399 { |
|
1400 error ("%s: this file is opened for writing only", type); |
|
1401 return retval; |
|
1402 } |
|
1403 |
|
1404 fptr = file.fptr (); |
|
1405 } |
|
1406 |
712
|
1407 if ((! fptr && args(0).is_string ()) |
368
|
1408 || (doing_fscanf && file.number () == 0)) |
1
|
1409 { |
1363
|
1410 const char *string; |
1
|
1411 |
|
1412 if (strcmp (type, "scanf") == 0) |
712
|
1413 scanf_fmt = args(0).string_value (); |
1
|
1414 |
|
1415 if (strcmp (type, "scanf") == 0 |
|
1416 || (doing_fscanf && file.number () == 0)) |
|
1417 { |
1358
|
1418 // XXX FIXME XXX -- this should probably be possible for |
|
1419 // more than just stdin/stdout pairs, using a list of output |
|
1420 // streams to flush. The list could be created with a |
|
1421 // function like iostream's tie(). |
870
|
1422 |
|
1423 flush_output_to_pager (); |
|
1424 |
1
|
1425 string = gnu_readline (""); |
870
|
1426 |
1
|
1427 if (string && *string) |
|
1428 maybe_save_history (string); |
|
1429 } |
|
1430 else |
712
|
1431 string = args(0).string_value (); |
1
|
1432 |
637
|
1433 tmp_file = octave_tmp_file_name (); |
1
|
1434 |
|
1435 fptr = fopen (tmp_file, "w+"); |
529
|
1436 if (! fptr) |
1
|
1437 { |
|
1438 error ("%s: error opening temporary file", type); |
|
1439 return retval; |
|
1440 } |
|
1441 tmp_file_open = 1; |
|
1442 unlink (tmp_file); |
|
1443 |
529
|
1444 if (! string) |
776
|
1445 { |
|
1446 error ("%s: no string to scan", type); |
|
1447 return retval; |
|
1448 } |
1
|
1449 |
|
1450 int success = fputs (string, fptr); |
|
1451 fflush (fptr); |
|
1452 rewind (fptr); |
|
1453 |
|
1454 if (success < 0) |
|
1455 { |
|
1456 error ("%s: trouble writing temporary file", type); |
|
1457 fclose (fptr); |
|
1458 return retval; |
|
1459 } |
|
1460 } |
|
1461 else if (! doing_fscanf) |
|
1462 { |
|
1463 error ("%s: first argument must be a string", type); |
|
1464 return retval; |
|
1465 } |
|
1466 |
1358
|
1467 // Scan scanf_fmt for % escapes and assign the arguments. |
1
|
1468 |
636
|
1469 retval.resize (nargout); |
1
|
1470 |
1363
|
1471 const char *ptr = scanf_fmt; |
1
|
1472 |
|
1473 for (;;) |
|
1474 { |
|
1475 ostrstream fmt; |
|
1476 char c; |
|
1477 while ((c = *ptr++) != '\0' && c != '%') |
|
1478 fmt << c; |
|
1479 |
|
1480 if (c == '\0') |
|
1481 break; |
|
1482 |
|
1483 if (*ptr == '%') |
|
1484 { |
|
1485 ptr++; |
|
1486 fmt << c; |
|
1487 continue; |
|
1488 } |
|
1489 |
1358
|
1490 // We must be looking at a format specifier. Extract it or |
|
1491 // fail. |
1
|
1492 |
368
|
1493 int status = process_scanf_format (ptr, fmt, type, nargout, |
|
1494 fptr, retval); |
1
|
1495 |
|
1496 if (status < 0) |
|
1497 { |
|
1498 if (fmt_arg_count == 0) |
497
|
1499 retval.resize (0); |
1
|
1500 break; |
|
1501 } |
|
1502 |
|
1503 ptr += status; |
|
1504 } |
|
1505 |
|
1506 if (tmp_file_open) |
|
1507 fclose (fptr); |
|
1508 |
|
1509 return retval; |
|
1510 } |
|
1511 |
1181
|
1512 DEFUN ("fscanf", Ffscanf, Sfscanf, 2, -1, |
|
1513 "[A, B, C, ...] = fscanf (FILENAME or FILENUM, FORMAT)") |
|
1514 { |
|
1515 Octave_object retval; |
|
1516 |
|
1517 int nargin = args.length (); |
|
1518 |
|
1519 if (nargin != 1 && nargin != 2) |
|
1520 print_usage ("fscanf"); |
|
1521 else |
|
1522 retval = do_scanf ("fscanf", args, nargout); |
|
1523 |
|
1524 return retval; |
|
1525 } |
|
1526 |
|
1527 // Formatted reading. |
|
1528 |
|
1529 DEFUN ("scanf", Fscanf, Sscanf, 1, -1, |
|
1530 "[A, B, C, ...] = scanf (FORMAT)") |
|
1531 { |
|
1532 Octave_object retval; |
|
1533 |
|
1534 int nargin = args.length (); |
|
1535 |
|
1536 if (nargin != 1) |
|
1537 print_usage ("scanf"); |
|
1538 else |
|
1539 retval = do_scanf ("scanf", args, nargout); |
|
1540 |
|
1541 return retval; |
|
1542 } |
|
1543 |
|
1544 // Formatted reading from a string. |
|
1545 |
|
1546 DEFUN ("sscanf", Fsscanf, Ssscanf, 2, -1, |
|
1547 "[A, B, C, ...] = sscanf (STRING, FORMAT)") |
|
1548 { |
|
1549 Octave_object retval; |
|
1550 |
|
1551 int nargin = args.length (); |
|
1552 |
|
1553 if (nargin != 2) |
|
1554 print_usage ("sscanf"); |
|
1555 else |
|
1556 retval = do_scanf ("sscanf", args, nargout); |
|
1557 |
|
1558 return retval; |
|
1559 } |
|
1560 |
761
|
1561 // Find out how many elements are left to read. |
|
1562 |
444
|
1563 static long |
1363
|
1564 num_items_remaining (FILE *fptr, const char *type) |
444
|
1565 { |
471
|
1566 size_t size; |
|
1567 |
|
1568 if (strcasecmp (type, "uchar") == 0) |
|
1569 size = sizeof (u_char); |
|
1570 else if (strcasecmp (type, "char") == 0) |
|
1571 size = sizeof (char); |
|
1572 else if (strcasecmp (type, "short") == 0) |
|
1573 size = sizeof (short); |
|
1574 else if (strcasecmp (type, "ushort") == 0) |
|
1575 size = sizeof (u_short); |
|
1576 else if (strcasecmp (type, "int") == 0) |
|
1577 size = sizeof (int); |
|
1578 else if (strcasecmp (type, "uint") == 0) |
|
1579 size = sizeof (u_int); |
|
1580 else if (strcasecmp (type, "long") == 0) |
|
1581 size = sizeof (long); |
|
1582 else if (strcasecmp (type, "ulong") == 0) |
|
1583 size = sizeof (u_long); |
|
1584 else if (strcasecmp (type, "float") == 0) |
|
1585 size = sizeof (float); |
|
1586 else if (strcasecmp (type, "double") == 0) |
|
1587 size = sizeof (double); |
|
1588 else |
|
1589 return 0; |
|
1590 |
444
|
1591 long curr_pos = ftell (fptr); |
|
1592 |
|
1593 fseek (fptr, 0, SEEK_END); |
|
1594 long end_of_file = ftell (fptr); |
|
1595 |
471
|
1596 fseek (fptr, curr_pos, SEEK_SET); |
444
|
1597 |
|
1598 long len = end_of_file - curr_pos; |
|
1599 |
471
|
1600 return len / size; |
444
|
1601 } |
|
1602 |
761
|
1603 // Read binary data from a file. |
|
1604 // |
|
1605 // [data, count] = fread (fid, size, 'precision') |
|
1606 // |
|
1607 // fid : the file id from fopen |
|
1608 // size : the size of the matrix or vector or scaler to read |
|
1609 // |
|
1610 // n : reads n elements of a column vector |
|
1611 // inf : reads to the end of file (default) |
|
1612 // [m, n] : reads enough elements to fill the matrix |
|
1613 // the number of columns can be inf |
|
1614 // |
|
1615 // precision : type of the element. Can be: |
|
1616 // |
|
1617 // char, uchar, schar, short, ushort, int, uint, |
|
1618 // long, ulong, float, double |
|
1619 // |
|
1620 // Default is uchar. |
|
1621 // |
|
1622 // data : output data |
|
1623 // count : number of elements read |
|
1624 |
1181
|
1625 static Octave_object |
506
|
1626 fread_internal (const Octave_object& args, int nargout) |
444
|
1627 { |
497
|
1628 Octave_object retval; |
444
|
1629 |
506
|
1630 int nargin = args.length (); |
|
1631 |
712
|
1632 Pix p = file_io_get_file (args(0), "r", "fread"); |
444
|
1633 |
529
|
1634 if (! p) |
444
|
1635 return retval; |
|
1636 |
1358
|
1637 // Get type and number of bytes per element to read. |
|
1638 |
1363
|
1639 const char *prec = "uchar"; |
712
|
1640 if (nargin > 2) |
444
|
1641 { |
712
|
1642 prec = args(2).string_value (); |
636
|
1643 |
|
1644 if (error_state) |
444
|
1645 { |
|
1646 error ("fread: precision must be a specified as a string"); |
|
1647 return retval; |
|
1648 } |
|
1649 } |
|
1650 |
|
1651 file_info file = file_list (p); |
471
|
1652 |
|
1653 FILE *fptr = file.fptr (); |
444
|
1654 |
1358
|
1655 // Set up matrix to read into. If specified in arguments use that |
|
1656 // number, otherwise read everyting left in file. |
444
|
1657 |
471
|
1658 double dnr = 0.0; |
|
1659 double dnc = 0.0; |
|
1660 int nr; |
|
1661 int nc; |
444
|
1662 |
712
|
1663 if (nargin > 1) |
444
|
1664 { |
712
|
1665 if (args(1).is_scalar_type ()) |
444
|
1666 { |
712
|
1667 dnr = args(1).double_value (); |
636
|
1668 |
|
1669 if (error_state) |
|
1670 return retval; |
|
1671 |
471
|
1672 dnc = 1.0; |
444
|
1673 } |
636
|
1674 else |
444
|
1675 { |
712
|
1676 ColumnVector tmp = args(1).vector_value (); |
444
|
1677 |
636
|
1678 if (error_state || tmp.length () != 2) |
471
|
1679 { |
|
1680 error ("fread: invalid size specification\n"); |
|
1681 return retval; |
|
1682 } |
636
|
1683 |
|
1684 dnr = tmp.elem (0); |
|
1685 dnc = tmp.elem (1); |
471
|
1686 } |
|
1687 |
|
1688 if ((xisinf (dnr)) && (xisinf (dnc))) |
444
|
1689 { |
471
|
1690 error ("fread: number of rows and columns cannot both be infinite"); |
444
|
1691 return retval; |
|
1692 } |
|
1693 |
471
|
1694 if (xisinf (dnr)) |
|
1695 { |
1086
|
1696 if (xisnan (dnc)) |
|
1697 { |
|
1698 error ("fread: NaN invalid as the number of columns"); |
|
1699 return retval; |
|
1700 } |
|
1701 else |
|
1702 { |
|
1703 nc = NINT (dnc); |
|
1704 int n = num_items_remaining (fptr, prec); |
|
1705 nr = n / nc; |
|
1706 if (n > nr * nc) |
|
1707 nr++; |
|
1708 } |
471
|
1709 } |
|
1710 else if (xisinf (dnc)) |
|
1711 { |
1086
|
1712 if (xisnan (dnr)) |
|
1713 { |
|
1714 error ("fread: NaN invalid as the number of rows"); |
|
1715 return retval; |
|
1716 } |
|
1717 else |
|
1718 { |
|
1719 nr = NINT (dnr); |
|
1720 int n = num_items_remaining (fptr, prec); |
|
1721 nc = n / nr; |
|
1722 if (n > nc * nr) |
|
1723 nc++; |
|
1724 } |
471
|
1725 } |
|
1726 else |
|
1727 { |
1086
|
1728 if (xisnan (dnr)) |
|
1729 { |
|
1730 error ("fread: NaN invalid as the number of rows"); |
|
1731 return retval; |
|
1732 } |
|
1733 else |
|
1734 nr = NINT (dnr); |
|
1735 |
|
1736 if (xisnan (dnc)) |
|
1737 { |
|
1738 error ("fread: NaN invalid as the number of columns"); |
|
1739 return retval; |
|
1740 } |
|
1741 else |
|
1742 nc = NINT (dnc); |
471
|
1743 } |
444
|
1744 } |
|
1745 else |
|
1746 { |
1358
|
1747 // No size parameter, read what's left of the file. |
|
1748 |
471
|
1749 nc = 1; |
|
1750 int n = num_items_remaining (fptr, prec); |
|
1751 nr = n / nc; |
|
1752 if (n > nr * nc) |
|
1753 nr++; |
444
|
1754 } |
|
1755 |
|
1756 Matrix m (nr, nc, octave_NaN); |
|
1757 |
1358
|
1758 // Read data. |
444
|
1759 |
471
|
1760 int count = m.read (fptr, prec); |
444
|
1761 |
|
1762 if (nargout > 1) |
636
|
1763 retval(1) = (double) count; |
444
|
1764 |
636
|
1765 retval(0) = m; |
444
|
1766 |
|
1767 return retval; |
|
1768 } |
|
1769 |
1181
|
1770 DEFUN ("fread", Ffread, Sfread, 3, 2, |
|
1771 "[DATA, COUNT] = fread (FILENUM, SIZE, PRECISION)\n\ |
529
|
1772 \n\ |
1181
|
1773 Reads data in binary form of type PRECISION from a file.\n\ |
529
|
1774 \n\ |
|
1775 FILENUM : file number from fopen\n\ |
1181
|
1776 SIZE : size specification for the Data matrix\n\ |
529
|
1777 PRECISION : type of data to read, valid types are\n\ |
|
1778 \n\ |
1230
|
1779 \"char\" \"schar\" \"short\" \"int\" \"long\" \"float\"\n\ |
|
1780 \"double\" \"uchar\" \"ushort\" \"uint\" \"ulong\"\n\ |
529
|
1781 \n\ |
1181
|
1782 DATA : matrix in which the data is stored\n\ |
|
1783 COUNT : number of elements read") |
529
|
1784 { |
|
1785 Octave_object retval; |
|
1786 |
|
1787 int nargin = args.length (); |
|
1788 |
1181
|
1789 if (nargin < 1 || nargin > 3) |
|
1790 print_usage ("fread"); |
529
|
1791 else |
1181
|
1792 retval = fread_internal (args, nargout); |
529
|
1793 |
|
1794 return retval; |
|
1795 } |
|
1796 |
761
|
1797 // Write binary data to a file. |
|
1798 // |
|
1799 // count = fwrite (fid, data, 'precision') |
|
1800 // |
|
1801 // fid : file id from fopen |
|
1802 // Data : data to be written |
|
1803 // precision : type of output element. Can be: |
|
1804 // |
|
1805 // char, uchar, schar, short, ushort, int, uint, |
|
1806 // long, float, double |
|
1807 // |
|
1808 // Default is uchar. |
|
1809 // |
|
1810 // count : the number of elements written |
|
1811 |
1181
|
1812 static Octave_object |
506
|
1813 fwrite_internal (const Octave_object& args, int nargout) |
444
|
1814 { |
497
|
1815 Octave_object retval; |
444
|
1816 |
506
|
1817 int nargin = args.length (); |
|
1818 |
761
|
1819 Pix p = file_io_get_file (args(0), "a+", "fwrite"); |
444
|
1820 |
529
|
1821 if (! p) |
444
|
1822 return retval; |
|
1823 |
1358
|
1824 // Get type and number of bytes per element to read. |
|
1825 |
1363
|
1826 const char *prec = "uchar"; |
712
|
1827 if (nargin > 2) |
444
|
1828 { |
761
|
1829 prec = args(2).string_value (); |
636
|
1830 |
|
1831 if (error_state) |
444
|
1832 { |
|
1833 error ("fwrite: precision must be a specified as a string"); |
|
1834 return retval; |
|
1835 } |
|
1836 } |
|
1837 |
|
1838 file_info file = file_list (p); |
|
1839 |
761
|
1840 Matrix m = args(1).matrix_value (); |
444
|
1841 |
636
|
1842 if (! error_state) |
|
1843 { |
|
1844 int count = m.write (file.fptr (), prec); |
444
|
1845 |
636
|
1846 retval(0) = (double) count; |
|
1847 } |
444
|
1848 |
|
1849 return retval; |
|
1850 } |
|
1851 |
1181
|
1852 DEFUN ("fwrite", Ffwrite, Sfwrite, 3, 1, |
|
1853 "COUNT = fwrite (FILENUM, DATA, PRECISION)\n\ |
|
1854 \n\ |
|
1855 Writes data to a file in binary form of size PRECISION\n\ |
|
1856 \n\ |
|
1857 FILENUM : file number from fopen\n\ |
|
1858 DATA : matrix of elements to be written\n\ |
|
1859 PRECISION : type of data to read, valid types are\n\ |
|
1860 \n\ |
1230
|
1861 \"char\" \"schar\" \"short\" \"int\" \"long\" \"float\"\n\ |
|
1862 \"double\" \"uchar\" \"ushort\" \"uint\" \"ulong\"\n\ |
1181
|
1863 \n\ |
|
1864 COUNT : number of elements written") |
|
1865 { |
|
1866 Octave_object retval; |
|
1867 |
|
1868 int nargin = args.length (); |
|
1869 |
|
1870 if (nargin < 2 || nargin > 3) |
|
1871 print_usage ("fwrite"); |
|
1872 else |
|
1873 retval = fwrite_internal (args, nargout); |
|
1874 |
|
1875 return retval; |
|
1876 } |
|
1877 |
|
1878 // Check for an EOF condition on a file opened by fopen. |
|
1879 // |
|
1880 // eof = feof (fid) |
|
1881 // |
|
1882 // fid : file id from fopen |
|
1883 // eof : non zero for an end of file condition |
|
1884 |
|
1885 static Octave_object |
|
1886 feof_internal (const Octave_object& args, int nargout) |
|
1887 { |
|
1888 Octave_object retval; |
|
1889 |
|
1890 Pix p = return_valid_file (args(0)); |
|
1891 |
|
1892 if (! p) |
|
1893 return retval; |
|
1894 |
|
1895 file_info file = file_list (p); |
|
1896 |
|
1897 retval(0) = (double) feof (file.fptr ()); |
|
1898 |
|
1899 return retval; |
|
1900 } |
|
1901 |
712
|
1902 DEFUN ("feof", Ffeof, Sfeof, 1, 1, |
529
|
1903 "ERROR = feof (FILENAME or FILENUM)\n\ |
|
1904 \n\ |
|
1905 Returns a non zero value for an end of file condition for the\n\ |
|
1906 file specified by FILENAME or FILENUM from fopen") |
|
1907 { |
|
1908 Octave_object retval; |
|
1909 |
|
1910 int nargin = args.length (); |
|
1911 |
712
|
1912 if (nargin != 1) |
529
|
1913 print_usage ("feof"); |
|
1914 else |
|
1915 retval = feof_internal (args, nargout); |
|
1916 |
|
1917 return retval; |
|
1918 } |
|
1919 |
1181
|
1920 // Check for an error condition on a file opened by fopen. |
761
|
1921 // |
1181
|
1922 // [message, errnum] = ferror (fid) |
761
|
1923 // |
1181
|
1924 // fid : file id from fopen |
|
1925 // message : system error message |
|
1926 // errnum : error number |
761
|
1927 |
1181
|
1928 static Octave_object |
|
1929 ferror_internal (const Octave_object& args, int nargout) |
444
|
1930 { |
497
|
1931 Octave_object retval; |
444
|
1932 |
1049
|
1933 Pix p = return_valid_file (args(0)); |
444
|
1934 |
529
|
1935 if (! p) |
444
|
1936 return retval; |
|
1937 |
|
1938 file_info file = file_list (p); |
|
1939 |
1181
|
1940 int ierr = ferror (file.fptr ()); |
|
1941 |
|
1942 if (nargout > 1) |
|
1943 retval(1) = (double) ierr; |
|
1944 |
|
1945 retval(0) = strsave (strerror (ierr)); |
529
|
1946 |
|
1947 return retval; |
|
1948 } |
|
1949 |
712
|
1950 DEFUN ("ferror", Fferror, Sferror, 1, 1, |
529
|
1951 "ERROR = ferror (FILENAME or FILENUM)\n\ |
|
1952 \n\ |
|
1953 Returns a non zero value for an error condition on the\n\ |
|
1954 file specified by FILENAME or FILENUM from fopen") |
|
1955 { |
|
1956 Octave_object retval; |
|
1957 |
|
1958 int nargin = args.length (); |
|
1959 |
712
|
1960 if (nargin != 1) |
529
|
1961 print_usage ("ferror"); |
|
1962 else |
|
1963 retval = ferror_internal (args, nargout); |
444
|
1964 |
|
1965 return retval; |
|
1966 } |
|
1967 |
1230
|
1968 static Octave_object |
|
1969 popen_internal (const Octave_object& args) |
|
1970 { |
|
1971 Octave_object retval; |
|
1972 |
|
1973 if (! args(0).is_string ()) |
|
1974 { |
|
1975 error ("popen: file name must be a string"); |
|
1976 return retval; |
|
1977 } |
|
1978 |
1363
|
1979 Pix p = return_valid_file (args(0)); |
1230
|
1980 |
|
1981 if (p) |
|
1982 { |
|
1983 file_info file = file_list (p); |
|
1984 |
|
1985 retval(0) = (double) file.number (); |
|
1986 |
|
1987 return retval; |
|
1988 } |
|
1989 |
|
1990 if (! args(1).is_string ()) |
|
1991 { |
|
1992 error ("popen: file mode must be a string"); |
|
1993 return retval; |
|
1994 } |
|
1995 |
1363
|
1996 const char *name = args(0).string_value (); |
|
1997 const char *mode = args(1).string_value (); |
1230
|
1998 |
|
1999 if (mode[1] || (mode[0] != 'w' && mode[0] != 'r')) |
|
2000 { |
|
2001 error ("popen: invalid mode, must be either \"r\" or \"w\"."); |
|
2002 return retval; |
|
2003 } |
|
2004 |
|
2005 struct stat buffer; |
|
2006 if (stat (name, &buffer) == 0 && (buffer.st_mode & S_IFDIR) == S_IFDIR) |
|
2007 { |
|
2008 error ("popen: can't open directory"); |
|
2009 return retval; |
|
2010 } |
|
2011 |
|
2012 FILE *file_ptr = popen (name, mode); |
|
2013 |
|
2014 if (! file_ptr) |
|
2015 { |
|
2016 error ("popen: unable to start process `%s'", name); |
|
2017 return retval; |
|
2018 } |
|
2019 |
|
2020 int number = file_list.length () + 1; |
|
2021 |
|
2022 file_info file (number, name, file_ptr, mode); |
|
2023 file_list.append (file); |
|
2024 |
|
2025 retval(0) = (double) number; |
|
2026 |
|
2027 return retval; |
|
2028 } |
|
2029 |
|
2030 DEFUN ("popen", Fpopen, Spopen, 2, 1, |
|
2031 "FILENUM = popen (FILENAME, MODE)\n\ |
|
2032 \n\ |
|
2033 start a process and create a pipe. Valid values for mode are:\n\ |
|
2034 \n\ |
|
2035 \"r\" : connect stdout of process to pipe\n\ |
|
2036 \"w\" : connect stdin of process to pipe") |
|
2037 { |
|
2038 Octave_object retval; |
|
2039 |
|
2040 int nargin = args.length (); |
|
2041 |
|
2042 if (nargin != 2) |
|
2043 print_usage ("popen"); |
|
2044 else |
|
2045 retval = popen_internal (args); |
|
2046 |
|
2047 return retval; |
|
2048 } |
|
2049 |
|
2050 static Octave_object |
|
2051 pclose_internal (const Octave_object& args) |
|
2052 { |
|
2053 Octave_object retval; |
|
2054 |
|
2055 Pix p = return_valid_file (args(0)); |
|
2056 |
|
2057 if (! p) |
|
2058 return retval; |
|
2059 |
|
2060 file_info file = file_list (p); |
|
2061 |
|
2062 if (file.number () < 3) |
|
2063 { |
|
2064 warning ("pclose: can't close stdin, stdout, or stderr!"); |
|
2065 return retval; |
|
2066 } |
|
2067 |
|
2068 int success = pclose (file.fptr ()); |
|
2069 file_list.del (p); |
|
2070 |
|
2071 if (success == 0) |
|
2072 retval(0) = 1.0; // succeeded |
|
2073 else |
|
2074 { |
|
2075 error ("pclose: error on closing file"); |
|
2076 retval(0) = 0.0; // failed |
|
2077 } |
|
2078 |
|
2079 return retval; |
|
2080 } |
|
2081 |
|
2082 DEFUN ("pclose", Fpclose, Spclose, 1, 1, |
|
2083 "pclose (FILENAME or FILENUM)\n\ |
|
2084 \n\ |
|
2085 Close a pipe and terminate the associated process") |
|
2086 { |
|
2087 Octave_object retval; |
|
2088 |
|
2089 int nargin = args.length (); |
|
2090 |
|
2091 if (nargin != 1) |
|
2092 print_usage ("pclose"); |
|
2093 else |
|
2094 retval = pclose_internal (args); |
|
2095 |
|
2096 return retval; |
|
2097 } |
|
2098 |
|
2099 static Octave_object |
|
2100 execute_internal (const Octave_object& args, int nargout) |
|
2101 { |
|
2102 Octave_object retval (3, tree_constant (-1.0)); |
|
2103 |
|
2104 pid_t pid = 0; |
|
2105 int stdin_pipe[2]; |
|
2106 int stdout_pipe[2]; |
|
2107 FILE *stdin_file; |
|
2108 FILE *stdout_file; |
|
2109 int new_stdin; |
|
2110 int new_stdout; |
|
2111 |
|
2112 if (! args(0).is_string ()) |
|
2113 { |
|
2114 error ("execute: file name must be a string"); |
|
2115 return retval; |
|
2116 } |
|
2117 |
1363
|
2118 const char *name = args(0).string_value (); |
1230
|
2119 |
|
2120 if (pipe (stdin_pipe) || pipe (stdout_pipe)) |
|
2121 { |
|
2122 error ("execute: pipe creation failed"); |
|
2123 return retval; |
|
2124 } |
|
2125 |
|
2126 pid = fork (); |
|
2127 |
|
2128 if (pid < (pid_t) 0) |
|
2129 { |
|
2130 error ("execute: fork failed - can't create child process"); |
|
2131 return retval; |
|
2132 } |
|
2133 |
|
2134 if (pid == (pid_t) 0) |
|
2135 { |
|
2136 close (stdin_pipe[1]); |
|
2137 close (stdout_pipe[0]); |
|
2138 |
|
2139 dup2 (stdin_pipe[0], STDIN_FILENO); |
|
2140 close (stdin_pipe[0]); |
|
2141 |
|
2142 dup2 (stdout_pipe[1], STDOUT_FILENO); |
|
2143 close (stdout_pipe[1]); |
|
2144 |
|
2145 if (execlp (name, name, 0) == -1) |
|
2146 error ("execute: unable to start process `%s'", name); |
|
2147 |
|
2148 exit (0); |
|
2149 return 0.0; |
|
2150 } |
|
2151 else |
|
2152 { |
|
2153 close (stdin_pipe[0]); |
|
2154 close (stdout_pipe[1]); |
|
2155 |
|
2156 stdout_file = fdopen (stdout_pipe[0], "r"); |
|
2157 stdin_file = fdopen (stdin_pipe[1], "w"); |
|
2158 |
|
2159 if (fcntl (fileno (stdout_file), F_SETFL, O_NONBLOCK) < 0) |
|
2160 { |
|
2161 error ("execute: error setting file mode"); |
|
2162 return retval; |
|
2163 } |
|
2164 |
|
2165 new_stdin = file_list.length () + 1; |
|
2166 new_stdout = new_stdin + 1; |
|
2167 |
|
2168 file_info new_stdin_file_ptr (new_stdin, name, stdin_file, "w"); |
|
2169 file_info new_stdout_file_ptr (new_stdout, name, stdout_file, "r"); |
|
2170 |
|
2171 file_list.append (new_stdin_file_ptr); |
|
2172 file_list.append (new_stdout_file_ptr); |
|
2173 } |
|
2174 |
|
2175 retval(2) = (double) pid; |
|
2176 retval(1) = (double) new_stdout; |
|
2177 retval(0) = (double) new_stdin; |
|
2178 |
|
2179 return retval; |
|
2180 } |
|
2181 |
|
2182 DEFUN ("execute", Fexecute, Sexecute, 1, 3, |
|
2183 "[STDIN, STDOUT, PID] = execute (COMMAND)\n\ |
|
2184 \n\ |
|
2185 Start a program and redirect its stdin to STDIN and its stdout to STDOUT") |
|
2186 { |
|
2187 Octave_object retval; |
|
2188 |
|
2189 int nargin = args.length (); |
|
2190 |
|
2191 if (nargin != 1) |
|
2192 print_usage ("execute"); |
|
2193 else |
|
2194 retval = execute_internal (args, nargout); |
|
2195 |
|
2196 return retval; |
|
2197 } |
|
2198 |
|
2199 static Octave_object |
|
2200 sync_system_internal (const Octave_object& args) |
|
2201 { |
|
2202 Octave_object retval (1, tree_constant (-1.0)); |
|
2203 |
|
2204 if (! args(0).is_string ()) |
|
2205 { |
|
2206 error ("sync_system: file name must be a string"); |
|
2207 return retval; |
|
2208 } |
|
2209 |
1363
|
2210 const char *name = args(0).string_value (); |
1230
|
2211 |
|
2212 retval (0) = (double) system (name); |
|
2213 return retval; |
|
2214 } |
|
2215 |
|
2216 DEFUN ("sync_system", Fsync_system, Ssync_system, 1, 1, |
|
2217 "RETCODE = sync_system (FILENAME)\n\ |
|
2218 \n\ |
|
2219 Start a program and wait until it terminates") |
|
2220 { |
|
2221 Octave_object retval; |
|
2222 |
|
2223 int nargin = args.length (); |
|
2224 |
|
2225 if (nargin != 1) |
|
2226 print_usage ("sync_system"); |
|
2227 else |
|
2228 retval = sync_system_internal (args); |
|
2229 |
|
2230 return retval; |
|
2231 } |
|
2232 |
|
2233 static Octave_object |
|
2234 async_system_internal (const Octave_object& args) |
|
2235 { |
|
2236 Octave_object retval (1, tree_constant (-1.0)); |
|
2237 pid_t pid; |
|
2238 |
|
2239 if (! args(0).is_string ()) |
|
2240 { |
|
2241 error ("async_system: file name must be a string"); |
|
2242 return retval; |
|
2243 } |
|
2244 |
1363
|
2245 const char *name = args(0).string_value (); |
1230
|
2246 |
|
2247 pid = fork (); |
|
2248 |
|
2249 if (pid < 0) |
|
2250 { |
|
2251 error ("async_system: fork failed -- can't create child process"); |
|
2252 return retval; |
|
2253 } |
|
2254 |
|
2255 if (pid == 0) |
|
2256 { |
|
2257 system (name); |
|
2258 exit (0); |
|
2259 retval (0) = 0.0; |
|
2260 return retval; |
|
2261 } |
|
2262 else |
|
2263 { |
|
2264 retval (0) = (double) pid; |
|
2265 return retval; |
|
2266 } |
|
2267 } |
|
2268 |
|
2269 DEFUN ("async_system", Fasync_system, Sasync_system, 1, 1, |
|
2270 "PID = async_system (FILENAME)\n\ |
|
2271 \n\ |
|
2272 Create a new process and start FILENAME") |
|
2273 { |
|
2274 Octave_object retval; |
|
2275 |
|
2276 int nargin = args.length (); |
|
2277 |
|
2278 if (nargin != 1) |
|
2279 print_usage ("async_system"); |
|
2280 else |
|
2281 retval = async_system_internal (args); |
|
2282 |
|
2283 return retval; |
|
2284 } |
|
2285 |
|
2286 static Octave_object |
|
2287 waitpid_internal (const Octave_object& args) |
|
2288 { |
|
2289 Octave_object retval (1, tree_constant (-1.0)); |
|
2290 |
|
2291 double pid_num = args(0).double_value (); |
|
2292 |
|
2293 if (! error_state) |
|
2294 { |
|
2295 if (D_NINT (pid_num) != pid_num) |
|
2296 error ("waitpid: PID must be an integer value"); |
|
2297 else |
|
2298 { |
|
2299 pid_t pid = (pid_t) pid_num; |
|
2300 |
|
2301 int options = 0; |
|
2302 |
|
2303 if (args.length () == 2) |
|
2304 { |
|
2305 double options_num = args(1).double_value (); |
|
2306 |
|
2307 if (! error_state) |
|
2308 { |
|
2309 if (D_NINT (options_num) != options_num) |
|
2310 error ("waitpid: PID must be an integer value"); |
|
2311 else |
|
2312 { |
|
2313 options = NINT (options_num); |
|
2314 if (options < 0 || options > 3) |
|
2315 error ("waitpid: invalid OPTIONS value specified"); |
|
2316 } |
|
2317 } |
|
2318 } |
|
2319 |
|
2320 if (! error_state) |
|
2321 retval (0) = (double) waitpid (pid, 0, options); |
|
2322 } |
|
2323 } |
|
2324 |
|
2325 return retval; |
|
2326 } |
|
2327 |
|
2328 DEFUN ("waitpid", Fwaitpid, Swaitpid, 1, 1, |
|
2329 "STATUS = waitpid (PID, OPTIONS)\n\ |
|
2330 \n\ |
|
2331 wait for process PID to terminate\n\ |
|
2332 \n\ |
|
2333 PID can be:\n\ |
|
2334 \n\ |
|
2335 -1 : wait for any child process\n\ |
|
2336 0 : wait for any child process whose process group ID is equal to\n\ |
|
2337 that of the Octave interpreter process.\n\ |
|
2338 > 0 : wait for termination of the child process with ID PID.\n\ |
|
2339 \n\ |
|
2340 OPTIONS is:\n\ |
|
2341 \n\ |
|
2342 0 : wait until signal is received or a child process exits (this\n\ |
|
2343 is the default if the OPTIONS argument is missing) \n\ |
|
2344 1 : do not hang if status is not immediately available\n\ |
|
2345 2 : report the status of any child processes that are\n\ |
|
2346 stopped, and whose status has not yet been reported\n\ |
|
2347 since they stopped\n\ |
|
2348 3 : implies both 1 and 2\n\ |
|
2349 \n\ |
|
2350 STATUS is:\n\ |
|
2351 \n\ |
|
2352 -1 : if an error occured\n\ |
|
2353 > 0 : the process ID of the child process that exited") |
|
2354 { |
|
2355 Octave_object retval; |
|
2356 |
|
2357 int nargin = args.length (); |
|
2358 |
|
2359 if (nargin == 1 || nargin == 2) |
|
2360 retval = waitpid_internal (args); |
|
2361 else |
|
2362 print_usage ("waitpid"); |
|
2363 |
|
2364 return retval; |
|
2365 } |
|
2366 |
|
2367 static Octave_object |
|
2368 mkfifo_internal (const Octave_object& args) |
|
2369 { |
|
2370 Octave_object retval (1, tree_constant (-1.0)); |
|
2371 |
|
2372 if (! args(0).is_string ()) |
|
2373 { |
|
2374 error ("mkfifo: file name must be a string"); |
|
2375 return retval; |
|
2376 } |
|
2377 |
1363
|
2378 const char *name = args(0).string_value (); |
1230
|
2379 |
|
2380 if (! args(1).is_scalar_type ()) |
|
2381 { |
|
2382 error ("mkfifo: MODE must be an integer"); |
|
2383 return retval; |
|
2384 } |
|
2385 |
|
2386 long mode = (long) args(1).double_value (); |
|
2387 |
|
2388 retval (0) = (double) mkfifo (name, mode); |
|
2389 |
|
2390 return retval; |
|
2391 } |
|
2392 |
|
2393 DEFUN ("mkfifo", Fmkfifo, Smkfifo, 2, 1, |
|
2394 "STATUS = mkfifo (NAME, MODE)\n\ |
|
2395 \n\ |
|
2396 Create a FIFO special file named NAME with file mode MODE\n\ |
|
2397 \n\ |
|
2398 STATUS is:\n\ |
|
2399 \n\ |
|
2400 != 0 : if mkfifo failed\n\ |
|
2401 0 : if the FIFO special file could be created") |
|
2402 { |
|
2403 Octave_object retval; |
|
2404 |
|
2405 int nargin = args.length (); |
|
2406 |
|
2407 if (nargin != 2) |
|
2408 print_usage ("mkfifo"); |
|
2409 else |
|
2410 retval = mkfifo_internal (args); |
|
2411 |
|
2412 return retval; |
|
2413 } |
|
2414 |
|
2415 static Octave_object |
|
2416 unlink_internal (const Octave_object& args) |
|
2417 { |
|
2418 Octave_object retval; |
|
2419 |
|
2420 if (! args(0).is_string ()) |
|
2421 { |
|
2422 error ("unlink: file name must be a string"); |
|
2423 retval (0) = -1.0; |
|
2424 return retval; |
|
2425 } |
|
2426 |
1363
|
2427 const char *name = args(0).string_value (); |
1230
|
2428 |
|
2429 retval (0) = (double) unlink (name); |
|
2430 |
|
2431 return retval; |
|
2432 } |
|
2433 |
|
2434 DEFUN ("unlink", Funlink, Sunlink, 1, 1, |
|
2435 "STATUS = unlink (NAME)\n\ |
|
2436 \n\ |
|
2437 Delete the file NAME\n\ |
|
2438 \n\ |
|
2439 STATUS is:\n\ |
|
2440 \n\ |
|
2441 != 0 : if unlink failed\n\ |
|
2442 0 : if the file could be successfully deleted") |
|
2443 { |
|
2444 Octave_object retval; |
|
2445 |
|
2446 int nargin = args.length (); |
|
2447 |
|
2448 if (nargin != 1) |
|
2449 print_usage ("unlink"); |
|
2450 else |
|
2451 retval = unlink_internal (args); |
|
2452 |
|
2453 return retval; |
|
2454 } |
|
2455 |
1377
|
2456 static Octave_map |
|
2457 mk_stat_map (struct stat& st) |
|
2458 { |
|
2459 Octave_map m; |
|
2460 |
1379
|
2461 char mode_as_string[11]; |
1381
|
2462 mode_string (st.st_mode, mode_as_string); |
1379
|
2463 mode_as_string[10] = '\0'; |
|
2464 |
1377
|
2465 m["dev"] = (double) st.st_dev; |
|
2466 m["ino"] = (double) st.st_ino; |
1379
|
2467 m["modestr"] = mode_as_string; |
1377
|
2468 m["nlink"] = (double) st.st_nlink; |
|
2469 m["uid"] = (double) st.st_uid; |
|
2470 m["gid"] = (double) st.st_gid; |
|
2471 #if defined (HAVE_ST_RDEV) |
|
2472 m["rdev"] = (double) st.st_rdev; |
|
2473 #endif |
|
2474 m["size"] = (double) st.st_size; |
|
2475 m["atime"] = (double) st.st_atime; |
|
2476 m["mtime"] = (double) st.st_mtime; |
|
2477 m["ctime"] = (double) st.st_ctime; |
|
2478 #if defined (HAVE_ST_BLKSIZE) |
|
2479 m["blksize"] = (double) st.st_blksize; |
|
2480 #endif |
|
2481 #if defined (HAVE_ST_BLOCKS) |
|
2482 m["blocks"] = (double) st.st_blocks; |
|
2483 #endif |
|
2484 |
|
2485 return m; |
|
2486 } |
|
2487 |
|
2488 DEFUN ("stat", Fstat, Sstat, 1, 1, |
|
2489 "stat (NAME)\n\ |
|
2490 \n\ |
|
2491 Given the name of a file, return a structure with the following |
|
2492 elements:\n\ |
|
2493 \n\ |
|
2494 dev : id of device containing a directory entry for this file\n\ |
|
2495 ino : file number of the file\n\ |
1381
|
2496 modestr : file mode, as a string of ten letters or dashes as in ls -l\n\ |
1377
|
2497 nlink : number of links\n\ |
|
2498 uid : user id of file's owner\n\ |
|
2499 gid : group id of file's group \n\ |
|
2500 rdev : id of device for block or character special files\n\ |
|
2501 size : size in bytes\n\ |
|
2502 atime : time of last access\n\ |
|
2503 mtime : time of last modification\n\ |
|
2504 ctime : time of last file status change\n\ |
|
2505 blksize : size of blocks in the file\n\ |
|
2506 blocks : number of blocks allocated for file\n\ |
|
2507 \n\ |
|
2508 If the file does not exist, -1 is returned.") |
|
2509 { |
|
2510 Octave_object retval; |
|
2511 |
|
2512 if (args.length () == 1) |
|
2513 { |
|
2514 const char *name = args(0).string_value (); |
|
2515 |
|
2516 static char *fname = 0; |
|
2517 |
|
2518 if (fname) |
|
2519 free (fname); |
|
2520 |
|
2521 fname = tilde_expand (name); |
|
2522 |
|
2523 if (! error_state) |
|
2524 { |
|
2525 struct stat buf; |
|
2526 |
|
2527 if (stat (fname, &buf) < 0) |
|
2528 retval = -1.0; |
|
2529 else |
|
2530 retval = tree_constant (mk_stat_map (buf)); |
|
2531 } |
|
2532 } |
|
2533 else |
|
2534 print_usage ("stat"); |
|
2535 |
|
2536 return retval; |
|
2537 } |
|
2538 |
1379
|
2539 DEFUN ("lstat", Flstat, Slstat, 1, 1, |
|
2540 "lstat (NAME)\n\ |
|
2541 \n\ |
|
2542 Like stat (NAME), but if NAME refers to a symbolic link, returns\n\ |
|
2543 information about the link itself, not the file that it points to.") |
|
2544 { |
|
2545 Octave_object retval; |
|
2546 |
|
2547 if (args.length () == 1) |
|
2548 { |
|
2549 const char *name = args(0).string_value (); |
|
2550 |
|
2551 static char *fname = 0; |
|
2552 |
|
2553 if (fname) |
|
2554 free (fname); |
|
2555 |
|
2556 fname = tilde_expand (name); |
|
2557 |
|
2558 if (! error_state) |
|
2559 { |
|
2560 struct stat buf; |
|
2561 |
|
2562 if (lstat (fname, &buf) < 0) |
|
2563 retval = -1.0; |
|
2564 else |
|
2565 retval = tree_constant (mk_stat_map (buf)); |
|
2566 } |
|
2567 } |
|
2568 else |
|
2569 print_usage ("stat"); |
|
2570 |
|
2571 return retval; |
|
2572 } |
|
2573 |
444
|
2574 /* |
1
|
2575 ;;; Local Variables: *** |
|
2576 ;;; mode: C++ *** |
|
2577 ;;; page-delimiter: "^/\\*" *** |
|
2578 ;;; End: *** |
|
2579 */ |