~ubuntu-branches/ubuntu/feisty/fpc/feisty

« back to all changes in this revision

Viewing changes to rtl/linux/suuid.inc

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-01-27 20:08:50 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070127200850-9mrptaqqjsx9nwa7
Tags: 2.0.4-5
* Fixed Build-Depends.
* Add myself to Uploaders in debian/control.
* Make sure that the sources are really patched before building them.
* Build unit 'libc' on powerpc too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
7
    Sysutils unit for linux
 
8
 
 
9
    See the file COPYING.FPC, included in this distribution,
 
10
    for details about the copyright.
 
11
 
 
12
    This program is distributed in the hope that it will be useful,
 
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
15
 
 
16
 **********************************************************************}
 
17
 
 
18
 
 
19
Const 
 
20
  KernelUUID       = '/proc/sys/kernel/random/uuid';
 
21
 
 
22
 
 
23
Procedure GetURandomBytes(Var Buf; NBytes : Integer);
 
24
 
 
25
Var
 
26
  fd,I : Integer;
 
27
  P : PByte;
 
28
  
 
29
begin
 
30
  P:=@Buf;
 
31
  fd:=FileOpen('/dev/urandom',fmOpenRead);
 
32
  if (fd>=0) then
 
33
    Try
 
34
      While (NBytes>0) do
 
35
        begin
 
36
        I:=FileRead(fd,P^,nbytes);
 
37
        If I>0 then
 
38
          begin
 
39
          Inc(P,I);
 
40
          Dec(NBytes,I);
 
41
          end;
 
42
        end;  
 
43
    Finally
 
44
      FileClose(Fd);
 
45
    end
 
46
  else
 
47
    GetRandomBytes(Buf,NBytes);
 
48
end;
 
49
 
 
50
 
 
51
Function CreateKernelGUID(Var GUID : TGUID) : Boolean;
 
52
 
 
53
Const
 
54
  UUIDLen = 36;
 
55
 
 
56
Var
 
57
  fd: Longint;
 
58
  S : String;
 
59
  
 
60
begin
 
61
  fd:=FileOpen(KernelUUID,fmOpenRead);
 
62
  Result:=(Fd>=0);
 
63
  if Result then
 
64
    begin
 
65
    SetLength(S,UUIDLen);
 
66
    SetLength(S,FileRead(fd,S[1],UUIDLen));
 
67
    Result:=(Length(S)=UUIDLen);
 
68
    If Result then
 
69
      begin
 
70
      GUID:=StringToGUID('{'+S+'}');
 
71
      //Writeln('Kernel ID = ',GuidToString(GUID));
 
72
      end;
 
73
    end;
 
74
end;
 
75
 
 
76
Function SysCreateGUID(out GUID : TGUID) : Integer;
 
77
 
 
78
begin
 
79
  if not CreateKernelGUID(Guid) then
 
80
    GetRandomBytes(GUID,SizeOf(Guid));  
 
81
  Result:=0;    
 
82
end;
 
83