mirror of
https://github.com/doublecmd/doublecmd.git
synced 2026-06-21 09:58:13 +00:00
152 lines
3.2 KiB
ObjectPascal
152 lines
3.2 KiB
ObjectPascal
{
|
|
File name: uUsersGroups.pas
|
|
Date: 2003/07/03
|
|
Author: Martin Matusu <xmat@volny.cz>
|
|
|
|
Copyright (C) 2003
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU General Public License as
|
|
published by the Free Software Foundation; either version 2 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
in a file called COPYING along with this program; if not, write to
|
|
the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
|
|
02139, USA.
|
|
}
|
|
|
|
{mate}
|
|
unit uUsersGroups;
|
|
|
|
{$mode objfpc}{$H+}
|
|
interface
|
|
uses
|
|
Libc, Classes;
|
|
|
|
const
|
|
groupInfo='/etc/group';
|
|
userInfo='/etc/passwd';
|
|
|
|
function uidToStr(uid: Cardinal): String;
|
|
function gidToStr(gid: Cardinal): String;
|
|
|
|
function strToUID(uname: AnsiString): Cardinal;
|
|
function strToGID(gname: AnsiString): Cardinal;
|
|
|
|
procedure getUsrGroups(uid:Cardinal; List: TStrings);
|
|
procedure getUsers(List: TStrings);
|
|
procedure getGroups(List: TStrings);
|
|
|
|
implementation
|
|
uses
|
|
SysUtils;
|
|
|
|
function uidToStr(uid: Cardinal): String;
|
|
var
|
|
uinfo: ppasswd;
|
|
begin
|
|
uinfo:=libc.getpwuid(uid);
|
|
if(uinfo=nil) then
|
|
result:=''
|
|
else
|
|
result:=String(uinfo^.pw_name);
|
|
|
|
end;
|
|
|
|
function gidToStr(gid: Cardinal): String;
|
|
var
|
|
ginfo: Pgroup;
|
|
begin
|
|
ginfo:=libc.getgrgid(gid);
|
|
if(ginfo=nil) then
|
|
result:=''
|
|
else
|
|
result:=String(ginfo^.gr_name);
|
|
end;
|
|
|
|
procedure getUsrGroups(uid:Cardinal; List: TStrings);
|
|
var
|
|
groups: TStrings;
|
|
iC,iD: integer;
|
|
sT: string;
|
|
begin
|
|
//parse groups records
|
|
groups:=TStringlist.Create;
|
|
try
|
|
List.Clear;
|
|
// try
|
|
groups.LoadFromFile(groupInfo);
|
|
{ except
|
|
exit;
|
|
end;
|
|
}
|
|
for ic:=0 to (groups.Count-1) do begin
|
|
st:=groups.Strings[ic]; //get one record to parse
|
|
id:=Pos(UIDtoStr(uid),st); //get position of uname
|
|
if ((id<>0) or (uid=0)) then begin
|
|
st:=copy(st,1,Pos(':',st)-1);
|
|
List.Append(st);
|
|
end; //if
|
|
end; //for
|
|
finally
|
|
FreeAndNil(groups);
|
|
end;
|
|
end;
|
|
|
|
procedure getGroups(List: TStrings);
|
|
begin
|
|
getUsrGroups(0,List);
|
|
end;
|
|
|
|
procedure GetUsers(List: TStrings);
|
|
var
|
|
Users: TStrings;
|
|
iC: integer;
|
|
sT: string;
|
|
begin
|
|
users:=TStringList.Create;
|
|
try
|
|
users.LoadFromFile(userInfo);
|
|
|
|
List.Clear;
|
|
for ic:=0 to (users.Count-1) do begin
|
|
st:=users.Strings[ic]; //get one record (line)
|
|
st:=copy(st,1,Pos(':',st)-1); //extract username
|
|
List.Append(st); //append to the list
|
|
end;
|
|
finally
|
|
FreeAndNil(users);
|
|
end;
|
|
end;
|
|
|
|
function strToUID(uname: AnsiString): Cardinal;
|
|
//Converts username to UID ('root' results to 0)
|
|
var
|
|
uinfo: PPasswordRecord;
|
|
begin
|
|
uinfo:=libc.getpwnam(PChar(uname));
|
|
if(uinfo=nil) then
|
|
result:=high(Cardinal)
|
|
else
|
|
result:=uinfo^.pw_uid;
|
|
end;
|
|
|
|
function strToGID(gname: AnsiString): Cardinal;
|
|
var
|
|
ginfo: Pgroup;
|
|
begin
|
|
ginfo:=libc.getgrnam(pChar(gname));
|
|
if(ginfo=nil) then
|
|
result:=high(Cardinal)
|
|
else
|
|
result:=ginfo^.gr_gid;
|
|
end;
|
|
{/mate}
|
|
end.
|
|
|