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