mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-19 21:03:48 -07:00
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:
parent
f942e1ed05
commit
8f262aa1bf
6 changed files with 150 additions and 20 deletions
|
@ -33,6 +33,10 @@
|
|||
#include <lualib.h>
|
||||
#include <lauxlib.h>
|
||||
|
||||
#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);
|
||||
|
|
91
client/scandir.c
Normal file
91
client/scandir.c
Normal 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
23
client/scandir.h
Normal 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__
|
|
@ -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
|
||||
|
||||
|
|
|
@ -22,8 +22,9 @@ int ukbhit(void) {
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
@ -602,3 +597,19 @@ uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor) {
|
|||
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 <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
|
||||
}
|
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue