diff --git a/client/cmdscript.c b/client/cmdscript.c index 073f4e1ca..3ab5dfe8d 100644 --- a/client/cmdscript.c +++ b/client/cmdscript.c @@ -33,6 +33,10 @@ #include #include +#ifdef _WIN32 +#include "scandir.h" +#endif + static int CmdHelp(const char *Cmd); static int CmdList(const char *Cmd); static int CmdRun(const char *Cmd); @@ -92,7 +96,7 @@ int CmdList(const char *Cmd) { for (uint16_t i = 0; i < n; i++) { if(str_ends_with(namelist[i]->d_name, ".lua")) - PrintAndLog("%-21s %s", namelist[i]->d_name, "A script file"); + PrintAndLog("%-21s", namelist[i]->d_name); free(namelist[i]); } free(namelist); diff --git a/client/scandir.c b/client/scandir.c new file mode 100644 index 000000000..aaba9c0ec --- /dev/null +++ b/client/scandir.c @@ -0,0 +1,91 @@ +/* scandir.cc + + Copyright 1998, 1999, 2000, 2001 Red Hat, Inc. + + Written by Corinna Vinschen + + This file is part of Cygwin. + + This software is a copyrighted work licensed under the terms of the + Cygwin license. Please consult the file "CYGWIN_LICENSE" for + details. */ + +#include "scandir.h" + + +#ifdef __cplusplus +extern "C" { +#endif +int scandir (const char *dir, + struct dirent ***namelist, + int (*select) (const struct dirent *), + int (*compar) (const struct dirent **, const struct dirent **)) +{ + DIR *dirp; + struct dirent *ent, *etmp, **nl = NULL, **ntmp; + int count = 0; + int allocated = 0; + int err_no = 0; + + if (!(dirp = opendir (dir))) + return -1; + + while ((ent = readdir (dirp))) { + if (!select || select (ent)) { + + err_no =0; + + if (count == allocated) { + if (allocated == 0) + allocated = 10; + else + allocated *= 2; + + ntmp = (struct dirent **) realloc (nl, allocated * sizeof *nl); + if (!ntmp) { + err_no = 1; + break; + } + nl = ntmp; + } + + if (!(etmp = (struct dirent *) malloc (sizeof *ent))) { + err_no = 1; + break; + } + *etmp = *ent; + nl[count++] = etmp; + } + } + + if (err_no != 0) { + closedir (dirp); + if (nl) { + while (count > 0) + free (nl[--count]); + free (nl); + } + return -1; + } + + closedir (dirp); + + qsort (nl, count, sizeof *nl, (int (*)(const void *, const void *)) compar); + if (namelist) + *namelist = nl; + return count; +} +#ifdef __cplusplus +} +#endif + +#ifdef __cplusplus +extern "C" { +#endif +int alphasort (const struct dirent **a, const struct dirent **b) +{ + return strcoll ((*a)->d_name, (*b)->d_name); +} +#ifdef __cplusplus +} +#endif diff --git a/client/scandir.h b/client/scandir.h new file mode 100644 index 000000000..c7c47e25c --- /dev/null +++ b/client/scandir.h @@ -0,0 +1,23 @@ +//----------------------------------------------------------------------------- +// Copyright (C) 2017 iceman +// +// This code is licensed to you under the terms of the GNU GPL, version 2 or, +// at your option, any later version. See the LICENSE.txt file for the text of +// the license. +//----------------------------------------------------------------------------- +// platform-independant sleep macros +//----------------------------------------------------------------------------- + +#ifndef SCANDIR_H__ +#define SCANDIR_H__ + +#include +#include + +#ifdef _WIN32 +#else +#endif // _WIN32 + +extern int scandir (const char *dir, struct dirent ***namelist, int (*select) (const struct dirent *), int (*compar) (const struct dirent **, const struct dirent **)); +extern int alphasort (const struct dirent **a, const struct dirent **b); +#endif // SCANDIR_H__ \ No newline at end of file diff --git a/client/scripts/remagic.lua b/client/scripts/remagic.lua index 806458999..5062d8cf4 100644 --- a/client/scripts/remagic.lua +++ b/client/scripts/remagic.lua @@ -14,21 +14,21 @@ Arguments: ]] --- -- A debug printout-function -function dbg(args) +local function dbg(args) if DEBUG then - print("###", args) + print('###', args) end end --- -- This is only meant to be used when errors occur -function oops(err) - print("ERROR: ",err) +local function oops(err) + print('ERROR: ',err) end --- -- Usage help -function help() +local function help() print(desc) - print("Example usage") + print('Example usage') print(example) end diff --git a/client/util.c b/client/util.c index e48d482ee..1907412ac 100644 --- a/client/util.c +++ b/client/util.c @@ -19,11 +19,12 @@ int ukbhit(void) { int error; static struct termios Otty, Ntty; - if ( tcgetattr(STDIN_FILENO, &Otty) == -1) return -1; + if ( tcgetattr(STDIN_FILENO, &Otty) == -1) return -1; Ntty = Otty; - Ntty.c_iflag = 0x000; // input mode - Ntty.c_oflag = 0x000; // output mode + + Ntty.c_iflag = 0x0000; // input mode + Ntty.c_oflag = 0x0000; // output mode Ntty.c_lflag &= ~ICANON; // control mode = raw Ntty.c_cc[VMIN] = 1; // return if at least 1 character is in the queue Ntty.c_cc[VTIME] = 0; // no timeout. Wait forever @@ -37,6 +38,7 @@ int ukbhit(void) { } #else + int ukbhit(void) { return kbhit(); } @@ -61,10 +63,7 @@ void AddLogLine(char *file, char *extData, char *c) { fprintf(f, "%s", extData); fprintf(f, "%s\n", c); fflush(f); - if (f) { - fclose(f); - f = NULL; - } + fclose(f); } void AddLogHex(char *fileName, char *extData, const uint8_t * data, const size_t len){ @@ -74,8 +73,7 @@ void AddLogHex(char *fileName, char *extData, const uint8_t * data, const size_t void AddLogUint64(char *fileName, char *extData, const uint64_t data) { char buf[20] = {0}; memset(buf, 0x00, sizeof(buf)); - //sprintf(buf, "%X%X", (unsigned int)((data & 0xFFFFFFFF00000000) >> 32), (unsigned int)(data & 0xFFFFFFFF)); - sprintf(buf, "%012" PRIx64 "", data); + sprintf(buf, "%016" PRIx64 "", data); AddLogLine(fileName, extData, buf); } @@ -318,7 +316,7 @@ int param_getlength(const char *line, int paramnum) char param_getchar(const char *line, int paramnum) { int bg, en; - if (param_getptr(line, &bg, &en, paramnum)) return 0x00; + if (param_getptr(line, &bg, &en, paramnum)) return 0; return line[bg]; } @@ -408,9 +406,6 @@ int param_gethex_ex(const char *line, int paramnum, uint8_t * data, int *hexcnt) int bg, en, i; uint32_t temp; - //if (hexcnt % 2) - // return 1; - if (param_getptr(line, &bg, &en, paramnum)) return 1; *hexcnt = en - bg + 1; @@ -601,4 +596,20 @@ uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor) { if(!(quotient == 0 && remainder == 0)) result += HornerScheme(quotient, divider, factor) * factor + remainder; return result; +} + +// determine number of logical CPU cores (use for multithreaded functions) +extern int num_CPUs(void) +{ +#if defined(_WIN32) + #include + SYSTEM_INFO sysinfo; + GetSystemInfo(&sysinfo); + return sysinfo.dwNumberOfProcessors; +#elif defined(__linux__) || defined(__APPLE__) + #include + return sysconf(_SC_NPROCESSORS_ONLN); +#else + return 1; +#endif } \ No newline at end of file diff --git a/client/util.h b/client/util.h index 8b59a9987..802354689 100644 --- a/client/util.h +++ b/client/util.h @@ -156,4 +156,5 @@ extern uint32_t SwapBits(uint32_t value, int nrbits); extern uint32_t reflect(uint32_t v, int b); extern uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor); +extern int num_CPUs(void); // number of logical CPUs #endif \ No newline at end of file