Using chdir() Causes Segmentation Fault - c++

I'm writing a batch emulator as a personal project. I'm trying to implement the cd command using chdir() from unistd.h. However, using this causes a segfault.
main.cpp:
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
//Custom headers
#include "splitting_algorithm.hpp"
#include "lowercase.hpp"
#include "chdir.hpp"
//Used to get and print the current working directory
#define GetCurrentDir getcwd
using namespace std;
int main(int argc, char* argv[])
{
string command;
//Begin REPL code
while (true)
{
//Prints current working directory
cout<<cCurrentPath<<": ";
std::getline(std::cin, command);
vector<string> tempCommand = strSplitter(command, " ");
string lowerCommand = makeLowercase(string(strSplitter(command, " ")[0]));
//Help text
if(tempCommand.size()==2 && string(tempCommand[1])=="/?")
{
cout<<helpText(lowerCommand);
}
//Exit command
else if(lowerCommand=="exit")
{
return 0;
}
else if(lowerCommand=="chdir")
{
cout<<string(tempCommand[1])<<endl;
chdir(tempCommand[1]);
}
else
cout<<"Can't recognize \'"<<string(tempCommand[0])<<"\' as an internal or external command, or batch script."<<endl;
}
return 0;
}
chdir.cpp:
#include <cstdlib>
#include <string>
#include <unistd.h>
void chdir(std::string path)
{
//Changes the current working directory to path
chdir(path);
}
Strangely enough, using cout to get the path for chdir works perfectly fine. How do I fix this?

You have recursive, unterminated behaviour in Your code. This overflows the stack.
Try to insert breakpoint in void chdir(std::string path) and see what happens.
You will see that the function chdir calls itself, and in turn calls itself again, and again and... well, segmentation fault.
Also, try to see what "call stack" is in the debugger, this issue is very visible there.

You should invoke the underlying chdir function using
::chdir(path.c_str());
or you will just call your own method again.
In unistd.h, chdir is defined as:
int chdir(const char *);
So you must call it with a const char* argument or the compiler will search for another function called "chdir" which take a std::string argument and use that instead.

Related

C++ regex_replace not replacing string

I'm a newbie with C++. I'm trying to learn string replacement.
I'm writing a short (supposed to be a tutorial) program to change directory names.
For example, I want to change "/home" at the beginning of a directory name with "/illumina/runs" thus:
#ifdef WINDOWS
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
#include<iostream>
#include <string>
#include <regex>
#include <iterator>
using namespace std;
std::string get_current_dir() {
char buff[FILENAME_MAX]; //create string buffer to hold path
GetCurrentDir( buff, FILENAME_MAX );
string current_working_dir(buff);
return current_working_dir;
}
int main() {
string b2_dir = get_current_dir();
std::regex_replace(b2_dir, std::regex("^/home"), "/illumina/runs");
cout << b2_dir << endl;
return 0;
}
I'm following an example from http://www.cplusplus.com/reference/regex/regex_replace/ but I don't see why this isn't changing.
In case what I've written doesn't make sense, the Perl equivalent is $dir =~ s/^\/home/\/illumina\/runs/ if that helps to understand the problem better
Why isn't this code altering the string as I tell it to? How can I get C++ to alter the string?
std::regex_replace does not modify its input argument. It produces a new std::string as a return value. See e.g. here: https://en.cppreference.com/w/cpp/regex/regex_replace (your call uses overload number 4).
This should fix your problem:
b2_dir = std::regex_replace(b2_dir, std::regex("^/home"), "/illumina/runs");

Why does my program only work with the debug build?

