Tcl Source Code

View Ticket
Login
Ticket UUID: aa9f3ef820b8c075c8fc663b6982704450e13912
Title: Inconsistent behavior of "file exists" command
Type: Bug Created on: 2025-11-12 12:43:04
Submitter: anonymous Assigned to: nobody
Subsystem: 37. File System Severity: Important
Priority: 5 Medium Last modified: 2025-11-12 12:43:04
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 8.6.17
Description:
Working with serial ports the "file exists" command has weird behavior, inconsistent with both "file type" and "::open" command (it's not the known pitfall of missing prefix \\.\ for serial ports on Windows platform):

---
a) The following returns 1 for EVERY serial port COM1 to COM9 (even if some of them do not exist):

    for {set i 1} {$i < 10} {incr i} {
        puts [file exists com$i]
    }

Compared to this "file type" command works as expected and returns "characterSpecial" for serial ports available and raises errors for non-existing serial ports.

---
b) Adding \\.\ prefix for Windows changes behavior (although behavior of "file type" does not change):

    for {set i 1} {$i < 10} {incr i} {
        puts [file exists [string cat {\\.\com} $i]]
    }

This returns zero for all serial ports COM1 to COM9 even if they are available. However the following succeeds (for serial ports available):

    file type [string cat {\\.\com} 13]

---
c) For COM ports > 10 "file exists" always return 0 even if they are available, e.g.

    puts [file exists {\\.\com13}]

always returns zero. However the following succeeds (given that serial port is available):

    puts [file type {\\.\com13}]
    # -> characterSpecial

    set h [::open {\\.\com13} r]
    close $h

---

The behavior seems inconsistent: Even if one defines that "file" commands work on disk files only why does "file exists" returns 1 for COM1 to COM9? And if one defines that "file" works on special devices like serial ports it should return 1 whenever "file type" returns "characterSpecial".

Commands "file readable" and "file writable" show similar behavior btw.

Platform is Windows 11 with Tcl 8.6.17 (other versions and platforms not tested yet).