home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / pub / users / Harry / Blitz / OSProject / p8 / Program2.c < prev    next >
Text File  |  2007-09-19  |  2KB  |  76 lines

  1. code Program2
  2.  
  3. -----------------------------  main  ---------------------------------
  4. --
  5. -- This program prints a message and then invokes the Exec syscall on itself.
  6. -- To prevent infinite recursion, we maintain a counter and we store that
  7. -- counter in the first 4 bytes of a file, which is assumed to be open upon entry.
  8. --
  9. -- The point of this test is to see whether the system stack is getting
  10. -- used up on every call to "Exec".
  11. --
  12. -- It also makes sure that files that are open before Exec are open afterwards.
  13. --
  14.  
  15. const fd = 0
  16.       NUMBER_OF_ITERATIONS = 20
  17.  
  18.   function main ()
  19.       var i, counter: int
  20.  
  21.       print ("Hello, world      ")
  22.  
  23.       -- Get the current counter value...
  24.       i = Sys_Seek (fd, 0)
  25.       if i != 0
  26.         print ("\n*****  ERROR: Unexpected return value from Sys_Seek (")
  27.         printInt (i)
  28.         print (")  *****\n")
  29.         Sys_Exit (-1)
  30.       endIf
  31.       i = Sys_Read (fd, (&counter) asPtrTo char, 4)
  32.       if i != 4
  33.         print ("\n*****  ERROR: Unexpected return value from Sys_Read  *****\n")
  34.         Sys_Exit (-1)
  35.       endIf
  36.  
  37.       -- Print the current value...
  38.       printInt (counter)
  39.       nl ()
  40.  
  41.       -- If the counter looks good...
  42.       if counter > 0 && counter < NUMBER_OF_ITERATIONS
  43.  
  44.         -- Increment it and update the file...
  45.         counter = counter + 1
  46.         i = Sys_Seek (fd, 0)
  47.         if i != 0
  48.           print ("\n*****  ERROR: Unexpected return value from Sys_Seek  *****\n")
  49.           Sys_Exit (-1)
  50.         endIf
  51.         i = Sys_Write (fd, (&counter) asPtrTo char, 4)
  52.         if i != 4
  53.           print ("\n*****  ERROR: Unexpected return value from Sys_Write  *****\n")
  54.           Sys_Exit (-1)
  55.         endIf
  56.  
  57.         -- Invoke "Exec" syscall on this very program...
  58.         i = Sys_Exec ("Program2")
  59.         print ("\n*****  ERROR: Unexpected return value from Sys_Exec  *****\n")
  60.         Sys_Exit (-1)
  61.  
  62.       -- If the counter is at the end...
  63.       elseIf counter == NUMBER_OF_ITERATIONS
  64.         print ("\n**********  Test Complete  **********\n\n")
  65.         Sys_Exit (0)
  66.  
  67.       -- If the counter has any other value...
  68.       else
  69.         print ("\n*****  ERROR: Unexpected value of counter  *****\n")
  70.         Sys_Exit (-1)
  71.       endIf
  72.  
  73.     endFunction
  74.  
  75. endCode
  76.