Recent

Author Topic: [Solved] Why such output of "./fpc"  (Read 3628 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2715
    • UVviewsoft
[Solved] Why such output of "./fpc"
« on: March 09, 2021, 09:37:36 am »
Quote
user@PC:~/fpcupdeluxe/fpcupdeluxe32/fpc/bin/x86_64-linux$ ./fpc
Free Pascal Compiler version 3.2.1-r48494 [2021/02/02] for x86_64
Copyright (c) 1993-2020 by Florian Klaempfl and others
/home/user/fpcupdeluxe/fpcupdeluxe32/fpc/bin/x86_64-linux/fpc [options] <inputfile> [options]
 Only options valid for the default or selected platform are listed.

I suggest not to print the enture path of 'fpc' binary here:

Quote
/home/user/fpcupdeluxe/fpcupdeluxe32/fpc/bin/x86_64-linux/fpc [options] <inputfile> [options]
« Last Edit: March 09, 2021, 12:01:47 pm by Alextp »

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1071
Re: Why such output of "./fpc"
« Reply #1 on: March 09, 2021, 11:25:21 am »
It prints paramstr(0), which under Linux includes the full path if that's what you typed on the command line. It's also important, because an fpc binary in a different location may be a different version that supports different options.

Warfley

  • Hero Member
  • *****
  • Posts: 2067
Re: [Solved] Why such output of "./fpc"
« Reply #2 on: March 09, 2021, 05:48:52 pm »
Thats not quite right, this is FPC specific behavior and has nothing to do with Linux.

On Linux usually the first argument (argv[0] in c, or $0 in bash) is the first argument given to the exec* call. In Bash this is exactly the path used in the command. The FPC however always compiles the code in a way that it will always return the absolute path (don't ask me why and how, I only observed this behavior from the outside). Example:
test.c:
Code: C  [Select][+][-]
  1. #include<stdio.h>
  2.  
  3. int main(int argc, char const * const *argv) {
  4.   printf("%s\n", argv[0]);
  5.   return 0;
  6. }
test.sh:
Code: Bash  [Select][+][-]
  1. echo $0
test.pas:
Code: Pascal  [Select][+][-]
  1. program Test;
  2. {$Mode ObjFPC}{$H+}
  3.  
  4. begin
  5.   WriteLn(ParamStr(0));
  6. end.

Outputs:
Code: Bash  [Select][+][-]
  1. user@ubuntu:~$ test/ctest
  2. test/ctest
  3. user@ubuntu:~$ test/pastest
  4. /home/user/test/pastest
  5. user@ubuntu:~$ bash test/test.sh
  6. test/test.sh
  7. user@ubuntu:~$ cd test
  8. user@ubuntu:~/test$ ./ctest
  9. ./ctest
  10. user@ubuntu:~/test$ ./pastest
  11. /home/user/test/pastest
  12. user@ubuntu:~/test$ bash ./test.sh
  13. ./test.sh
  14.  

For C cli applications it is common pratice for help pages to simply print argv[0] [options] as first line, something the FPC also does with ParamStr(0). The thing is that ParamStr behaves differently then most other languages.

It should be noted that this means that information is actually lost, as you can see here:
Code: Bash  [Select][+][-]
  1. test@ubuntu:~$ test/../test/../test/ctest
  2. test/../test/../test/ctest
The complete path information of the call is stored, even the redundant one. I don't know of any possibility to access this information from fpc programs
« Last Edit: March 09, 2021, 05:50:49 pm by Warfley »

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: [Solved] Why such output of "./fpc"
« Reply #3 on: March 09, 2021, 06:00:06 pm »
In any event, an inexperienced user shouldn't be messing around with a malformed command line.

OTOH I think that complaining that FPC doesn't support the --version and --help commands would be fair criticism.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 10.2 user
Re: [Solved] Why such output of "./fpc"
« Reply #4 on: March 10, 2021, 07:10:01 am »
I didn't think using paramStr(0), or any function which uses it, on any UNIX or UNIX-like platform to determine the location of an executable was advised.

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: [Solved] Why such output of "./fpc"
« Reply #5 on: March 10, 2021, 07:23:04 am »
I didn't think using paramStr(0), or any function which uses it, on any UNIX or UNIX-like platform to determine the location of an executable was advised.

It isn't, because it has many potential pitfalls. The RTL tries to overcome most of them (which is probably why it returns an absolute path no matter what) but they are there nevertheless.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12905
  • FPC developer.
Re: [Solved] Why such output of "./fpc"
« Reply #6 on: March 10, 2021, 08:19:41 am »
Did you try argv[0] ?

 

TinyPortal © 2005-2018