Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-09

Re: How to get the Home Directory of the user?

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Stefan Becker

Hi,
this is how you can get some info's from a logged User:

unit pwdlibc;
{--------------------------------------------------------------------------- CncWare
(c) Copyright 1999
---------------------------------------------------------------------------- Filename..: pwdlibc.pp
Programmer: Ken J. Wright
Date......: 10/01/1999

Purpose - Link to the Linux 'c' library for system passwd functions.

-------------------------------< Revisions

---------------------------------

Revision| Date | Prog| Description
---------------------------------------------------------------------------- -
1.00 | 10/01/99 | kjw | Initial release.
---------------------------------------------------------------------------- -
}

interface
{$linklib c}

{$PACKRECORDS 4}
Type

   pPasswd = ^passwd;
   passwd  = Record
      pw_name,                         { Username }
      pw_passwd : pChar;               { Password }
      pw_uid,                          { User ID }
      pw_gid : longint;                { Group ID }
      pw_gecos,                        { Real Name }
      pw_dir,                          { Home directory }
      pw_shell : pChar;                { Shell program }
   End;
   pGroup = ^group;
   group  = Record
      gr_name,                         { Group name }
      gr_passwd : pChar;               { Password }
      gr_gid : longint;                { Group ID }
      gr_mem : pChar;                  { Member list }
   End;
{ returns a pointer to a passwd record }
Function getpwuid(uid : longint) : pPasswd; cdecl;
Function getpwnam(nam : pChar) : pPasswd; cdecl;
{ returns a pointer to a group record }
Function getgrgid(gid : longint) : pGroup; cdecl;
Function getgrnam(nam : pChar) : pGroup; cdecl;
implementation
Function getpwuid(uid : longint) : pPasswd; cdecl; External;
Function getpwnam(nam : pChar) : pPasswd; cdecl; External;
Function getgrgid(gid : longint) : pGroup; cdecl; External;
Function getgrnam(nam : pChar) : pGroup; cdecl; External;
end.

my regards,
Stefan

Hello, Every body Can you tell me how to get the Home Directory of a user in free pascal

under

linux? Thank you very much. Yours, mili

Reply by email

Hélio Cavichiolo Jr
to Stefan Becker

How to check if a call to getpwnam had success?
How to change user information?

I made this program to test but the 'if' never matches. Entering a valid user, I had to remove de 'if' to see user's gecos, by the other hand, entering an invalid user, the program crashes on the 'if'.

program test;
uses
  pwdlibc;
var
  pwd : pPasswd;
  user: pChar;
begin
  user := '';
  write('Usuário: ');
  readln(user);
  pwd := Getpwnam(user);
  if user = pwd^.pw_name then
    begin
      writeln('(',pwd^.pw_name,')');
      writeln('(',pwd^.pw_gecos,')');
    end
  else
    writeln(user,' not found!');
end.
Show quoted text 88 lines
Hi, this is how you can get some info's from a logged User: unit pwdlibc; {--------------------------------------------------------------------------- CncWare (c) Copyright 1999 ---------------------------------------------------------------------------- Filename..: pwdlibc.pp Programmer: Ken J. Wright Date......: 10/01/1999 Purpose - Link to the Linux 'c' library for system passwd functions. -------------------------------< Revisions >--------------------------------- Revision| Date | Prog| Description ---------------------------------------------------------------------------- - 1.00 | 10/01/99 | kjw | Initial release. ---------------------------------------------------------------------------- - } interface {$linklib c} {$PACKRECORDS 4} Type pPasswd = ^passwd; passwd = Record pw_name, { Username } pw_passwd : pChar; { Password } pw_uid, { User ID } pw_gid : longint; { Group ID } pw_gecos, { Real Name } pw_dir, { Home directory } pw_shell : pChar; { Shell program } End; pGroup = ^group; group = Record gr_name, { Group name } gr_passwd : pChar; { Password } gr_gid : longint; { Group ID } gr_mem : pChar; { Member list } End; { returns a pointer to a passwd record } Function getpwuid(uid : longint) : pPasswd; cdecl; Function getpwnam(nam : pChar) : pPasswd; cdecl; { returns a pointer to a group record } Function getgrgid(gid : longint) : pGroup; cdecl; Function getgrnam(nam : pChar) : pGroup; cdecl; implementation Function getpwuid(uid : longint) : pPasswd; cdecl; External; Function getpwnam(nam : pChar) : pPasswd; cdecl; External; Function getgrgid(gid : longint) : pGroup; cdecl; External; Function getgrnam(nam : pChar) : pGroup; cdecl; External; end. my regards, Stefan >Hello, Every body > > Can you tell me how to get the Home Directory of a user in free > pascal under >linux? Thank you very much. > > Yours, > mili _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Helio Cavichiolo Jr Analista de Sistemas Esc Telecomunicações Ltda D e f i n i t y L i n u x

