genfuncs.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. {$ifndef FPC_USE_LIBC}
  55. Function fpgetenv(name:pchar):pchar;
  56. var
  57. p : ppchar;
  58. found : boolean;
  59. np,cp : pchar;
  60. len,i : longint;
  61. Begin
  62. if (name=nil) or (envp=NIL) Then
  63. exit(NIL);
  64. np:=name;
  65. while (np^<>#0) and (np^<>'=') DO
  66. inc(np);
  67. len:=np-name;
  68. p:=envp;
  69. while (p^<>NIL) DO
  70. Begin
  71. cp:=p^;
  72. np:=name;
  73. i:=len;
  74. while (i<>0) and (cp^<>#0) DO
  75. Begin
  76. if cp^<>np^ Then
  77. Begin
  78. inc(cp); inc(np);
  79. break;
  80. End;
  81. inc(cp); inc(np);
  82. dec(i)
  83. End;
  84. if (i=0) and (cp^='=') Then
  85. exit(cp+1);
  86. inc(p);
  87. end;
  88. fpgetenv:=nil;
  89. End;
  90. {$ENDIF}
  91. Function fpgetenv(name:string):Pchar; [public, alias : 'FPC_SYSC_FPGETENV'];
  92. {
  93. Searches the environment for a string with name p and
  94. returns a pchar to it's value.
  95. A pchar is used to accomodate for strings of length > 255
  96. }
  97. Begin
  98. name:=name+'='; {Else HOST will also find HOSTNAME, etc}
  99. fpgetenv:=fpgetenv(@name[1]);
  100. end;
  101. {
  102. $Log$
  103. Revision 1.3 2003-12-30 12:24:01 marco
  104. * FPC_USE_LIBC
  105. Revision 1.2 2003/09/14 20:15:01 marco
  106. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  107. Revision 1.1 2002/12/18 16:50:39 marco
  108. * Unix RTL generic parts. Linux working, *BSD will follow shortly
  109. Revision 1.2 2002/10/27 17:21:30 marco
  110. * Only "difficult" functions + execvp + termios + rewinddir left to do
  111. Revision 1.1 2002/10/27 13:16:54 marco
  112. * Routines that certainly will be shared between Linux and *BSD
  113. }