VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
Begin VB.Form Form1 
   Caption         =   " Singlix"
   ClientHeight    =   2925
   ClientLeft      =   165
   ClientTop       =   450
   ClientWidth     =   6405
   Icon            =   "frmDiskFSFD.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   2925
   ScaleWidth      =   6405
   StartUpPosition =   1  'CenterOwner
   Begin MSComDlg.CommonDialog CommonDialog1 
      Left            =   0
      Top             =   120
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
      CancelError     =   -1  'True
   End
   Begin VB.CommandButton Command3 
      Caption         =   "File Info"
      Height          =   375
      Left            =   3240
      TabIndex        =   2
      Top             =   2280
      Width           =   1335
   End
   Begin VB.CommandButton Command4 
      Caption         =   "Exit"
      Height          =   375
      Left            =   4920
      TabIndex        =   3
      Top             =   2280
      Width           =   1335
   End
   Begin VB.CommandButton Command2 
      Caption         =   "Image File Info"
      Height          =   375
      Left            =   1680
      TabIndex        =   1
      Top             =   2280
      Width           =   1335
   End
   Begin VB.CommandButton Command1 
      Caption         =   "FS Info"
      Height          =   375
      Left            =   120
      TabIndex        =   0
      Top             =   2280
      Width           =   1335
   End
   Begin VB.Label Label1 
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   162
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   1695
      Left            =   120
      TabIndex        =   4
      Top             =   240
      Width           =   6255
   End
   Begin VB.Menu fsfdImage 
      Caption         =   "FS Drive"
      Begin VB.Menu fsfdNewImage 
         Caption         =   "New Singlix FS"
      End
      Begin VB.Menu fsfdSeparator 
         Caption         =   "-"
      End
      Begin VB.Menu fdfdExit 
         Caption         =   "Exit"
      End
   End
   Begin VB.Menu WinFile 
      Caption         =   "Windows File"
      Begin VB.Menu WinOpenFile 
         Caption         =   "Open Windows File"
      End
      Begin VB.Menu WinSaveFile 
         Caption         =   "Save as Windows File"
      End
   End
   Begin VB.Menu fsFile 
      Caption         =   "FS File"
      Begin VB.Menu fsOpenFile 
         Caption         =   "Open FS File"
      End
      Begin VB.Menu fsOpenNextFile 
         Caption         =   "Open Next FS File"
      End
      Begin VB.Menu fsSaveFile 
         Caption         =   "Save as FS File"
      End
      Begin VB.Menu fsSaveStartupFile 
         Caption         =   "Save as FS Startup File"
      End
      Begin VB.Menu fsDeleteFile 
         Caption         =   "Delete FS File"
      End
      Begin VB.Menu fsMoveFile 
         Caption         =   "Move FS File"
      End
      Begin VB.Menu fsRenameFile 
         Caption         =   "Rename FS File"
      End
      Begin VB.Menu fsStartupFileInfo 
         Caption         =   "FS Startup File Info"
      End
      Begin VB.Menu fsDeleteStartupFile 
         Caption         =   "Delete FS Startup File"
      End
   End
   Begin VB.Menu fsDir 
      Caption         =   "FS Directory"
      Begin VB.Menu fsCurDir 
         Caption         =   "Current FS Directory"
      End
      Begin VB.Menu fsChDir 
         Caption         =   "Change FS Directory"
      End
      Begin VB.Menu fsMkDir 
         Caption         =   "Make FS Directory"
      End
      Begin VB.Menu fsDirList 
         Caption         =   "Current FS Directory List"
      End
      Begin VB.Menu fsRmDir 
         Caption         =   "Delete FS Directory"
      End
      Begin VB.Menu fsMvDir 
         Caption         =   "Move FS Directory"
      End
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Dim X As Integer
Dim Y As Byte
Dim L As Integer
Dim VolumeName As String
Dim Satir As String
Dim MAT_Location As Double
Dim S1 As Double
Dim S2 As Long
Dim S3 As Long
Dim S4 As Integer
Dim ReturnCode As Long
Dim RDT_Location As Double
Dim VolSerialNoStr As String
Dim Volume_Size As Double
Dim Free_Sectors As Double
Dim ImageFileNumber As Integer
Dim WinFileName As String
Dim WinFileSize As Double
Dim WinFileCDateTime As String
Dim FS_DirSerial1 As Long
Dim FS_DirSerial2 As Long
Dim DAT_Location As Double
Dim DAT_SCount As Long
Dim FreeSpace As Double
Dim RequiredSpace As Double
Dim FS_FileName As String
Dim FS_DirName As String
Dim SubDirName As String
Dim K As Double
Dim SectorCount As Double
Dim BeginningSector As Double
Dim FDT_Location As Double
Dim FS_Second As Integer
Dim FS_Minute As Integer
Dim FS_Hour As Integer
Dim FS_Day As Integer
Dim FS_Month As Integer
Dim FS_Year As Integer
Dim FS_Century As Integer
Dim WinFileNumber As Integer
Dim R As Integer
Dim AttributeStr As String
Dim DDT_Location As Double


Private Sub Command1_Click()
MousePointer = 11
Call LoadFSImage
MousePointer = 0
End Sub

Private Sub Command2_Click()
If ImageFileName = "" Then
   GoTo ImgNotLoaded
End If
If Dir(ImageFileName) <> "" Then
   ImageFileSize = FileLen(ImageFileName)
   ImageFileCDateTime = FileDateTime(ImageFileName)
   Label1.Caption = "Image File Name : " + ImageFileName + Chr$(13) + Chr$(10) + "Image File Size : " + CStr(ImageFileSize) + " bytes" + Chr$(13) + Chr$(10) + "File Date and Time : " + ImageFileCDateTime
Else
   ImageFileName = ""
ImgNotLoaded:
   Label1.Caption = "FS Image File : NONE" + Chr$(13) + Chr$(10) + Chr$(13) + Chr$(10)
   If DriveName = "A:" Then
      Label1.Caption = Label1.Caption + "FS Drv Name : fd0 (A:)"
   Else
      If DriveName = "B:" Then
         Label1.Caption = Label1.Caption + "FS Drv Name : fd1 (B:)"
      End If
   End If
End If
End Sub

Private Sub Command3_Click()
If WinFileName <> "" Then
   WinFileSize = FileLen(WinFileName)
   WinFileCDateTime = FileDateTime(WinFileName)
   Label1.Caption = "Windows File Name : " + WinFileName + Chr$(13) + Chr$(10) + "Windows File Size : " + CStr(WinFileSize) + " bytes" + Chr$(13) + Chr$(10) + "File Date and Time : " + WinFileCDateTime
Else
   Label1.Caption = "Windows File : NONE"
End If
End Sub

Private Sub Command4_Click()
End
End Sub

Private Sub fdfdExit_Click()
End
End Sub

Private Sub Form_Activate()
Randomize Timer
End Sub

Private Sub Form_Load()
Call RDT_Reset
FS_CDIR = "/"
LoadFSImage
End Sub

Private Sub fsChDir_Click()
Rem Temporary !!!
Satir = InputBox("Directory Name", "Change FS Directory")
Satir = Trim$(Satir)
If Satir <> "" Then
   K = ChangeDirectory(0, Satir)
   If K > 0 Then
      MsgBox ("Current directory has been changed to '" + FS_CDIR + "' ..."), 0, " Change FS Directory"
      Label1.Caption = "FS Directory : " + FS_CDIR
   Else
      MsgBox ("Directory not found !")
   End If
End If
End Sub

Private Sub fsCurDir_Click()
If Trim$(FS_CDIR) <> "" Then
   Label1.Caption = "FS Directory : " + FS_CDIR
Else
   Label1.Caption = "FS Directory : NONE"
End If
End Sub

Private Sub fsDeleteFile_Click()
Satir = InputBox("FS File Name :", " Delete FS File")
Satir = Trim$(Satir)
L = Len(Satir)
If L < 1 Then Exit Sub
K = FindFirstFile(0, Satir)
If K > 0 Then
   ReturnCode = ReadSector(DriveName, K, 1, FDT())
   If ReturnCode = 0 Then
      If FDT(0) = AscB("F") And FDT(1) = AscB("D") And FDT(2) = AscB("T") Then
         Call SetOpenFileStructure(FDT())
      Else
         MsgBox ("File could not be found!"), 0, " Delete FS File"
         Exit Sub
      End If
   End If
Else
   MsgBox ("File could not be found!"), 0, " Delete FS File"
   Exit Sub
End If
Rem If FD.SectorCount < 1 Then Exit Sub
FS_FileName = Trim$(FD.FileName)
If MsgBox("Do you want to delete file '" + FS_FileName + "' ?", 36, " Delete FS File") <> 6 Then Exit Sub
FS_DirNumber = FD.ParentDirNumber
FS_DirSerial1 = FD.ParentDirSerial1
FS_DirSerial2 = FD.ParentDirSerial2
Rem If FS_DirNumber < 1 Then Exit Sub
ANS:
Rem Erased/Deleted Sign
FDT(2) = AscB("E")
ReturnCode = WriteSector(DriveName, K, 1, FDT())
If ReturnCode <> 0 Then
   MsgBox ("Writing Error !"), 48, " Writing FDT Sector"
   Exit Sub
End If
FDT_Location = FD.FileNumber
SectorCount = FD.SectorCount
FreeSpace = Free_Sectors + SectorCount + 1
ReturnCode = ReadSector(DriveName, DAT_Location, DAT_SCount, DAT())
If ReturnCode <> 0 Then
   MsgBox ("Reading Error !"), 48, " Reading DAT Sectors"
   Exit Sub
End If
For K = FDT_Location To FDT_Location + SectorCount
    L = CInt(K)
    DAT(L) = &H90
Next K
ReturnCode = WriteSector(DriveName, DAT_Location, DAT_SCount, DAT())
If ReturnCode <> 0 Then
   MsgBox ("Writing Error !"), 48, " Writing DAT Sectors"
   Exit Sub
End If
Free_Sectors = FreeSpace
ReturnCode = ReadSector(DriveName, MAT_Location, 1, MAT())
If ReturnCode <> 0 Then
   MsgBox ("Reading Error !"), 48, " Reading MAT Sector"
   Exit Sub
End If
Rem FreeSectors
K = FreeSpace
L = CInt(K Mod 256)
MAT(20) = CByte(L)
K = (K - L) / 256
L = K Mod 256
MAT(21) = CByte(L)
K = (K - L) / 256
L = K Mod 256
MAT(22) = CByte(L)
K = (K - L) / 256
MAT(23) = CByte(L)
Rem FirstFreeSector
S1 = CDbl(MAT(27))
S1 = 16777216 * S1
S2 = CLng(MAT(26))
S2 = 65536 * S2
S3 = CLng(MAT(25))
S3 = S3 * 256
S4 = CInt(MAT(24))
K = S1 + S2 + S3 + S4
If K > FDT_Location Then
   K = FDT_Location
   L = CInt(K Mod 256)
   MAT(24) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   MAT(25) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   MAT(26) = CByte(L)
   K = (K - L) / 256
   MAT(27) = CByte(K)
End If
ReturnCode = WriteSector(DriveName, MAT_Location, 1, MAT())
If ReturnCode <> 0 Then
   MsgBox ("Writing Error !"), 48, " Writing MAT Sector"
   Exit Sub
End If
SectorCount = SectorCount + FD.SectorCount + 1
K = FD.NextFDTNumber
If K > 0 Then
   ReturnCode = ReadSector(DriveName, K, 1, FDT())
   If ReturnCode <> 0 Then Exit Sub
   If FDT(0) = AscB("F") And FDT(1) = AscB("D") And FDT(2) = AscB("T") Then
      Call SetOpenFileStructure(FDT())
   End If
   GoTo ANS
End If
Rem Deleting Entry On Directory Data Sector
Dim Z As Long
K = FS_DirNumber
BNS:
ReturnCode = ReadSector(DriveName, K, 1, DDT())
If ReturnCode <> 0 Then
   MsgBox ("Reading Error !"), 48, " Reading DDT Sector"
   Exit Sub
End If
Call SetOpenDirStructure(DDT())
If DD.DDTSign <> "DDT" And DD.DDTSign <> "RDT" Then
   MsgBox ("Invalid Directory Descriptor Table !"), 48, " Reading DDT Sector"
   Exit Sub
End If
Rem If DD.DirectorySerial1 <> FS_DirSerial1 Or DD.DirectorySerial2 <> FS_DirSerial2 Then
  Rem  MsgBox ("Wrong Directory Serial Number !"), 48, " Reading DDT Sector"
  Rem  Exit Sub
Rem End If
K = DD.DirectoryNumber + 1
SectorCount = DD.SectorCount
Z = 1
CNS:
ReturnCode = ReadSector(DriveName, K, Z, SD_Data())
If ReturnCode <> 0 Then
   MsgBox ("Reading Error !"), 48, " Reading Directory Data"
   Exit Sub
End If
For R = 0 To 511 Step 4
    S1 = CDbl(SD_Data(R + 3))
    S1 = 16777216 * S1
    S2 = CLng(SD_Data(R + 2))
    S2 = 65536 * S2
    S3 = CLng(SD_Data(R + 1))
    S3 = S3 * 256
    S4 = CInt(SD_Data(R))
    K = S1 + S2 + S3 + S4
    If FDT_Location = K Then
       Rem Deleting/Erasing Directory Entry
       SD_Data(R) = 255
       SD_Data(R + 1) = 255
       SD_Data(R + 2) = 255
       SD_Data(R + 3) = 255
       Exit For
    Else
       K = 0
    End If
Next R
If K > 0 Then
   K = DD.DirectoryNumber + 1
   ReturnCode = WriteSector(DriveName, K, Z, SD_Data())
   If ReturnCode <> 0 Then
      MsgBox ("Writing Error !"), 48, " Writing Directory Data"
   Else
      MsgBox ("'" + FS_FileName + "' has been deleted..."), 0, " Delete FS File"
      Call UpdateFSInfo
   End If
Else
   If Z < SectorCount Then
      Z = Z + 1
      GoTo CNS
   Else
      K = DD.NextDDTNumber
      If K > 0 Then
         GoTo BNS
      Else
         MsgBox ("Directory Entry could not be found !"), 48, " Delete FS File [ Error ]"
      End If
   End If
End If
End Sub

Private Sub fsDirList_Click()
If FS_CDIR = "/" Then
   ReturnCode = ReadSector(DriveName, FS_DirNumber + 1, 1, RD_Data())
Else
   ReturnCode = ReadSector(DriveName, FS_DirNumber + 1, 1, SD_Data())
End If
R = 0
If FS_CDIR = "/" Then
   For X = 0 To 511 Step 4
       S1 = CDbl(RD_Data(X + 3))
       S1 = 16777216 * S1
       S2 = CLng(RD_Data(X + 2))
       S2 = 65536 * S2
       S3 = CLng(RD_Data(X + 1))
       S3 = S3 * 256
       S4 = CInt(RD_Data(X))
       FDT_Location = S1 + S2 + S3 + S4
       If FDT_Location > 0 Then
          If FDT_Location < 4294967295# Then
             Call FillFlexGridCells
          End If
       Else
         Exit For
       End If
   Next X
Else
   For X = 0 To 511 Step 4
       S1 = CDbl(SD_Data(X + 3))
       S1 = 16777216 * S1
       S2 = CLng(SD_Data(X + 2))
       S2 = 65536 * S2
       S3 = CLng(SD_Data(X + 1))
       S3 = S3 * 256
       S4 = CInt(SD_Data(X))
       FDT_Location = S1 + S2 + S3 + S4
       If FDT_Location > 0 Then
          If FDT_Location < 4294967295# Then
             Call FillFlexGridCells
          End If
       Else
          Exit For
       End If
   Next X
End If
frmListe.Show
End Sub

Private Sub fsfdNewImage_Click()
If MsgBox("Do you want to build a new Singlix File System on drive " + DriveName + " ?", 36, " New Singlix File System") <> 6 Then Exit Sub
Label1.Caption = "Building FS Boot Sector ..."
Call BS_reset
VolumeName = ""
Repeat1:
VolumeName = InputBox("Operation System Name :", " New Singlix File System", VolumeName)
VolumeName = Trim$(VolumeName)
L = Len(VolumeName)
If L > 0 Then
   If L > 16 Then
      MsgBox ("Operation system name must not be more then 16 bytes !"), 48, " SINGLIX FS SETUP"
      GoTo Repeat1
   End If
   For X = 1 To L
       BS(43 + X) = AscB(Mid(VolumeName, X, 1))
   Next X
   If L < 16 Then
      For X = L + 1 To 16
          BS(43 + X) = 0
      Next X
   End If
Else
   For X = 1 To 16
       Y = BS(43 + X)
       If Asc(Y) > 0 Then
          VolumeName = VolumeName + Chr$(Y)
       Else
          Exit For
       End If
   Next X
End If
MsgBox ("FS Operation System Name: " + VolumeName), 0, " New Singlix File System"
ReturnCode = WriteSector(DriveName, 0, 1, BS())
Label1.Caption = Label1.Caption + " OK..." + Chr$(13) + Chr$(10)
X = Second(Now)
Do While X = Second(Now)
Loop
Label1.Caption = Label1.Caption + "Building Disk Allocation Table ..."
X = Second(Now)
Do While X = Second(Now)
Loop
Call MAT_Reset
ReturnCode = WriteSector(DriveName, 1, 1, MAT())
Call DAT_Reset
ReturnCode = WriteSector(DriveName, 2, 6, DAT())
Label1.Caption = Label1.Caption + " OK..." + Chr$(13) + Chr$(10)
X = Second(Now)
Do While X = Second(Now)
Loop
Label1.Caption = Label1.Caption + "Building Root Directory ..."
Call RDT_Reset
VolumeName = ""
Repeat2:
VolumeName = InputBox("FS Volume Name :", " New Singlix File System", VolumeName)
VolumeName = Trim$(VolumeName)
L = Len(VolumeName)
If L > 0 Then
   If L > 64 Then
      MsgBox ("Volume name must not be more then 64 bytes !"), 48, " SINGLIX FS SETUP"
      GoTo Repeat2
   End If
   For X = 1 To L
       RDT(63 + X) = AscB(Mid(VolumeName, X, 1))
   Next X
   If L < 64 Then
      For X = L + 1 To 64
          RDT(63 + X) = 0
      Next X
   End If
