; ; @ANO_SCRIPT_NAME file_operations ; @ANO_SCRIPT_VERSION 0.0.1 ; @ANO_SCRIPT_DESCRIPTION Simple file operations example ; ; @ANO_FLAGS_USE_PROTOS [ ] ; @ANO_FLAGS_VAR_NAME_SUBS [ ] ; @ANO_FLAGS_VAR_WARN_UNUSED [ ] ; ; Copyright (c) 2016-2026, Jani Salonen ; All rights reserved. ; ; This example shows how to use some file operations with mov command, like ; opening, reading, writing, seeking and so on with the file. Example creates ; one file to tmpdir, writes to it, reads from it, and finally deletes the ; file. ; ; Define fallback tmpdir. ; define fallback_tmpdir ("/tmp") main [exit: 0] { print "File test return values are:" . "\n\n" print " file does not exists / error .. 0," . "\n" print " unknown file .................. 1," . "\n" print " file is pipe or FIFO .......... 2," . "\n" print " character special file ........ 3," . "\n" print " directory ..................... 4," . "\n" print " block special file ............ 5," . "\n" print " regular file .................. 6," . "\n" print " symbolic link ................. 7," . "\n" print " socket ........................ 8, and" . "\n" print " whiteout ...................... 9." . "\n\n" ; Test tmpdir directory existence... ; _tmpdir = env_get("TMPDIR") cmp _tmpdir (NULL) jne "tmpdir_is_ok" mov _tmpdir (fallback_tmpdir) tmpdir_is_ok: mov _retval ( open for writing, truncate existing file or create new, ; <> open for reading/writing, truncate existing file or create new, ; >> open for appending write, create if file does not exists, and ; <>> open for appending read/write, create if file does not exists. ; ;mov _filehandle ( <> "" . _tmpdir . "/testing_example.txt") mov _filename ("" . _tmpdir . "/testing_example.txt"); mov _filehandle ( <> _filename) ; Write something to file... ; mov _data ("Hello, world!") mov _filehandle ( >- _data) print "Write '" . _data . "' to '" . _filename . "'.\n" ; ...and close it. ; mov _retval ( <~ _filehandle) print "Close file '" . _filename . "', return value is: " . \ _retval . ".\n\n" ; Open file again for reading. ; ;mov _filename ("" . _tmpdir . "/testing_example.txt"); ;mov _filehandle ( < _filename) mov _filehandle ( < "" . _tmpdir . "/testing_example.txt") ; Get file name from handle... ; mov _name ( <$ _filehandle) print "File name behind the handle is '" . _name . "'.\n" ; ...and its size. ; mov _size ( <% _filehandle) print "File size in disk is " . _size . " bytes.\n" mov _size ( <# _filehandle) print "File content size in memory is " . _size . " bytes.\n\n" ; Set chunk size for read... ; mov _chunk_size (4) mov _filehandle (>: _chunk_size) mov _chunk_size ( <: _filehandle) print "Set read chunk size, it is now " . _chunk_size . " bytes.\n" ; ...and read three chunks. ; mov _data ( <- _filehandle) mov _addr ( <* _filehandle) print "Read one chunk at address " . _addr . ": '" . _data . "'\n" mov _data ( <- _filehandle) print "Read another chunk: '" . _data . "'\n" mov _data ( <- _filehandle) print "Read one more: '" . _data . "'\n" mov _pos ( _filehandle) print "Delete and close '" . _filename . "' for good, return value" \ " is: " . _retval . ".\n\n" ; Finally test file existence. ; mov _retval (