system.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team.
  5. This is a prototype file to show all function that need to be implemented
  6. for a new operating system (provided the processor specific
  7. function are already implemented !)
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. { no stack check in system }
  15. {$S-}
  16. unit System;
  17. interface
  18. { include system-independent routine headers }
  19. {$I systemh.inc}
  20. type
  21. THandle = longint;
  22. TThreadID = THandle;
  23. { include heap support headers }
  24. {$I heaph.inc}
  25. {Platform specific information}
  26. const
  27. LineEnding = #10;
  28. LFNSupport = true;
  29. DirectorySeparator = '/';
  30. DriveSeparator = ':';
  31. PathSeparator = ':';
  32. { FileNameCaseSensitive is defined separately below!!! }
  33. maxExitCode = 255;
  34. MaxPathLen = 256;
  35. const
  36. FileNameCaseSensitive : boolean = true;
  37. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  38. sLineBreak : string[1] = LineEnding;
  39. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  40. var
  41. argc : longint;
  42. argv : ppchar;
  43. envp : ppchar;
  44. errno : longint; // MvdV: yuckie
  45. UnusedHandle:longint;
  46. StdInputHandle:longint;
  47. StdOutputHandle:longint;
  48. StdErrorHandle:longint;
  49. implementation
  50. {$I sysfiles.inc}
  51. function sys_unlink (a:cardinal;name:pchar):longint; cdecl; external name 'sys_unlink';
  52. function sys_rename (a:cardinal;p1:pchar;b:cardinal;p2:pchar):longint; cdecl; external name 'sys_rename';
  53. function sys_create_area (name:pchar; var start:pointer; a,b,c,d:longint):longint; cdecl; external name 'sys_create_area';
  54. function sys_resize_area (handle:cardinal; size:longint):longint; cdecl; external name 'sys_resize_area';
  55. function sys_mkdir (a:cardinal; name:pchar; mode:cardinal):longint; cdecl; external name 'sys_mkdir';
  56. function sys_chdir (a:cardinal; name:pchar):longint; cdecl; external name 'sys_chdir';
  57. function sys_rmdir (a:cardinal; name:pchar):longint; cdecl; external name 'sys_rmdir';
  58. {$I system.inc}
  59. {*****************************************************************************
  60. System Dependent Exit code
  61. *****************************************************************************}
  62. procedure prthaltproc;external name '_haltproc';
  63. procedure system_exit;
  64. begin
  65. asm
  66. jmp prthaltproc
  67. end;
  68. End;
  69. {*****************************************************************************
  70. Stack check code
  71. *****************************************************************************}
  72. { cheking the stack is done system independend in 1.1
  73. procedure int_stackcheck(stack_size:longint);[public,alias:'FPC_STACKCHECK'];
  74. {
  75. called when trying to get local stack if the compiler directive $S
  76. is set this function must preserve esi !!!! because esi is set by
  77. the calling proc for methods it must preserve all registers !!
  78. With a 2048 byte safe area used to write to StdIo without crossing
  79. the stack boundary
  80. }
  81. begin
  82. end;
  83. }
  84. {*****************************************************************************
  85. ParamStr/Randomize
  86. *****************************************************************************}
  87. { number of args }
  88. function paramcount : longint;
  89. begin
  90. paramcount := argc - 1;
  91. end;
  92. { argument number l }
  93. function paramstr(l : longint) : string;
  94. begin
  95. if (l>=0) and (l+1<=argc) then
  96. paramstr:=strpas(argv[l])
  97. else
  98. paramstr:='';
  99. end;
  100. { set randseed to a new pseudo random value }
  101. procedure randomize;
  102. begin
  103. {regs.realeax:=$2c00;
  104. sysrealintr($21,regs);
  105. hl:=regs.realedx and $ffff;
  106. randseed:=hl*$10000+ (regs.realecx and $ffff);}
  107. randseed:=0;
  108. end;
  109. {*****************************************************************************
  110. Heap Management
  111. *****************************************************************************}
  112. var myheapstart:pointer;
  113. myheapsize:longint;
  114. myheaprealsize:longint;
  115. heap_handle:longint;
  116. zero:longint;
  117. { function to allocate size bytes more for the program }
  118. { must return the first address of new data space or nil if fail }
  119. function Sbrk(size : longint):pointer;
  120. var newsize,newrealsize:longint;
  121. begin
  122. if (myheapsize+size)<=myheaprealsize then begin
  123. Sbrk:=myheapstart+myheapsize;
  124. myheapsize:=myheapsize+size;
  125. exit;
  126. end;
  127. newsize:=myheapsize+size;
  128. newrealsize:=(newsize and $FFFFF000)+$1000;
  129. if sys_resize_area(heap_handle,newrealsize)=0 then begin
  130. Sbrk:=myheapstart+myheapsize;
  131. myheapsize:=newsize;
  132. myheaprealsize:=newrealsize;
  133. exit;
  134. end;
  135. Sbrk:=nil;
  136. end;
  137. {*****************************************************************************
  138. OS Memory allocation / deallocation
  139. ****************************************************************************}
  140. function SysOSAlloc(size: ptrint): pointer;
  141. begin
  142. result := sbrk(size);
  143. end;
  144. { include standard heap management }
  145. {$I heap.inc}
  146. {****************************************************************************
  147. Low level File Routines
  148. All these functions can set InOutRes on errors
  149. ****************************************************************************}
  150. { close a file from the handle value }
  151. procedure do_close(handle : longint);
  152. begin
  153. { writeln ('CLOSE ',handle);}
  154. if handle<=2 then exit;
  155. InOutRes:=sys_close(handle);
  156. end;
  157. procedure do_erase(p : pchar);
  158. begin
  159. if sys_unlink($FF000000,p)<>0 then InOutRes:=1
  160. else InOutRes:=0;
  161. end;
  162. procedure do_rename(p1,p2 : pchar);
  163. begin
  164. InOutRes:=sys_rename($FF000000,p1,$FF000000,p2);
  165. end;
  166. function do_write(h:longint;addr:pointer;len : longint) : longint;
  167. begin
  168. { if h>0 then begin
  169. sys_write ('WRITE handle=%d ',h);
  170. printf ('addr=%x ',addr);
  171. printf ('len=%d',len);
  172. printf ('%c',10);
  173. end;}
  174. do_write:=sys_write (h,addr,len,zero);
  175. if (do_write<0) then begin
  176. InOutRes:=do_write;
  177. do_write:=0;
  178. end else InOutRes:=0;
  179. end;
  180. function do_read(h:longint;addr:pointer;len : longint) : longint;
  181. begin
  182. { if h>2 then begin
  183. printf ('READ handle=%d ',h);
  184. printf ('addr=%x ',addr);
  185. printf ('len=%d',len);
  186. end;}
  187. do_read:=sys_read (h,addr,len,zero);
  188. if (do_read<0) then begin
  189. InOutRes:=do_read;
  190. do_read:=0;
  191. end else InOutRes:=0;
  192. end;
  193. function do_filepos(handle : longint) : longint;
  194. begin
  195. do_filepos:=sys_lseek(handle,0,1); {1=SEEK_CUR}
  196. if (do_filepos<0) then begin
  197. InOutRes:=do_filepos;
  198. do_filepos:=0;
  199. end else InOutRes:=0;
  200. end;
  201. procedure do_seek(handle,pos : longint);
  202. begin
  203. InOutRes:=sys_lseek(handle,pos,0);
  204. if InOutRes>0 then InOutRes:=0;
  205. end;
  206. function do_seekend(handle:longint):longint;
  207. begin
  208. do_seekend:=sys_lseek (handle,0,2); {2=SEEK_END}
  209. if do_seekend<0 then begin
  210. InOutRes:=do_seekend;
  211. do_seekend:=0;
  212. end else InOutRes:=0;
  213. end;
  214. function do_filesize(handle : longint) : longint;
  215. var cur:longint;
  216. begin
  217. cur:=sys_lseek (handle,0,1); {1=SEEK_CUR}
  218. if cur<0 then begin
  219. InOutRes:=cur;
  220. do_filesize:=0;
  221. exit;
  222. end;
  223. do_filesize:=sys_lseek (handle,0,2); {2=SEEK_END}
  224. if do_filesize<0 then begin
  225. InOutRes:=do_filesize;
  226. do_filesize:=0;
  227. exit;
  228. end;
  229. cur:=sys_lseek (handle,cur,0); {0=SEEK_POS}
  230. if cur<0 then begin
  231. InOutRes:=cur;
  232. do_filesize:=0;
  233. exit;
  234. end;
  235. end;
  236. { truncate at a given position }
  237. procedure do_truncate (handle,pos:longint);
  238. begin
  239. InOutRes:=1;
  240. end;
  241. procedure do_open(var f;p:pchar;flags:longint);
  242. {
  243. filerec and textrec have both handle and mode as the first items so
  244. they could use the same routine for opening/creating.
  245. when (flags and $100) the file will be append
  246. when (flags and $1000) the file will be truncate/rewritten
  247. when (flags and $10000) there is no check for close (needed for textfiles)
  248. }
  249. var m:longint;
  250. mode,h:longint;
  251. begin
  252. { printf ('OPEN %d ',longint(f));
  253. printf (' %s',longint(p));
  254. printf (' %x',flags);}
  255. m:=0;
  256. case (flags and $3) of
  257. $0: begin m:=m or O_RDONLY; mode:=fminput; end;
  258. $1: begin m:=m or O_WRONLY; mode:=fmoutput;end;
  259. $2: begin m:=m or O_RDWR; mode:=fminout; end;
  260. end;
  261. if (flags and $100)<>0 then m:=m or O_APPEND;
  262. if (flags and $1000)<>0 then m:=m or O_TRUNC or O_CREAT;
  263. { if (flags and $10000)<>0 then m:=m or O_TEXT else m:=m or O_BINARY;}
  264. h:=sys_open($FF000000,p,m,0,0);
  265. if h<0 then InOutRes:=h
  266. else InOutRes:=0;
  267. if InOutRes=0 then begin
  268. FileRec(f).handle:=h;
  269. FileRec(f).mode:=mode;
  270. end;
  271. end;
  272. function do_isdevice(handle:THandle):boolean;
  273. begin
  274. do_isdevice:=false;
  275. InOutRes:=0;
  276. end;
  277. {*****************************************************************************
  278. UnTyped File Handling
  279. *****************************************************************************}
  280. {$i file.inc}
  281. {*****************************************************************************
  282. Typed File Handling
  283. *****************************************************************************}
  284. {$i typefile.inc}
  285. {*****************************************************************************
  286. Text File Handling
  287. *****************************************************************************}
  288. {$i text.inc}
  289. {*****************************************************************************
  290. Directory Handling
  291. *****************************************************************************}
  292. procedure mkdir(const s : string);[IOCheck];
  293. var t:string;
  294. begin
  295. t:=s+#0;
  296. InOutRes:=sys_mkdir ($FF000000,@t[1],493);
  297. end;
  298. procedure rmdir(const s : string);[IOCheck];
  299. var t:string;
  300. begin
  301. t:=s+#0;
  302. InOutRes:=sys_rmdir ($FF000000,@t[1]);
  303. end;
  304. procedure chdir(const s : string);[IOCheck];
  305. var t:string;
  306. begin
  307. t:=s+#0;
  308. InOutRes:=sys_chdir ($FF000000,@t[1]);
  309. end;
  310. {*****************************************************************************
  311. getdir procedure
  312. *****************************************************************************}
  313. type dirent = packed record
  314. d_dev:longint;
  315. d_pdev:longint;
  316. d_ino:int64;
  317. d_pino:int64;
  318. d_reclen:word;
  319. d_name:array[0..255] of char;
  320. end;
  321. stat = packed record
  322. dev:longint; {"device" that this file resides on}
  323. ino:int64; {this file's inode #, unique per device}
  324. mode:dword; {mode bits (rwx for user, group, etc)}
  325. nlink:longint; {number of hard links to this file}
  326. uid:dword; {user id of the owner of this file}
  327. gid:dword; {group id of the owner of this file}
  328. size:int64; {size of this file (in bytes)}
  329. rdev:longint; {device type (not used)}
  330. blksize:longint; {preferref block size for i/o}
  331. atime:longint; {last access time}
  332. mtime:longint; {last modification time}
  333. ctime:longint; {last change time, not creation time}
  334. crtime:longint; {creation time}
  335. end;
  336. pstat = ^stat;
  337. function sys_stat (a:cardinal;path:pchar;info:pstat;n:longint):longint; cdecl; external name 'sys_stat';
  338. function FStat(Path:String;Var Info:stat):Boolean;
  339. {
  340. Get all information on a file, and return it in Info.
  341. }
  342. var tmp:string;
  343. var p:pchar;
  344. begin
  345. tmp:=path+#0;
  346. p:=@tmp[1];
  347. FStat:=(sys_stat($FF000000,p,@Info,0)=0);
  348. end;
  349. function sys_opendir (a:cardinal;path:pchar;b:longint):longint; cdecl; external name 'sys_opendir';
  350. function sys_readdir (fd:longint;var de:dirent;a:longint;b:byte):longint; cdecl; external name 'sys_readdir';
  351. function parentdir(fd:longint;dev:longint;ino:int64;var err:longint):string;
  352. var len:longint;
  353. ent:dirent;
  354. name:string;
  355. begin
  356. err:=0;
  357. parentdir:='';
  358. if sys_readdir(fd,ent,$11C,1)=0 then begin
  359. err:=1;
  360. exit;
  361. end;
  362. len:=StrLen(@ent.d_name);
  363. Move(ent.d_name,name[1],len);
  364. name[0]:=chr(len);
  365. { writeln ('NAME: "',name,'" = ',ent.d_dev,',',ent.d_ino);}
  366. if (dev=ent.d_dev) and (ino=ent.d_ino) then begin
  367. err:=0;
  368. parentdir:='/'+name;
  369. exit;
  370. end;
  371. err:=0;
  372. end;
  373. function getdir2:string;
  374. var tmp:string;
  375. info:stat;
  376. info2:stat;
  377. fd:longint;
  378. name:string;
  379. cur:string;
  380. res:string;
  381. err:longint;
  382. begin
  383. res:='';
  384. cur:='';
  385. repeat
  386. FStat(cur+'.',info);
  387. FStat(cur+'..',info2);
  388. { writeln ('"." = ',info.dev,',',info.ino);}
  389. if ((info.dev=info2.dev) and (info.ino=info2.ino)) then begin
  390. if res='' then getdir2:='/' else getdir2:=res;
  391. exit;
  392. end;
  393. tmp:=cur+'..'+#0;
  394. fd:=sys_opendir ($FF000000,@tmp[1],0);
  395. repeat
  396. name:=parentdir(fd,info.dev,info.ino,err);
  397. until (err<>0) or (name<>'');
  398. if err<>0 then begin
  399. getdir2:='';
  400. exit;
  401. end;
  402. res:=name+res;
  403. { writeln(res);}
  404. cur:=cur+'../';
  405. until false;
  406. end;
  407. procedure getdir(drivenr : byte;var dir : shortstring);
  408. begin
  409. drivenr:=0;
  410. dir:=getdir2;
  411. end;
  412. function GetProcessID:SizeUInt;
  413. begin
  414. {$WARNING To be corrected by platform maintainer}
  415. GetProcessID := 1;
  416. end;
  417. {*****************************************************************************
  418. SystemUnit Initialization
  419. *****************************************************************************}
  420. procedure SysInitStdIO;
  421. begin
  422. { Setup stdin, stdout and stderr, for GUI apps redirect stderr,stdout to be
  423. displayed in and messagebox }
  424. StdInputHandle:=0;
  425. StdOutputHandle:=1;
  426. StdErrorHandle:=2;
  427. OpenStdIO(Input,fmInput,StdInputHandle);
  428. OpenStdIO(Output,fmOutput,StdOutputHandle);
  429. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  430. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  431. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  432. end;
  433. begin
  434. { Setup heap }
  435. zero:=0;
  436. myheapsize:=$2000;
  437. myheaprealsize:=$2000;
  438. myheapstart:=nil;
  439. heap_handle:=sys_create_area('fpcheap',myheapstart,0,myheaprealsize,0,3);//!!
  440. if heap_handle>0 then begin
  441. InitHeap;
  442. end else system_exit;
  443. SysInitExceptions;
  444. { Setup IO }
  445. SysInitStdIO;
  446. { Reset IO Error }
  447. InOutRes:=0;
  448. (* This should be changed to a real value during *)
  449. (* thread driver initialization if appropriate. *)
  450. ThreadID := 1;
  451. {$ifdef HASVARIANT}
  452. initvariantmanager;
  453. {$endif HASVARIANT}
  454. {$ifdef HASWIDESTRING}
  455. initwidestringmanager;
  456. {$endif HASWIDESTRING}
  457. end.
  458. {
  459. $Log$
  460. Revision 1.24 2005-05-12 20:29:04 michael
  461. + Added maxpathlen constant (maximum length of filename path)
  462. Revision 1.23 2005/04/13 20:10:50 florian
  463. + TThreadID
  464. Revision 1.22 2005/04/03 21:10:59 hajny
  465. * EOF_CTRLZ conditional define replaced with CtrlZMarksEOF, #26 handling made more consistent (fix for bug 2453)
  466. Revision 1.21 2005/02/14 17:13:21 peter
  467. * truncate log
  468. Revision 1.20 2005/02/01 20:22:49 florian
  469. * improved widestring infrastructure manager
  470. }