Device access in TIC

From Ninerpedia
Revision as of 23:25, 5 November 2011 by Mizapf (talk | contribs) (Created page with "== 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, independent of the file format, by...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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, 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);
   }
}