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