passant linters a vore si trobe variables sense inicialitzar
This commit is contained in:
@@ -13,40 +13,40 @@ namespace SystemShutdown {
|
||||
|
||||
#ifndef _WIN32
|
||||
// Función auxiliar para sistemas Unix-like
|
||||
ShutdownResult executeUnixShutdown(const char* command, char* const args[]) {
|
||||
pid_t pid = fork();
|
||||
|
||||
if (pid == 0) {
|
||||
// Proceso hijo
|
||||
execvp(command, args);
|
||||
// Si llegamos aquí, execvp falló
|
||||
std::cerr << "Error: No se pudo ejecutar " << command << std::endl;
|
||||
_exit(1);
|
||||
} else if (pid > 0) {
|
||||
// Proceso padre
|
||||
int status;
|
||||
waitpid(pid, &status, 0);
|
||||
return (WEXITSTATUS(status) == 0) ? ShutdownResult::SUCCESS : ShutdownResult::ERROR_SYSTEM_CALL;
|
||||
} else {
|
||||
return ShutdownResult::ERROR_FORK_FAILED;
|
||||
}
|
||||
auto executeUnixShutdown(const char* command, char* const args[]) -> ShutdownResult {
|
||||
pid_t pid = fork();
|
||||
|
||||
if (pid == 0) {
|
||||
// Proceso hijo
|
||||
execvp(command, args);
|
||||
// Si llegamos aquí, execvp falló
|
||||
std::cerr << "Error: No se pudo ejecutar " << command << '\n';
|
||||
_exit(1);
|
||||
} else if (pid > 0) {
|
||||
// Proceso padre
|
||||
int status;
|
||||
waitpid(pid, &status, 0);
|
||||
return (WEXITSTATUS(status) == 0) ? ShutdownResult::SUCCESS : ShutdownResult::ERROR_SYSTEM_CALL;
|
||||
} else {
|
||||
return ShutdownResult::ERROR_FORK_FAILED;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Implementación de las funciones públicas
|
||||
ShutdownResult shutdownSystem() {
|
||||
ShutdownConfig config;
|
||||
return shutdownSystem(config);
|
||||
}
|
||||
auto shutdownSystem() -> ShutdownResult {
|
||||
ShutdownConfig config;
|
||||
return shutdownSystem(config);
|
||||
}
|
||||
|
||||
ShutdownResult shutdownSystem(int delay_seconds, bool force_apps) {
|
||||
ShutdownConfig config;
|
||||
config.delay_seconds = delay_seconds;
|
||||
config.force_close_apps = force_apps;
|
||||
return shutdownSystem(config);
|
||||
}
|
||||
auto shutdownSystem(int delay_seconds, bool force_apps) -> ShutdownResult {
|
||||
ShutdownConfig config;
|
||||
config.delay_seconds = delay_seconds;
|
||||
config.force_close_apps = force_apps;
|
||||
return shutdownSystem(config);
|
||||
}
|
||||
|
||||
ShutdownResult shutdownSystem(const ShutdownConfig& config) {
|
||||
auto shutdownSystem(const ShutdownConfig& config) -> ShutdownResult {
|
||||
#ifdef _WIN32
|
||||
// Windows: Usar CreateProcess
|
||||
STARTUPINFOA si = {0};
|
||||
@@ -107,42 +107,41 @@ namespace SystemShutdown {
|
||||
const_cast<char*>("shutdown"),
|
||||
const_cast<char*>("-h"),
|
||||
const_cast<char*>("now"),
|
||||
NULL
|
||||
};
|
||||
|
||||
nullptr};
|
||||
|
||||
return executeUnixShutdown("shutdown", args);
|
||||
|
||||
#else
|
||||
return ShutdownResult::ERROR_UNSUPPORTED;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
const char* resultToString(ShutdownResult result) {
|
||||
switch (result) {
|
||||
case ShutdownResult::SUCCESS:
|
||||
return "Apagado iniciado exitosamente";
|
||||
case ShutdownResult::ERROR_PERMISSION:
|
||||
return "Error: Permisos insuficientes";
|
||||
case ShutdownResult::ERROR_SYSTEM_CALL:
|
||||
return "Error: Fallo en la llamada al sistema";
|
||||
case ShutdownResult::ERROR_FORK_FAILED:
|
||||
return "Error: No se pudo crear proceso hijo";
|
||||
case ShutdownResult::ERROR_UNSUPPORTED:
|
||||
return "Error: Sistema operativo no soportado";
|
||||
default:
|
||||
return "Error desconocido";
|
||||
}
|
||||
auto resultToString(ShutdownResult result) -> const char* {
|
||||
switch (result) {
|
||||
case ShutdownResult::SUCCESS:
|
||||
return "Apagado iniciado exitosamente";
|
||||
case ShutdownResult::ERROR_PERMISSION:
|
||||
return "Error: Permisos insuficientes";
|
||||
case ShutdownResult::ERROR_SYSTEM_CALL:
|
||||
return "Error: Fallo en la llamada al sistema";
|
||||
case ShutdownResult::ERROR_FORK_FAILED:
|
||||
return "Error: No se pudo crear proceso hijo";
|
||||
case ShutdownResult::ERROR_UNSUPPORTED:
|
||||
return "Error: Sistema operativo no soportado";
|
||||
default:
|
||||
return "Error desconocido";
|
||||
}
|
||||
}
|
||||
|
||||
bool isShutdownSupported() {
|
||||
auto isShutdownSupported() -> bool {
|
||||
#if defined(_WIN32) || defined(__APPLE__) || defined(__linux__)
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
const char* getRequiredPermissions() {
|
||||
auto getRequiredPermissions() -> const char* {
|
||||
#ifdef _WIN32
|
||||
return "Requiere permisos de Administrador en Windows";
|
||||
#elif defined(__APPLE__) || defined(__linux__)
|
||||
@@ -150,6 +149,6 @@ namespace SystemShutdown {
|
||||
#else
|
||||
return "Sistema no soportado";
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace SystemShutdown
|
||||
Reference in New Issue
Block a user