genfuncs.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2002 by Marco van de Voort.
  5. A few general purpose routines. General purpose enough for *BSD
  6. and Linux at least.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. function InternalCreateShellArgV(cmd:pChar; len:longint):ppchar;
  14. {
  15. Create an argv which executes a command in a shell using /bin/sh -c
  16. }
  17. const Shell = '/bin/sh'#0'-c'#0;
  18. var
  19. pp,p : ppchar;
  20. // temp : string; !! Never pass a local var back!!
  21. begin
  22. getmem(pp,4*4);
  23. p:=pp;
  24. p^:=@Shell[1];
  25. inc(p);
  26. p^:=@Shell[9];
  27. inc(p);
  28. getmem(p^,len+1);
  29. move(cmd^,p^^,len);
  30. pchar(p^)[len]:=#0;
  31. inc(p);
  32. p^:=Nil;
  33. InternalCreateShellArgV:=pp;
  34. end;
  35. function CreateShellArgV(const prog:string):ppchar;
  36. begin
  37. CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog));
  38. end;
  39. function CreateShellArgV(const prog:Ansistring):ppchar;
  40. {
  41. Create an argv which executes a command in a shell using /bin/sh -c
  42. using a AnsiString;
  43. }
  44. begin
  45. CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog)); // if ppc works like delphi this also work when @prog[1] is invalid (len=0)
  46. end;
  47. procedure FreeShellArgV(p:ppchar);
  48. begin
  49. if (p<>nil) then begin
  50. freemem(p[2]);
  51. freemem(p);
  52. end;
  53. end;
  54. Function fpgetenv(name:pchar):pchar;
  55. var
  56. p : ppchar;
  57. found : boolean;
  58. np,cp : pchar;
  59. len,i : longint;
  60. Begin
  61. if (name=nil) or (envp=NIL) Then
  62. exit(NIL);
  63. np:=name;
  64. while (np^<>#0) and (np^<>'=') DO
  65. inc(np);
  66. len:=np-name;
  67. p:=envp;
  68. while (p^<>NIL) DO
  69. Begin
  70. cp:=p^;
  71. np:=name;
  72. i:=len;
  73. while (i<>0) and (cp^<>#0) DO
  74. Begin
  75. if cp^<>np^ Then
  76. Begin
  77. inc(cp); inc(np);
  78. break;
  79. End;
  80. inc(cp); inc(np);
  81. dec(i)
  82. End;
  83. if (i=0) and (cp^='=') Then
  84. exit(cp+1);
  85. inc(p);
  86. end;
  87. fpgetenv:=nil;
  88. End;
  89. Function fpgetenv(name:string):Pchar; [public, alias : 'FPC_SYSC_FPGETENV'];
  90. {
  91. Searches the environment for a string with name p and
  92. returns a pchar to it's value.
  93. A pchar is used to accomodate for strings of length > 255
  94. }
  95. Begin
  96. name:=name+'='; {Else HOST will also find HOSTNAME, etc}
  97. fpgetenv:=fpgetenv(@name[1]);
  98. end;
  99. {
  100. $Log$
  101. Revision 1.2 2003-09-14 20:15:01 marco
  102. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  103. Revision 1.1 2002/12/18 16:50:39 marco
  104. * Unix RTL generic parts. Linux working, *BSD will follow shortly
  105. Revision 1.2 2002/10/27 17:21:30 marco
  106. * Only "difficult" functions + execvp + termios + rewinddir left to do
  107. Revision 1.1 2002/10/27 13:16:54 marco
  108. * Routines that certainly will be shared between Linux and *BSD
  109. }