Recent

Author Topic: [SOLVED] Linux, GetUsername( I can't get it!  (Read 2026 times)

QEnnay

  • Jr. Member
  • **
  • Posts: 59
[SOLVED] Linux, GetUsername( I can't get it!
« on: July 12, 2020, 07:46:24 pm »
Mint 20.0, Lazarus 2.0.8

Hi,
Must be missing something here, but I need the User name or Home-Folder Path. I checked the "users.pas" and plenty of options, but they all seem to need the User-name to start with.

Code: Pascal  [Select][+][-]
  1.   GetUserName( UserName : String) : String;
  2.  

If I know the name to put in there, why would I need "GetUserName(" ?

Is there some way to get the current User-name with first knowing it and hard coding it in?
« Last Edit: July 13, 2020, 04:38:01 pm by QEnnay »
Laz 2.0.8
Linux-Mint 20.04.0 + Cinnamon, Kernel 5.8 (NOT stock Ubuntu/Mint 5.4 Kernel)
Lenovo Flex 5 Ryzen 5 4500, 512GB-NVMe, 16GB memory

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3867
  • I like bugs.
Re: Linux, GetUsername( I can't get it!
« Reply #1 on: July 12, 2020, 07:55:40 pm »
Code: Pascal  [Select][+][-]
  1.   GetUserName( UserName : String) : String;
  2.  
Whare did you find that?
This should work :
Code: Pascal  [Select][+][-]
  1. UserName:=GetEnvironmentVariable('USER');
You need SysUtils in uses section then.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux.

rvk

  • Hero Member
  • *****
  • Posts: 4334
Re: Linux, GetUsername( I can't get it!
« Reply #2 on: July 12, 2020, 08:07:18 pm »
Or
Code: Pascal  [Select][+][-]
  1. uses users,baseunix;
  2. begin
  3.   Writeln(GetUserName(fpgetuid));
  4. end.

The string parameter you are referring to is not the username but the user uid (which you can get with fpgetuid).

Not sure why the parameter name would be username which is confusing.
« Last Edit: July 12, 2020, 08:11:29 pm by rvk »

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3867
  • I like bugs.
Re: Linux, GetUsername( I can't get it!
« Reply #3 on: July 12, 2020, 09:13:12 pm »
Code: Pascal  [Select][+][-]
  1. uses users,baseunix;
  2. begin
  3.   Writeln(GetUserName(fpgetuid));
  4. end.
The string parameter you are referring to is not the username but the user uid (which you can get with fpgetuid).
Not sure why the parameter name would be username which is confusing.
Ok, I did not remember the GetUserName() function. Actually its signature is :
Code: Pascal  [Select][+][-]
  1. function GetUserName(UID : TUID) : String;
Parameter name UID is quite descriptive.

GetEnvironmentVariable('USER') has an advantage because it is cross-platform. IIRC it works on Windows, too. (is it so ?)
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux.

rvk

  • Hero Member
  • *****
  • Posts: 4334
Re: Linux, GetUsername( I can't get it!
« Reply #4 on: July 12, 2020, 09:28:01 pm »
GetEnvironmentVariable('USER') has an advantage because it is cross-platform. IIRC it works on Windows, too. (is it so ?)
On Windows it should be USERNAME so you would still need to do IFDEFs.

And the environment variables can be manipulated easily. I can make myself root  8)

So sometimes the api is preferred (although also not bulletproof).

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3867
  • I like bugs.
Re: Linux, GetUsername( I can't get it!
« Reply #5 on: July 12, 2020, 09:53:18 pm »
On Windows it should be USERNAME so you would still need to do IFDEFs.
Ok, I have not done much WinAPI programming.
However I found a function for Lazarus IDE's internal use, in unit ide/IDEProcs :
Code: Pascal  [Select][+][-]
  1. function GetCurrentUserName: string;
  2. begin
  3.   Result:=GetEnvironmentVariableUTF8('USER');
  4. end;
GetEnvironmentVariableUTF8 is just a wrapper for GetEnvironmentVariableWide (Windows) or GetEnvironmentVariable (other systems).
GetCurrentUserName is used for inserting your name in editor from menu : Source -> Insert General -> Current Username
Can you please test if it works on Windows. My guess is that recent Windows versions have 'USER' as an alternative env.variable. If not, then we have a bug and must add IFDEFs to the function.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux.

rvk

  • Hero Member
  • *****
  • Posts: 4334
Re: Linux, GetUsername( I can't get it!
« Reply #6 on: July 12, 2020, 10:18:19 pm »
Can you please test if it works on Windows. My guess is that recent Windows versions have 'USER' as an alternative env.variable. If not, then we have a bug and must add IFDEFs to the function.
Nope, it's just USERNAME on Windows. Not USER.
Quote
C:\Users\Rik>echo %USERNAME%
Rik

C:\Users\Rik>echo %USER%
%USER%

C:\Users\Rik>set user
USERDOMAIN=XPS420
USERDOMAIN_ROAMINGPROFILE=XPS420
USERNAME=Rik
USERPROFILE=C:\Users\Rik

This:
Code: Pascal  [Select][+][-]
  1. uses lazutf8;
  2.  
  3. function GetCurrentUserName1: string;
  4. begin
  5.   Result := GetEnvironmentVariableUTF8('USER');
  6. end;
  7.  
  8. function GetCurrentUserName2: string;
  9. begin
  10.   Result := GetEnvironmentVariableUTF8('USERNAME');
  11. end;
  12.  
  13. procedure TForm1.Button1Click(Sender: TObject);
  14. begin
  15.   Memo1.Lines.Add('1 - ' + GetCurrentUserName1);
  16.   Memo1.Lines.Add('2 - ' + GetCurrentUserName2);
  17. end;
Results in
Quote
1 -
2 - Rik

So yes, the GetCurrentUserName in ideprocs.pp is really wrong.
But it isn't really used much in the IDE code anyway.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3867
  • I like bugs.
Re: Linux, GetUsername( I can't get it!
« Reply #7 on: July 12, 2020, 11:13:17 pm »
Ok. I fixed it in r63547.
Now Source -> Insert General -> "Current Username" should work also on Windows.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux.

rvk

  • Hero Member
  • *****
  • Posts: 4334
Re: Linux, GetUsername( I can't get it!
« Reply #8 on: July 12, 2020, 11:38:16 pm »
Ok. I fixed it in r63547.
Now Source -> Insert General -> "Current Username" should work also on Windows.
Fix confirmed on Windows.

There is also a GetCurrentMailAddress which uses GetCurrentUser in combination with HOSTNAME environment variable.
But there is no such thing on Windows 10 professional (there is a COMPUTERNAME though).

And from the other recent topic... there might also not be a HOSTNAME environment variable on Linux Debian either (there might be on other Linux versions). It's a bash/shell variable on Debian. https://forum.lazarus.freepascal.org/index.php/topic,50533.0.html

So on Windows and Linux Debian/Raspberry pi etc...
Quote
Source -> Insert General -> "ChangeLog Entry"
results in:
Quote
12-07-2020   Rik <Rik@>

Not very critical. I've never used those options anyway (and with me probably nobody on Windows and Debian  ;D ).

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3867
  • I like bugs.
Re: Linux, GetUsername( I can't get it!
« Reply #9 on: July 13, 2020, 09:58:50 am »
Ok. If there is a sensible way to fix it, I can apply a patch. However it sounds like COMPUTERNAME would not be very useful in GetCurrentMailAddress.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux.

rvk

  • Hero Member
  • *****
  • Posts: 4334
Re: Linux, GetUsername( I can't get it!
« Reply #10 on: July 13, 2020, 10:21:52 am »
Ok. If there is a sensible way to fix it, I can apply a patch. However it sounds like COMPUTERNAME would not be very useful in GetCurrentMailAddress.
HOSTNAME (of a linux machine) is also not very sensible in a emailaddress.
On linux it's the local machinename which is not a mailserver to receive email at all.

The only place GetCurrentMailAddress is used, I think, is in that insert ChangeLog Entry.
So GetCurrentMailAddress is a wrongly named procedure anyway.

I can't comment on what's sensible because I wouldn't even know what that "insert ChangeLog Entry" is used for. If it needs the real email address then the function is flawed in linux too. If it needs username@computername it needs COMPUTERNAME for Windows and HOSTNAME for linux (with the notice that Debian doesn't have HOSTNAME in the env).

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3867
  • I like bugs.
Re: Linux, GetUsername( I can't get it!
« Reply #11 on: July 13, 2020, 11:23:16 am »
In r63552 I renamed the function as GetCurrentChangeLog and added COMPUTERNAME for Windows.
Not perfect but better than earlier.
I don't know the history of this function. It has been there forever.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux.

rvk

  • Hero Member
  • *****
  • Posts: 4334
Re: Linux, GetUsername( I can't get it!
« Reply #12 on: July 13, 2020, 12:48:20 pm »
In r63552 I renamed the function as GetCurrentChangeLog and added COMPUTERNAME for Windows.
Fix confirmed on Windows
Code: [Select]
13-07-2020   Rik <Rik@XPS8500>

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3867
  • I like bugs.
Re: Linux, GetUsername( I can't get it!
« Reply #13 on: July 13, 2020, 01:35:33 pm »
Code: [Select]
13-07-2020   Rik <Rik@XPS8500>
Strange. In my Manjaro Linux the ChangeLog Entry gives :
Code: [Select]
13.07.2020   juha <juha@>It means HOSTNAME gives an empty string. However it works in console :
Code: [Select]
$ echo $HOSTNAME
juha-fp30
USER works as expected in both cases :
Code: [Select]
$ echo $USER
juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux.

rvk

  • Hero Member
  • *****
  • Posts: 4334
Re: Linux, GetUsername( I can't get it!
« Reply #14 on: July 13, 2020, 01:42:28 pm »
Code: [Select]
13-07-2020   Rik <Rik@XPS8500>
Strange. In my Manjaro Linux the ChangeLog Entry gives :
Code: [Select]
13.07.2020   juha <juha@>It means HOSTNAME gives an empty string. However it works in console :
Code: [Select]
$ echo $HOSTNAME
juha-fp30
USER works as expected in both cases :
Code: [Select]
$ echo $USER
juha
That was discussed in a recent topic.
https://forum.lazarus.freepascal.org/index.php/topic,50533.0.html

On some Linux/Unix systems, the HOSTNAME is NOT a environment variable, but a bash/shell variable. There is a difference.

If you do env | grep HOSTNAME you get nothing (because on Debian and apparently Manjaro there no environment variable HOSTNAME).
But if you do set | grep HOSTNAME you'll see the HOSTNAME. (set gives all the shell variables)
(and echo does both substitution for environment and shell variables)

Only way to reliably get the hostname is using the GetHostName from baseunix or unix unit.
Or pulling it out of /proc/sys/kernel/hostname pseudo-file.

 

TinyPortal © 2005-2018