Reply by email

Michael Van Canneyt
to Hélio Cavichiolo Jr
Show quoted text 6 lines
How to check if a call to getpwnam had success? How to change user information? I made this program to test but the 'if' never matches. Entering a valid user, I had to remove de 'if' to see user's gecos, by the other hand, entering an invalid user, the program crashes on the 'if'.

You are trying to compare strings with pchars; this will not work.

Show quoted text 9 lines
program test; uses pwdlibc; var pwd : pPasswd; user: pChar;
You should define this as a string. Define a second pchar 'puser';
    user : string;
    puser : pchar;
Show quoted text 5 lines
begin user := ''; write('Usu�rio: '); readln(user);

Add here

   user:=user+#0;
   ;
pwd := Getpwnam(user); if user = pwd^.pw_name then
Change the above by
    if strcmp(puser,pwd^.pw_name)=0 then

and it should work.

Michael.

Show quoted text 110 lines
begin writeln('(',pwd^.pw_name,')'); writeln('(',pwd^.pw_gecos,')'); end else writeln(user,' not found!'); end. > Hi, > this is how you can get some info's from a logged User: > > unit pwdlibc; > {--------------------------------------------------------------------------- > CncWare > (c) Copyright 1999 > ---------------------------------------------------------------------------- > Filename..: pwdlibc.pp > Programmer: Ken J. Wright > Date......: 10/01/1999 > > Purpose - Link to the Linux 'c' library for system passwd functions. > > -------------------------------< Revisions >>--------------------------------- > Revision| Date | Prog| Description > ---------------------------------------------------------------------------- > - > 1.00 | 10/01/99 | kjw | Initial release. > ---------------------------------------------------------------------------- > - > } > > interface > > {$linklib c} > > {$PACKRECORDS 4} > Type > pPasswd = ^passwd; > passwd = Record > pw_name, { Username } > pw_passwd : pChar; { Password } > pw_uid, { User ID } > pw_gid : longint; { Group ID } > pw_gecos, { Real Name } > pw_dir, { Home directory } > pw_shell : pChar; { Shell program } > End; > > pGroup = ^group; > group = Record > gr_name, { Group name } > gr_passwd : pChar; { Password } > gr_gid : longint; { Group ID } > gr_mem : pChar; { Member list } > End; > > { returns a pointer to a passwd record } > Function getpwuid(uid : longint) : pPasswd; cdecl; > Function getpwnam(nam : pChar) : pPasswd; cdecl; > > { returns a pointer to a group record } > Function getgrgid(gid : longint) : pGroup; cdecl; > Function getgrnam(nam : pChar) : pGroup; cdecl; > > implementation > > Function getpwuid(uid : longint) : pPasswd; cdecl; External; > Function getpwnam(nam : pChar) : pPasswd; cdecl; External; > Function getgrgid(gid : longint) : pGroup; cdecl; External; > Function getgrnam(nam : pChar) : pGroup; cdecl; External; > > end. > > my regards, > Stefan > > > >>Hello, Every body >> >> Can you tell me how to get the Home Directory of a user in free >> pascal > under >>linux? Thank you very much. >> >> Yours, >> mili > > > > > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal -- Helio Cavichiolo Jr Analista de Sistemas Esc Telecomunica��es Ltda D e f i n i t y L i n u x _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Hélio Cavichiolo Jr
to Michael Van Canneyt
On Fri, 6 Sep 2002, [iso-8859-1] Hélio Cavichiolo Jr wrote: > How to check if a call to getpwnam had success? > How to change user information?

