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

Help with Directory's

Started by jay5476, 16 August 2012 - 08:46 AM
jay5476 #1
Posted 16 August 2012 - 10:46 AM
Note this is posted on a phone so may not be as good

The real problem is that I want to make a strong login program with the function to add new users by use f:write() and all that crap
So here is what I want to know how to do

Open a directory without exiting the program

Read the given username that contains the password

That's basically it I can sorta do the reading part
Noodle #2
Posted 16 August 2012 - 11:50 PM
FS Api
write("Username: ")
username = read()
hFile = fs.open("username", "w")
hFile.write(username)
hFile.close()
Grim Reaper #3
Posted 17 August 2012 - 03:44 PM
I would suggest saving the password in a file that is titled as the username.

Like my user file would be called Reaper, and within it the password would be 1234.
From there you could read the password out of the file, like Noodle has shown above into a table or just a couple of variables.

The following code is assuming that the user file is in the directory 'Users'.
Here's my code:

Username = ""; -- Set up a variable to hold the username that is entered by the user.
Password = ""; -- Set up a variable to hold the password that is read from the user file.
PasswordAttempt = ""; -- Set up a variable to hold the password entered by the use who is trying to login.
write( "Username: " ); -- Prompt the user for their username.
Username = read(); -- Get input from the user as to what their username is.
if CheckUserName( Username ) then -- Check if the username entered exists as a file in the 'Users' directory.
print( "" ); -- Position the cursor on the next line by printing an empty line.
write( "Password: " ); -- Prompt the user to enter their password.
PasswordAttempt = read(); -- Get input from the user as to what their password is.

LoadPassword( Username ); -- Load the password of the username entered into the Password variable.
if PasswordAttempt == Password then -- If the password entered matches the password read from the user's file.
  shell.exit(); -- Exit the program.
else -- If the password entered does not match the password read from the user's file.
  os.shutdown(); -- Shutdown the machine.
end
else -- If the username entered does not exist in the 'Users' directory.
os.shutdown(); -- Shutdown the machine.
end
function CheckUserName( Name ) -- Checks to see if the given name exists as a file within the 'Users' directory.
local tFiles = fs.list( "Users" ); -- Gets all of the file and directory names in the 'Users' directory into a table.

for i=1, #tFiles do -- Go through all of the indexes in the tFiles table.
  if tFiles[i] == Name then -- If the username matched an index in the tFiles table.
   return true; -- Return true; the username exists.
  end
end

-- If the loop completed without returning then the name does not exist as a file within the 'Users' directory.
return false; -- Return false; the user name does not exist.
end
function LoadPassword( Name )
local File = fs.open( Users .. "/" .. Name, "r" ); -- Open the file named the username in read mode.
Password = file.readLine(); -- Get the first line in the file and read it into the Password variable.
File.close(); -- Close the file.
end

It may seem a bit complex at first due to the massive amount of comments, but if you have any questions feel free to PM me.
Hope I helped! :(/>/>
jay5476 #4
Posted 18 August 2012 - 03:50 AM
ive already done it i will be releasing the OS(Lava OS) in a few days i just need to make some adjustments
i used for registering

f = io.open("LavaOS/"..wUsername, "w")
f:write( pass )
f:close()
and for login

if fs.exists("LavaOS/"..user) then
local hRead = assert(fs.open("LavaOS/"..user, "r"))
	    uPass = hRead.readLine() -- Read a line of data from the handle
	    hRead.close()
if uPass == password then