Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 68 additions & 6 deletions src/emc/tooldata/tooldata_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
Expand All @@ -30,6 +31,7 @@
#define DB_VERSION "v2.1"
#define MAX_DB_PROGRAM_ARGS 10
#define UNEXPECTED_MSG fprintf(stderr,"UNEXPECTED %s %d\n",__FILE__,__LINE__);
#define MAX_CMD_LEN 1024

static bool db_live = 0;
static bool db_show = 0; // use environmental var: DB_SHOW
Expand Down Expand Up @@ -243,6 +245,13 @@ int tooldata_db_init(char progname_plus_args[],int random_toolchanger)
char* child_argv[MAX_DB_PROGRAM_ARGS] = {0};
char* saveptr;
char* token = strtok_r(progname_plus_args, " ", &saveptr);
int n;
char* fork_argv[MAX_DB_PROGRAM_ARGS] = {0};
char* cp;
char* envpath;
char prog_name[PATH_MAX] = "\0";
char prog_path[PATH_MAX];
struct stat stat_buf;
while (token != NULL) {
child_argv[child_argc] = token;
child_argc++;
Expand All @@ -255,13 +264,66 @@ int tooldata_db_init(char progname_plus_args[],int random_toolchanger)
}
snprintf(db_childname,sizeof(db_childname),"%s",child_argv[0]);
is_random_toolchanger = random_toolchanger;

if (access(child_argv[0],X_OK)) {
fprintf(stderr,"!!!!db_init: <%s> not executable\n",child_argv[0]);
if (db_childname[0] != '/') { // prog path not absolute
if(strchr(db_childname, '/') != NULL){ // prog path is relative
strncpy(prog_path, db_childname, PATH_MAX);
}
else { // just prog name, search in the actual directory or PATH
envpath = getenv( (char*)"PATH");
Comment on lines +267 to +272
Copy link
Copy Markdown
Contributor

@BsAtHome BsAtHome May 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and the rest...
I think this needs to be redesigned using std::string and std::vector<std::string>. There is no point nowadays to use C style (more error prone) string handling in a C++ source file. It was already outdated in 2021 when written and added. Now, when you try to fix it, it should be modernized to remove the standard pitfalls.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I'm not much familiar with c++

if ( envpath != NULL ) {
while ( *envpath != '\0' ) {
/* copy a single directory from the PATH env variable */
n = 0;
while ( (*envpath != ':') && (*envpath != '\0') && (n < MAX_CMD_LEN)) {
prog_path[n++] = *envpath++;
}
/* append '/' and program name */
if ( n < MAX_CMD_LEN ) {
prog_path[n++] = '/';
}
cp = db_childname;
while ((*cp != '\0') && ( n < MAX_CMD_LEN)) {
prog_path[n++] = *cp++;
}
prog_path[n] = '\0';
fprintf(stdout,"Trying '%s'\n",prog_path);
if ( stat(prog_path, &stat_buf) != 0 ) {
/* no luck, clear prog_path to indicate failure */
prog_path[0] = '\0';
/* and get ready to try the next directory */
if ( *envpath == ':' ) {
envpath++;
}
} else {
/* success, break out of loop */

break;
}
}

}
}
}
else {
strncpy(prog_path, db_childname, PATH_MAX);
}
if (prog_path[0] == '\0'){
strncpy(prog_name, db_childname, PATH_MAX);
fork_argv[0] = prog_name;
}
else {
fork_argv[0] = prog_path;
}
if (access(prog_path, F_OK)){
fprintf(stderr,"!!!!db_init: <%s> not found\n",fork_argv[0]);
return -1;
}
if (access(prog_path,X_OK)) {
fprintf(stderr,"!!!!db_init: <%s> not executable\n",fork_argv[0]);
return -1;
}
//fprintf(stderr,"=====db_childname=%s\n",db_childname);
if (0 == fork_create(child_argc,child_argv) ) {
if (0 == fork_create(child_argc,fork_argv) ) {
//fprintf(stderr,"=====db_init forked %s\n",child_argv[0]);
} else {
fprintf(stderr,"!!!!!db_init: fork FAIL\n");
Expand All @@ -271,7 +333,7 @@ int tooldata_db_init(char progname_plus_args[],int random_toolchanger)

// block for response
fprintf(stderr,"====Waiting for %s version reply from %s\n",
DB_VERSION,child_argv[0]);
DB_VERSION,fork_argv[0]);
char reply[CANON_TOOL_ENTRY_LEN];

if (read_reply(reply,sizeof(reply)) < 0 ) {
Expand All @@ -286,7 +348,7 @@ int tooldata_db_init(char progname_plus_args[],int random_toolchanger)
db_live = 0;
return -1;
} else {
fprintf(stderr,"====Connected to %s\n",child_argv[0]);
fprintf(stderr,"====Connected to %s\n",fork_argv[0]);
}
}
if (db_debug) {
Expand Down
Loading