Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string>
- #include <vector>
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/wait.h>
- #include <errno.h>
- #include <dirent.h>
- #include <sstream>
- #define PIPE_READ 0
- #define PIPE_WRITE 1
- using namespace std;
- std::string createChild(const char* bashCmd)
- {
- int aStdinPipe[2];
- int aStdoutPipe[2];
- int nChild;
- int nResult;
- char buffer[4096];
- char ch;
- std::string commandResult;
- if (pipe(aStdinPipe) < 0)
- {
- exit(1);
- }
- if (pipe(aStdoutPipe) < 0)
- {
- close(aStdinPipe[PIPE_READ]);
- close(aStdinPipe[PIPE_WRITE]);
- exit(1);
- }
- nChild = fork();
- if (0 == nChild)
- {
- if (dup2(aStdinPipe[PIPE_READ], STDIN_FILENO) == -1)//redirect stdin
- {
- exit(1);
- }
- if (dup2(aStdoutPipe[PIPE_WRITE], STDOUT_FILENO) == -1)
- {
- exit(1);
- }
- close(aStdinPipe[PIPE_READ]);
- close(aStdinPipe[PIPE_WRITE]);
- close(aStdoutPipe[PIPE_READ]);
- close(aStdoutPipe[PIPE_WRITE]);
- nResult = execl("/bin/bash", "bash", "-c", bashCmd, (char*)0);
- exit(nResult);
- }
- else if (nChild > 0)
- {
- close(aStdinPipe[PIPE_READ]);
- close(aStdoutPipe[PIPE_WRITE]);
- memset(buffer, 0, sizeof(buffer));
- while (read(aStdoutPipe[PIPE_READ], &ch, 1) > 0)
- {
- if (ch != 0)
- commandResult.push_back(ch);
- else
- {
- commandResult.append(" ");
- }
- }
- wait(NULL);//wait for child proc
- close(aStdinPipe[PIPE_WRITE]);
- close(aStdoutPipe[PIPE_READ]);
- }
- else
- {
- close(aStdinPipe[PIPE_READ]);
- close(aStdinPipe[PIPE_WRITE]);
- close(aStdoutPipe[PIPE_READ]);
- close(aStdoutPipe[PIPE_WRITE]);
- }
- return commandResult;
- }
- std::string exeQte(std::string command)
- {
- std::string commandResult, reCmd, reCommand;
- char ccommand[4096];
- reCommand = command;
- strcpy(ccommand, reCommand.c_str());
- commandResult = createChild(ccommand);
- return commandResult;
- }
- int main(void)
- {
- std::string diskName, commandResult;
- std::stringstream scommand;
- std::string path = "/Volumes/encryb/fileb";
- //std::string path = "/Volumes/notencry";
- std::string delimiter = "/";
- size_t pos = 0;
- std::string token;
- int counter = 0;
- while ((pos = path.find(delimiter)) != std::string::npos)
- {
- token = path.substr(0, pos);
- if (counter == 2)
- {
- diskName = token;
- }
- path.erase(0, pos + delimiter.length());
- counter++;
- }
- scommand.str("");
- scommand << "diskutil cs list | grep -e \"Volume Name:\" | awk '{print$3}'";
- commandResult = exeQte(scommand.str());
- commandResult.pop_back();
- if (diskName.compare(commandResult) == 0)
- {
- scommand.str("");
- scommand << "diskutil cs list | grep -e \"Conversion\" | awk '{print$3}'";
- commandResult = exeQte(scommand.str());
- if(commandResult.find("Complete") != std::string::npos)
- {
- cout<< "Disk is encrypted" <<endl;
- }
- }
- else
- {
- cout<< "Disk is not encrypted" <<endl;
- }
- return 0;
- }
- //g++ -g -w -o testEncrypt main.cpp && ./testEncrypt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement