initc.pp 1.9 KB

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