134 lines
4.0 KiB
C
134 lines
4.0 KiB
C
/*
|
|
* wincmd.h
|
|
*
|
|
* Copyright 2026
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
* MA 02110-1301, USA.
|
|
*
|
|
*
|
|
*/
|
|
|
|
#if (!defined(__LIB_wincmd_H_))
|
|
#define __LIB_wincmd_H_
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define BUFFER_SIZE 256
|
|
#define VERSION "1.0"
|
|
|
|
static void wincmd_error(char* _err) {
|
|
printf("\033[1mwincmd: \033[91m%s\033[0m\n", _err);
|
|
}
|
|
|
|
typedef unsigned char bool;
|
|
|
|
#define true 1
|
|
#define false 0
|
|
|
|
static void __help(void) {
|
|
puts (
|
|
"Console wincmd on Wine API V" VERSION "\n"
|
|
" " "Run Windows command on the Linux" "\n"
|
|
"Using:" "\n"
|
|
" " "wincmd SCRIPT_NAME" "\n"
|
|
" " "wincmd [--help/--version]" "\n"
|
|
" " "wincmd --command WINDOWS_COMMAND"
|
|
);
|
|
}
|
|
|
|
int _lib_wincmd_main(int argc, char** argv) {
|
|
size_t pos = 1;
|
|
char command_buffer[BUFFER_SIZE] = "wine cmd.exe /C ";
|
|
long add_count = BUFFER_SIZE - strlen(command_buffer) - 1;
|
|
bool get_command = false;
|
|
char* file_name = NULL;
|
|
while (pos != (size_t)argc) {
|
|
char* str = argv[pos];
|
|
if (get_command == true) {
|
|
if (add_count <= 0) {
|
|
wincmd_error("Buffer overflowed!");
|
|
return EXIT_FAILURE;
|
|
}
|
|
strncat(command_buffer, " ", add_count);
|
|
add_count --;
|
|
strncat(command_buffer, str, add_count);
|
|
add_count -= strlen(str);
|
|
pos ++;
|
|
continue;
|
|
}
|
|
if (str[0] == '-') {
|
|
unsigned char loc = 0;
|
|
if (str[1] == '-') loc ++;
|
|
switch (str[1 + loc]) {
|
|
default:
|
|
if (get_command == true) break;
|
|
printf("\033[1mwincmd: \033[91mCannot find option \"%s\"!\033[0m\n", str);
|
|
return EXIT_FAILURE;
|
|
break;
|
|
case 'c':
|
|
get_command = true;
|
|
break;
|
|
case 'v':
|
|
puts(VERSION);
|
|
return EXIT_SUCCESS;
|
|
case 'h':
|
|
__help();
|
|
return EXIT_SUCCESS;
|
|
}
|
|
} else {
|
|
file_name = str;
|
|
break;
|
|
}
|
|
pos ++;
|
|
}
|
|
int stat;
|
|
if (system("echo -n \"Wine API (CMD) \" && wine --version") != EXIT_SUCCESS) {
|
|
wincmd_error("Unable to find critical dependencies for running the CMD.EXE shell!");
|
|
return EXIT_FAILURE;
|
|
}
|
|
if (file_name != NULL) {
|
|
strcpy(command_buffer, "wine cmd.exe /C call ");
|
|
strncat(command_buffer, file_name, BUFFER_SIZE - 10);
|
|
command_buffer[BUFFER_SIZE - 1] = '\0';
|
|
stat = system(command_buffer);
|
|
if (stat != EXIT_SUCCESS) {
|
|
printf("\033[1mwincmd: \033[91mThe program terminates with status %d!\033[0m\n", stat);
|
|
return EXIT_FAILURE;
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|
|
if (get_command == true) {
|
|
stat = system(command_buffer);
|
|
if (stat != EXIT_SUCCESS) {
|
|
printf("\033[1mwincmd: \033[91mThe program terminates with status %d!\033[0m\n", stat);
|
|
return EXIT_FAILURE;
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|
|
if ((stat = system("wine cmd.exe")) != EXIT_SUCCESS) {
|
|
printf("\033[1mwincmd: \033[91mThe program terminates with status %d!\033[0m\n", stat);
|
|
return EXIT_FAILURE;
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
#undef VERSION
|
|
|
|
#endif /* __LIB_wincmd_H_ */
|
|
|