Lister les lecteurs

5 avril 2009 - 322 mots - purebasic

Voici un code qui vous permettra de lister les différents lecteurs possibles de votre disque.

  • Systèmes :
    • Windows
    • Linux
    • MacOs
  • PureBasic 4.30
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Linux ;{
    Structure mntent
      mnt_fsname.s
      mnt_dir.s
      mnt_type.s
      mnt_opts.s
      mnt_freq.l
      mnt_passno.l
    EndStructure
  ;}
CompilerEndSelect
Structure S_Drives
  drive.s{255}
  options.s{255}
  
  unix_fsname.s{255}
  unix_type.s{255}
  unix_freq.l
  unix_passno.l
  
  windows_type.l
EndStructure
Structure S_ListDrives
  nb.l
  ptrMem.l
EndStructure
ProcedureDLL System_GetDrives()
  Protected Dim MyDrives.S_Drives(0)
  Protected *ListDrives.S_ListDrives = AllocateMemory(SizeOf(S_ListDrives))
  Protected lSize.l

  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows ;{
      Protected sDrives.s = Space(255)
      Protected lRes.l = GetLogicalDriveStrings_(255, sDrives)
      Protected lOffset.l
      Repeat
        MyDrives(lSize)\drive = PeekS(@sDrives+lOffset,4)
        MyDrives(lSize)\windows_type = GetDriveType_(@MyDrives(lSize)\drive)
        lOffset + 4
        lSize +1
        ReDim MyDrives.S_Drives(lSize)
      Until PeekS(@sDrives+lOffset,4)=""
    ;}
    CompilerCase #PB_OS_Linux ;{
      ; http://man.developpez.com/man3/getmntent.3.php
      Protected hMtab.l, hPointer.l
      Protected *info.mntent
    
      hMtab = setmntent_("/etc/mtab", "r")
      If hMtab
        hPointer = getmntent_(hMtab)
        While hPointer
          *info = hPointer
          
          MyDrives(lSize)\drive = *info\mnt_dir
          MyDrives(lSize)\options = *info\mnt_opts
          MyDrives(lSize)\unix_fsname = *info\mnt_fsname
          MyDrives(lSize)\unix_type = *info\mnt_type
          MyDrives(lSize)\unix_freq = *info\mnt_freq
          MyDrives(lSize)\unix_passno = *info\mnt_passno
          
          lSize +1
          ReDim MyDrives.S_Drives(lSize)
          hPointer = getmntent_(hMtab)
        Wend
        endmntent_(hMtab)
      Else
        ProcedureReturn #False
      EndIf 
    ;}
  CompilerEndSelect

  *ListDrives\nb = lSize
  *ListDrives\ptrMem = AllocateMemory(lSize * SizeOf(S_Drives))
  MoveMemory(@MyDrives(), *ListDrives\ptrMem, lSize * SizeOf(S_Drives))
  ProcedureReturn *ListDrives
EndProcedure

lMem = System_GetDrives()

Define.S_ListDrives DrivesListing
MoveMemory(lMem, @DrivesListing, SizeOf(S_ListDrives))
Dim Drives.S_Drives(DrivesListing\nb)

MoveMemory(DrivesListing\ptrMem, @Drives(), DrivesListing\nb * SizeOf(S_Drives))

For Inc = 0 To DrivesListing\nb -1
  Debug "Drive <" + Drives(Inc)\drive
  Debug "Options < " + Drives(Inc)\options
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux ;{
      Debug Drives(Inc)\unix_fsname
    ;}
    CompilerCase #PB_OS_Windows ;{
      Debug "Type < "+Str(Drives(Inc)\windows_type)
    ;}
  CompilerEndSelect
  
Next

Laisser un commentaire

Merci. Votre message a bien été enregistré.