ADD: num_CPUs(void) method from pm3 master

CHG: clean up in util.c
ADD: seems like mingw doesn't have scandir. Had to add one. *untested*
This commit is contained in:
iceman1001 2017-07-27 09:36:16 +02:00
commit 8f262aa1bf
6 changed files with 150 additions and 20 deletions

View file

@ -33,6 +33,10 @@
#include <lualib.h> #include <lualib.h>
#include <lauxlib.h> #include <lauxlib.h>
#ifdef _WIN32
#include "scandir.h"
#endif
static int CmdHelp(const char *Cmd); static int CmdHelp(const char *Cmd);
static int CmdList(const char *Cmd); static int CmdList(const char *Cmd);
static int CmdRun(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++) { for (uint16_t i = 0; i < n; i++) {
if(str_ends_with(namelist[i]->d_name, ".lua")) 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[i]);
} }
free(namelist); free(namelist);

91
client/scandir.c Normal file
View file

@ -0,0 +1,91 @@
/* scandir.cc
Copyright 1998, 1999, 2000, 2001 Red Hat, Inc.
Written by Corinna Vinschen <corinna.vinschen@cityweb.de>
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

23
client/scandir.h Normal file
View file

@ -0,0 +1,23 @@
//-----------------------------------------------------------------------------
// Copyright (C) 2017 iceman <iceman at iuse.se>
//
// 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 <dirent.h>
#include <stdlib.h>
#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__

View file

@ -14,21 +14,21 @@ Arguments:
]] ]]
--- ---
-- A debug printout-function -- A debug printout-function
function dbg(args) local function dbg(args)
if DEBUG then if DEBUG then
print("###", args) print('###', args)
end end
end end
--- ---
-- This is only meant to be used when errors occur -- This is only meant to be used when errors occur
function oops(err) local function oops(err)
print("ERROR: ",err) print('ERROR: ',err)
end end
--- ---
-- Usage help -- Usage help
function help() local function help()
print(desc) print(desc)
print("Example usage") print('Example usage')
print(example) print(example)
end end

View file

@ -19,11 +19,12 @@ int ukbhit(void) {
int error; int error;
static struct termios Otty, Ntty; static struct termios Otty, Ntty;
if ( tcgetattr(STDIN_FILENO, &Otty) == -1) return -1; if ( tcgetattr(STDIN_FILENO, &Otty) == -1) return -1;
Ntty = Otty; 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_lflag &= ~ICANON; // control mode = raw
Ntty.c_cc[VMIN] = 1; // return if at least 1 character is in the queue Ntty.c_cc[VMIN] = 1; // return if at least 1 character is in the queue
Ntty.c_cc[VTIME] = 0; // no timeout. Wait forever Ntty.c_cc[VTIME] = 0; // no timeout. Wait forever
@ -37,6 +38,7 @@ int ukbhit(void) {
} }
#else #else
int ukbhit(void) { int ukbhit(void) {
return kbhit(); return kbhit();
} }
@ -61,10 +63,7 @@ void AddLogLine(char *file, char *extData, char *c) {
fprintf(f, "%s", extData); fprintf(f, "%s", extData);
fprintf(f, "%s\n", c); fprintf(f, "%s\n", c);
fflush(f); fflush(f);
if (f) { fclose(f);
fclose(f);
f = NULL;
}
} }
void AddLogHex(char *fileName, char *extData, const uint8_t * data, const size_t len){ 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) { void AddLogUint64(char *fileName, char *extData, const uint64_t data) {
char buf[20] = {0}; char buf[20] = {0};
memset(buf, 0x00, sizeof(buf)); memset(buf, 0x00, sizeof(buf));
//sprintf(buf, "%X%X", (unsigned int)((data & 0xFFFFFFFF00000000) >> 32), (unsigned int)(data & 0xFFFFFFFF)); sprintf(buf, "%016" PRIx64 "", data);
sprintf(buf, "%012" PRIx64 "", data);
AddLogLine(fileName, extData, buf); AddLogLine(fileName, extData, buf);
} }
@ -318,7 +316,7 @@ int param_getlength(const char *line, int paramnum)
char param_getchar(const char *line, int paramnum) char param_getchar(const char *line, int paramnum)
{ {
int bg, en; int bg, en;
if (param_getptr(line, &bg, &en, paramnum)) return 0x00; if (param_getptr(line, &bg, &en, paramnum)) return 0;
return line[bg]; 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; int bg, en, i;
uint32_t temp; uint32_t temp;
//if (hexcnt % 2)
// return 1;
if (param_getptr(line, &bg, &en, paramnum)) return 1; if (param_getptr(line, &bg, &en, paramnum)) return 1;
*hexcnt = en - bg + 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)) if(!(quotient == 0 && remainder == 0))
result += HornerScheme(quotient, divider, factor) * factor + remainder; result += HornerScheme(quotient, divider, factor) * factor + remainder;
return result; return result;
}
// determine number of logical CPU cores (use for multithreaded functions)
extern int num_CPUs(void)
{
#if defined(_WIN32)
#include <sysinfoapi.h>
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
#elif defined(__linux__) || defined(__APPLE__)
#include <unistd.h>
return sysconf(_SC_NPROCESSORS_ONLN);
#else
return 1;
#endif
} }

View file

@ -156,4 +156,5 @@ extern uint32_t SwapBits(uint32_t value, int nrbits);
extern uint32_t reflect(uint32_t v, int b); extern uint32_t reflect(uint32_t v, int b);
extern uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor); extern uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor);
extern int num_CPUs(void); // number of logical CPUs
#endif #endif