system.pp 7.2 KB

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