mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-19 21:03:48 -07:00
fix experimental_lib/example_c/test_grab: no fork&pipe needed, use pm3_grabbed_output_get
This commit is contained in:
parent
1fa84f203d
commit
b91f7742c1
2 changed files with 26 additions and 52 deletions
|
@ -9,6 +9,6 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
pm3 *p;
|
pm3 *p;
|
||||||
p = pm3_open(argv[1]);
|
p = pm3_open(argv[1]);
|
||||||
pm3_console(p, "hw status");
|
pm3_console(p, "hw status", true);
|
||||||
pm3_close(p);
|
pm3_close(p);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,7 @@
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
int pipefd[2];
|
// char buf[8196 + 1];
|
||||||
char buf[8196 + 1];
|
|
||||||
size_t n;
|
size_t n;
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
|
@ -15,60 +14,35 @@ int main(int argc, char *argv[]) {
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pipe(pipefd) == -1) {
|
pm3 *p;
|
||||||
exit(-1);
|
p = pm3_open(argv[1]);
|
||||||
}
|
|
||||||
|
|
||||||
int pid = fork();
|
// Execute the command
|
||||||
if (pid == -1) {
|
pm3_console(p, "hw status", false);
|
||||||
perror("fork");
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// child
|
const char *buf = pm3_grabbed_output_get(p);
|
||||||
if (pid == 0) {
|
const char *line_start = buf;
|
||||||
printf("[INFO] inside child\n");
|
const char *newline_pos;
|
||||||
|
while ((newline_pos = strchr(line_start, '\n')) != NULL) {
|
||||||
|
// Determine the length of the line
|
||||||
|
size_t line_length = newline_pos - line_start;
|
||||||
|
|
||||||
// Redirect stdout to the write end of the pipe
|
// Create a temporary buffer to hold the line
|
||||||
dup2(pipefd[1], STDOUT_FILENO);
|
char line[line_length + 1];
|
||||||
|
strncpy(line, line_start, line_length);
|
||||||
|
line[line_length] = '\0'; // Null-terminate the string
|
||||||
|
|
||||||
close(pipefd[0]); // Child: close read end of the pipe
|
if (strstr(line, "ERROR") != NULL) {
|
||||||
close(pipefd[1]); // Close original write end
|
printf("%s", line);
|
||||||
|
}
|
||||||
pm3 *p;
|
if (strstr(line, "Unique ID") != NULL) {
|
||||||
p = pm3_open(argv[1]);
|
printf("%s", line);
|
||||||
|
|
||||||
// Execute the command
|
|
||||||
pm3_console(p, "hw status");
|
|
||||||
pm3_close(p);
|
|
||||||
_exit(-1);
|
|
||||||
} else {
|
|
||||||
|
|
||||||
printf("[INFO] inside parent\n");
|
|
||||||
// Parent: close write end of the pipe
|
|
||||||
close(pipefd[1]);
|
|
||||||
|
|
||||||
// Read from the pipe
|
|
||||||
while (1) {
|
|
||||||
n = read(pipefd[0], buf, sizeof(buf));
|
|
||||||
if (n == -1) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (n == 0) {
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
// null termination
|
|
||||||
buf[n] = 0;
|
|
||||||
if (strstr(buf, "ERROR") != NULL) {
|
|
||||||
printf("%s", buf);
|
|
||||||
}
|
|
||||||
if (strstr(buf, "Unique ID") != NULL) {
|
|
||||||
printf("%s", buf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close read end
|
// Move to the next line
|
||||||
close(pipefd[0]);
|
line_start = newline_pos + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pm3_close(p);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue