delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2013/03/29/14:42:50

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
X-Recipient: djgpp AT delorie DOT com
X-Authenticated: #27081556
X-Provags-ID: V01U2FsdGVkX1/htY4NAfzhET/UvU57FAdAd/Xv4NArNx13f7ycX8
XSBEsOHbGBUGpV
Message-ID: <5155DAEC.4040606@gmx.de>
Date: Fri, 29 Mar 2013 19:18:20 +0100
From: Juan Manuel Guerrero <juan DOT guerrero AT gmx DOT de>
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121025 Thunderbird/16.0.2
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: DJGPP scandir and alphasort example code
References: <261476b1-c137-495d-95df-a92075cd9960 AT googlegroups DOT com> <2245ebea-be44-4a40-8b2f-7ef5e0e67157 AT googlegroups DOT com>
In-Reply-To: <2245ebea-be44-4a40-8b2f-7ef5e0e67157@googlegroups.com>
X-Y-GMX-Trusted: 0
Reply-To: djgpp AT delorie DOT com

Am 26.03.2013 12:50, schrieb RayeR:
> I tested the test program but it crashed in my case of running from drive C: (not crashen from other drive). It may be confused by some LFNs or hidden files and crash only for my specific directory structure? (Run under WinXP). I din't checked the code, maybe there's some bug...
>
> C:\1sc>test
> 01: !zaloha
> 02: 1sc
> 03: 1st64sec.bin
> 04: Cypress
> 05: Documents and Settings
> 06: EMBEDDED WORLD - NORIMBERK 2013
> 07: Program Files
> 08: _rfid
> 09: altium10
> 10: armgcc
> 11: autoexec.bat
> 12: boot.ini
> 13: bootfont.bin
> 14: bootsect.dos
> 15: command.com
> 16: config.sys
> 17: djgpp
> 18: dn
> 19: dos
> 20: io.sys
> 21: kernel.sys
> 22: linux
> 23: mapview
> 24: mingw32
> 25: msdos.sys
> 26: ntdetect.com
> 27: ntldr
> 28: orcad
> 29: pagefile.sys
> 30: temp
> 31: winavr
> 32: winavr.433
> 33: windows
> 34: winnt
> Exiting due to signal SIGSEGV
> General Protection Fault at eip=00004254
> eax=000a0178 ebx=00000040 ecx=00298a48 edx=0009e52c esi=0009e4e4 edi=0001d68c
> ebp=0009d5d8 esp=0009d5d0 program=C:\1SC\TEST.EXE
> cs: sel=01a7  base=029e0000  limit=000affff
> ds: sel=01af  base=029e0000  limit=000affff
> es: sel=01af  base=029e0000  limit=000affff
> fs: sel=017f  base=00012220  limit=0000ffff
> gs: sel=01bf  base=00000000  limit=0010ffff
> ss: sel=01af  base=029e0000  limit=000affff
> App stack: [0009d688..0001d68c]  Exceptn stack: [0001d59c..0001b65c]
>
> Call frame traceback EIPs:
>    0x00004254
>    0x00001ffb
>    0x00003cd4
>
> C:\1sc>
>
> All directory from root was listed, it crashed after...
>

The reason why this program crashes is that your globing pattern makes 
no sens.
You are using djgpp and djgpp is posix centric to certain extend. In this
particular case it means you cannot assume that the directory separator is a
backslash.  You have always to check for both slash and backslash.
The following line in your code may not work as you intended:

     //remove trailing slash
     wildcardpos=strrchr(wildcard,'\\');

The real reason for the crash is the following line:

     //add * for all entries
     strcat(wildcard,"\\*");

This line produces a pattern that looks like:
    /\*
and this only matches for the root directory itself.  In other words it 
matches the slash
of the root directory and nothing else.  There will certainly never be 
an slash followed
by a backslash as a valid DOS file name.  You get only 1 entry. Because 
you increment
the counter 2 times more:

     if(glob(wildcard, GLOB_NOESCAPE|GLOB_NOCHECK|GLOB_NOSORT, NULL, 
&glob_results) == 0)
     cnt = glob_results.gl_pathc +2; //add two for . and ..

your *namelist[] pointer array is always 3 entries large, no matter how much
entries may exist in the root directory.  Usually malloc allocates more 
space
than requested, so the code works and you are able to store the file 
names and
print them but later when you free the pointer array calling:

     free(namelist);

your application crashes.

Replacing:
     strcat(wildcard,"\\*");
with
     strcat(wildcard,"*");
produces "/*" as globing pattern and now you will get all entries in the 
root
directory.  Now the array is always allocated large enough to contain 
the required
pointers and the application runs flawlessly for me on all partitions.  
There is
certainly no LFN issue.  If you want to fully understand this read the 
glob.c code
of djgpp.

HTH,
Juan M. Guerrero

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019