suuid.inc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. {
  2. $Id: sysutils.pp,v 1.59 2005/03/25 22:53:39 jonas Exp $
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Florian Klaempfl
  5. member of the Free Pascal development team
  6. Sysutils unit for linux
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {$IF DEFINED(FREEBSD) AND NOT DEFINED(FREEBSD4)}
  14. {$IFDEF FPC_USE_LIBC}
  15. function cuuidgen(p:PGUID;x:cint):cint; external clib name 'uuidgen';
  16. {$ELSE}
  17. function cuuidgen(p:pguid;x:cint):cint;
  18. begin
  19. result:=do_syscall(syscall_nr_uuidgen,tsysparam(p),tsysparam(x));
  20. end;
  21. {$ENDIF}
  22. Function SysCreateGUID(out GUID : TGUID) : Integer;
  23. begin
  24. result:=0;
  25. if cuuidgen(@guid,1)=-1 then
  26. GetRandomBytes(GUID,SizeOf(Guid));
  27. end;
  28. {$ELSE}
  29. Const
  30. RandomDevice = '/dev/urandom';
  31. Function GetURandomBytes(Var Buf; NBytes : Integer) : Boolean;
  32. Var
  33. fd,I : Integer;
  34. P : PByte;
  35. begin
  36. P:=@Buf;
  37. fd:=FileOpen(RandomDevice,fmOpenRead or fmShareDenyNone);
  38. Result:=(fd>=0);
  39. if Result then
  40. Try
  41. While (NBytes>0) do
  42. begin
  43. I:=FileRead(fd,P^,nbytes);
  44. If I>0 then
  45. begin
  46. Inc(P,I);
  47. Dec(NBytes,I);
  48. end;
  49. end;
  50. Finally
  51. FileClose(Fd);
  52. end;
  53. end;
  54. Function SysCreateGUID(out GUID : TGUID) : Integer;
  55. begin
  56. if not GetUrandomBytes(Guid,SizeOf(GUID)) then
  57. GetRandomBytes(GUID,SizeOf(Guid));
  58. Result:=0;
  59. end;
  60. {$ENDIF}