Сагура Николай, 392, сев. филиал Fat12 8989
Dimitry Fat12 10626
f1import sysf1import sys
22
3def main():3def main():
n4    data = sys.stdin.buffer.read()n4    file_data = sys.stdin.buffer.read()
5    boot = data[:512]5    boot_sector = file_data[:512]
6    bytes_per_sector = int.from_bytes(boot[11:13], 'little')6    sector_size = int.from_bytes(boot_sector[11:13], 'little')
7    sectors_per_cluster = boot[13]7    cluster_size_sectors = boot_sector[13]
8    reserved_sectors = int.from_bytes(boot[14:16], 'little')8    reserved_sectors_count = int.from_bytes(boot_sector[14:16], 'little'
 >)
9    num_fats = boot[16]9    fat_count = boot_sector[16]
10    max_root_entries = int.from_bytes(boot[17:19], 'little')10    root_entries_max = int.from_bytes(boot_sector[17:19], 'little')
11    sectors_per_fat = int.from_bytes(boot[22:24], 'little')11    fat_sectors = int.from_bytes(boot_sector[22:24], 'little')
12    root_dir_sector = reserved_sectors + num_fats * sectors_per_fat12    root_start_sector = reserved_sectors_count + fat_count * fat_sectors
13    root_dir_size_bytes = max_root_entries * 3213    root_size_bytes = root_entries_max * 32
14    root_dir_sectors = (root_dir_size_bytes + bytes_per_sector - 1) // b14    root_sectors_needed = (root_size_bytes + sector_size - 1) // sector_
>ytes_per_sector>size
15    root_dir_offset = root_dir_sector * bytes_per_sector15    root_data_start = root_start_sector * sector_size
16    root_dir = data[root_dir_offset:root_dir_offset + root_dir_sectors *16    root_data = file_data[root_data_start:root_data_start + root_sectors
> bytes_per_sector]>_needed * sector_size]
17    entries = []17    items = []
18    for off in range(0, root_dir_size_bytes, 32):18    for position in range(0, root_size_bytes, 32):
19        entry = root_dir[off:off + 32]19        record = root_data[position:position + 32]
20        if len(entry) < 32:20        if len(record) < 32:
21            break21            break
n22        first_byte = entry[0]n22        first_char = record[0]
23        if first_byte == 0:23        if first_char == 0:
24            break24            break
n25        if first_byte == 229:n25        if first_char == 229:
26            continue26            continue
n27        attr = entry[11]n27        attributes = record[11]
28        if attr == 15:28        if attributes == 15:
29            continue29            continue
n30        if attr & 8:n30        if attributes & 8:
31            continue31            continue
n32        name_raw = entry[0:8]n32        name_part = record[0:8]
33        ext_raw = entry[8:11]33        extension_part = record[8:11]
34        name = name_raw.decode('cp866', errors='replace').rstrip()
35        ext = ext_raw.decode('cp866', errors='replace').rstrip()34        name_text = name_part.decode('cp866', errors='replace').rstrip()
35        extension_text = extension_part.decode('cp866', errors='replace'
 >).rstrip()
36        if not name:36        if not name_text:
37            continue37            continue
n38        if attr & 16:n38        if attributes & 16:
39            if name.startswith('.'):39            if name_text.startswith('.'):
40                continue40                continue
n41            size_repr = 'dir'n41            item_size = 'dir'
42        else:42        else:
t43            size = int.from_bytes(entry[28:32], 'little')t43            file_size = int.from_bytes(record[28:32], 'little')
44            size_repr = str(size)44            item_size = str(file_size)
45        full_name = name45        complete_name = name_text
46        if ext:46        if extension_text:
47            full_name = f'{name}.{ext}'47            complete_name = f'{name_text}.{extension_text}'
48        entries.append((full_name, size_repr))48        items.append((complete_name, item_size))
49    entries.sort(key=lambda xx[0])49    items.sort(key=lambda itemitem[0])
50    for fname, sz in entries:50    for filename, size_info in items:
51        print(f'{fname:<12} {sz}')51        print(f'{filename:<12} {size_info}')
52if __name__ == '__main__':52if __name__ == '__main__':
53    main()53    main()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op