And the above questions, anyone knows the answers?

Show quoted text 38 lines
> > I made this program to test but the 'if' never matches. > Entering a valid user, I had to remove de 'if' to see user's gecos, by > the other hand, entering an invalid user, the program crashes on the > 'if'. You are trying to compare strings with pchars; this will not work. > > program test; > > uses > pwdlibc; > > var > pwd : pPasswd; > user: pChar; You should define this as a string. Define a second pchar 'puser'; user : string; puser : pchar; > > begin > user := ''; > write('Usuário: '); > readln(user); Add here user:=user+#0; ; > pwd := Getpwnam(user); > if user = pwd^.pw_name then Change the above by if strcmp(puser,pwd^.pw_name)=0 then

I think you mean strcomp from strings unit, right? It works if user exists, otherwise the program crashes. I think there is another way to know when the Getpwname returns ok or fail.

Show quoted text 12 lines
and it should work. Michael. > begin > writeln('(',pwd^.pw_name,')'); > writeln('(',pwd^.pw_gecos,')'); > end > else > writeln(user,' not found!'); > end. >
Helio Cavichiolo Jr Analista de Sistemas Esc Telecomunicações Ltda D e f i n i t y L i n u x

Reply by email

Unknown author

I missed the rest of this thread, but in case you still want to know, the very easiest way to get the users home directory:

Uses linux;
Var
HomeDir :String
<snip>
HomeDir := GetENV('HOME');

Original Message:
-----------------

Earlier message 78 lines
From: Hélio Cavichiolo Jr Date: Mon, 9 Sep 2002 12:22:08 -0300 (BRT) To: Subject: Re: [fpc-pascal]How to get the Home Directory of the user? > On Fri, 6 Sep 2002, [iso-8859-1] Hélio Cavichiolo Jr wrote: > >> How to check if a call to getpwnam had success? >> How to change user information? And the above questions, anyone knows the answers? >> >> I made this program to test but the 'if' never matches. >> Entering a valid user, I had to remove de 'if' to see user's gecos, by >> the other hand, entering an invalid user, the program crashes on the >> 'if'. > > You are trying to compare strings with pchars; this will not work. > >> >> program test; >> >> uses >> pwdlibc; >> >> var >> pwd : pPasswd; >> user: pChar; > > You should define this as a string. Define a second pchar 'puser'; > user : string; > puser : pchar; > >> >> begin >> user := ''; >> write('Usuário: '); >> readln(user); > > Add here > > user:=user+#0; > ; > >> pwd := Getpwnam(user); >> if user = pwd^.pw_name then > > Change the above by > if strcmp(puser,pwd^.pw_name)=0 then I think you mean strcomp from strings unit, right? It works if user exists, otherwise the program crashes. I think there is another way to know when the Getpwname returns ok or fail. > > and it should work. > > Michael. >> begin >> writeln('(',pwd^.pw_name,')'); >> writeln('(',pwd^.pw_gecos,')'); >> end >> else >> writeln(user,' not found!'); >> end. >> -- Helio Cavichiolo Jr Analista de Sistemas Esc Telecomunicações Ltda D e f i n i t y L i n u x

Reply by email

Hélio Cavichiolo Jr
to Unknown author

LOL

I didn't mean subject one, I mean the following ones:

How to check if a call to getpwnam had success?

How to change user information?

Show quoted text 27 lines
I missed the rest of this thread, but in case you still want to know, the very easiest way to get the users home directory: Uses linux; Var HomeDir :String <snip> HomeDir := GetENV('HOME'); Original Message: ----------------- From: Hélio Cavichiolo Jr Date: Mon, 9 Sep 2002 12:22:08 -0300 (BRT) To: Subject: Re: [fpc-pascal]How to get the Home Directory of the user? > On Fri, 6 Sep 2002, [iso-8859-1] Hélio Cavichiolo Jr wrote: > >> How to check if a call to getpwnam had success? >> How to change user information? And the above questions, anyone knows the answers?
Helio Cavichiolo Jr Analista de Sistemas Esc Telecomunicações Ltda D e f i n i t y L i n u x

Reply by email