strange behaviour with wine emulator, multithreading and C stdio - c

I've the following code:
#include <stdio.h>
#include <windows.h>
#include <process.h>
int main() {
/* DWORD WINAPI */void input(/* LPVOID */void* lpParam) {
Sleep(1000);
printf("hello mingw!\n");
// return 0;
}
_beginthread(input, 0, NULL);
getchar();
return 0;
}
I cross-compile from ubuntu 22.04.01 with mingw-w64 with:
...$ x86_64-w64-mingw32-gcc -o example.exe example.c
In a native Win7 box it works as expected:
...$ example.exe
hello mingw! (after 1 second)
a
...$
but in wine when entering 'a' from keyboard, the 'a' climbs up one line:
...$ example.exe
aello mingw!
...$
It seems as if printf output from a thread didn't update console cursor. This only happens when read code (getchar()) executes before the write code (printf), hence the Sleep.
Any pointers?

Related

Problem with printing extended ascii using ncurses

I have got problem with printing extended ascii in terminal using wprintw function. This program prints letters instead squares. I was trying to change my locales but without effect. What should I change in my system to print it correctly?
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <unistd.h>
#include <locale.h>
#include <wchar.h>
int main(void)
{
setlocale(LC_ALL, "");
initscr();
WINDOW *game_window;
game_window=newwin(40,40,1,1);
wrefresh(game_window);
while (TRUE) {
wclear(game_window);
wprintw(game_window, "██████████████████████");
wrefresh(game_window);
sleep(3);
break;
}
endwin();
return 0;
}
I am working On Debian Jessie 10 and these are my locale:
LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
Ok, I find the solution, when compiling the program you should use
this command:
gcc main.c -o main -lncursesw
Instead this:
gcc main.c -o main -lncurses

return WCHAR[] in C

I have the following code written in C:
#include <windows.h>
#include <stdio.h>
WCHAR* test(){
WCHAR var[256];
int nSize = GetEnvironmentVariableW(L"SystemRoot", NULL, 0);
GetEnvironmentVariableW(L"SystemRoot", &var, nSize);
wprintf(L"%s\n", var);
return var;
}
int main() {
WCHAR* var = test();
wprintf(L"%s\n", var);
return 0;
}
When I compile it in Visual Studio and run it it works as expected. It prints result two times - in main func and in test. Output is:
C:\Windows
C:\Windows
But when I compile it on linux with mingw compiler via command
i686-w64-mingw32-gcc -o test.exe -O3 -Os -static -s test.c
it gives this output after starting:
C:\Windows
(null)
Why do the test() func return NULL when I'm using mingw and what to do to make it work properly?
Thanks.
You cannot return the address of a local variable, you get a compiler warning (which is actually more of an error).
You want this:
#include <windows.h>
#include <stdio.h>
WCHAR* test(WCHAR var[], int nSize) {
GetEnvironmentVariableW(L"SystemRoot", var, nSize);
wprintf(L"%s\n", var);
return var;
}
int main() {
// declare var outside the test function
WCHAR var[256];
// pass the address of var to test
test(var, 256);
wprintf(L"%s\n", var);
return 0;
}
But be aware that 256 (like in var[256]) may not be enough. I leave it as an exercise to you to resolve this issue properly.

Printing emoji with wprintf() on windows

#include <locale.h>
#include <stdio.h>
#include <wchar.h>
int main() {
setlocale(LC_CTYPE, "");
wprintf(L"٩(◕‿◕。)۶\n");
wprintf(L"😊\n");
return 0;
}
The code above works perfectly on linux exactly as expected:
$ ./a.out
٩(◕‿◕。)۶
😊
but completely breaks on windows:
PS> ./a.exe
()
why does this happen?
How do I then, print emojis on windows?
Im using powershell with gcc 9.2.0 on windows and WSL with gcc 9.3.0 on powershell for linux.

Simple C Program Lags [Homework]

For an assignment I have we are to find vulnerabilities in a certain C program and exploit them using various buffer overflow attacks. However when I run the .out file in the terminal with it's input argument it just stalls and doesn't do anything.
Even when I run GDB, that just lags too. I'm not looking for a solution to the assignment, I'm just looking for reasons why it's not running?
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
void partialwin()
{
printf("Achieved 1/2!\n");
}
void fullwin(){
printf("Achieved 2/2\n");
}
void vuln(){
char buffer[36];
gets(buffer);
printf("Buffer contents are %s\n",buffer);
}
int main(int argc,char**argv){
vuln();
}
Providing your sourc file is called assignment1.c and you're using gcc this should work, $ being your command prompt (which could be different on your platform)
$ gcc assignment1.c
$ a.out
Hello
Buffer contents are Hello
$

printf is not printing even after enabling buffering

so I was having a problem with a big code that uses strings and file names. I had many issues with it and I addedd some printf in bwtween to Keep check of how the Code works. I couldnt get anything in the output so i decided to take to old good "hello world" program to see if printf was the issue.
so here is the Code :
#include <stdio.h>
int main (void){
printf("hello world\n");
}
I thought Maybe it is a Buffering issue, so I have tried some variations like :
#include <stdio.h>
int main (void){
setvbuf(stdout, NULL, _IOLBF, 0);
printf("hello world\n");
}
and
#include <stdio.h>
int main (void){
printf("hello world\n");
setbuf(stdout, NULL);
}
still Nothing is printed. any idea on how to find the issue? ps: I am writing on Notepad and i execute using gcc on terminal. gcc is installed just fine and I am working on Windows.
I checked the file's path and it is fine. The command I use is gcc filename.c

Resources