Else
   For X = 1 To 64
       Y = RDT(63 + X)
       If Asc(Y) > 0 Then
          VolumeName = VolumeName + Chr$(Y)
       Else
          Exit For
       End If
   Next X
End If
MsgBox ("FS Volume Name: " + VolumeName), 0, " New Singlix File System"
ReturnCode = WriteSector(DriveName, 8, 1, RDT())
Call RD_Data_Reset
ReturnCode = WriteSector(DriveName, 9, 1, RD_Data())
Label1.Caption = Label1.Caption + " OK..." + Chr$(13) + Chr$(10)
X = Second(Now)
Do While X = Second(Now)
Loop
Rem Label1.Caption = Label1.Caption + "Building Data Sectors ..."
Rem Y = &HF6
Rem For X = 0 To 511
Rem    File_Data(X) = Y
Rem Next X
Rem For X = 1 To 2870
Rem ReturnCode = WriteSector(DriveName, 9 + X, 1, File_Data())
Rem Next X
Rem Label1.Caption = Label1.Caption + " OK..." + Chr$(13) + Chr$(10)
X = Second(Now)
Do While X = Second(Now)
Loop
MsgBox ("Singlix File System has been built..." + Chr$(13) + Chr$(10) + "File System Size: 2880 sectors, 512 bytes/sector" + Chr$(13) + Chr$(10) + "File System Type : FS1, 1.44 MB Floppy Disk Image"), 0, " New Singlix File System Image File Name : " + "FSFD.IMG"
End Sub

Private Sub fsfdOpenFSImage()
CommonDialog1.DialogTitle = "OPEN FS IMAGE FILE"
CommonDialog1.FileName = Chr(0)
CommonDialog1.Filter = "All files (*.*)|*.*|IMG files (*.img)|*.img|DAT files (*.dat)|*.dat|BIN files (*.bin)|*.bin"
CommonDialog1.Flags = OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY Or OFN_EXPLORER
On Error Resume Next
CommonDialog1.ShowOpen
If Err = 0 Then
   ImageFileName = CommonDialog1.FileName
   ImageFileSize = FileLen(ImageFileName)
   If ImageFileSize < 1 Then
      ImageFileName = ""
   End If
Else
   If Err <> 32755 Then
      ImageFileName = ""
      GoTo ErrMsg
   Else
      Label1.Caption = "Open FS Image File : NONE"
      MsgBox ("SINGLIX FS Image File could not open!..."), 48, "! ERROR !"
      Label1.Caption = ""
      ImageFileName = ""
      Exit Sub
   End If
End If
If ImageFileName <> "" Then
   ImageFileSize = FileLen(ImageFileName)
   ImageFileCDateTime = FileDateTime(ImageFileName)
   Label1.Caption = "FS Image File Name : " + ImageFileName + Chr$(13) + Chr$(10) + "FS Image File Size : " + CStr(ImageFileSize) + " bytes" + Chr$(13) + Chr$(10) + "Image File Date and Time : " + ImageFileCDateTime
Else
   Label1.Caption = "Open FS Image File : NONE"
   MsgBox ("SINGLIX FS Image File could not open!..." + Chr$(13) + Chr$(13) + "Error Number: " + Str$(Err)), 48, "! ERROR !"
   Label1.Caption = ""
End If
ImageFileName = Dir(ImageFileName)
If FileLen(ImageFileName) / 512 <> 2880 Then
   MsgBox ("FS Image file is not valid !"), 48, " Open SINGLIX Image"
   ImageFileName = ""
Else
   Call LoadFSImage
End If
Exit Sub
ErrMsg:
Beep
MsgBox ("SINGLIX FS Image File could not open!..." + Chr$(13) + Chr$(13) + "Error Number: " + Str$(Err)), 48, "! ERROR !"
Label1.Caption = ""
End Sub

Private Sub LoadFSImage()
Rem On Error Resume Next
   ReturnCode = ReadSector(DriveName, 0, 1, BS())
   If ReturnCode <> 0 Then
      MsgBox ("Reading Error !"), 48, " Reading Boot Sector"
      Exit Sub
   End If
   VolumeName = ""
   For X = 1 To 16
       Y = BS(47 + X)
       If Asc(Y) > 0 Then
          VolumeName = VolumeName + Chr$(Y)
       Else
          Exit For
       End If
   Next X
   Label1.Caption = "Operation System Name : " + VolumeName
   S1 = CDbl(BS(27))
   S1 = 16777216 * S1
   S2 = CLng(BS(26))
   S2 = 65536 * S2
   S3 = CLng(BS(25))
   S3 = S3 * 256
   S4 = CInt(BS(24))
   Rem Sector
   MAT_Location = S1 + S2 + S3 + S4
   ReturnCode = ReadSector(DriveName, MAT_Location, 1, MAT())
   If ReturnCode <> 0 Then
      MsgBox ("Reading Error !"), 48, " Reading MAT Sector"
      Exit Sub
   End If
   S1 = CDbl(MAT(15))
   S1 = 16777216 * S1
   S2 = CLng(MAT(14))
   S2 = 65536 * S2
   S3 = CLng(MAT(13))
   S3 = S3 * 256
   S4 = CInt(MAT(12))
   Rem Sector
   DAT_Location = S1 + S2 + S3 + S4
   S1 = CDbl(MAT(19))
   S1 = 16777216 * S1
   S2 = CLng(MAT(18))
   S2 = 65536 * S2
   S3 = CLng(MAT(17))
   S3 = S3 * 256
   S4 = CInt(MAT(16))
   Rem Sector Count
   DAT_SCount = CLng(S1 + S2 + S3 + S4)
   ReturnCode = ReadSector(DriveName, DAT_Location, DAT_SCount, DAT())
   If ReturnCode <> 0 Then
      MsgBox ("Reading Error !"), 48, " Reading DAT Sectors"
      Exit Sub
   End If
   S1 = CDbl(BS(31))
   S1 = 16777216 * S1
   S2 = CLng(BS(30))
   S2 = 65536 * S2
   S3 = CLng(BS(29))
   S3 = S3 * 256
   S4 = CInt(BS(28))
   Rem Sector
   RDT_Location = S1 + S2 + S3 + S4
   Rem CHDIR
   RDT(0) = 0
   RDT(1) = 0
   RDT(2) = 0
   ReturnCode = ReadSector(DriveName, RDT_Location, 1, RDT())
   If ReturnCode <> 0 Then
      MsgBox ("Reading Error !"), 48, " Reading RDT Sector"
      Exit Sub
   End If
   VolumeName = ""
   For X = 1 To 64
       Y = RDT(63 + X)
       If Asc(Y) > 0 Then
          VolumeName = VolumeName + Chr$(Y)
       Else
          Exit For
       End If
   Next X
   Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + Chr$(13) + Chr$(10) + "FS Volume Name : " + VolumeName
   VolSerialNoStr = ByteToHex(RDT(31)) + ByteToHex(RDT(30)) + "-" + ByteToHex(RDT(29)) + ByteToHex(RDT(28))
   Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "Volume Serial No : " + VolSerialNoStr
   S1 = CDbl(MAT(7))
   S1 = 16777216 * S1
   S2 = CLng(MAT(6))
   S2 = 65536 * S2
   S3 = CLng(MAT(5))
   S3 = S3 * 256
   S4 = CInt(MAT(4))
   Volume_Size = S1 + S2 + S3 + S4
   Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + Chr$(13) + Chr$(10) + "Volume Size : " + CStr(Volume_Size) + " sectors  ( " + CStr(512 * Volume_Size) + " bytes )"
   S1 = CDbl(MAT(23))
   S1 = 16777216 * S1
   S2 = CLng(MAT(22))
   S2 = 65536 * S2
   S3 = CLng(MAT(21))
   S3 = S3 * 256
   S4 = CInt(MAT(20))
   Free_Sectors = S1 + S2 + S3 + S4
   Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "Free Sectors : " + CStr(Free_Sectors) + "  ( " + CStr(512 * Free_Sectors) + " bytes )"
   FS_CDIR = "/"
   S1 = CDbl(RDT(11))
   S1 = 16777216 * S1
   S2 = CLng(RDT(10))
   S2 = 65536 * S2
   S3 = CLng(RDT(9))
   S3 = S3 * 256
   S4 = CInt(RDT(8))
   FS_DirNumber = S1 + S2 + S3 + S4
   S1 = CLng(RDT(31))
   S1 = CLng(256 * S1)
   S2 = CLng(RDT(30))
   S3 = CLng(RDT(29))
   S3 = CLng(S3 * 256)
   S4 = CInt(RDT(28))
   FS_DirSerial2 = S1 + S2
   FS_DirSerial1 = S3 + S4
   FS_VolSerialNo = ((S1 + S2) * 65536) + S3 + S4
   S1 = CDbl(BS(23))
   S1 = 16777216 * S1
   S2 = CLng(BS(22))
   S2 = 65536 * S2
   S3 = CLng(BS(21))
   S3 = S3 * 256
   S4 = CInt(BS(20))
   Rem 20-10-2008 StartupFile Procedures
   FS_StartupFileNumber = S1 + S2 + S3 + S4
   If ImageFile = True Then
      Me.Caption = " FS Image File [ " + ImageFileName + " ]"
   Else
      If DriveName = "A:" Then
         Me.Caption = " Singlix FS Drive :  fd0 (A:)"
      Else
         If DriveName = "B:" Then
            Me.Caption = " Singlix FS Drive :  fd1 (B:)"
         End If
      End If
   End If
End Sub

Private Function ReadSector(DrvName As String, Sector As Double, SectorCount As Long, Buffer() As Byte) As Long
If ImageFile = True Then
   ReadSector = ImageFileReadSector(ImageFileName, Sector, SectorCount, Buffer())
Else
   Dim SB As Long
   SB = CLng(SectorCount * BytesPerSector)
   ReadSector = DirectReadDriveNT(DrvName, CLng(Sector), 0, abResult(), SB)
   If ReadSector > 0 Then
      CopyMemory Buffer(0), abResult(0), SB
      ReadSector = 0
   Rem Else
      Rem If ReadSector = 0 Then
         Rem ReadSector = -1
      Rem End If
   End If
End If
End Function

Private Function ByteToHex(BinaryData As Byte) As String
ByteToHex = Hex(BinaryData)
If Len(ByteToHex) <> 2 Then
   ByteToHex = "0" + ByteToHex
End If
End Function

Private Sub fsMkDir_Click()
Label1.Caption = ""
If ImageFileName = "" Then
   Label1.Caption = "FS Image File : NONE"
   MsgBox ("There is not a loaded SINGLIX FS Image file !"), 48, " Make FS Directory"
   Label1.Caption = ""
   Exit Sub
