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