Why am I able to cat a directory?

Marc Fournier scrappy at hub.org
Sun Jan 22 20:11:42 PST 2023


Oops, sorry ... my bad ... the code below is for PHP, not perl ... same 
effect, just slightly different methods ...


On 2023-01-22 18:52, Stanislav Syekirin wrote:
> Thank you for the example, but, to be honest, I'm even more confused 
> now. I'm not sure what the array part does: $arrFiles[] is not valid 
> syntax, and array() looks like a subroutine call but there is no such 
> subroutine. It also seems to me after some testing that
>
> - opendir with one argument does not exist and it should have been 
> opendir($handle, $shifts_directory)
> - $entry == "." compares strings as numbers and it should have been 
> $entry ne "."
> - !== FALSE is not a thing and it should have been while ($entry = 
> readdir($handle))
>
> I've tried the following adjusted code, and it works on Dragonfly...
>
>     my $directory = $ARGV[0];
>     opendir(my $handle, $directory) or die "can't open $directory";
>
>     while (my $entry = readdir $handle) {
>         if ($entry ne "." && $entry ne "..") {
>             print "$entry\n";
>         }
>     }
>
>     closedir $handle;
>
> ...but it works just as well on Linux, where cat on directories 
> doesn't work, so it can't rely on that.
>
> Can you please elaborate?
>
> Regards
> Stanislav
>
> On So, 22 Jan 2023 22:44:27 +0000
>  Marc Fournier <scrappy at hub.org> wrote:
>>
>> Simplistically, a directory is just a file with binary data in it ... 
>> you can manipulate it in the same way as any other file ... I tend to 
>> write perl scripts that open a directory, read the contents ( files ) 
>> and then manipulate the files within it:
>>
>> -- 
>> $handle = opendir( $shifts_directory );
>>
>>   if ($handle) {
>>     $arrFiles = array();
>>     while (($entry = readdir($handle)) !== FALSE) {
>>       if ($entry != "." && $entry != "..") {
>>         $arrFiles[] = $entry;
>>       }
>>     }
>>   } else {
>>     exit ( 'directory does not exist: ' . $shifts_directory );
>>   }
>>
>>   closedir($handle);
>> -- 
>>
>> Using cat isn't very effective, but as effective as using cat to view 
>> an image file, or excel spreadsheet ...
>>
>>
>> On 2023-01-22 10:37, Stanislav Syekirin wrote:
>>> Hi all,
>>>
>>> I accidentally noticed that the following runs without error (even 
>>> though it only outputs some binary data I can't understand) on my 
>>> Dragonfly 6.2 install:
>>>
>>>     mkdir a
>>>     cat a
>>>
>>> The same happens on NetBSD, so it probably isn't a bug, but I really 
>>> can't imagine the intended use case. Can someone please explain?
>>>
>>> Regards
>>> Stanislav
>


More information about the Users mailing list