system.pp 6.9 KB

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