NO

Author Topic: _wmakepath returns incomplete string  (Read 99 times)

Offline sgraffe

  • Member
  • *
  • Posts: 9
_wmakepath returns incomplete string
« on: June 20, 2017, 02:22:29 AM »
Hello All:
I am not getting correct result from function _wmakepath. The following program demonstrate output from functions _makepath and _wmakepath.
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   char path [FILENAME_MAX];
   char drive[] = "C:";
   char dir  [] = "\\foldername\\";
   char name [] = "filename";
   char ext  [] = "txt";

   _makepath(path, drive, dir, name, ext);
   printf("%s \n", path);

   wchar_t wpath [FILENAME_MAX];
   wchar_t wdrive[] = L"C:";
   wchar_t wdir  [] = L"\\foldername\\";
   wchar_t wname [] = L"filename";
   wchar_t wext  [] = L"txt";

   _wmakepath(wpath, wdrive, wdir, wname, wext);
   printf("%ls \n", wpath);

   return 0;
}
Output:
C:\foldername\filename.txt    (from _makepath)
C:\foldername\                       (from _wmakepath)

Output from _wmakepath is missing the filename and extension components.
Compiler options are: -std:C11 -Tx86-coff -Ot -Ob1 -fp:precise -W1 -Gd -Zx
Version: 8.00.60 (Win64)

Any ideas?
« Last Edit: June 20, 2017, 03:48:20 AM by sgraffe »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1554
Re: _wmakepath returns incomplete string
« Reply #1 on: June 20, 2017, 11:34:43 AM »
You found a bug.
Code: [Select]
7703978E  |.  66:833A 00    CMP WORD PTR DS:[EDX],0
77039792  |.  74 2F         JE SHORT 770397C3
77039794  |>  66:8B0A       /MOV CX,WORD PTR DS:[EDX]
77039797  |.  66:8908       |MOV WORD PTR DS:[EAX],CX
7703979A  |.  83C2 02       |ADD EDX,2
7703979D  |.  83C0 02       |ADD EAX,2
770397A0  |.  66:85C9       |TEST CX,CX   <--- bug ? should be CMP WORD PTR DS:[EDX],0
770397A3  |.  74 04         |JZ SHORT 770397A9
770397A5  |.  39F8          |CMP EAX,EDI
770397A7  |.^ 72 EB         \JB SHORT 77039794
May the source be with you

Offline sgraffe

  • Member
  • *
  • Posts: 9
Re: _wmakepath returns incomplete string
« Reply #2 on: June 20, 2017, 12:57:09 PM »
Oh, thank you, Timo. Shall I copy this message to the Bugs section?

Compiler 1, User 1, :-).

Regards,

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1554
Re: _wmakepath returns incomplete string
« Reply #3 on: June 20, 2017, 01:19:14 PM »
Sure you can do it.

In attachment a test project to fix that wmakepath bug if someone want to replace it in crt.lib.
« Last Edit: June 20, 2017, 02:11:58 PM by TimoVJL »
May the source be with you

Offline sgraffe

  • Member
  • *
  • Posts: 9
Re: _wmakepath returns incomplete string
« Reply #4 on: June 20, 2017, 09:30:40 PM »
Hello again:

I modified my sample program, and changed the last line to use wprintf instead of printf:
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>

int main(void)
{
char path [FILENAME_MAX];
char drive[] = "C:";
char dir  [] = "\\foldername\\";
char name [] = "filename";
char ext  [] = "txt";

_makepath(path, drive, dir, name, ext);
printf("%s \n", path);

wchar_t wpath [FILENAME_MAX];
wchar_t wdrive[] = L"C:";
wchar_t wdir  [] = L"\\foldername\\";
wchar_t wname [] = L"filename";
wchar_t wext  [] = L"txt";

_wmakepath(wpath, wdrive, wdir, wname, wext);
wprintf(L"%ls \n", wpath);

return 0;
}

now wprintf does not give any output. The output for the program is just
C:\foldername\filename.txt  (the printf at the beginning)

so, is the handling of wchar_t type not reliable?
 

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1554
Re: _wmakepath returns incomplete string
« Reply #5 on: June 21, 2017, 07:30:47 AM »
Not a bug. Each stream has an orientation: byte-oriented or wide-oriented. See fwide().
May the source be with you

Offline sgraffe

  • Member
  • *
  • Posts: 9
Re: _wmakepath returns incomplete string
« Reply #6 on: June 21, 2017, 02:46:19 PM »
Thank you, Timo.

I was not aware of the stream orientation. Maybe wprintf should have a reference to the topic.

Compiler 2, user 1 :-)

PS.
 I was checking an example from Microsoft, and they do not use fwide. The switch beteeen printf and wprintf without problem. It seems that their compiler handles differently the orientation of the stream.

Regards,
« Last Edit: June 21, 2017, 02:51:52 PM by sgraffe »