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