Difference between revisions of "Programming in MDOS"

From Ninerpedia
Jump to navigation Jump to search
(Created page with "== Device access in TIC == === Reading and writing disk sectors === The following code demonstrates how level-2 file access can be used in a TIC program. It copies a file, inde...")
 
m
 
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Device access in TIC ==
* [[MDOS command line|Getting the arguments from the command line]]
* [[Device access in TIC]]
* [[MDOS XOP Definitions]]
* [[Geneve keyboard control]]
* [[Benchmarking]]
* [[Setting the Geneve clock|Setting the real-time clock]]


=== Reading and writing disk sectors ===
[[Category:MDOS]]
 
[[Category:Programming| ]]
The following code demonstrates how level-2 file access can be used in a TIC program. It copies a file, independent of the file format, by reading a sector and writing to a target file. Each sector is allocated when written.
 
This sample code is certainly not the best way to copy files (wasting a lot of time by repeatedly and writing single sectors); it is only intended to illustrate the level-2 access.
 
#include <stdio_h>
#include <stdlib_h>
main(argc, argv)
int argc; char *argv[];
{
    int  res, sect, i;
    char rbuf[256];
    int  *ibuf;
    if (argc<2)
    {
        printf("Syntax: filecopy <from_file> <to_file>\n");
        return;
    }
    /* First we get the metadata */
    res = bread(argv[1], 0, 0, rbuf);
    check(res);
    /* write the old metadata as new metadata */
    res = bwrite(argv[2], 0, 0, rbuf);
    check(res);
    /* copy the file sector by sector */
    ibuf = rbuf;
    sect = ibuf[2];
    for (i=0; i < sect; i++)
    {
        printf("Copying sector %d of %d\r", i+1, sect);
        res = bread(argv[1], i, 1, rbuf);
        check(res);
        res = bwrite(argv[2], i, 1, rbuf);
        check(res);
    }
}
void check(code)
int code;
{
    if (code != 0)
    {
        printf("Error, code = %d\n", code);
        exit(7);
    }
}

Latest revision as of 13:38, 28 May 2016