I have a project that has the main method accessing another method from another source file, BigDog(int). I'm pretty sure the code is right but CodeBlocks seems to not be able to detect the definition of the method unless I build the other file using debug build in CodeBlocks. In Release, I get the following error when building:
Error: undefined reference to 'BigDog(int)'
Why is that so?
main.cpp
#include <iostream>
using namespace std;
void BigDog(int KibblesCount);
int main()
{
BigDog(3);
return 0;
}
mystuff.cpp
#include <iostream>
using namespace std;
void BigDog(int KibblesCount)
{
cout << KibblesCount;
}
If you're adding a new file in codeblocks, make sure to check the checkmarks in the dialog to add it to both the debug and the release build.
Also its better practice to move your declarations to a header file and include that where needed, like this:
main.cpp:
#include "mystuff.h"
int main()
{
BigDog(3);
return 0;
}
mystuff.h:
#pragma once
void BigDog(int KibblesCount);
mystuff.cpp:
#include "mystuff.h"
#include <iostream>
void BigDog(int KibblesCount)
{
// add a newline so the line gets printed immediately
std::cout << KibblesCount << "\n";
}

readdir hangs up if libpocofoundation is linked with my tiny app

I've started debbuging on some app, which hangs up in a loop based on readdir call.
Step by step I've cut everything but problem code, this is it:
So, in basic, it shows name of first entry and nothing more. It even does not exits, just waiting for something.
Also, I've found, that if don't lin it against libpocofoundation, it works.
But I have to do it because it used in the original app.
I'm a little bit confused, I don't use Poco in this example in any way, but it some way hangs it.
Please help me, I'm in panic :D
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <cstring>
#include <string>
#include <fcntl.h>
using namespace std;
int main(int argc, char *argv[])
{
const char TMP_DIR[] = "/opt";
DIR *dir = opendir(TMP_DIR);
std::cerr
<< readdir(dir)->d_name
<< readdir(dir)->d_name
<< std::endl;
return 0;
}
So... I don't know why it was happening. So I just dropped libpoco.

How to call a powershell script from a C code

In my case, I needed to call a powershell script from a c or c++ code source, found few links which were pretty clumsy and not good with c++, I simply want a roadmap if its possible invoking a powershell script which lists directory contents from a code snippet written in c or c++
C++ code :
#include<iostream>
#include <io.h> // For access().
#include <sys/types.h> // For stat().
#include <sys/stat.h> // For stat().
#include <string>
using namespace std;
void main()
{
string strPath = "d:\\callPowerShell.ps1";
//access function:
//The function returns 0 if the file has the given mode.
//The function returns –1 if the named file does not exist or does not have the given mode
if(access(strPath.c_str(),0) == 0)
{
system("start powershell.exe Set-ExecutionPolicy RemoteSigned \n");
system("start powershell.exe d:\\callPowerShell.ps1");
system("cls");
}
else
{
system("cls");
cout << "File is not exist";
system("pause");
}
}
First error :
#include <io.h> // For access().
access is in this lib:
#include <cstdlib>
Next :
error: 'system' was not declared in this scope
#include <unistd.h>
And finally :
The caractere '\' is a special caractere for C/C++ then you have to add another '\' like :
system("start powershell.exe C:\\users\\sqtk-mal\\script1.ps1");
In C++
#include <cstdlib>
std::system("command");
In c
#include <stdlib.h>
system("command");

segmentation fault reading json file

I need to read the information contained in a json file like this:
{"first":10, "second":"0", "P1":"1.e-20","P2":"1000","P3":"1000","P4":"1000","P5":"1"}
Since I do not have experience with this issue, I started by playing with the short code you can see below these lines. It does compile with no problem but it gives a segmentation fault back upon execution. The file general.json is in the same folder. The information contained in the json file is correctly printed in the screen if I comment the last line. Could anyone tell me what am I doing wrong?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fstream> // fstream.h in old versions of g++
#include <iostream> //para cout
#include <sstream>
#include <json/json.h>
using namespace std;
int main() {
struct json_object *new_json, *json_arr, *json_reg, *json_field;
string line;
stringstream jsonfile;
ifstream json("file.json", ios::in);
{getline(json, line); do {jsonfile << line;} while (getline(json, line));}
json.close();
cout << jsonfile.str().c_str();
new_json=json_tokener_parse(jsonfile.str().c_str());
json_field=json_object_object_get(json_reg, "first");
}
You are using the json_reg pointer without initializing it and the function dereferences it. You are (most likely) using json-c where:
json_object_object_get calls json_object_object_get_ex on the object
json_object_object_get_ex does switch(jso->o_type) dereferencing an invalid pointer