End If
SubDirName = ""
RepeatD1:
Label1.Caption = "FS Current Directory : " + FS_CDIR
SubDirName = InputBox("FS File Name :", " Make a Singlix FS Directory")
SubDirName = Trim$(SubDirName)
Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "FS New Sub Directory : " + FS_DirName
L = Len(SubDirName)
If L > 0 Then
   If L > 64 Then
      MsgBox ("FS Directory name must not be more then 64 bytes !"), 48, " Make FS Directory"
      GoTo RepeatD1
   Else
      For X = 1 To L
          Y = AscB(Mid$(SubDirName, X, 1))
          If Y = AscB("/") Or Y = AscB("?") Or Y = AscB("*") Or Y = AscB(":") Or Y = AscB("\") Then
             MsgBox ("Invalid directory name chars ('/','?','*',':','\') !..."), 48, " Make FS Directory"
             L = 0
             Exit For
          End If
      Next X
      If L < 1 Then GoTo RepeatD1
   End If
Else
   Exit Sub
End If
Free_Sectors = GetFreeSpace()
If Free_Sectors > 2 Then
   If Free_Sectors < 2880 Then
      SectorCount = 2
   Else
       If Free_Sectors < 65536 Then
          SectorCount = 3
       Else
          If Free_Sectors < 1048576 Then
             SectorCount = 5
          Else
             If Free_Sectors < (131072 * 65536) Then
                SectorCount = 9
             Else
                SectorCount = 17
             End If
          End If
       End If
   End If
Else
   MsgBox ("The is no enough free space to make a new sub directory on the Singlix File System!..." + Chr$(13) + Chr$(10) + "Free Sectors = " + CStr(Free_Sectors) + " sector(s)"), 48, " Make FS Directory"
   Exit Sub
End If
BeginningSector = GetFirstFreeSector()
MkDirGFFS:
DDT_Location = GetFirstFreeSection(BeginningSector, SectorCount)
If DDT_Location = 0 Then
   If SectorCount > 2 Then
      SectorCount = ((SectorCount - 1) / 2) + 1
      GoTo MkDirGFFS
   End If
   MsgBox ("Free consecutive sectors are not enough!..."), 48, " Make FS Directory"
Else
   Call ResetDDTBuffer
   DD.DDTSign = "DDT"
   DD.DDTVersion = 0
   DD.SectorSize = 512
   DD.DDTNumber = 0
   DD.DirectoryNumber = DDT_Location
   DD.NextDDTNumber = 0
   DD.SectorCount = SectorCount - 1
   DD.ParentDirNumber = FS_DirNumber
   DD.ParentDirSerial1 = FS_DirSerial1
   DD.ParentDirSerial2 = FS_DirSerial2
   FS_Second = Second(Now)
   FS_Minute = Minute(Now)
   FS_Hour = Hour(Now)
   FS_Day = Day(Now)
   FS_Month = Month(Now)
   FS_Year = Year(Now)
   S1 = CLng(FS_Day)
   S1 = CLng(256 * S1)
   S2 = CLng(FS_Minute)
   S3 = CLng(FS_Second)
   S3 = CLng(S3 * 256)
   S4 = Int(256 * Rnd)
   DD.DirectorySerial2 = S1 + S2
   DD.DirectorySerial1 = S3 + S4
   DD.EntrySize = &H4
   DD.Reserved = 0
   DD.DirectoryType = 0
   DD.CheckSum = 0
   DD.Attributes = 1
   DD.OwnerCode = 0
   DD.GroupCode = 0
   DD.Country = 90
   DD.TimeZone = 0
   DD.CreatingCentury = SetBCD(FS_Century)
   DD.CreatingYear = SetBCD(FS_Year)
   DD.CreatingMonth = SetBCD(FS_Month)
   DD.CreatingDay = SetBCD(FS_Day)
   DD.CreatingHour = SetBCD(FS_Hour)
   DD.CreatingMinute = SetBCD(FS_Minute)
   DD.CreatingSecond = SetBCD(FS_Second)
   DD.CTimeMode = 0
   DD.LastModifCentury = DD.CreatingCentury
   DD.LastModifYear = DD.CreatingYear
   DD.LastModifMonth = DD.CreatingMonth
   DD.LastModifDay = DD.CreatingDay
   DD.LastModifHour = DD.CreatingHour
   DD.LastModifMinute = DD.CreatingMinute
   DD.LastModifSecond = DD.CreatingSecond
   DD.LMTimeMode = 0
   DD.DirectoryName = SubDirName
   DD.DirectoryDescription = 0
   Call SetDDTBuffer
   If Chr$(DDT(0)) = "D" And Chr$(DDT(1)) = "D" And Chr$(DDT(2)) = "T" And DDT(3) = 0 And DDT(4) = 0 And DDT(5) = 2 Then
      Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "Writing DDT Sector..."
      ReturnCode = WriteSector(DriveName, DDT_Location, 1, DDT())
      If ReturnCode <> 0 Then
         MsgBox ("Writing Error !"), 48, " Writing DDT"
         Exit Sub
      End If
      Label1.Caption = Label1.Caption + " OK."
      Call SD_Data_Reset
      Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "Writing File Data..."
      X = 0
      BeginningSector = DDT_Location
      For L = 1 To SectorCount
          BeginningSector = BeginningSector + 1
          ReturnCode = WriteSector(DriveName, BeginningSector, 1, SD_Data())
          If ReturnCode <> 0 Then
             MsgBox ("Writing Error !"), 48, " Writing Sub Directory Data"
             Exit For
          End If
      Next L
      If ReturnCode <> 0 Then Exit Sub
      Label1.Caption = Label1.Caption + " OK."
      If FS_CDIR = "/" Then
         ReturnCode = ReadSector(DriveName, FS_DirNumber, 1, RDT())
      Else
         ReturnCode = ReadSector(DriveName, FS_DirNumber, 1, DDT())
      End If
      If ReturnCode <> 0 Then
         MsgBox ("Directory Descriptor Table Reading Error !"), 48, " Reading Directory Descriptor Table"
         Exit Sub
      End If
      FS_Second = Second(Now)
      FS_Minute = Minute(Now)
      FS_Hour = Hour(Now)
      FS_Day = Day(Now)
      FS_Month = Month(Now)
      FS_Year = Year(Now)
      Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "Updating Parent Directory Data..."
      If FS_CDIR = "/" Then
         K = SetFSDate(FS_Year, FS_Month, FS_Day)
         L = CInt(K Mod 256)
         RDT(56) = CByte(L)
         K = (K - L) / 256
         L = K Mod 256
         RDT(57) = CByte(L)
         K = (K - L) / 256
         L = K Mod 256
         RDT(58) = CByte(L)
         K = (K - L) / 256
         RDT(59) = CByte(K)
         K = SetFSTime(FS_Hour, FS_Minute, FS_Second)
         L = CInt(K Mod 256)
         RDT(60) = CByte(L)
         K = (K - L) / 256
         L = K Mod 256
         RDT(61) = CByte(L)
         K = (K - L) / 256
         L = K Mod 256
         RDT(62) = CByte(L)
         RDT(63) = 0
      Else
         K = SetFSDate(FS_Year, FS_Month, FS_Day)
         L = CInt(K Mod 256)
         DDT(56) = CByte(L)
         K = (K - L) / 256
         L = K Mod 256
         DDT(57) = CByte(L)
         K = (K - L) / 256
         L = K Mod 256
         DDT(58) = CByte(L)
         K = (K - L) / 256
         DDT(59) = CByte(K)
         K = SetFSTime(FS_Hour, FS_Minute, FS_Second)
         L = CInt(K Mod 256)
         DDT(60) = CByte(L)
         K = (K - L) / 256
         L = K Mod 256
         DDT(61) = CByte(L)
         K = (K - L) / 256
         L = K Mod 256
         DDT(62) = CByte(L)
         DDT(63) = 0
      End If
      If FS_CDIR = "/" Then
         ReturnCode = WriteSector(DriveName, FS_DirNumber, 1, RDT())
      Else
         ReturnCode = WriteSector(DriveName, FS_DirNumber, 1, DDT())
      End If
      If ReturnCode <> 0 Then
         MsgBox ("Directory Descriptor Table Writing Error !"), 48, " Updating Parent Directory Data"
         Exit Sub
      End If
      If FS_CDIR = "/" Then
         ReturnCode = ReadSector(DriveName, FS_DirNumber + 1, 1, RD_Data())
      Else
         ReturnCode = ReadSector(DriveName, FS_DirNumber + 1, 1, SD_Data())
      End If
      If ReturnCode <> 0 Then
         MsgBox ("Directory Data Reading Error !"), 48, " Updating Parent Directory Data"
         Exit Sub
      End If
      If FS_CDIR = "/" Then
         For X = 0 To 511 Step 4
             If (RD_Data(X) = 255 And RD_Data(X + 1) = 255 And RD_Data(X + 2) = 255 And RD_Data(X + 3) = 255) Or (RD_Data(X) = 0 And RD_Data(X + 1) = 0 And RD_Data(X + 2) = 0 And RD_Data(X + 3) = 0) Then
                K = DDT_Location
                L = CInt(K Mod 256)
                RD_Data(X) = CByte(L)
                K = (K - L) / 256
                L = K Mod 256
                RD_Data(X + 1) = CByte(L)
                K = (K - L) / 256
                L = K Mod 256
                RD_Data(X + 2) = CByte(L)
                K = (K - L) / 256
                RD_Data(X + 3) = CByte(K)
                Exit For
             End If
          Next X
      Else
         For X = 0 To 511 Step 4
             If (SD_Data(X) = 255 And SD_Data(X + 1) = 255 And SD_Data(X + 2) = 255 And SD_Data(X + 3) = 255) Or (SD_Data(X) = 0 And SD_Data(X + 1) = 0 And SD_Data(X + 2) = 0 And SD_Data(X + 3) = 0) Then
                K = DDT_Location
                L = CInt(K Mod 256)
                SD_Data(X) = CByte(L)
                K = (K - L) / 256
                L = K Mod 256
                SD_Data(X + 1) = CByte(L)
                K = (K - L) / 256
                L = K Mod 256
                SD_Data(X + 2) = CByte(L)
                K = (K - L) / 256
                SD_Data(X + 3) = CByte(K)
                Exit For
             End If
          Next X
      End If
      If FS_CDIR = "/" Then
         ReturnCode = WriteSector(DriveName, FS_DirNumber + 1, 1, RD_Data())
      Else
         ReturnCode = WriteSector(DriveName, FS_DirNumber + 1, 1, SD_Data())
      End If
      If ReturnCode <> 0 Then
         MsgBox ("Directory Data Writing Error !"), 48, " Updating Parent Directory Data"
         Exit Sub
      End If
      Label1.Caption = Label1.Caption + " OK."
      ReturnCode = ReadSector(DriveName, MAT_Location, 1, MAT())
      If ReturnCode <> 0 Then
         MsgBox ("Reading Error !"), 48, " Reading MAT Sector"
         Exit Sub
      End If
      Rem S1 = CDbl(MAT(15))
      Rem S1 = 16777216 * S1
      Rem S2 = CLng(MAT(14))
      Rem S2 = 65536 * S2
      Rem S3 = CLng(MAT(13))
      Rem S3 = S3 * 256
      Rem S4 = CInt(MAT(12))
      Rem Sector
      Rem DAT_Location = S1 + S2 + S3 + S4
      Rem S1 = CDbl(MAT(19))
      Rem S1 = 16777216 * S1
      Rem S2 = CLng(MAT(18))
      Rem S2 = 65536 * S2
      Rem S3 = CLng(MAT(17))
      Rem S3 = S3 * 256
      Rem S4 = CInt(MAT(16))
      Rem Sector Count
      Rem DAT_SCount = CLng(S1 + S2 + S3 + S4)
      S1 = GetFreeSpace()
      FreeSpace = S1 - SectorCount
      ReturnCode = ReadSector(DriveName, DAT_Location, DAT_SCount, DAT())
      If ReturnCode <> 0 Then
         MsgBox ("Reading Error !"), 48, " Reading DAT Sectors"
         Exit Sub
      End If
      L = CInt(DDT_Location)
      DAT(L) = &H6
      For L = L + 1 To L + SectorCount - 1
          DAT(L) = &HF0
      Next L
      ReturnCode = WriteSector(DriveName, DAT_Location, DAT_SCount, DAT())
      If ReturnCode <> 0 Then
         MsgBox ("Writing Error !"), 48, " Writing DAT Sectors"
         Exit Sub
      End If
      Free_Sectors = FreeSpace
      K = FreeSpace
      L = CInt(K Mod 256)
      MAT(20) = CByte(L)
      K = (K - L) / 256
      L = K Mod 256
      MAT(21) = CByte(L)
      K = (K - L) / 256
      L = K Mod 256
      MAT(22) = CByte(L)
      K = (K - L) / 256
      MAT(23) = CByte(K)
      S1 = CDbl(MAT(27))
      S1 = 16777216 * S1
      S2 = CLng(MAT(26))
      S2 = 65536 * S2
      S3 = CLng(MAT(25))
      S3 = S3 * 256
      S4 = CInt(MAT(24))
      BeginningSector = S1 + S2 + S3 + S4
      K = GetFirstFreeSection(BeginningSector, 1)
      L = CInt(K Mod 256)
      MAT(24) = CByte(L)
      K = (K - L) / 256
      L = K Mod 256
      MAT(25) = CByte(L)
      K = (K - L) / 256
      L = K Mod 256
      MAT(26) = CByte(L)
      K = (K - L) / 256
      MAT(27) = CByte(K)
      ReturnCode = WriteSector(DriveName, MAT_Location, 1, MAT())
      If ReturnCode <> 0 Then
         MsgBox ("Writing Error !"), 48, " Writing MAT Sector"
         Exit Sub
      End If
      MsgBox ("Directory has been recorded on the Singlix FS '" + FS_CDIR + "' directory as '" + SubDirName + "'"), 0, " Make Singlix FS Sub Directory"
      Label1.Caption = "FS Parent Directory : " + FS_CDIR
      Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "FS Sub Directory : " + SubDirName
   End If
End If
End Sub

Private Sub fsOpenFile_Click()
Satir = InputBox("FS File (Directory) Name :", " Find First File (or Directory)")
Satir = Trim$(Satir)
L = Len(Satir)
If L < 1 Then Exit Sub
K = FindFirstFile(0, Satir)
If K > 0 Then
   ReturnCode = ReadSector(DriveName, K, 1, FDT())
   If ReturnCode = 0 Then
      If FDT(0) = AscB("F") And FDT(1) = AscB("D") And FDT(2) = AscB("T") Then
         Call SetOpenFileStructure(FDT())
         FS_FileName = Trim$(FD.FileName)
         MsgBox ("FS File Name : " + FS_FileName), 0, " Find First File [ Found ]"
      Else
         If FDT(0) = AscB("D") And FDT(1) = AscB("D") And FDT(2) = AscB("T") Then
            Call SetOpenDirStructure(FDT())
            FS_DirName = Trim$(DD.DirectoryName)
            MsgBox ("FS Directory Name : " + FS_DirName), 0, " Find First File [ Found ]"
         Else
            MsgBox ("File or Directory could not be found!"), 0, " Find First File [ Not Found ]"
         End If
      End If
   Else
      MsgBox ("File or Directory could not be found!"), 0, " Find First File [ Not Found ]"
   End If
Else
   MsgBox ("File or Directory could not be found!"), 0, " Find First File [ Not Found ]"
End If
End Sub

Private Sub fsOpenNextFile_Click()
Satir = InputBox("FS File (Directory) Name :", " Find Next File (or Directory)")
Satir = Trim$(Satir)
L = Len(Satir)
If L < 1 Then Exit Sub
K = FindFirstFile(0, Satir)
If K > 0 Then
   ReturnCode = ReadSector(FFF_DRV, K, 1, FDT())
   If ReturnCode = 0 Then
      If FDT(0) = AscB("F") And FDT(1) = AscB("D") And FDT(2) = AscB("T") Then
         Call SetOpenFileStructure(FDT())
         FS_FileName = Trim$(FD.FileName)
         MsgBox ("FS File Name : " + FS_FileName), 0, " Find Next File [ Found ]"
FNFRepeat1:
         K = FindNextFile()
         If K > 0 Then
            ReturnCode = ReadSector(FFF_DRV, K, 1, FDT())
            If FDT(0) = AscB("F") And FDT(1) = AscB("D") And FDT(2) = AscB("T") Then
               Call SetOpenFileStructure(FDT())
               FS_FileName = Trim$(FD.FileName)
               MsgBox ("FS Next File Name : " + FS_FileName), 0, " Find Next File [ Found ]"
               GoTo FNFRepeat1
            Else
               If FDT(0) = AscB("D") And FDT(1) = AscB("D") And FDT(2) = AscB("T") Then
                  Call SetOpenDirStructure(FDT())
                  FS_DirName = Trim$(DD.DirectoryName)
                  MsgBox ("FS Next Directory Name : " + FS_DirName), 0, " Find Next File [ Found ]"
                  GoTo FNFRepeat1
               Else
                  Exit Sub
               End If
            End If
         End If
      Else
         If FDT(0) = AscB("D") And FDT(1) = AscB("D") And FDT(2) = AscB("T") Then
            Call SetOpenDirStructure(FDT())
            FS_DirName = Trim$(DD.DirectoryName)
            MsgBox ("FS Directory Name : " + FS_DirName), 0, " Find Next File [ Found ]"
            GoTo FNFRepeat1
         Else
            MsgBox ("File or Directory could not be found!"), 0, " Find Next File [ Not Found ]"
         End If
      End If
   Else
      MsgBox ("File or Directory could not be found!"), 0, " Find Next File [ Not Found ]"
   End If
Else
   MsgBox ("File or Directory could not be found!"), 0, " Find Next File [ Not Found ]"
End If
End Sub

Private Sub fsRmDir_Click()
Satir = InputBox("FS Directory Name :", " Delete FS Directory")
Satir = Trim$(Satir)
L = Len(Satir)
If L < 1 Then
   Exit Sub
Else
   If L = 1 Then
      If Satir = "/" Then Exit Sub
   End If
End If
K = FindFirstFile(0, Satir)
If K > 0 Then
   ReturnCode = ReadSector(DriveName, K, 1, DDT())
   If ReturnCode = 0 Then
      If FDT(0) = AscB("D") And FDT(1) = AscB("D") And FDT(2) = AscB("T") Then
         Call SetOpenDirStructure(DDT())
      Else
         MsgBox ("Directory could not be found!"), 0, " Delete FS Directory"
         Exit Sub
      End If
   End If
Else
   MsgBox ("Directory could not be found!"), 0, " Delete FS Directory"
   Exit Sub
End If
If K = FS_DirNumber Then
   MsgBox ("Current directory can not be deleted !"), 0, " Delete FS Directory"
   Exit Sub
End If
FS_DirName = Trim$(DD.DirectoryName)
If MsgBox("Do you want to delete directory '" + FS_DirName + "' ?", 36, " Delete FS Directory") <> 6 Then Exit Sub
Dim Z As Long
Dim DirNotEmpty As Boolean
Rem DirNotEmpty = False
FS_DirNumber = DD.ParentDirNumber
FS_DirSerial1 = DD.ParentDirSerial1
FS_DirSerial2 = DD.ParentDirSerial2
Rem ***
REP0:
K = DD.DirectoryNumber
If DD.SectorCount < 1 Then GoTo PASS1
Rem Z = 0
REP1:
Z = Z + 1
ReturnCode = ReadSector(DriveName, K + Z, 1, SD_Data())
If ReturnCode <> 0 Then
   MsgBox ("Reading Error !"), 48, " Reading Directory Data"
   Exit Sub
End If
For R = 0 To 511 Step 4
    S1 = CDbl(SD_Data(R + 3))
    S1 = 16777216 * S1
    S2 = CLng(SD_Data(R + 2))
    S2 = 65536 * S2
    S3 = CLng(SD_Data(R + 1))
    S3 = S3 * 256
    S4 = CInt(SD_Data(R))
    FDT_Location = S1 + S2 + S3 + S4
    If FDT_Location > 0 Then
       If FDT_Location < 4294967295# Then
          DirNotEmpty = True
          Exit For
       End If
    Else
       Exit For
    End If
Next R
If DirNotEmpty = False Then
   If FDT_Location = 0 Then GoTo PASS1
   If Z < DD.SectorCount Then
      GoTo REP1
   Else
      If DD.NextDDTNumber > 0 Then
         K = DD.NextDDTNumber
         ReturnCode = ReadSector(DriveName, K, 1, DDT())
         If ReturnCode = 0 Then
            If DDT(0) = AscB("D") And DDT(1) = AscB("D") And DDT(2) = AscB("T") Then
               Call SetOpenDirStructure(DDT())
               Z = 0
               GoTo REP0
            End If
         Else
            MsgBox ("Reading Error !"), 48, " Reading Directory Data"
            Exit Sub
         End If
      End If
   End If
Else
   MsgBox ("Directory is not empty!"), 0, " Delete FS Directory"
   Exit Sub
End If
PASS1:
Rem ***
Rem If FS_DirNumber < 1 Then Exit Sub
ANS:
Rem Erased/Deleted Sign
DDT(2) = AscB("E")
ReturnCode = WriteSector(DriveName, K, 1, DDT())
If ReturnCode <> 0 Then
   MsgBox ("Writing Error !"), 48, " Writing DDT Sector"
   Exit Sub
End If
FDT_Location = DD.DirectoryNumber
SectorCount = DD.SectorCount
FreeSpace = Free_Sectors + SectorCount + 1
ReturnCode = ReadSector(DriveName, DAT_Location, DAT_SCount, DAT())
If ReturnCode <> 0 Then
   MsgBox ("Reading Error !"), 48, " Reading DAT Sectors"
   Exit Sub
End If
For K = FDT_Location To FDT_Location + SectorCount
    L = CInt(K)
    DAT(L) = &H90
Next K
ReturnCode = WriteSector(DriveName, DAT_Location, DAT_SCount, DAT())
If ReturnCode <> 0 Then
   MsgBox ("Writing Error !"), 48, " Writing DAT Sectors"
   Exit Sub
End If
Free_Sectors = FreeSpace
ReturnCode = ReadSector(DriveName, MAT_Location, 1, MAT())
If ReturnCode <> 0 Then
   MsgBox ("Reading Error !"), 48, " Reading MAT Sector"
   Exit Sub
End If
Rem FreeSectors
K = FreeSpace
L = CInt(K Mod 256)
MAT(20) = CByte(L)
K = (K - L) / 256
L = K Mod 256
MAT(21) = CByte(L)
K = (K - L) / 256
L = K Mod 256
MAT(22) = CByte(L)
K = (K - L) / 256
MAT(23) = CByte(K)
Rem FirstFreeSector
S1 = CDbl(MAT(27))
S1 = 16777216 * S1
S2 = CLng(MAT(26))
S2 = 65536 * S2
S3 = CLng(MAT(25))
S3 = S3 * 256
S4 = CInt(MAT(24))
K = S1 + S2 + S3 + S4
If K > FDT_Location Then
   K = FDT_Location
   L = CInt(K Mod 256)
   MAT(24) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   MAT(25) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   MAT(26) = CByte(L)
   K = (K - L) / 256
   MAT(27) = CByte(K)
End If
ReturnCode = WriteSector(DriveName, MAT_Location, 1, MAT())
If ReturnCode <> 0 Then
   MsgBox ("Writing Error !"), 48, " Writing MAT Sector"
   Exit Sub
End If
SectorCount = SectorCount + FD.SectorCount + 1
K = FD.NextFDTNumber
If K > 0 Then
   ReturnCode = ReadSector(DriveName, K, 1, FDT())
   If ReturnCode <> 0 Then Exit Sub
   If FDT(0) = AscB("F") And FDT(1) = AscB("D") And FDT(2) = AscB("T") Then
      Call SetOpenFileStructure(FDT())
   End If
   GoTo ANS
End If
Rem Deleting Entry On Parent Directory Data Sector
K = FS_DirNumber
BNS:
ReturnCode = ReadSector(DriveName, K, 1, DDT())
If ReturnCode <> 0 Then
   MsgBox ("Reading Error !"), 48, " Reading Parent DDT Sector"
   Exit Sub
End If
Call SetOpenDirStructure(DDT())
If DD.DDTSign <> "DDT" And DD.DDTSign <> "RDT" Then
   MsgBox ("Invalid Directory Descriptor Table !"), 48, " Reading Parent DDT Sector"
   Exit Sub
End If
If DD.DirectorySerial1 <> FS_DirSerial1 Or DD.DirectorySerial2 <> FS_DirSerial2 Then
   MsgBox ("Wrong Directory Serial Number !"), 48, " Reading Parent DDT Sector"
   Exit Sub
End If
K = DD.DirectoryNumber + 1
SectorCount = DD.SectorCount
Z = 1
CNS:
ReturnCode = ReadSector(DriveName, K, Z, SD_Data())
If ReturnCode <> 0 Then
   MsgBox ("Reading Error !"), 48, " Reading Parent Directory Data"
   Exit Sub
End If
For R = 0 To 511 Step 4
    S1 = CDbl(SD_Data(R + 3))
    S1 = 16777216 * S1
    S2 = CLng(SD_Data(R + 2))
    S2 = 65536 * S2
    S3 = CLng(SD_Data(R + 1))
    S3 = S3 * 256
    S4 = CInt(SD_Data(R))
    K = S1 + S2 + S3 + S4
    If FDT_Location = K Then
       Rem Deleting/Erasing Directory Entry
       SD_Data(R) = 255
       SD_Data(R + 1) = 255
       SD_Data(R + 2) = 255
       SD_Data(R + 3) = 255
       Exit For
    Else
       K = 0
    End If
Next R
If K > 0 Then
   K = DD.DirectoryNumber + 1
   ReturnCode = WriteSector(DriveName, K, Z, SD_Data())
   If ReturnCode <> 0 Then
      MsgBox ("Writing Error !"), 48, " Writing Directory Data"
   Else
      MsgBox ("'" + FS_DirName + "' has been deleted..."), 0, " Delete FS Directory"
      Call UpdateFSInfo
   End If
Else
   If Z < SectorCount Then
      Z = Z + 1
      GoTo CNS
   Else
      K = DD.NextDDTNumber
      If K > 0 Then
         GoTo BNS
      Else
         MsgBox ("Directory Entry could not be found !"), 48, " Delete FS Directory [ Error ]"
      End If
   End If
End If

End Sub

Private Sub fsSaveFile_Click()
Label1.Caption = ""
If WinFileName = "" Then
   Label1.Caption = "Windows File : NONE"
   MsgBox ("There is not a loaded Windows file !"), 48, " Save as FS File"
   Label1.Caption = ""
   Exit Sub
End If
FS_FileName = ""
Repeat1:
Label1.Caption = "FS Directory : " + FS_CDIR
FS_FileName = InputBox("FS File Name :", " Save as Singlix FS File", WinFileName)
FS_FileName = Trim$(FS_FileName)
Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "FS File : " + FS_FileName
L = Len(FS_FileName)
If L > 0 Then
   If L > 64 Then
      MsgBox ("FS File name must not be more then 64 bytes !"), 48, " Save as Singlix FS File"
      GoTo Repeat1
   Else
      For X = 1 To L
          Y = AscB(Mid$(FS_FileName, X, 1))
          If Y = AscB("/") Or Y = AscB("?") Or Y = AscB("*") Or Y = AscB(":") Or Y = AscB("\") Then
             MsgBox ("Invalid file name chars ('/','?','*',':','\') !..."), 48, " Save as Singlix FS File"
             L = 0
             Exit For
          End If
      Next X
      If L < 1 Then GoTo Repeat1
   End If
Else
   Exit Sub
End If
Free_Sectors = GetFreeSpace()
FreeSpace = 512 * Free_Sectors
RequiredSpace = WinFileSize + 512
If FreeSpace < RequiredSpace Then
   MsgBox ("The is no enough free space to save the file on the Singlix File System!..." + Chr$(13) + Chr$(10) + "Free Space = " + CStr(FreeSpace) + " bytes"), 48, " Save as FS File"
   Exit Sub
End If
Rem Sector
K = WinFileSize Mod 512
If K > 0 Then
   SectorCount = ((WinFileSize - K) / 512) + 1
Else
   SectorCount = (WinFileSize - K) / 512
End If
SectorCount = SectorCount + 1
BeginningSector = GetFirstFreeSector()
FDT_Location = GetFirstFreeSection(BeginningSector, SectorCount)
If FDT_Location = 0 Then
   Rem Allocation will be done later, as multi sections
   MsgBox ("Free consecutive sectors are not enough!..."), 48, " Save as FS File"
Else
   Call ResetFDTBuffer
   FD.FDTSign = "FDT"
   FD.FDTVersion = 0
   FD.SectorSize = 512
   FD.FDTNumber = 0
   FD.FileNumber = FDT_Location
   FD.NextFDTNumber = 0
   FD.SectorCount = SectorCount - 1
   FD.ParentDirNumber = FS_DirNumber
   FD.ParentDirSerial1 = FS_DirSerial1
   FD.ParentDirSerial2 = FS_DirSerial2
   FD.FileSize = WinFileSize
   FD.FileSizeHigh = 0
   FD.FileType = 0
   FD.CheckSum = 0
   FD.Attributes = 1
   FD.OwnerCode = 0
   FD.GroupCode = 0
   FD.Country = 90
   FD.TimeZone = 0
   FS_Second = Second(Now)
   FS_Minute = Minute(Now)
   FS_Hour = Hour(Now)
   FS_Day = Day(Now)
   FS_Month = Month(Now)
   FS_Year = Year(Now) - 2000
   FS_Century = 20
   FD.CreatingCentury = SetBCD(FS_Century)
   FD.CreatingYear = SetBCD(FS_Year)
   FD.CreatingMonth = SetBCD(FS_Month)
   FD.CreatingDay = SetBCD(FS_Day)
   FD.CreatingHour = SetBCD(FS_Hour)
   FD.CreatingMinute = SetBCD(FS_Minute)
   FD.CreatingSecond = SetBCD(FS_Second)
   FD.CTimeMode = 0
   FD.LastModifCentury = FD.CreatingCentury
   FD.LastModifYear = FD.CreatingYear
   FD.LastModifMonth = FD.CreatingMonth
   FD.LastModifDay = FD.CreatingDay
   FD.LastModifHour = FD.CreatingHour
   FD.LastModifMinute = FD.CreatingMinute
   FD.LastModifSecond = FD.CreatingSecond
   FD.LMTimeMode = 0
   FD.FileName = FS_FileName
   FD.FileDescription = 0
   Call SetFDTBuffer
   If Chr$(FDT(0)) = "F" And Chr$(FDT(1)) = "D" And Chr$(FDT(2)) = "T" And FDT(3) = 0 And FDT(4) = 0 And FDT(5) = 2 Then
      Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "Writing FDT Sector..."
      ReturnCode = WriteSector(DriveName, FDT_Location, 1, FDT())
      If ReturnCode <> 0 Then
         MsgBox ("Writing Error !"), 48, " Writing FDT"
         Exit Sub
      End If
      Label1.Caption = Label1.Caption + " OK."
      WinFileNumber = FreeFile
      Open WinFileName For Binary As WinFileNumber Len = 1
      If FileLen(WinFileName) <> WinFileSize Then
         MsgBox ("File Error !"), 48, " Writing File Data"
         Exit Sub
      End If
      Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "Writing File Data..."
      X = 0
      BeginningSector = FDT_Location
      For S1 = 1 To WinFileSize
          Get WinFileNumber, S1, Y
          File_Data(X) = Y
          X = X + 1
          If X = 512 Then
             BeginningSector = BeginningSector + 1
             ReturnCode = WriteSector(DriveName, BeginningSector, 1, File_Data())
             If ReturnCode <> 0 Then
                MsgBox ("Writing Error !"), 48, " Writing File Data"
                Exit For
             End If
             X = 0
          End If
      Next S1
      If ReturnCode <> 0 Then Exit Sub
      If X > 0 And X < 512 Then
         For X = X To 511
             File_Data(X) = 0
         Next X
         BeginningSector = BeginningSector + 1
         ReturnCode = WriteSector(DriveName, BeginningSector, 1, File_Data())
         If ReturnCode <> 0 Then
            MsgBox ("Writing Error !"), 48, " Writing File Data"
            Exit Sub
         End If
      End If
      Label1.Caption = Label1.Caption + " OK."
      
      If FS_CDIR = "/" Then
         ReturnCode = ReadSector(DriveName, FS_DirNumber, 1, RDT())
      Else
         ReturnCode = ReadSector(DriveName, FS_DirNumber, 1, DDT())
      End If
      If ReturnCode <> 0 Then
         MsgBox ("Directory Descriptor Table Reading Error !"), 48, " Reading Directory Descriptor Table"
         Exit Sub
      End If
      FS_Second = Second(Now)
      FS_Minute = Minute(Now)
      FS_Hour = Hour(Now)
      FS_Day = Day(Now)
      FS_Month = Month(Now)
      FS_Year = Year(Now) - 2000
      FS_Century = 20
      Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "Updating Directory Data..."
      If FS_CDIR = "/" Then
         RDT(56) = SetBCD(FS_Century)
         RDT(57) = SetBCD(FS_Year)
         RDT(58) = SetBCD(FS_Month)
         RDT(59) = SetBCD(FS_Day)
         RDT(60) = SetBCD(FS_Hour)
         RDT(61) = SetBCD(FS_Minute)
         RDT(62) = SetBCD(FS_Second)
         RDT(63) = 0
      Else
         DDT(56) = SetBCD(FS_Century)
         DDT(57) = SetBCD(FS_Year)
         DDT(58) = SetBCD(FS_Month)
         DDT(59) = SetBCD(FS_Day)
         DDT(60) = SetBCD(FS_Hour)
         DDT(61) = SetBCD(FS_Minute)
         DDT(62) = SetBCD(FS_Second)
         DDT(63) = 0
      End If
      If FS_CDIR = "/" Then
         ReturnCode = WriteSector(DriveName, FS_DirNumber, 1, RDT())
      Else
         ReturnCode = WriteSector(DriveName, FS_DirNumber, 1, DDT())
      End If
      If ReturnCode <> 0 Then
         MsgBox ("Directory Descriptor Table Writing Error !"), 48, " Updating Directory Data"
         Exit Sub
      End If
      If FS_CDIR = "/" Then
         ReturnCode = ReadSector(DriveName, FS_DirNumber + 1, 1, RD_Data())
      Else
         ReturnCode = ReadSector(DriveName, FS_DirNumber + 1, 1, SD_Data())
      End If
      If ReturnCode <> 0 Then
         MsgBox ("Directory Data Reading Error !"), 48, " Updating Directory Data"
         Exit Sub
      End If
      If FS_CDIR = "/" Then
         For X = 0 To 511 Step 4
             If (RD_Data(X) = 255 And RD_Data(X + 1) = 255 And RD_Data(X + 2) = 255 And RD_Data(X + 3) = 255) Or (RD_Data(X) = 0 And RD_Data(X + 1) = 0 And RD_Data(X + 2) = 0 And RD_Data(X + 3) = 0) Then
                K = FDT_Location
                L = CInt(K Mod 256)
                RD_Data(X) = CByte(L)
                K = (K - L) / 256
                L = K Mod 256
                RD_Data(X + 1) = CByte(L)
                K = (K - L) / 256
                L = K Mod 256
                RD_Data(X + 2) = CByte(L)
                K = (K - L) / 256
                RD_Data(X + 3) = CByte(K)
                Exit For
             End If
          Next X
      Else
         For X = 0 To 511 Step 4
             If (SD_Data(X) = 255 And SD_Data(X + 1) = 255 And SD_Data(X + 2) = 255 And SD_Data(X + 3) = 255) Or (SD_Data(X) = 0 And SD_Data(X + 1) = 0 And SD_Data(X + 2) = 0 And SD_Data(X + 3) = 0) Then
                K = FDT_Location
                L = CInt(K Mod 256)
                SD_Data(X) = CByte(L)
                K = (K - L) / 256
                L = K Mod 256
                SD_Data(X + 1) = CByte(L)
                K = (K - L) / 256
                L = K Mod 256
                SD_Data(X + 2) = CByte(L)
                K = (K - L) / 256
                SD_Data(X + 3) = CByte(K)
                Exit For
             End If
          Next X
      End If
      If FS_CDIR = "/" Then
         ReturnCode = WriteSector(DriveName, FS_DirNumber + 1, 1, RD_Data())
      Else
         ReturnCode = WriteSector(DriveName, FS_DirNumber + 1, 1, SD_Data())
      End If
      If ReturnCode <> 0 Then
         MsgBox ("Directory Data Writing Error !"), 48, " Updating Directory Data"
         Exit Sub
      End If
      Label1.Caption = Label1.Caption + " OK."
      ReturnCode = ReadSector(DriveName, MAT_Location, 1, MAT())
      If ReturnCode <> 0 Then
         MsgBox ("Reading Error !"), 48, " Reading MAT Sector"
         Exit Sub
      End If
      Rem S1 = CDbl(MAT(15))
      Rem S1 = 16777216 * S1
      Rem S2 = CLng(MAT(14))
      Rem S2 = 65536 * S2
      Rem S3 = CLng(MAT(13))
      Rem S3 = S3 * 256
      Rem S4 = CInt(MAT(12))
      Rem Sector
      Rem DAT_Location = S1 + S2 + S3 + S4
      Rem S1 = CDbl(MAT(19))
      Rem S1 = 16777216 * S1
      Rem S2 = CLng(MAT(18))
      Rem S2 = 65536 * S2
      Rem S3 = CLng(MAT(17))
      Rem S3 = S3 * 256
      Rem S4 = CInt(MAT(16))
      Rem Sector Count
      Rem DAT_SCount = CLng(S1 + S2 + S3 + S4)
      L = WinFileSize Mod 512
      If L > 0 Then
         K = ((WinFileSize - L) / 512) + 1
      Else
         K = (WinFileSize / 512) + 1
      End If
      S1 = GetFreeSpace()
      FreeSpace = S1 - (K + 1)
      ReturnCode = ReadSector(DriveName, DAT_Location, DAT_SCount, DAT())
      If ReturnCode <> 0 Then
         MsgBox ("Reading Error !"), 48, " Reading DAT Sectors"
         Exit Sub
      End If
      L = CInt(FDT_Location)
      DAT(L) = &HF
      For K = L + 1 To L + K
          L = CInt(K)
          DAT(L) = &HF0
      Next K
      ReturnCode = WriteSector(DriveName, DAT_Location, DAT_SCount, DAT())
      If ReturnCode <> 0 Then
         MsgBox ("Writing Error !"), 48, " Writing DAT Sectors"
         Exit Sub
      End If
      Free_Sectors = FreeSpace
      K = FreeSpace
      L = CInt(K Mod 256)
      MAT(20) = CByte(L)
      K = (K - L) / 256
      L = K Mod 256
      MAT(21) = CByte(L)
      K = (K - L) / 256
      L = K Mod 256
      MAT(22) = CByte(L)
      K = (K - L) / 256
      MAT(23) = CByte(K)
      S1 = CDbl(MAT(27))
      S1 = 16777216 * S1
      S2 = CLng(MAT(26))
      S2 = 65536 * S2
      S3 = CLng(MAT(25))
      S3 = S3 * 256
      S4 = CInt(MAT(24))
      BeginningSector = S1 + S2 + S3 + S4
      K = GetFirstFreeSection(BeginningSector, 1)
      L = CInt(K Mod 256)
      MAT(24) = CByte(L)
      K = (K - L) / 256
      L = K Mod 256
      MAT(25) = CByte(L)
      K = (K - L) / 256
      L = K Mod 256
      MAT(26) = CByte(L)
      K = (K - L) / 256
      MAT(27) = CByte(K)
      ReturnCode = WriteSector(DriveName, MAT_Location, 1, MAT())
      If ReturnCode <> 0 Then
         MsgBox ("Writing Error !"), 48, " Writing MAT Sector"
         Exit Sub
      End If
      MsgBox ("File has been recorded on the Singlix FS '" + FS_CDIR + "' directory as '" + FS_FileName + "'"), 0, " Save Open/Windows File as Singlix FS File"
      Label1.Caption = "FS Directory : " + FS_CDIR
      Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "FS File : " + FS_FileName
      Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "File Size: " + CStr(WinFileSize) + " bytes."
   End If
End If
End Sub

Private Sub WinOpenFile_Click()
CommonDialog1.DialogTitle = "OPEN FILE"
CommonDialog1.FileName = Chr(0)
Rem CommonDialog1.Filter = "All files (*.*)|*.*|EXE files (*.exe)|*.exe|COM files (*.com)|*.com|BIN files (*.bin)|*.bin |SYS files (*.sys)|*.sys|DLL files (*.dll)|*.dll|HEX files (*.hex) IMPORT|*.hex"
CommonDialog1.Flags = OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY Or OFN_EXPLORER
On Error Resume Next
CommonDialog1.ShowOpen
If Err = 0 Then
   WinFileName = CommonDialog1.FileName
   WinFileSize = FileLen(WinFileName)
   If WinFileSize < 1 Then
      WinFileName = ""
   End If
Else
   If Err <> 32755 Then
      WinFileName = ""
      GoTo ErrMsg
   Else
      Exit Sub
   End If
End If
If WinFileName <> "" Then
   WinFileSize = FileLen(WinFileName)
   WinFileCDateTime = FileDateTime(WinFileName)
   Label1.Caption = "Windows File Name : " + WinFileName + Chr$(13) + Chr$(10) + "Windows File Size : " + CStr(WinFileSize) + " bytes" + Chr$(13) + Chr$(10) + "File Date and Time : " + WinFileCDateTime
Else
   Label1.Caption = "Open Windows File : NONE"
End If
Exit Sub
ErrMsg:
Beep
MsgBox ("File could not open!..." + Chr$(13) + Chr$(13) + "Error Number: " + Str$(Err)), 48, "! ERROR !"
End Sub

Private Function GetFreeSpace() As Double
   S1 = CDbl(MAT(23))
   S1 = 16777216 * S1
   S2 = CLng(MAT(22))
   S2 = 65536 * S2
   S3 = CLng(MAT(21))
   S3 = S3 * 256
   S4 = CInt(MAT(20))
   GetFreeSpace = S1 + S2 + S3 + S4
End Function

Private Function GetFirstFreeSector() As Double
   S1 = CDbl(MAT(27))
   S1 = 16777216 * S1
   S2 = CLng(MAT(26))
   S2 = 65536 * S2
   S3 = CLng(MAT(25))
   S3 = S3 * 256
   S4 = CInt(MAT(24))
   GetFirstFreeSector = S1 + S2 + S3 + S4
End Function

Private Function GetFirstFreeSection(BSect As Double, SCount As Double) As Double
Dim C As Integer
Dim B As Double
If BSect > 2879 Then
   GetFirstFreeSection = 0
   Exit Function
Else
   If SCount < 1 Or SCount > 2870 Then
      GetFirstFreeSection = 0
      Exit Function
   End If
End If
C = 0
GetFirstFreeSection = 0
For B = BSect To 2879
    If DAT(B) = &H90 Then
       If C < SCount Then
          C = C + 1
       Else
          GetFirstFreeSection = B - C
          Exit For
       End If
    Else
       If C > 0 Then C = 0
    End If
Next B
End Function

Private Function ResetFDTBuffer()
For X = 0 To 511
    FDT(X) = 0
Next X
End Function

Private Function SetFDTBuffer()
Dim L As Double
   For X = 0 To 2
       FDT(X) = AscB(Mid$(FD.FDTSign, X + 1, 1))
   Next X
   FDT(3) = FD.FDTVersion
   L = FD.SectorSize Mod 256
   FDT(4) = CByte(L)
   L = FD.SectorSize - L
   L = L / 256
   FDT(5) = CByte(L)
   L = FD.FDTNumber Mod 256
   FDT(6) = CByte(L)
   L = FD.FDTNumber - L
   L = L / 256
   FDT(7) = CByte(L)
   K = FD.FileNumber
   L = CInt(K Mod 256)
   FDT(8) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   FDT(9) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   FDT(10) = CByte(L)
   K = (K - L) / 256
   FDT(11) = CByte(K)
   K = FD.NextFDTNumber
   L = CInt(K Mod 256)
   FDT(12) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   FDT(13) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   FDT(14) = CByte(L)
   K = (K - L) / 256
   FDT(15) = CByte(K)
   K = FD.SectorCount
   L = CInt(K Mod 256)
   FDT(16) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   FDT(17) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   FDT(18) = CByte(L)
   K = (K - L) / 256
   FDT(19) = CByte(K)
   K = FD.ParentDirNumber
   L = CInt(K Mod 256)
   FDT(20) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   FDT(21) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   FDT(22) = CByte(L)
   K = (K - L) / 256
   FDT(23) = CByte(K)
   K = FD.ParentDirSerial1
   L = K Mod 256
   FDT(24) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   FDT(25) = CByte(L)
   K = FD.ParentDirSerial2
   L = K Mod 256
   FDT(26) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   FDT(27) = CByte(L)
   K = FD.FileSize
   L = CInt(K Mod 256)
   FDT(28) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   FDT(29) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   FDT(30) = CByte(L)
   K = (K - L) / 256
   FDT(31) = CByte(K)
   L = CInt(FD.FileSizeHigh Mod 256)
   FDT(32) = CByte(L)
   L = FD.FileSizeHigh - L
   L = L / 256
   FDT(33) = CByte(L)
   L = CInt(FD.FileType Mod 256)
   FDT(34) = CByte(L)
   L = FD.FileType - L
   L = L / 256
   FDT(35) = CByte(L)
   FDT(36) = FD.CheckSum
   FDT(37) = FD.Attributes
   For X = 38 To 45
       Rem Reserved for TR-Multix, not in use
       FDT(X) = 0
   Next X
   FDT(46) = FD.Country
   FDT(47) = FD.TimeZone
   FDT(48) = FD.CreatingCentury
   FDT(49) = FD.CreatingYear
   FDT(50) = FD.CreatingMonth
   FDT(51) = FD.CreatingDay
   FDT(52) = FD.CreatingHour
   FDT(53) = FD.CreatingMinute
   FDT(54) = FD.CreatingSecond
   FDT(55) = FD.CTimeMode
   FDT(56) = FD.LastModifCentury
   FDT(57) = FD.LastModifYear
   FDT(58) = FD.LastModifMonth
   FDT(59) = FD.LastModifDay
   FDT(60) = FD.LastModifHour
   FDT(61) = FD.LastModifMinute
   FDT(62) = FD.LastModifSecond
   FDT(63) = FD.LMTimeMode
   Satir = Trim$(FD.FileName)
   L = Len(Satir)
   If L > 0 Then
      For X = 1 To L
          FDT(63 + X) = AscB(Mid$(Satir, X, 1))
      Next X
   End If
   If L < 64 Then
      For X = L + 1 To 64
          FDT(63 + X) = 0
      Next X
   End If
   Satir = Trim$(FD.FileDescription)
   L = Len(Satir)
   If L > 0 Then
      For X = 1 To L
          FDT(127 + X) = AscB(Mid$(Satir, X, 1))
      Next X
   End If
   If L < 128 Then
      For X = L + 1 To 128
          FDT(127 + X) = 0
      Next X
   End If
End Function

Private Function SetFSDate(Y As Integer, M As Integer, D As Integer) As Double
If Y > 2020 Or Y < 2008 Then
   Y = Year(Now)
End If
If M > 12 Or M < 1 Then
   M = Month(Now)
End If
If D > 31 Or D < 1 Then
   D = Day(Now)
End If
SetFSDate = Y * 65536
SetFSDate = (M * 256) + SetFSDate
SetFSDate = D + SetFSDate
End Function

Private Function SetFSTime(H As Integer, M As Integer, S As Integer) As Double
If H > 24 Or H < 0 Then
   H = Hour(Now)
End If
If M > 59 Or M < 0 Then
   M = Minute(Now)
End If
If S > 59 Or S < 0 Then
   S = Second(Now)
End If
SetFSTime = H * 65536
SetFSTime = (M * 256) + SetFSTime
SetFSTime = S + SetFSTime
End Function

Private Function WriteSector(DrvName As String, Sector As Double, SectorCount As Long, Buffer() As Byte) As Long
If ImageFile = True Then
   WriteSector = ImageFileWriteSector(ImageFileName, Sector, SectorCount, Buffer())
Else
   Dim SB As Long
   SB = CLng(SectorCount * BytesPerSector)
   WriteSector = DirectWriteDriveNT(DrvName, CLng(Sector), 0, Buffer(), SB)
   If WriteSector > 0 Then
      WriteSector = 0
   Else
      WriteSector = -1
   End If
End If
End Function

Public Function OpenFile(DrvName As String, FileNumber As Double) As Integer
Dim LL As Long
ReturnCode = ReadSector(DrvName, FileNumber, 1, FDT())
If ReturnCode <> 0 Then
OF_FDT_ReadError:
   MsgBox ("File Descriptor Table Reading Error !"), 48, " Reading File Descriptor Table"
   OpenFile = 0
   Exit Function
Else
   If Chr$(FDT(0)) <> "F" Or Chr$(FDT(1)) <> "D" Or Chr$(FDT(2)) <> "T" Or FDT(3) <> 0 Or FDT(4) <> 0 Or FDT(5) <> 2 Then
      GoTo OF_FDT_ReadError
   End If
   Call SetOpenFileStructure(FDT())
   OpenFile = FreeFile
   FS_FileName = Trim$(FD.FileName)
   If Dir(FS_FileName) = FS_FileName Then
      Kill FS_FileName
   End If
   Open FS_FileName For Binary As OpenFile Len = 1
   Rem MultiSection reading is not ready yet !
   SectorCount = FD.SectorCount
   Rem LL = 0
   For K = 1 To SectorCount
       ReturnCode = ReadSector(DriveName, FileNumber + K, 1, File_Data())
       If ReturnCode <> 0 Then Exit For
       If FD.FileSize > LL + 512 Then
          For X = 0 To 511
              LL = LL + 1
              Y = File_Data(X)
              Put OpenFile, LL, Y
          Next X
       Else
          If FD.FileSize > LL Then
             For X = 0 To (FD.FileSize - LL) - 1
                 LL = LL + 1
                 Y = File_Data(X)
                 Put OpenFile, LL, Y
             Next X
          End If
          Exit For
       End If
   Next K
   If ReturnCode <> 0 Then
OF_File_ReadError:
          MsgBox ("File Reading Error !"), 48, " Reading File Data"
          Close OpenFile
          Kill FS_FileName
          OpenFile = 0
   Else
       Close OpenFile
       If FileLen(FS_FileName) <> FD.FileSize Then
          MsgBox ("File Size Error !"), 48, " Reading File Data"
          Kill FS_FileName
          OpenFile = 0
       Else
          Form1.Label1.Caption = "Open FS File : " + FS_FileName
       End If
   End If
End If
End Function

Private Sub SetOpenFileStructure(Buffer() As Byte)
   FD.FDTSign = Chr(Buffer(0)) + Chr(Buffer(1)) + Chr(Buffer(2))
   FD.FDTVersion = Buffer(3)
   FD.SectorSize = Buffer(5) * 256 + Buffer(4)
   S2 = CLng(Buffer(7))
   S2 = 256 * S2
   S3 = CInt(Buffer(6))
   FD.FDTNumber = S2 + S3
   S1 = CDbl(Buffer(11))
   S1 = 16777216 * S1
   S2 = CLng(Buffer(10))
   S2 = 65536 * S2
   S3 = CLng(Buffer(9))
   S3 = S3 * 256
   S4 = CInt(Buffer(8))
   FD.FileNumber = S1 + S2 + S3 + S4
   S1 = CDbl(Buffer(15))
   S1 = 16777216 * S1
   S2 = CLng(Buffer(14))
   S2 = 65536 * S2
   S3 = CLng(Buffer(13))
   S3 = S3 * 256
   S4 = CInt(Buffer(12))
   FD.NextFDTNumber = S1 + S2 + S3 + S4
   S1 = CDbl(Buffer(19))
   S1 = 16777216 * S1
   S2 = CLng(Buffer(18))
   S2 = 65536 * S2
   S3 = CLng(Buffer(17))
   S3 = S3 * 256
   S4 = CInt(Buffer(16))
   FD.SectorCount = S1 + S2 + S3 + S4
   S1 = CDbl(Buffer(23))
   S1 = 16777216 * S1
   S2 = CLng(Buffer(22))
   S2 = 65536 * S2
   S3 = CLng(Buffer(21))
   S3 = S3 * 256
   S4 = CInt(Buffer(20))
   FD.ParentDirNumber = S1 + S2 + S3 + S4
   S1 = CLng(Buffer(27))
   S1 = CLng(256 * S1)
   S2 = CLng(Buffer(26))
   S3 = CLng(Buffer(25))
   S3 = CLng(S3 * 256)
   S4 = CInt(Buffer(24))
   FD.ParentDirSerial2 = S1 + S2
   FD.ParentDirSerial1 = S3 + S4
   S1 = CDbl(Buffer(31))
   S1 = 16777216 * S1
   S2 = CLng(Buffer(30))
   S2 = 65536 * S2
   S3 = CLng(Buffer(29))
   S3 = S3 * 256
   S4 = CInt(Buffer(28))
   FD.FileSize = S1 + S2 + S3 + S4
   FD.FileSizeHigh = Buffer(33) * 256 + Buffer(32)
   FD.FileType = Buffer(35) * 256 + Buffer(34)
   FD.CheckSum = Buffer(36)
   FD.Attributes = Buffer(37)
   FD.OwnerCode = 0
   FD.GroupCode = 0
   FD.Country = Buffer(46)
   FD.TimeZone = Buffer(47)
   FD.CreatingCentury = Buffer(48)
   FD.CreatingYear = Buffer(49)
   FD.CreatingMonth = Buffer(50)
   FD.CreatingDay = Buffer(51)
   FD.CreatingHour = Buffer(52)
   FD.CreatingMinute = Buffer(53)
   FD.CreatingSecond = Buffer(54)
   FD.CTimeMode = Buffer(55)
   FD.LastModifCentury = Buffer(56)
   FD.LastModifYear = Buffer(57)
   FD.LastModifMonth = Buffer(58)
   FD.LastModifDay = Buffer(59)
   FD.LastModifHour = Buffer(60)
   FD.LastModifMinute = Buffer(61)
   FD.LastModifSecond = Buffer(62)
   FD.LMTimeMode = Buffer(63)
   Satir = ""
   For X = 1 To 64
       Y = Buffer(63 + X)
       If Y > 0 Then
          Satir = Satir + Chr(Y)
       Else
          Exit For
       End If
   Next X
   FD.FileName = Satir
   Satir = ""
   For X = 1 To 128
       Y = Buffer(127 + X)
       If Y > 0 Then
          Satir = Satir + Chr(Y)
       Else
          Exit For
       End If
   Next X
   FD.FileDescription = Satir
End Sub

Private Sub ResetDDTBuffer()
For X = 0 To 511
    DDT(X) = 0
Next X
End Sub

Private Function SetDDTBuffer()
For X = 0 To 2
    DDT(X) = AscB(Mid$(DD.DDTSign, X + 1, 1))
Next X
DDT(3) = DD.DDTVersion
L = DD.SectorSize Mod 256
DDT(4) = CByte(L)
L = DD.SectorSize - L
L = L / 256
DDT(5) = CByte(L)
L = DD.DDTNumber Mod 256
DDT(6) = CByte(L)
L = DD.DDTNumber - L
L = L / 256
DDT(7) = CByte(L)
K = DD.DirectoryNumber
L = CInt(K Mod 256)
DDT(8) = CByte(L)
K = (K - L) / 256
L = K Mod 256
DDT(9) = CByte(L)
K = (K - L) / 256
L = K Mod 256
DDT(10) = CByte(L)
K = (K - L) / 256
DDT(11) = CByte(K)
K = DD.NextDDTNumber
L = CInt(K Mod 256)
DDT(12) = CByte(L)
K = (K - L) / 256
L = K Mod 256
DDT(13) = CByte(L)
K = (K - L) / 256
L = K Mod 256
DDT(14) = CByte(L)
K = (K - L) / 256
DDT(15) = CByte(K)
K = DD.SectorCount
L = CInt(K Mod 256)
DDT(16) = CByte(L)
K = (K - L) / 256
L = K Mod 256
DDT(17) = CByte(L)
K = (K - L) / 256
L = K Mod 256
DDT(18) = CByte(L)
K = (K - L) / 256
DDT(19) = CByte(K)
K = DD.ParentDirNumber
L = CInt(K Mod 256)
DDT(20) = CByte(L)
K = (K - L) / 256
L = K Mod 256
DDT(21) = CByte(L)
K = (K - L) / 256
L = K Mod 256
DDT(22) = CByte(L)
K = (K - L) / 256
DDT(23) = CByte(K)
K = DD.ParentDirSerial1
L = K Mod 256
DDT(24) = CByte(L)
K = (K - L) / 256
L = K Mod 256
DDT(25) = CByte(L)
K = DD.ParentDirSerial2
L = K Mod 256
DDT(26) = CByte(L)
K = (K - L) / 256
L = K Mod 256
DDT(27) = CByte(L)
K = DD.DirectorySerial1
L = K Mod 256
DDT(28) = CByte(L)
K = (K - L) / 256
L = K Mod 256
DDT(29) = CByte(L)
K = DD.DirectorySerial2
L = K Mod 256
DDT(30) = CByte(L)
K = (K - L) / 256
L = K Mod 256
DDT(31) = CByte(L)
DDT(32) = &H4
Rem Sub Directory Level (Must Be Modified Later)
DDT(33) = 0
L = CInt(DD.DirectoryType Mod 256)
DDT(34) = CByte(L)
L = DD.DirectoryType - L
L = L / 256
DDT(35) = CByte(L)
DDT(36) = DD.CheckSum
DDT(37) = DD.Attributes
For X = 38 To 45
DDT(X) = 0
Next X
DDT(46) = DD.Country
DDT(47) = DD.TimeZone
DDT(48) = DD.CreatingCentury
DDT(49) = DD.CreatingYear
DDT(50) = DD.CreatingMonth
DDT(51) = DD.CreatingDay
DDT(52) = DD.CreatingHour
DDT(53) = DD.CreatingMinute
DDT(54) = DD.CreatingSecond
DDT(55) = DD.CTimeMode
DDT(56) = DD.LastModifCentury
DDT(57) = DD.LastModifYear
DDT(58) = DD.LastModifMonth
DDT(59) = DD.LastModifDay
DDT(60) = DD.LastModifHour
DDT(61) = DD.LastModifMinute
DDT(62) = DD.LastModifSecond
DDT(63) = DD.LMTimeMode
Satir = Trim$(DD.DirectoryName)
L = Len(Satir)
If L > 0 Then
   For X = 1 To L
       DDT(63 + X) = AscB(Mid$(Satir, X, 1))
   Next X
End If
If L < 64 Then
   For X = L + 1 To 64
       DDT(63 + X) = 0
   Next X
End If
Satir = Trim$(DD.DirectoryDescription)
L = Len(Satir)
If L > 0 Then
   For X = 1 To L
       DDT(127 + X) = AscB(Mid$(Satir, X, 1))
   Next X
End If
If L < 128 Then
   For X = L + 1 To 128
       DDT(127 + X) = 0
   Next X
End If
End Function

Private Function FindFirstFile(Drive As Integer, File As String) As Double
Rem Found First File - Drive Number
FFF_DRV = 0
Rem Found First File - File Name
FFF_FNAME = ""
Rem Found First File - Directory Descriptor Table Location
FFF_DDT = 0
Rem Found First File - Directory Data Sector
FFF_DSEC = 0
Rem Found First File - Directory Entry Number
FFF_DENUM = 0
Rem Above parameters are for FindNextFile Procedures
Rem They will be assigned/valid if the first proper file wiil be found.
Call ParseFileName(Drive, File, DirNameBuffer())
FindFirstFile = 0
If PName <> "" And FName <> "" Then
   DDT_Location = LocateOnDirectory(0, PName)
   If DDT_Location = 0 Then
      Exit Function
   Else
      If PName = "/" Then
         ReturnCode = ReadSector(DriveName, DDT_Location, 1, RDT())
         If ReturnCode <> 0 Then
            Exit Function
         End If
         Call SetOpenDirStructure(RDT())
      Else
         Call SetOpenDirStructure(DDT())
      End If
   End If
Repeat1:
   Rem If DD.SectorCount < 1 Then
      Rem FindFirstFile = 0
      Rem Exit Function
   Rem End If
   For K = 1 To DD.SectorCount
       ReturnCode = ReadSector(DriveName, DDT_Location + K, 1, SD_Data())
       If ReturnCode <> 0 Then
          Exit Function
       End If
       For R = 0 To 511 Step 4
           File = ""
           S1 = CDbl(SD_Data(R + 3))
           S1 = 16777216 * S1
           S2 = CLng(SD_Data(R + 2))
           S2 = 65536 * S2
           S3 = CLng(SD_Data(R + 1))
           S3 = S3 * 256
           S4 = CInt(SD_Data(R))
           FDT_Location = S1 + S2 + S3 + S4
           If FDT_Location < 1 Then Exit For
           If FDT_Location < 4294967295# Then
              ReturnCode = ReadSector(DriveName, FDT_Location, 1, FDT())
              If ReturnCode <> 0 Then
                 FDT_Location = 0
                 Exit For
              End If
              For X = 1 To 64
                  Y = FDT(63 + X)
                  If Y = 0 Then Exit For
                  File = File + Chr(Y)
              Next X
              If NameCmp(FName, File) <> False Then
                 FFF_DRV = Drive
                 FFF_FNAME = FName
                 FFF_DDT = DDT_Location
                 FFF_DSEC = CLng(K)
                 FFF_DENUM = R / 4
                 Exit For
              End If
           End If
       Next R
       If FDT_Location < 1 Then Exit For
   Next K
   If FDT_Location < 1 Then Exit Function
   If File = "" Then
      K = DD.NextDDTNumber
      If K > 0 Then
         ReturnCode = ReadSector(DriveName, K, 1, DDT())
         If ReturnCode = 0 Then
            Call SetOpenDirStructure(DDT())
            If DD.DDTSign = "DDT" Or DD.DDTSign = "RDT" Then
               DDT_Location = K
               GoTo Repeat1
            End If
         End If
      End If
   Else
      FindFirstFile = FDT_Location
   End If
End If
End Function

Private Function ParseFileName(Drive As Integer, File As String, NBuffer() As Byte) As Integer
Dim CDir As String
Dim C As Integer
Dim S As Integer
Dim T As Integer
Dim P As Integer
PName = ""
FName = ""
ParseFileName = 0
File = Trim$(File)
L = Len(File)
If L < 1 Then Exit Function
For X = 1 To L
    Y = AscB(Mid$(File, X, 1))
    If Y = AscB("/") Then
       S = X
    End If
Next X
If S < L Then
   FName = Right$(File, L - S)
End If
If S > 0 Then
   PName = Left$(File, S)
End If
L = Len(PName)
If L > 0 Then
   For X = 1 To L
       Y = AscB(Mid$(PName, X, 1))
       If Y = AscB("?") Or Y = AscB("*") Or Y = AscB(":") Or Y = AscB("\") Then
          L = -1
          Exit For
       End If
   Next X
End If
If L < 0 Then Exit Function
L = Len(FName)
If L > 0 Then
   For X = 1 To L
       Y = AscB(Mid$(FName, X, 1))
       If Y = AscB("/") Or Y = AscB(":") Or Y = AscB("\") Then
          L = -1
          Exit For
       End If
   Next X
End If
If L < 0 Then Exit Function
If FS_DirName = "" Then FS_DirName = "/"
If PName <> "" Then
   If Left$(PName, 1) <> "/" Then
      If Right$(FS_DirName, 1) <> "/" Then
         FS_DirName = FS_DirName + "/"
      End If
      PName = FS_DirName + PName
   End If
Else
   Rem Current Directory, no path
   Rem !!! NOTE Here !!! Remove That Later !!! PName Must Be Empty !
   PName = FS_DirName
End If
L = Len(PName)
If L > 512 Then
   L = 512
   S = 0
   For X = 1 To L
       Y = AscB(Mid$(PName, X, 1))
       If Y = AscB("/") Then
          S = X
       End If
   Next X
   PName = Left$(PName, S)
End If
End Function

Private Function LocateOnDirectory(Drive As Integer, Directory As String) As Double
Dim LDir As String
Dim C As String * 1
Dim S As Integer
Dim D As Double
L = Len(Directory)
If L > 1 Then
   If Left$(Directory, 1) = "/" Then
      LocateOnDirectory = RDT_Location
      S = 2
   Else
      LocateOnDirectory = DDT_Location
      S = 1
   End If
Else
   If Directory = "/" Then
      LocateOnDirectory = RDT_Location
      S = 2
   Else
      LocateOnDirectory = DDT_Location
      S = 1
   End If
End If
If S > L Or LocateOnDirectory < 1 Then Exit Function
X = S
S = 0
For X = X To L
    C = Mid$(Directory, X, 1)
    If C <> "/" Then
       LDir = LDir + C
    Else
       S = X
       Exit For
    End If
Next X
If S = 0 Then S = L
LODRepeat:
If LDir = "" Then
   LocateOnDirectory = 0
   Exit Function
End If
ReturnCode = ReadSector(DriveName, LocateOnDirectory, 1, DDT())
If ReturnCode <> 0 Then
   LocateOnDirectory = 0
   Exit Function
Else
   Call SetOpenDirStructure(DDT())
End If
If (DD.DDTSign <> "DDT" And DD.DDTSign <> "RDT") Or DD.DDTVersion <> 0 Or DD.SectorSize <> 512 Or DD.SectorCount < 1 Then
   LocateOnDirectory = 0
   Exit Function
End If
For K = 1 To DD.SectorCount
    ReturnCode = ReadSector(DriveName, LocateOnDirectory + K, 1, SD_Data())
    If ReturnCode <> 0 Then
       LocateOnDirectory = 0
       Exit For
    Else
       For D = 0 To 511 Step 4
           S1 = CDbl(SD_Data(D + 3))
           S1 = 16777216 * S1
           S2 = CLng(SD_Data(D + 2))
           S2 = 65536 * S2
           S3 = CLng(SD_Data(D + 1))
           S3 = S3 * 256
           S4 = CInt(SD_Data(D))
           DDT_Location = S1 + S2 + S3 + S4
           If DDT_Location = 0 Then
              LocateOnDirectory = 0
              Exit For
           End If
           If DDT_Location < 4294967295# Then
              ReturnCode = ReadSector(DriveName, DDT_Location, 1, DDT())
              FS_DirName = ""
              If ReturnCode = 0 Then
                 If DDT(0) = AscB("D") And DDT(1) = AscB("D") And DDT(2) = AscB("T") Then
                    For X = 1 To 64
                        Y = DDT(63 + X)
                        If Y > 0 Then
                           FS_DirName = FS_DirName + Chr$(Y)
                        Else
                           Exit For
                        End If
                    Next X
                 End If
                 If FS_DirName <> "" Then
                    If Trim$(FS_DirName) = LDir Then
                       LocateOnDirectory = DDT_Location
                       Exit For
                    End If
                 End If
              Else
                 LocateOnDirectory = 0
                 Exit For
              End If
           End If
       Next D
    End If
Next K
If LocateOnDirectory = 0 Then Exit Function
If S < L Then
   LDir = ""
   For X = S + 1 To L
       C = Mid$(Directory, X, 1)
       If C <> "/" Then
          S = S + 1
          LDir = LDir + C
       Else
          S = X
          Exit For
       End If
   Next X
   GoTo LODRepeat
End If
End Function

Private Sub SetOpenDirStructure(Buffer() As Byte)
   DD.DDTSign = Chr(Buffer(0)) + Chr(Buffer(1)) + Chr(Buffer(2))
   DD.DDTVersion = Buffer(3)
   DD.SectorSize = Buffer(5) * 256 + Buffer(4)
   S2 = CLng(Buffer(7))
   S2 = 256 * S2
   S3 = CInt(Buffer(6))
   DD.DDTNumber = S2 + S3
   S1 = CDbl(Buffer(11))
   S1 = 16777216 * S1
   S2 = CLng(Buffer(10))
   S2 = 65536 * S2
   S3 = CLng(Buffer(9))
   S3 = S3 * 256
   S4 = CInt(Buffer(8))
   DD.DirectoryNumber = S1 + S2 + S3 + S4
   S1 = CDbl(Buffer(15))
   S1 = 16777216 * S1
   S2 = CLng(Buffer(14))
   S2 = 65536 * S2
   S3 = CLng(Buffer(13))
   S3 = S3 * 256
   S4 = CInt(Buffer(12))
   DD.NextDDTNumber = S1 + S2 + S3 + S4
   S1 = CDbl(Buffer(19))
   S1 = 16777216 * S1
   S2 = CLng(Buffer(18))
   S2 = 65536 * S2
   S3 = CLng(Buffer(17))
   S3 = S3 * 256
   S4 = CInt(Buffer(16))
   DD.SectorCount = S1 + S2 + S3 + S4
   S1 = CDbl(Buffer(23))
   S1 = 16777216 * S1
   S2 = CLng(Buffer(22))
   S2 = 65536 * S2
   S3 = CLng(Buffer(21))
   S3 = S3 * 256
   S4 = CInt(Buffer(20))
   DD.ParentDirNumber = S1 + S2 + S3 + S4
   S1 = CLng(Buffer(27))
   S1 = CLng(256 * S1)
   S2 = CLng(Buffer(26))
   S3 = CLng(Buffer(25))
   S3 = CLng(S3 * 256)
   S4 = CInt(Buffer(24))
   DD.ParentDirSerial2 = S1 + S2
   DD.ParentDirSerial1 = S3 + S4
   S1 = CLng(Buffer(31))
   S1 = CLng(256 * S1)
   S2 = CLng(Buffer(30))
   S3 = CLng(Buffer(29))
   S3 = CLng(S3 * 256)
   S4 = CInt(Buffer(28))
   DD.DirectorySerial2 = S1 + S2
   DD.DirectorySerial1 = S3 + S4
   DD.EntrySize = Buffer(33) * 256 + Buffer(32)
   DD.DirectoryType = Buffer(35) * 256 + Buffer(34)
   DD.CheckSum = Buffer(36)
   DD.Attributes = Buffer(37)
   DD.OwnerCode = 0
   DD.GroupCode = 0
   DD.Country = Buffer(46)
   DD.TimeZone = Buffer(47)
   DD.CreatingCentury = Buffer(48)
   DD.CreatingYear = Buffer(49)
   DD.CreatingMonth = Buffer(50)
   DD.CreatingDay = Buffer(51)
   DD.CreatingHour = Buffer(52)
   DD.CreatingMinute = Buffer(53)
   DD.CreatingSecond = Buffer(54)
   DD.CTimeMode = Buffer(55)
   DD.LastModifCentury = Buffer(56)
   DD.LastModifYear = Buffer(57)
   DD.LastModifMonth = Buffer(58)
   DD.LastModifDay = Buffer(59)
   DD.LastModifHour = Buffer(60)
   DD.LastModifMinute = Buffer(61)
   DD.LastModifSecond = Buffer(62)
   DD.LMTimeMode = Buffer(63)
   Satir = ""
   For X = 1 To 64
       Y = Buffer(63 + X)
       If Y > 0 Then
          Satir = Satir + Chr(Y)
       Else
          Exit For
       End If
   Next X
   DD.DirectoryName = Satir
   Satir = ""
   For X = 1 To 128
       Y = Buffer(127 + X)
       If Y > 0 Then
          Satir = Satir + Chr(Y)
       Else
          Exit For
       End If
   Next X
   DD.DirectoryDescription = Satir
End Sub

Public Function ChangeDirectory(Drive As Integer, Directory As String) As Double
Call ParseFileName(Drive, Directory, DirNameBuffer())
Rem If PName <> "" And FName <> "" Then
If PName <> "" Then
   If FName <> "" Then
      If PName = "/" Then
         SubDirName = "/" + FName
      Else
         If Right$(PName, 1) <> "/" Then
            SubDirName = PName + "/" + FName
         Else
            SubDirName = PName + FName
         End If
      End If
   Else
      SubDirName = PName
   End If
   ChangeDirectory = LocateOnDirectory(0, SubDirName)
Else
   If FName <> "" Then
      SubDirName = FName
      ChangeDirectory = LocateOnDirectory(0, SubDirName)
   Else
      ChangeDirectory = 0
   End If
End If
If ChangeDirectory > 0 Then
   FS_CDIR = SubDirName
   FS_DirNumber = ChangeDirectory
End If
End Function

Private Sub FillFlexGridCells()
ReturnCode = ReadSector(DriveName, FDT_Location, 1, FDT())
If ReturnCode = 0 Then
   FS_FileName = ""
   For S4 = 1 To 64
       Y = FDT(63 + S4)
       If Asc(Y) > 0 Then
          FS_FileName = FS_FileName + Chr$(Y)
       Else
          Exit For
       End If
   Next S4
   R = R + 1
   frmListe.MSFlexGrid1.Rows = R + 1
   frmListe.MSFlexGrid1.Row = R
   frmListe.MSFlexGrid1.Col = 0
   frmListe.MSFlexGrid1.Text = CStr(FDT_Location)
   frmListe.MSFlexGrid1.Col = 1
   frmListe.MSFlexGrid1.Text = FS_FileName
   frmListe.MSFlexGrid1.Col = 2
   If Chr(FDT(0)) = "F" And Chr(FDT(1)) = "D" And Chr(FDT(2)) = "T" Then
      frmListe.MSFlexGrid1.Text = "File"
   Else
      If Chr(FDT(0)) = "D" And Chr(FDT(1)) = "D" And Chr(FDT(2)) = "T" Then
         frmListe.MSFlexGrid1.Text = "DIR"
      Else
         frmListe.MSFlexGrid1.Text = "-"
      End If
   End If
   AttributeStr = "r"
   S4 = CInt(FDT(37))
   L = S4 Mod 2
   If L > 0 Then
      AttributeStr = AttributeStr + "w"
   Else
      AttributeStr = AttributeStr + "-"
   End If
   S4 = (S4 - L) / 2
   L = S4 Mod 2
   If L > 0 Then
      AttributeStr = AttributeStr + "x"
   Else
      AttributeStr = AttributeStr + "-"
   End If
   S4 = (S4 - L) / 2
   L = S4 Mod 2
   If L > 0 Then
      AttributeStr = AttributeStr + "r"
   Else
      AttributeStr = AttributeStr + "-"
   End If
   S4 = (S4 - L) / 2
   L = S4 Mod 2
   If L > 0 Then
      AttributeStr = AttributeStr + "w"
   Else
      AttributeStr = AttributeStr + "-"
   End If
   S4 = (S4 - L) / 2
   L = S4 Mod 2
   If L > 0 Then
      AttributeStr = AttributeStr + "x"
   Else
      AttributeStr = AttributeStr + "-"
   End If
   S4 = (S4 - L) / 2
   L = S4 Mod 2
   If L > 0 Then
      AttributeStr = AttributeStr + "r"
   Else
      AttributeStr = AttributeStr + "-"
   End If
   S4 = (S4 - L) / 2
   L = S4 Mod 2
   If L > 0 Then
      AttributeStr = AttributeStr + "w"
   Else
      AttributeStr = AttributeStr + "-"
   End If
   S4 = (S4 - L) / 2
   L = S4 Mod 2
   If L > 0 Then
      AttributeStr = AttributeStr + "x"
   Else
      AttributeStr = AttributeStr + "-"
   End If
   frmListe.MSFlexGrid1.Col = 3
   frmListe.MSFlexGrid1.Text = AttributeStr
   frmListe.MSFlexGrid1.Col = 4
   If Chr(FDT(0)) = "F" And Chr(FDT(1)) = "D" And Chr(FDT(2)) = "T" Then
      S1 = CDbl(FDT(31))
      S1 = 16777216 * S1
      S2 = CLng(FDT(30))
      S2 = 65536 * S2
      S3 = CLng(FDT(29))
      S3 = S3 * 256
      S4 = CInt(FDT(28))
      K = S1 + S2 + S3 + S4
      frmListe.MSFlexGrid1.Text = CStr(K) + " bytes "
   Else
      frmListe.MSFlexGrid1.Text = ""
   End If
   frmListe.MSFlexGrid1.Col = 5
   frmListe.MSFlexGrid1.Text = ByteToHex(FDT(59)) + "/" + ByteToHex(FDT(58)) + "/" + ByteToHex(FDT(56)) + ByteToHex(FDT(57))
   frmListe.MSFlexGrid1.Col = 6
   frmListe.MSFlexGrid1.Text = ByteToHex(FDT(60)) + ":" + ByteToHex(FDT(61)) + ":" + ByteToHex(FDT(62))
End If
End Sub

Private Function NameCmp(Name1 As String, Name2 As String) As Boolean
Dim A As Integer
Dim B As Integer
Dim C As Integer
Dim D As Integer
Dim E As Integer
Dim F As Integer
Dim H1 As String * 1
Dim H2 As String * 1
Dim Star As Boolean
NameCmp = False
A = Len(Name1)
B = Len(Name2)
If A = 0 Or B = 0 Then Exit Function
If A = 1 Then
   If Name1 = "*" Then
      NameCmp = True
      Exit Function
   Else
      If Name1 = "?" And B = 1 Then
         NameCmp = True
         Exit Function
      Else
         If Name1 = Name2 Then
            NameCmp = True
         End If
      End If
   End If
Else
   E = 1
   Rem F = 0
   For C = 1 To A
       F = F + 1
       H1 = Mid$(Name1, F, 1)
       If H1 = "*" Then
          Star = True
R1:
          If F = A Then
             NameCmp = True
             Exit For
          End If
          F = F + 1
          H1 = Mid$(Name1, F, 1)
          If H1 = "*" Then GoTo R1
          For D = E To B
              H2 = Mid$(Name2, D, 1)
              If H2 = H1 Then
                 If D < B Then
                    E = D + 1
                 End If
                 D = 0
                 Exit For
              End If
          Next D
          If D <> 0 Then Exit For
       Else
          If H1 <> "?" Then
             For D = E To B
                 H2 = Mid$(Name2, D, 1)
                 If H2 = H1 Then
                    If D = B Then
                       E = B
                       NameCmp = True
                    End If
                    E = E + 1
                    Exit For
                 Else
                    If Star = True Then
                       E = E + 1
                    Else
                       E = B + 1
                       Exit For
                    End If
                 End If
             Next D
             If E > B Then
                Exit For
             End If
          Else
             If E = B Then
                NameCmp = True
                Exit For
             Else
                E = E + 1
             End If
          End If
       End If
   Next C
End If
End Function

Private Sub UpdateFSInfo()
   Label1.Caption = "FS Volume Name : " + VolumeName
   K = FS_VolSerialNo
   L = CInt(K Mod 256)
   VolSerialNoStr = ByteToHex(CByte(L))
   K = (K - L) / 256
   L = K Mod 256
   VolSerialNoStr = VolSerialNoStr + ByteToHex(CByte(L))
   K = (K - L) / 256
   L = K Mod 256
   VolSerialNoStr = VolSerialNoStr + "-" + ByteToHex(CByte(L))
   K = (K - L) / 256
   VolSerialNoStr = VolSerialNoStr + ByteToHex(CByte(K))
   Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "Volume Serial No : " + VolSerialNoStr
   Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + Chr$(13) + Chr$(10) + "Volume Size : " + CStr(Volume_Size) + " sectors  ( " + CStr(512 * Volume_Size) + " bytes )"
   Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "Free Sectors : " + CStr(Free_Sectors) + "  ( " + CStr(512 * Free_Sectors) + " bytes )"
End Sub

Private Function FindNextFile() As Double
FindNextFile = 0
DDT_Location = FFF_DDT
Rem ReturnCode = ReadSector(FFF_DRV, FFF_DDT, 1, DDT())
Rem If ReturnCode<=0 Then
   Rem Exit Function
Rem Else
   Rem Call SetOpenDirStructure(DDT())
Rem End If
If FFF_DENUM > 126 Then
   If FFF_DSEC < DD.SectorCount Then
      FFF_DSEC = FFF_DSEC + 1
      FFF_DENUM = 0
   Else
      If DD.NextDDTNumber > 0 Then
         FFF_DDT = DD.NextDDTNumber
         FFF_DSEC = 1
         FFF_DENUM = 0
         DDT_Location = FFF_DDT
      Else
         Exit Function
      End If
   End If
Else
   FFF_DENUM = FFF_DENUM + 1
End If
If FFF_DSEC > 0 And FFF_DSEC <= DD.SectorCount Then
NRepeat1:
Rem If DD.SectorCount < 1 Then
    Rem FindNextFile = 0
    Rem Exit Function
Rem End If
   For K = FFF_DSEC To DD.SectorCount
       ReturnCode = ReadSector(FFF_DRV, DDT_Location + K, 1, SD_Data())
       If ReturnCode <> 0 Then
          Exit Function
       End If
       For R = (4 * FFF_DENUM) To 511 Step 4
           Satir = ""
           S1 = CDbl(SD_Data(R + 3))
           S1 = 16777216 * S1
           S2 = CLng(SD_Data(R + 2))
           S2 = 65536 * S2
           S3 = CLng(SD_Data(R + 1))
           S3 = S3 * 256
           S4 = CInt(SD_Data(R))
           FDT_Location = S1 + S2 + S3 + S4
           If FDT_Location < 1 Then Exit For
           If FDT_Location < 4294967295# Then
              ReturnCode = ReadSector(FFF_DRV, FDT_Location, 1, FDT())
              If ReturnCode <> 0 Then
                 FDT_Location = 0
                 Exit For
              End If
              For X = 1 To 64
                  Y = FDT(63 + X)
                  If Y = 0 Then Exit For
                  Satir = Satir + Chr(Y)
              Next X
              If NameCmp(FFF_FNAME, Satir) <> False Then
                 FFF_DDT = DDT_Location
                 FFF_DSEC = CLng(K)
                 FFF_DENUM = R / 4
                 Exit For
              End If
           End If
       Next R
       If FDT_Location < 1 Then Exit For
   Next K
   If FDT_Location < 1 Then Exit Function
   If Satir = "" Then
      K = DD.NextDDTNumber
      If K > 0 Then
         ReturnCode = ReadSector(FFF_DRV, K, 1, DDT())
         If ReturnCode = 0 Then
            Call SetOpenDirStructure(DDT())
            If DD.DDTSign = "DDT" Or DD.DDTSign = "RDT" Then
               DDT_Location = K
               FFF_DSEC = 1
               FFF_DENUM = 0
               GoTo NRepeat1
            End If
         End If
      End If
   Else
      FindNextFile = FDT_Location
   End If
End If
End Function

Private Sub fsStartupFileInfo_Click()
If FS_StartupFileNumber = 0 Then
   MsgBox ("FS Startup File is not configured yet!"), 48, " FS Startup File Information"
   Exit Sub
End If
ReturnCode = ReadSector(DriveName, FS_StartupFileNumber, 1, FDT())
If ReturnCode <> 0 Then
OSF_FDT_ReadError:
   MsgBox ("File Descriptor Table Reading Error !"), 48, " Reading File Descriptor Table"
   Exit Sub
Else
   If Chr$(FDT(0)) <> "F" Or Chr$(FDT(1)) <> "D" Or Chr$(FDT(2)) <> "T" Or FDT(3) <> 0 Or FDT(4) <> 0 Or FDT(5) <> 2 Then
      GoTo OSF_FDT_ReadError
   End If
   FS_FileName = ""
   For S4 = 1 To 64
       Y = FDT(63 + S4)
       If Asc(Chr$(Y)) > 0 Then
          FS_FileName = FS_FileName + Chr$(Y)
       Else
          Exit For
       End If
   Next S4
   Label1.Caption = "FS Startup File Name : " + Trim$(FS_FileName) + Chr$(13) + Chr$(10)
   AttributeStr = "r"
   S4 = CInt(FDT(37))
   L = S4 Mod 2
   If L > 0 Then
      AttributeStr = AttributeStr + "w"
   Else
      AttributeStr = AttributeStr + "-"
   End If
   S4 = (S4 - L) / 2
   L = S4 Mod 2
   If L > 0 Then
      AttributeStr = AttributeStr + "x"
   Else
      AttributeStr = AttributeStr + "-"
   End If
   S4 = (S4 - L) / 2
   L = S4 Mod 2
   If L > 0 Then
      AttributeStr = AttributeStr + "r"
   Else
      AttributeStr = AttributeStr + "-"
   End If
   S4 = (S4 - L) / 2
   L = S4 Mod 2
   If L > 0 Then
      AttributeStr = AttributeStr + "w"
   Else
      AttributeStr = AttributeStr + "-"
   End If
   S4 = (S4 - L) / 2
   L = S4 Mod 2
   If L > 0 Then
      AttributeStr = AttributeStr + "x"
   Else
      AttributeStr = AttributeStr + "-"
   End If
   S4 = (S4 - L) / 2
   L = S4 Mod 2
   If L > 0 Then
      AttributeStr = AttributeStr + "r"
   Else
      AttributeStr = AttributeStr + "-"
   End If
   S4 = (S4 - L) / 2
   L = S4 Mod 2
   If L > 0 Then
      AttributeStr = AttributeStr + "w"
   Else
      AttributeStr = AttributeStr + "-"
   End If
   S4 = (S4 - L) / 2
   L = S4 Mod 2
   If L > 0 Then
      AttributeStr = AttributeStr + "x"
   Else
      AttributeStr = AttributeStr + "-"
   End If
   Label1.Caption = Label1.Caption + "Attributes : " + AttributeStr + Chr$(13) + Chr$(10)
   S1 = CDbl(FDT(31))
   S1 = 16777216 * S1
   S2 = CLng(FDT(30))
   S2 = 65536 * S2
   S3 = CLng(FDT(29))
   S3 = S3 * 256
   S4 = CInt(FDT(28))
   K = S1 + S2 + S3 + S4
   Label1.Caption = Label1.Caption + "File Size : " + CStr(K) + " bytes" + Chr$(13) + Chr$(10)
   Label1.Caption = Label1.Caption + "Last Modification Date : " + ByteToHex(FDT(59)) + "/" + ByteToHex(FDT(58)) + "/" + ByteToHex(FDT(56)) + ByteToHex(FDT(57)) + Chr$(13) + Chr$(10)
   Label1.Caption = Label1.Caption + "Last Modification Time : " + ByteToHex(FDT(60)) + ":" + ByteToHex(FDT(61)) + ":" + ByteToHex(FDT(62))
End If
End Sub

Private Sub fsDeleteStartupFile_Click()
If FS_StartupFileNumber = 0 Then
   MsgBox ("FS Startup File is not configured !"), 0, " Delete FS Startup File"
   Exit Sub
End If
ReturnCode = ReadSector(DriveName, FS_StartupFileNumber, 1, FDT())
If ReturnCode > 0 Then
   If FDT(0) = AscB("F") And FDT(1) = AscB("D") And FDT(2) = AscB("T") Then
      Call SetOpenFileStructure(FDT())
   Else
      MsgBox ("File could not be found!"), 0, " Delete FS Startup File"
      Exit Sub
   End If
Else
   MsgBox ("File could not be found!"), 0, " Delete FS Startup File"
   Exit Sub
End If
Rem If FD.SectorCount < 1 Then Exit Sub
FS_FileName = Trim$(FD.FileName)
If MsgBox("Do you want to delete startup file '" + FS_FileName + "' ?", 36, " Delete FS Startup File") <> 6 Then Exit Sub
Label1.Caption = "FS Startup File FDT : " + CStr(FS_StartupFileNumber)
DNS:
Rem Erased/Deleted Sign
FDT(2) = AscB("E")
ReturnCode = WriteSector(DriveName, FS_StartupFileNumber, 1, FDT())
If ReturnCode <> 0 Then
   MsgBox ("Writing Error !"), 48, " Writing FDT Sector"
   Exit Sub
End If
FDT_Location = FD.FileNumber
SectorCount = FD.SectorCount
FreeSpace = Free_Sectors + SectorCount + 1
ReturnCode = ReadSector(DriveName, DAT_Location, DAT_SCount, DAT())
If ReturnCode <> 0 Then
   MsgBox ("Reading Error !"), 48, " Reading DAT Sectors"
   Exit Sub
End If
For K = FDT_Location To FDT_Location + SectorCount
    L = CInt(K)
    DAT(L) = &H90
Next K
ReturnCode = WriteSector(DriveName, DAT_Location, DAT_SCount, DAT())
If ReturnCode <> 0 Then
   MsgBox ("Writing Error !"), 48, " Writing DAT Sectors"
   Exit Sub
End If
Free_Sectors = FreeSpace
ReturnCode = ReadSector(DriveName, MAT_Location, 1, MAT())
If ReturnCode <> 0 Then
   MsgBox ("Reading Error !"), 48, " Reading MAT Sector"
   Exit Sub
End If
Rem FreeSectors
K = FreeSpace
L = CInt(K Mod 256)
MAT(20) = CByte(L)
K = (K - L) / 256
L = K Mod 256
MAT(21) = CByte(L)
K = (K - L) / 256
L = K Mod 256
MAT(22) = CByte(L)
K = (K - L) / 256
MAT(23) = CByte(K)
Rem FirstFreeSector
S1 = CDbl(MAT(27))
S1 = 16777216 * S1
S2 = CLng(MAT(26))
S2 = 65536 * S2
S3 = CLng(MAT(25))
S3 = S3 * 256
S4 = CInt(MAT(24))
K = S1 + S2 + S3 + S4
If K > FDT_Location Then
   K = FDT_Location
   L = CInt(K Mod 256)
   MAT(24) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   MAT(25) = CByte(L)
   K = (K - L) / 256
   L = K Mod 256
   MAT(26) = CByte(L)
   K = (K - L) / 256
   MAT(27) = CByte(K)
End If
ReturnCode = WriteSector(DriveName, MAT_Location, 1, MAT())
If ReturnCode <> 0 Then
   MsgBox ("Writing Error !"), 48, " Writing MAT Sector"
   Exit Sub
End If
SectorCount = SectorCount + FD.SectorCount + 1
K = FD.NextFDTNumber
If K > 0 Then
   ReturnCode = ReadSector(DriveName, K, 1, FDT())
   If ReturnCode <> 0 Then Exit Sub
   If FDT(0) = AscB("F") And FDT(1) = AscB("D") And FDT(2) = AscB("T") Then
      Call SetOpenFileStructure(FDT())
   End If
   GoTo DNS
End If
Rem Deleting Startup File FDT Number on the Boot Sector
ReturnCode = ReadSector(DriveName, 0, 1, BS())
If ReturnCode <> 0 Then
   MsgBox ("Reading Error !"), 48, " Reading Boot Sector"
   Exit Sub
End If
BS(20) = 0
BS(21) = 0
BS(22) = 0
BS(23) = 0
ReturnCode = WriteSector(DriveName, 0, 1, BS())
If ReturnCode <> 0 Then
   MsgBox ("Writing Error !"), 48, " Writing Boot Sector"
Else
   FS_StartupFileNumber = 0
   MsgBox ("FS Startup File '" + FS_FileName + "' has been deleted..."), 0, " Delete FS Startup File"
   Call UpdateFSInfo
End If
End Sub

Private Sub fsfdOpenImage_Click()
CommonDialog1.DialogTitle = "OPEN FS IMAGE FILE"
CommonDialog1.FileName = Chr(0)
CommonDialog1.Filter = "All files (*.*)|*.*|IMG files (*.img)|*.img|DAT files (*.dat)|*.dat|BIN files (*.bin)|*.bin"
CommonDialog1.Flags = OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY Or OFN_EXPLORER
On Error Resume Next
CommonDialog1.ShowOpen
If Err = 0 Then
   ImageFileName = CommonDialog1.FileName
   ImageFileSize = FileLen(ImageFileName)
   If ImageFileSize < 1 Then
      ImageFileName = ""
   End If
Else
   If Err <> 32755 Then
      ImageFileName = ""
      GoTo ErrMsg
   End If
End If
If ImageFileName <> "" Then
   ImageFileSize = FileLen(ImageFileName)
   ImageFileCDateTime = FileDateTime(ImageFileName)
   Label1.Caption = "FS Image File Name : " + ImageFileName + Chr$(13) + Chr$(10) + "FS Image File Size : " + CStr(ImageFileSize) + " bytes" + Chr$(13) + Chr$(10) + "Image File Date and Time : " + ImageFileCDateTime
   If FileLen(ImageFileName) / 512 <> 2880 Then
      MsgBox ("FS Image file is not valid !"), 48, " Open SINGLIX Image"
      ImageFileName = ""
   Else
      Call LoadFSImage
   End If
Else
   Label1.Caption = "Open FS Image File : NONE"
End If
Exit Sub
ErrMsg:
Beep
MsgBox ("SINGLIX FS Image File could not open!..." + Chr$(13) + Chr$(13) + "Error Number: " + Str$(Err)), 48, "! ERROR !"
Label1.Caption = ""
End Sub

Private Sub fsfdSaveImage_Click()
If ImageFileName = "" Then Exit Sub
Dim NewImageFileName As String
CommonDialog1.DialogTitle = "Save FSFD Image File as"
CommonDialog1.FileName = Dir(ImageFileName)
CommonDialog1.Filter = "All files (*.*)|*.*|IMG files (*.img)|*.img|BIN files (*.bin)|*.bin"
CommonDialog1.Flags = OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY Or OFN_EXPLORER
On Error Resume Next
CommonDialog1.ShowSave
If Err = 0 Then
   NewImageFileName = CommonDialog1.FileName
   If NewImageFileName <> "" Then
      If NewImageFileName <> ImageFileName Then
         If Dir(NewImageFileName) <> "" Then
            If MsgBox("Do you want to overwrite file '" + NewImageFileName + " ' ?", 36, " Save FSFD Image File as '" + Dir(NewImageFileName) + "'") <> 6 Then
               Exit Sub
            End If
         End If
         FileCopy ImageFileName, NewImageFileName
         ImageFileName = NewImageFileName
      End If
   End If
   ImageFileSize = FileLen(ImageFileName)
   ImageFileCDateTime = FileDateTime(ImageFileName)
   Label1.Caption = "FS Image File Name : " + ImageFileName + Chr$(13) + Chr$(10) + "FS Image File Size : " + CStr(ImageFileSize) + " bytes" + Chr$(13) + Chr$(10) + "Image File Date and Time : " + ImageFileCDateTime
   MsgBox ("FS Image File has been saved as '" + ImageFileName + "' ..."), 0, " Save FSFD Image File as '" + Dir(NewImageFileName) + "'"
End If
End Sub

Private Sub fsRenameFile_Click()
Dim FS_FileNumber As Double
Dim NewFileName As String
Repeat1:
Satir = InputBox("FS File Name :", " Rename FS File")
Satir = Trim$(Satir)
L = Len(Satir)
If L > 0 Then
   If L > 64 Then
      MsgBox ("FS File name must not be more then 64 bytes !"), 48, " Rename FS File"
      GoTo Repeat1
   Else
      For X = 1 To L
          Y = AscB(Mid$(Satir, X, 1))
          If Y = AscB("/") Or Y = AscB("?") Or Y = AscB("*") Or Y = AscB(":") Or Y = AscB("\") Then
             MsgBox ("Invalid file name chars ('/','?','*',':','\') !..."), 48, " Rename FS File"
             L = 0
             Exit For
          End If
      Next X
      If L < 1 Then GoTo Repeat1
   End If
Else
   Exit Sub
End If
FS_FileNumber = FindFirstFile(0, Satir)
If FS_FileNumber > 0 Then
   ReturnCode = ReadSector(DriveName, FS_FileNumber, 1, FDT())
   If ReturnCode = 0 Then
      If FDT(0) = AscB("F") And FDT(1) = AscB("D") And FDT(2) = AscB("T") Then
         Call SetOpenFileStructure(FDT())
      Else
         MsgBox ("File could not be found!"), 0, " Rename FS File"
         Exit Sub
      End If
   End If
Else
   MsgBox ("File could not be found!"), 0, " Rename FS File"
   Exit Sub
End If
Rem If FD.SectorCount < 1 Then Exit Sub
FS_FileName = Trim$(FD.FileName)
FS_DirNumber = FD.ParentDirNumber
FS_DirSerial1 = FD.ParentDirSerial1
FS_DirSerial2 = FD.ParentDirSerial2
Repeat2:
Satir = InputBox("New FS File Name :", " Rename FS File", FS_FileName)
Satir = Trim$(Satir)
L = Len(Satir)
If L > 0 Then
   If L > 64 Then
      MsgBox ("FS File name must not be more then 64 bytes !"), 48, " Rename FS File"
      GoTo Repeat2
   Else
      For X = 1 To L
          Y = AscB(Mid$(Satir, X, 1))
          If Y = AscB("/") Or Y = AscB("?") Or Y = AscB("*") Or Y = AscB(":") Or Y = AscB("\") Then
             MsgBox ("Invalid file name chars ('/','?','*',':','\') !..."), 48, " Rename FS File"
             L = 0
             Exit For
          End If
      Next X
      If L < 1 Then GoTo Repeat2
   End If
Else
   Exit Sub
End If
If Satir = FS_FileName Then Exit Sub
NewFileName = Satir
K = FindFirstFile(0, Satir)
If K > 0 Then
   MsgBox ("File name '" + NewFileName + "' exists!"), 0, " Rename FS File"
   Exit Sub
End If
If MsgBox("Do you want to rename '" + FS_FileName + "' file as '" + NewFileName + "' ?", 36, " Rename FS File") <> 6 Then Exit Sub
Rem If FS_DirNumber <> FD.ParentDirNumber Or FS_DirSerial <> FD.ParentDirSerial Then
   Rem MsgBox ("Directory/Path name can not be different for renaming a file !"), 0, " Rename FS File"
   Rem Exit Sub
Rem End If
ReturnCode = ReadSector(DriveName, FS_FileNumber, 1, FDT())
If ReturnCode = 0 Then
   L = Len(NewFileName)
   For X = 1 To L
       FDT(63 + X) = AscB(Mid$(NewFileName, X, 1))
   Next X
   If L < 64 Then
      For X = L + 1 To 64
          FDT(63 + X) = 0
      Next X
   End If
   ReturnCode = WriteSector(DriveName, FS_FileNumber, 1, FDT())
   If ReturnCode <> 0 Then
      MsgBox ("Writing Error !"), 48, " Writing FDT Sector"
   Else
      MsgBox ("'" + FS_FileName + "' has been renamed as '" + NewFileName + " ..."), 0, " Rename FS File"
   End If
Else
   MsgBox ("Reading Error !"), 48, " Reading FDT Sector"
End If
End Sub

Private Sub fsSaveStartupFile_Click()
Rem 20-10-2008 StartupFile Procedures
Label1.Caption = ""
If WinFileName = "" Then
   Label1.Caption = "Windows File : NONE"
   MsgBox ("There is not a loaded Windows file !"), 48, " Save as FS Startup File"
   Label1.Caption = ""
   Exit Sub
Else
   Label1.Caption = "FS StartupFile FDT : " + CStr(FS_StartupFileNumber)
   If FS_StartupFileNumber > 0 Then
      MsgBox ("FS Startup File is configured before !" + Chr$(13) + Chr$(10) + "If you want to configure new one, you must delete current Startup File ... "), 48, " Save as FS Startup File"
      Exit Sub
   End If
End If
FS_FileName = ""
Repeat1:
Label1.Caption = "FS Startup File : "
FS_FileName = InputBox("FS Startup File Name :", " Save as Singlix FS Startup File", WinFileName)
FS_FileName = Trim$(FS_FileName)
Label1.Caption = Label1.Caption + FS_FileName
L = Len(FS_FileName)
If L > 0 Then
   If L > 64 Then
      MsgBox ("FS Startup File name must not be more then 64 bytes !"), 48, " Save as Singlix FS Startup File"
      GoTo Repeat1
   Else
      For X = 1 To L
          Y = AscB(Mid$(FS_FileName, X, 1))
          If Y = AscB("/") Or Y = AscB("?") Or Y = AscB("*") Or Y = AscB(":") Or Y = AscB("\") Then
             MsgBox ("Invalid file name chars ('/','?','*',':','\') !..."), 48, " Save as Singlix FS Startup File"
             L = 0
             Exit For
          End If
      Next X
      If L < 1 Then GoTo Repeat1
   End If
Else
   Exit Sub
End If
Free_Sectors = GetFreeSpace()
FreeSpace = 512 * Free_Sectors
RequiredSpace = WinFileSize + 512
If FreeSpace < RequiredSpace Then
   MsgBox ("The is no enough free space to save the file on the Singlix File System!..." + Chr$(13) + Chr$(10) + "Free Space = " + CStr(FreeSpace) + " bytes"), 48, " Save as FS Startup File"
   Exit Sub
End If
Rem Sector
K = WinFileSize Mod 512
If K > 0 Then
   SectorCount = ((WinFileSize - K) / 512) + 1
Else
   SectorCount = (WinFileSize - K) / 512
End If
SectorCount = SectorCount + 1
BeginningSector = GetFirstFreeSector()
FDT_Location = GetFirstFreeSection(BeginningSector, SectorCount)
If FDT_Location = 0 Then
   Rem Allocation will be done later, as multi sections
   MsgBox ("Free consecutive sectors are not enough!..."), 48, " Save as FS Startup File"
Else
   Call ResetFDTBuffer
   FD.FDTSign = "FDT"
   FD.FDTVersion = 0
   FD.SectorSize = 512
   FD.FDTNumber = 0
   FD.FileNumber = FDT_Location
   FD.NextFDTNumber = 0
   FD.SectorCount = SectorCount - 1
   FD.ParentDirNumber = 0
   FD.ParentDirSerial1 = 0
   FD.ParentDirSerial2 = 0
   FD.FileSize = WinFileSize
   FD.FileSizeHigh = 0
   FD.FileType = 0
   FD.CheckSum = 0
   FD.Attributes = 1
   FD.OwnerCode = 0
   FD.GroupCode = 0
   FD.Country = 90
   FD.TimeZone = 0
   FS_Second = Second(Now)
   FS_Minute = Minute(Now)
   FS_Hour = Hour(Now)
   FS_Day = Day(Now)
   FS_Month = Month(Now)
   FS_Year = Year(Now)
   FS_Century = 20
   FD.CreatingCentury = SetBCD(FS_Century)
   FD.CreatingYear = SetBCD(FS_Year)
   FD.CreatingMonth = SetBCD(FS_Month)
   FD.CreatingDay = SetBCD(FS_Day)
   FD.CreatingHour = SetBCD(FS_Hour)
   FD.CreatingMinute = SetBCD(FS_Minute)
   FD.CreatingSecond = SetBCD(FS_Second)
   FD.CTimeMode = 0
   FD.LastModifCentury = FD.CreatingCentury
   FD.LastModifYear = FD.CreatingYear
   FD.LastModifMonth = FD.CreatingMonth
   FD.LastModifDay = FD.CreatingDay
   FD.LastModifHour = FD.CreatingHour
   FD.LastModifMinute = FD.CreatingMinute
   FD.LastModifSecond = FD.CreatingSecond
   FD.LMTimeMode = 0
   FD.FileName = FS_FileName
   FD.FileDescription = 0
   Call SetFDTBuffer
   If Chr$(FDT(0)) = "F" And Chr$(FDT(1)) = "D" And Chr$(FDT(2)) = "T" And FDT(3) = 0 And FDT(4) = 0 And FDT(5) = 2 Then
      Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "Writing FDT Sector..."
      ReturnCode = WriteSector(DriveName, FDT_Location, 1, FDT())
      If ReturnCode <> 0 Then
         MsgBox ("Writing Error !"), 48, " Writing FDT"
         Exit Sub
      End If
      Label1.Caption = Label1.Caption + " OK."
      WinFileNumber = FreeFile
      Open WinFileName For Binary As WinFileNumber Len = 1
      If FileLen(WinFileName) <> WinFileSize Then
         MsgBox ("File Error !"), 48, " Writing File Data"
         Exit Sub
      End If
      Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "Writing File Data..."
      X = 0
      BeginningSector = FDT_Location
      For S1 = 1 To WinFileSize
          Get WinFileNumber, S1, Y
          File_Data(X) = Y
          X = X + 1
          If X = 512 Then
             BeginningSector = BeginningSector + 1
             ReturnCode = WriteSector(DriveName, BeginningSector, 1, File_Data())
             If ReturnCode <> 0 Then
                MsgBox ("Writing Error !"), 48, " Writing File Data"
                Exit For
             End If
             X = 0
          End If
      Next S1
      If ReturnCode <> 0 Then Exit Sub
      If X > 0 And X < 512 Then
         For X = X To 511
             File_Data(X) = 0
         Next X
         BeginningSector = BeginningSector + 1
         ReturnCode = WriteSector(DriveName, BeginningSector, 1, File_Data())
         If ReturnCode <> 0 Then
            MsgBox ("Writing Error !"), 48, " Writing File Data"
            Exit Sub
         End If
      End If
      Label1.Caption = Label1.Caption + " OK."
      
      ReturnCode = ReadSector(DriveName, MAT_Location, 1, MAT())
      If ReturnCode <> 0 Then
         MsgBox ("Reading Error !"), 48, " Reading MAT Sector"
         Exit Sub
      End If
      Rem S1 = CDbl(MAT(15))
      Rem S1 = 16777216 * S1
      Rem S2 = CLng(MAT(14))
      Rem S2 = 65536 * S2
      Rem S3 = CLng(MAT(13))
      Rem S3 = S3 * 256
      Rem S4 = CInt(MAT(12))
      Rem Sector
      Rem DAT_Location = S1 + S2 + S3 + S4
      Rem S1 = CDbl(MAT(19))
      Rem S1 = 16777216 * S1
      Rem S2 = CLng(MAT(18))
      Rem S2 = 65536 * S2
      Rem S3 = CLng(MAT(17))
      Rem S3 = S3 * 256
      Rem S4 = CInt(MAT(16))
      Rem Sector Count
      Rem DAT_SCount = CLng(S1 + S2 + S3 + S4)
      L = WinFileSize Mod 512
      If L > 0 Then
         K = ((WinFileSize - L) / 512) + 1
      Else
         K = (WinFileSize / 512) + 1
      End If
      S1 = GetFreeSpace()
      FreeSpace = S1 - (K + 1)
      ReturnCode = ReadSector(DriveName, DAT_Location, DAT_SCount, DAT())
      If ReturnCode <> 0 Then
         MsgBox ("Reading Error !"), 48, " Reading DAT Sectors"
         Exit Sub
      End If
      L = CInt(FDT_Location)
      DAT(L) = &H5
      For K = L + 1 To L + K
          L = CInt(K)
          DAT(L) = &HF0
      Next K
      ReturnCode = WriteSector(DriveName, DAT_Location, DAT_SCount, DAT())
      If ReturnCode <> 0 Then
         MsgBox ("Writing Error !"), 48, " Writing DAT Sectors"
         Exit Sub
      End If
      Free_Sectors = FreeSpace
      K = FreeSpace
      L = CInt(K Mod 256)
      MAT(20) = CByte(L)
      K = (K - L) / 256
      L = K Mod 256
      MAT(21) = CByte(L)
      K = (K - L) / 256
      L = K Mod 256
      MAT(22) = CByte(L)
      K = (K - L) / 256
      MAT(23) = CByte(K)
      S1 = CDbl(MAT(27))
      S1 = 16777216 * S1
      S2 = CLng(MAT(26))
      S2 = 65536 * S2
      S3 = CLng(MAT(25))
      S3 = S3 * 256
      S4 = CInt(MAT(24))
      BeginningSector = S1 + S2 + S3 + S4
      K = GetFirstFreeSection(BeginningSector, 1)
      L = CInt(K Mod 256)
      MAT(24) = CByte(L)
      K = (K - L) / 256
      L = K Mod 256
      MAT(25) = CByte(L)
      K = (K - L) / 256
      L = K Mod 256
      MAT(26) = CByte(L)
      K = (K - L) / 256
      MAT(27) = CByte(K)
      ReturnCode = WriteSector(DriveName, MAT_Location, 1, MAT())
      If ReturnCode <> 0 Then
         MsgBox ("Writing Error !"), 48, " Writing MAT Sector"
         Exit Sub
      End If
      
      ReturnCode = ReadSector(DriveName, 0, 1, BS())
      If ReturnCode <> 0 Then
         MsgBox ("Reading Error !"), 48, " Reading Boot Sector"
         Exit Sub
      End If
            
      L = CInt(FDT_Location Mod 256)
      BS(20) = CByte(L)
      K = (FDT_Location - L) / 256
      L = K Mod 256
      BS(21) = CByte(L)
      K = (K - L) / 256
      L = K Mod 256
      BS(22) = CByte(L)
      K = (K - L) / 256
      BS(23) = CByte(K)
      
      ReturnCode = WriteSector(DriveName, 0, 1, BS())
      If ReturnCode <> 0 Then
         MsgBox ("Writing Error !"), 48, " Writing Boot Sector"
         Exit Sub
      End If
      
      FS_StartupFileNumber = FDT_Location
      
      MsgBox ("File '" + FS_FileName + "' has been recorded as Singlix FS Startup File"), 0, " Save Open/Windows File as Singlix FS File"
      Label1.Caption = "FS Startup File : " + FS_FileName
      Label1.Caption = Label1.Caption + Chr$(13) + Chr$(10) + "File Size: " + CStr(WinFileSize) + " bytes."
   End If
End If
End Sub

Private Function ImageFileReadSector(ImageFileName As String, Sector As Double, SectorCount As Long, Buffer() As Byte) As Long
Dim Offset As Double
Dim Count As Double
Dim Location As Double
Dim ReadByte As Byte
Dim Counter As Integer
If ImageFileName = "" Then
   ImageFileReadSector = -1
   Exit Function
End If
If Dir(ImageFileName) = "" Then
   ImageFileReadSector = -1
   Exit Function
End If
ImageFileSize = FileLen(ImageFileName)
If ImageFileSize <> 1474560 Then
   ImageFileReadSector = -1
   Exit Function
End If
Offset = (Sector * 512)
Count = SectorCount * 512
If Count < 512 Then
   ImageFileReadSector = -1
   Exit Function
Else
   If Offset > ImageFileSize - 512 Then
      ImageFileReadSector = -1
      Exit Function
   End If
End If
On Error GoTo ReturnErrCode
ImageFileNumber = FreeFile
Open ImageFileName For Binary As ImageFileNumber Len = 1
For Location = Offset + 1 To Offset + Count
    Get ImageFileNumber, Location, ReadByte
    Buffer(Counter) = ReadByte
    Counter = Counter + 1
Next Location
ImageFileReadSector = 0
Close ImageFileNumber
FCikis:
Exit Function
ReturnErrCode:
ImageFileReadSector = CLng(Err.Number)
Close ImageFileNumber
GoTo FCikis
End Function

Private Function ImageFileWriteSector(ImageFileName As String, Sector As Double, SectorCount As Long, Buffer() As Byte) As Long
Dim Offset As Double
Dim Count As Double
Dim Location As Double
Dim WriteByte As Byte
Dim Counter As Integer
If ImageFileName = "" Then
   ImageFileWriteSector = -1
   Exit Function
End If
If Dir(ImageFileName) = "" Then
   ImageFileWriteSector = -1
   Exit Function
End If
ImageFileSize = FileLen(ImageFileName)
If ImageFileSize <> 2880 * 512 Then
   ImageFileWriteSector = -1
   Exit Function
End If
Offset = (Sector * 512)
Count = SectorCount * 512
If Count < 512 Then
   ImageFileWriteSector = -1
   Exit Function
Else
   If Offset > ImageFileSize - 512 Then
      ImageFileWriteSector = -1
      Exit Function
   End If
End If
On Error GoTo ReturnErrCode
ImageFileNumber = FreeFile
Open ImageFileName For Binary As ImageFileNumber Len = 1
For Location = Offset + 1 To Offset + Count
    WriteByte = Buffer(Counter)
    Put ImageFileNumber, Location, WriteByte
    Counter = Counter + 1
Next Location
ImageFileWriteSector = 0
Close ImageFileNumber
FCikis:
Exit Function
ReturnErrCode:
ImageFileWriteSector = CLng(Err.Number)
Close ImageFileNumber
GoTo FCikis
End Function
