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