[fpc-pascal]How to get the Home Directory of the user? (using pchar correctly)
Stefan Becker
becker at lufa-sp.vdlufa.de
Sun Sep 8 02:05:08 CEST 2002
Hi,
just by quickly looking at the example you gave I would say that
you pass pointers to the memory areas where the results
should be returned, but never declare the accual memory.
if you pass a pchar type - meaning a "pointer to a area of char memory".
Then this memory must be valid.
I would declare something like:
{$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;
var
pwd : pPasswd;
userdata:array[0..255] of char;
user: pChar;
BEGIN
user:=@userdata;
........
now your progi could work!!
Stefan
-----Ursprüngliche Nachricht-----
Von: Hélio Cavichiolo Jr <helio at definitylinux.com.br>
An: fpc-pascal at deadlock.et.tudelft.nl <fpc-pascal at deadlock.et.tudelft.nl>
Datum: Freitag, 6. September 2002 19:10
Betreff: Re: [fpc-pascal]How to get the Home Directory of the user?
>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.
>
>> 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 - fpc-pascal at lists.freepascal.org
>> 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 - fpc-pascal at lists.freepascal.org
>http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
More information about the fpc-pascal
mailing list