system.pp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team.
  4. QNX system unit
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. { no stack check in system }
  12. {$S-}
  13. unit System;
  14. interface
  15. { include system-independent routine headers }
  16. {$I systemh.inc}
  17. { include heap support headers }
  18. {$I heaph.inc}
  19. var
  20. argc : longint; public name 'operatingsystem_parameter_argc';
  21. argv : ppchar;public name 'operatingsystem_parameter_argv';
  22. envp : ppchar;public name 'operatingsystem_parameter_envp';
  23. var
  24. Errno : longint; external name 'errno'; { declared in libc }
  25. var
  26. UnusedHandle:longint;
  27. StdInputHandle:longint;
  28. StdOutputHandle:longint;
  29. StdErrorHandle:longint;
  30. {Platform specific information}
  31. const
  32. LineEnding = #10;
  33. LFNSupport = true;
  34. DirectorySeparator = '/';
  35. DriveSeparator = '';
  36. PathSeparator = ':';
  37. FileNameCaseSensitive = True;
  38. implementation
  39. {$I system.inc}
  40. {$i errno.inc} { Error numbers }
  41. {$I osposixh.inc} { include POSIX types / constants }
  42. {$I osposix.inc} { include POSIX system calls }
  43. {$i sysposix.inc}
  44. {*****************************************************************************
  45. Executable filename
  46. *****************************************************************************}
  47. Function FileSearch(const path:shortstring;dirlist:shortstring):shortstring;
  48. {
  49. Searches for a file 'path' in the list of direcories in 'dirlist'.
  50. returns an empty string if not found. Wildcards are NOT allowed.
  51. If dirlist is empty, it is set to '.'
  52. }
  53. Var
  54. NewDir : shortstring;
  55. p1 : Longint;
  56. Info : Stat;
  57. buffer : array[0..PATH_MAX+1] of char;
  58. Begin
  59. Move(path[1], Buffer, Length(path));
  60. Buffer[Length(path)]:=#0;
  61. if (length(Path)>0) and (path[1]='/') and (sys_stat(pchar(@Buffer),info)=0) then
  62. begin
  63. FileSearch:=path;
  64. exit;
  65. end;
  66. {Replace ':' with ';'}
  67. for p1:=1to length(dirlist) do
  68. if dirlist[p1]=':' then
  69. dirlist[p1]:=';';
  70. {Check for WildCards}
  71. If (Pos('?',Path) <> 0) or (Pos('*',Path) <> 0) Then
  72. FileSearch:='' {No wildcards allowed in these things.}
  73. Else
  74. Begin
  75. Dirlist:='.;'+dirlist;{Make sure current dir is first to be searched.}
  76. Repeat
  77. p1:=Pos(';',DirList);
  78. If p1=0 Then
  79. p1:=255;
  80. NewDir:=Copy(DirList,1,P1 - 1);
  81. if NewDir[Length(NewDir)]<>'/' then
  82. NewDir:=NewDir+'/';
  83. NewDir:=NewDir+Path;
  84. Delete(DirList,1,p1);
  85. Move(NewDir[1], Buffer, Length(NewDir));
  86. Buffer[Length(NewDir)]:=#0;
  87. if sys_stat(pchar(@Buffer),Info)=0 then
  88. Begin
  89. If Pos('./',NewDir)=1 Then
  90. Delete(NewDir,1,2);
  91. {DOS strips off an initial .\}
  92. End
  93. Else
  94. NewDir:='';
  95. Until (DirList='') or (Length(NewDir) > 0);
  96. FileSearch:=NewDir;
  97. End;
  98. End;
  99. Function GetEnv(EnvVar:shortstring):shortstring;
  100. {
  101. Searches the environment for a string with name p and
  102. returns a pchar to it's value.
  103. A pchar is used to accomodate for strings of length > 255
  104. }
  105. var
  106. ep : ppchar;
  107. found : boolean;
  108. p1 : pchar;
  109. Begin
  110. EnvVar:=EnvVar+'='; {Else HOST will also find HOSTNAME, etc}
  111. ep:=envp;
  112. found:=false;
  113. if ep<>nil then
  114. begin
  115. while (not found) and (ep^<>nil) do
  116. begin
  117. if (pos(EnvVar,strpas(ep^))=1) then
  118. found:=true
  119. else
  120. inc(ep);
  121. end;
  122. end;
  123. if found then
  124. p1:=ep^+length(EnvVar)
  125. else
  126. p1:=nil;
  127. if p1 = nil then
  128. GetEnv := ''
  129. else
  130. GetEnv := StrPas(p1);
  131. end;
  132. { this routine sets up the paramstr(0) string at startup }
  133. procedure setupexecname;
  134. var
  135. fstr: shortstring;
  136. begin
  137. execpathstr := strpas(argv[0]);
  138. fstr:=filesearch(strpas(argv[0]), getenv('PATH'));
  139. if fstr<>'' then
  140. execpathstr:=fstr;
  141. end;
  142. {*****************************************************************************
  143. Heap Management
  144. *****************************************************************************}
  145. function malloc(size: size_t): pointer; cdecl; external name 'malloc';
  146. { IMPORTANT SOLARIS PORT NOTE: mmap() cannot be used, since ANONYMOUS }
  147. { requests are only available starting from Solaris 8. sbrk() cannot }
  148. { be used either since C libraries linked in with the runtime library may }
  149. { use malloc(), and the man pages of Solaris indicate that mixing both }
  150. { sbrk() and malloc() is a no-no. }
  151. function Sbrk(size : longint):longint;
  152. var ptr : pointer;
  153. begin
  154. ptr := malloc(size_t(size));
  155. if ptr = nil then
  156. sbrk := -1
  157. else
  158. begin
  159. sbrk := longint(ptr);
  160. errno := 0;
  161. end;
  162. end;
  163. { include standard heap management }
  164. {$I heap.inc}
  165. {****************************************************************************
  166. Low level File Routines
  167. All these functions can set InOutRes on errors
  168. ****************************************************************************}
  169. function do_isdevice(handle:longint):boolean;
  170. begin
  171. do_isdevice:= (handle=StdInputHandle) or
  172. (handle=StdOutputHandle) or
  173. (handle=StdErrorHandle);
  174. end;
  175. {*****************************************************************************
  176. UnTyped File Handling
  177. *****************************************************************************}
  178. {$i file.inc}
  179. {*****************************************************************************
  180. Typed File Handling
  181. *****************************************************************************}
  182. {$i typefile.inc}
  183. {*****************************************************************************
  184. Text File Handling
  185. *****************************************************************************}
  186. {$DEFINE SHORT_LINEBREAK}
  187. { DEFINE EXTENDED_EOF}
  188. {$i text.inc}
  189. {*****************************************************************************
  190. SystemUnit Initialization
  191. *****************************************************************************}
  192. var
  193. stacklength : longint;external name '__stklen';
  194. begin
  195. { setup lowest value of stack pointer }
  196. StackBottom := SPtr - StackLength;
  197. InitHeap;
  198. { Set up signals handlers }
  199. InstallSignals;
  200. { Setup heap }
  201. InitExceptions;
  202. { Arguments }
  203. SetupCmdLine;
  204. { Setup IO }
  205. StdInputHandle:=0;
  206. StdOutputHandle:=1;
  207. StdErrorHandle:=2;
  208. OpenStdIO(Input,fmInput,StdInputHandle);
  209. OpenStdIO(Output,fmOutput,StdOutputHandle);
  210. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  211. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  212. { Reset IO Error }
  213. InOutRes:=0;
  214. setupexecname;
  215. end.