2
0

genfuncs.inc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2002 by Marco van de Voort.
  4. A few general purpose routines. General purpose enough for *BSD
  5. and Linux at least.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. function InternalCreateShellArgV(cmd:pChar; len:longint):ppchar;
  13. {
  14. Create an argv which executes a command in a shell using /bin/sh -c
  15. }
  16. const Shell = '/bin/sh'#0'-c'#0;
  17. var
  18. pp,p : ppchar;
  19. // temp : string; !! Never pass a local var back!!
  20. begin
  21. getmem(pp,4*sizeof(pointer));
  22. p:=pp;
  23. p^:=@Shell[1];
  24. inc(p);
  25. p^:=@Shell[9];
  26. inc(p);
  27. getmem(p^,len+1);
  28. move(cmd^,p^^,len);
  29. pchar(p^)[len]:=#0;
  30. inc(p);
  31. p^:=Nil;
  32. InternalCreateShellArgV:=pp;
  33. end;
  34. function CreateShellArgV(const prog:string):ppchar;
  35. begin
  36. CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog));
  37. end;
  38. function CreateShellArgV(const prog:Ansistring):ppchar;
  39. {
  40. Create an argv which executes a command in a shell using /bin/sh -c
  41. using a AnsiString;
  42. }
  43. begin
  44. CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog)); // if ppc works like delphi this also work when @prog[1] is invalid (len=0)
  45. end;
  46. procedure FreeShellArgV(p:ppchar);
  47. begin
  48. if (p<>nil) then begin
  49. freemem(p[2]);
  50. freemem(p);
  51. end;
  52. end;
  53. Function fpgetenv(name:string):Pchar; [public, alias : 'FPC_SYSC_FPGETENV'];
  54. {
  55. Searches the environment for a string with name p and
  56. returns a pchar to it's value.
  57. A pchar is used to accomodate for strings of length > 255
  58. }
  59. Begin
  60. {$ifndef FPC_USE_LIBC}
  61. name:=name+'='; {Else HOST will also find HOSTNAME, etc}
  62. {$else}
  63. name:=name+#0;
  64. {$endif}
  65. fpgetenv:=fpgetenv(@name[1]);
  66. end;