initc.pp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. {
  2. $Id$
  3. }
  4. unit initc;
  5. interface
  6. { 0.99.12 had a bug that initialization/finalization only worked for
  7. objfpc,delphi mode }
  8. {$ifdef VER0_99_12}
  9. {$mode objfpc}
  10. {$endif}
  11. {$LINKLIB cygwin}
  12. {$linklib kernel32}
  13. { this unit is just ment to run
  14. startup code to get C code to work correctly PM }
  15. implementation
  16. {$i textrec.inc}
  17. procedure cygwin_crt0(p : pointer);cdecl;external;
  18. {
  19. procedure do_global_dtors;cdecl;external;
  20. this does not work because
  21. do_global_dtors is a static C function PM
  22. it is inserted into the atexit chain,
  23. but how do we call this from FPC ???
  24. it seems to be done in exit function
  25. but that one ends with _exit that is system dependent !! }
  26. { avoid loading of cygwin _exit code
  27. so that exit returns }
  28. procedure _exit(status : longint);cdecl;
  29. begin
  30. end;
  31. procedure C_exit(status : longint);popstack;external name '_exit';
  32. const
  33. STD_INPUT_HANDLE = $fffffff6;
  34. STD_OUTPUT_HANDLE = $fffffff5;
  35. STD_ERROR_HANDLE = $fffffff4;
  36. function GetStdHandle(nStdHandle:DWORD):longint;external 'kernel32' name 'GetStdHandle';
  37. procedure UpdateStdHandle(var t:TextRec;var stdHandle:longint;newHandle:longint);
  38. { Check if the stdHandle is the same as the one in the TextRec, then
  39. also update the TextRec }
  40. begin
  41. if t.Handle=stdHandle then
  42. t.Handle:=newHandle;
  43. stdHandle:=newHandle;
  44. end;
  45. initialization
  46. cygwin_crt0(nil);
  47. { Reinitialize std handles that can be changed }
  48. UpdateStdHandle(TextRec(Input),StdInputHandle,GetStdHandle(STD_INPUT_HANDLE));
  49. UpdateStdHandle(TextRec(Output),StdOutputHandle,GetStdHandle(STD_OUTPUT_HANDLE));
  50. TextRec(StdOut).Handle:=StdOutputHandle;
  51. UpdateStdHandle(TextRec(Stderr),StdErrorHandle,GetStdHandle(STD_ERROR_HANDLE));
  52. finalization
  53. { should we pass exit code ?
  54. its apparently only used by _exit so it doesn't matter PM }
  55. C_exit(0);
  56. end.
  57. {
  58. $Log$
  59. Revision 1.3 2000-12-30 17:48:36 peter
  60. * update std handles after initing c
  61. Revision 1.2 2000/07/13 11:33:57 michael
  62. + removed logs
  63. }