with System.Os_Lib;
use System.Os_Lib;
with Text_Io;
use Text_Io;
with Ada.Strings.Fixed;
use Ada.Strings.Fixed;
with Np, Np.User;
use Np, Np.User;
procedure Main is
function Get_Password return String_Access;
-- Saisir le mot de passe.
function Get_Password return String_Access is
Passwd : String_Access := new String ' ("" );
Buffer : String_access := new String ' ("" );
Char : Character;
begin
loop
Get_Immediate(Char);
case Char is
when Character'Val(127) =>
if Passwd'Length > 1 then
Free(Buffer);
Buffer := new String ' (Passwd(1..Passwd'Length-1));
else
Free(Buffer);
Buffer := new String ' ("" );
end if;
when Character'Val(10) | Character'Val(13) =>
exit;
when others =>
Free(Buffer);
Buffer := new String ' (Passwd.all & Char);
end case;
Free(Passwd);
Passwd := new String ' (Buffer.all);
end loop;
return new String ' (Digest(Passwd.all));
end Get_Password;
subtype T_Password is String(1..32);
File : File_Type;
Env : Env_Type;
Expr : String(1..256);
Last : Natural;
New_Value : array (Env_Type'Range) of boolean := (others => False);
Rewrite : Boolean := False;
begin
if Argument_Count /= 0 then
-- lire les arguments
for Argv in 1..Argument_Count-1 loop
if Argument(Argv) = "-h" and then
Argument(Argv+1)'Length /= 0 then
Current_User.Personality(Home).Value :=
new String ' (Argument(Argv+1));
New_Value(Home) := True;
exit;
end if;
end loop;
for Argv in 1..Argument_Count-1 loop
if Argument(Argv) = "-l" and then
Argument(Argv+1)'Length /= 0 then
Current_User.Personality(logname).Value :=
new String ' (Argument(Argv+1));
New_Value(Logname) := True;
exit;
end if;
end loop;
for Argv in 1..Argument_Count-1 loop
if Argument(Argv) = "-m" and then
Argument(Argv+1)'Length /= 0 then
Current_User.Personality(mail).Value :=
new String ' (Argument(Argv+1));
New_Value(Mail) := True;
exit;
end if;
end loop;
for Argv in 1..Argument_Count-1 loop
if Argument(Argv) = "-g" and then
Argument(Argv+1)'Length /= 0 then
Current_User.Personality(group).Value :=
new String ' (Argument(Argv+1));
New_Value(Group) := True;
exit;
end if;
end loop;
end if;
for Env in Env_Type'Range loop
if New_Value(Env) then
Rewrite := True;
-- réécrire le fichier de configuration;
exit;
end if;
end loop;
if Is_Regular_File(Getenv("HOME" ).all & "/.conf" ) then
-- Lire le fichier de configuration si il existe.
Open(File, In_File, Getenv("HOME" ).all & "/.conf" );
while not End_Of_File(File) loop
Get_Line(File, Expr, Last);
if Last /= 0 then
Env := Env_Type'Value(Expr(1..Index(Expr(1..Last), "=" )- 1));
case Env is
when Home | Logname | Mail | Group =>
if New_Value(Env) then
Put_Line("Setting " & Env_Type'Image(Env) & " to new value." );
else
Current_User.Personality(Env).Value :=
new String ' (Expr(Index(Expr(1..Last), "=" )+1..last));
end if;
when Password =>
Current_User.Personality(Env).Value :=
new String ' (Expr(Index(Expr(1..Last), "=" )+1..last));
Put("password:" );
if Current_User.Personality(env).Value.all = Get_Password.all then
New_Line;
else
Put_Line("Bad password." );
return;
end if;
end case;
end if;
end loop;
Close(File);
else
Rewrite := True;
end if;
if Rewrite then
Create(File, Out_File, Getenv("HOME" ).all & "/.conf" );
-- Ecraser le fichier de configuration
for I in User.env_Type'Range loop
if Current_User.Personality(I).Value'Length = 0 then
case I is
when Home | Logname | Mail =>
raise Program_Error;
when Password =>
if Current_User.Personality(I).Value'Length = 0 then
Put("password:" );
Current_User.Personality(I).Value := Get_Password;
New_Line;
end if;
Put("password:" );
if Current_User.Personality(I).Value.all = Get_Password.all then
Put_line(File, Env_Type'Image(I) & "=" & Current_User.Personality(I).Value.all);
else
Put_Line("Bad password." );
Delete(File);
return;
end if;
New_Line;
when Group =>
null;
end case;
else
Put_line(File, Env_Type'Image(I) & "=" & Current_User.Personality(I).Value.all);
end if;
end loop;
Close(File);
end if;
Put_Line("Hello " & Current_User.Personality(Logname).Value.all & " !" );
-- nous disons ""Bonjour"" à l'utilisateur.
end Main;