So... Use the GetHostName function from the linux unit.
http://porthos.ist.utl.pt/docs/fpc/units/node288.html
Does not work either...
I did this:
uses
{$IFNDEF FPC} //Delphi
Windows,
{$ENDIF}
{$IFDEF UNIX}
Linux, //Added in order to resolve hostname issue
{$ENDIF}
Classes,
SysUtils;
....
{$IFDEF UNIX}
slHeader.Add('Computer name: ' + GetHostName; //GetEnvironmentVariable(COMPUTER)); //Using suggested call, not working
{$ELSE}
slHeader.Add('Computer name: ' + GetEnvironmentVariable(COMPUTER));
{$ENDIF}
But when I try to compile this I get the error:
logagi.pas(285,42) Error: Identifier not found "GetHostName"
But I have tested using the command "hostname" on the command line in a terminal on both Raspbian and Ubuntu and both return a valid reply.
And like Raspbian HOSTNAME is not available as an env variable, but echo $HOSTNAME shows the correct result.
Raspbian and Ubuntu operate the same.
So if I were writing a shellscript I could easily use this command to get the name I want.
But now I am writing a FreePascal console program using Lazarus and then I don't see how I can use this kind of call at all, now that the suggested solution actually does not work...
Seems like I really have to open a file on the disk (cat /etc/hostname) in order to read the hostname from it...
For example making a function like this (tested to be working):
{$IFDEF UNIX}
{Since the env var HOSTNAME does not exist on Linux we need to read a file...}
function TLogAGI.MyGetHostName: string;
var
sl: TStringList;
begin
Result := '';
sl := TStringList.Create;
try
sl.LoadFromFile('/etc/hostname');
if sl.Count > 0 then
Result := sl[0];
finally
sl.Free;
end;
end;
{$ENDIF}