This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sammich Lord's profile picture

I have some questions

Started by Sammich Lord, 15 August 2012 - 05:12 AM
Sammich Lord #1
Posted 15 August 2012 - 07:12 AM
Hey I wanted to get some answer for some questions.
  1. How would I make it so if I make a OS certain user accounts don't have access to edit and run certain programs?(I know some people have said to overwrite the fs API but I do not know how to overwrite it without modifying the main program.(
  2. How would I make it so the program constantly listens for a key press no matter what program it is running? A ex is: Let's say I run the startup script and the startup script constantly listens for a key press no matter a program that I run. So basically a background program.
Those are basically my questions I would greatly appreciate any answers.
makerimages #2
Posted 15 August 2012 - 07:20 AM
1: simple: have 2 versions of your OS and an identifier that checks from idunno- a floppy? for the user rank and opens the specific version
ardera #3
Posted 15 August 2012 - 07:26 AM
overwrite:

local aaa=fs.open --doesn't call the function, saves it in aaa
fs.open=function(path, mode)
  allowed="startup"
  if path==allowed then
    return aaa(path, mode)
  else
    error("Attempt to edit a unallowed Path", 2)
  end
end
something like this…
I use this too for my system files
jay5476 #4
Posted 17 August 2012 - 10:42 AM
of the top of my head i would say that this is a hard one hmmm? but the only way i know of is to make a long program like

program = read()
if program == "copy" or program == "delete" or program == "edit" then --etc.
print("Acsess Denied")
else
shell.run( program )
-- but you would have to add options for args in the programs like cd <dir>
so thats a basic ide of what u have to do
Grim Reaper #5
Posted 17 August 2012 - 03:25 PM
Well, as far as user privileges, you could have a separate hidden file for each user account that contains their privilege level. To obtain the different privilege levels you could read them into a table along with other user specific data, like username and password.

As for always listening for key presses, you could use the parallel API combined with loadstring to get a function version of the program you wish to run.
Here's an example:

tStack = {}; -- The program, or rather, function stack.
function ListenForKeys() -- Listens for a key press and then handles the press.
local event, key = os.pullEvent( "key" );

if key == 28 then -- If enter was pressed.
  os.shutdown(); -- Shutdown the machine.
end
end
function LoadProgram() -- Returns a function or false.
local File = fs.open( "TestProgram", "w" ); -- Open the file containing the program we want to run.
local FileContents = File.readAll(); -- Get the contents of the file.
File.close(); -- Close the file.

local Program = loadstring( FileContents ); -- Attempt to load the program string into a function.
if Program then -- If there were no interpretation errors; Program loaded correctly.
  return Program; -- Return the function.
else -- If there was some kind of error.
  return "FailedToLoad"; -- Return false.
end
end
function ExecuteStack( ... ) -- Executes all functions with the parallel API that are passed as arguments.
parallel.waitForAny( unpack( arg ) ); -- Execute the functions passed.
end
tStack[#tStack+1] = ListenForKeys; -- Add the key listening function to the stack.
tStack[#tStack+1] = LoadProgram(); -- Add the loaded function to the stack.
if tStack[#tStack] ~= "FailedToLoad" then -- If the loaded function is not equal to nil.
-- Run the main program loop.
while true do
  ExecuteStack( unpack( tStack ) ); -- Execute all functions with the parallel API that are within the stack.
end
else
print( "Interpretation error in stack." );
end

I haven't tested the code above, but I think that it is what you are looking for.
Hope I helped! :(/>/>