Question

can someone help me with changing this to c++ language #include <stdlib.h> #include <stdio.h> #include <pthread.h>...

can someone help me with changing this to c++ language


#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <dirent.h>
#include <sys/wait.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>

char *pathLog;
char *pathRep;   
struct stat attr;
int fileNum, curNum;

char *create_logPath(char *directory);   
void *funcChecker(void *pathSub);
void *funcprintTimeAndChanges(void *pathSub);   

void main(int argc, char *argv[])
{
   if(argc < 3)
   {
printf("%s must be executed with exactly two additional arguments (pathRep, pathSub)!\n", argv[0]);
printf("Typed count is %d\n", argc);
printf("Aborted automatically...\n");
exit(-1);

}
   else if(argc > 3)
   {
printf("Too many arguments!. %s must be executed with exactly two additional arguments (pathRep, pathSub)!\n", argv[0]);
printf("Aborted automatically...\n");
exit(-1);
}

printf("Main function ->\n");
printf("pathRep: %s\n", argv[1]);
printf("pathSub: %s\n", argv[2]);

printf("\nfuncChecker ->\n");
printf("pathChk: %s\n\n", argv[2]);

pathLog = create_logPath(argv[1]);

printf("funcprintTimeAndChanges ->\n");
printf("pathChk: %s\n", argv[2]);
printf("pathLog: %s\n\n", pathLog);

pathRep = argv[1];
char *pathSub = argv[2];

pthread_t threadChecker;
if(pthread_create(&threadChecker, NULL, funcChecker, (void *)pathSub))
{
printf("Error! Return code from pthread_create()\n");
exit(-1);
}

pthread_join(threadChecker, NULL);   

exit(0);
}

// Get the 0_repHist.log file path
char* create_logPath(char *directory)
{
char pathFile[100];

char* logname = "_0-repHist.log";
char* lpath = malloc(strlen(directory) + strlen(logname));

strcpy(lpath, directory);
strcat(lpath, logname);

return lpath;
}

// Check is there any change in FolderSubmission
void *funcChecker(void *pathSub)
{
pthread_t threadWorker_TimeAndChanges;
fileNum = curNum = 0;
char *sub = (char *) pathSub;

DIR *d;
struct dirent *dir;   
d = opendir(sub);

if (d)
{
while ((dir = readdir(d)) != NULL)
{
if(dir->d_name[0] != '.')
fileNum++;
}
closedir(d);
}

while(1)
{
d = opendir(sub);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (dir->d_name[0] != '.')
curNum++;
}
closedir(d);

if(curNum != fileNum)   
{
   stat(sub, &attr);
}
if(pthread_create(&threadWorker_TimeAndChanges, NULL, funcprintTimeAndChanges, (void *)sub))   
{
printf("Error! Return code from pthread_create()\n");
exit(-1);
}
pthread_join(threadWorker_TimeAndChanges, NULL);
fileNum = curNum;
}
curNum = 0;
sleep(1);   
}
}

void *funcprintTimeAndChanges(void *pathSub)
{
char *sub = (char *) pathSub;
char pathFile[300];
char pathFile2[400];

DIR *d;
struct dirent *dir;
d = opendir(sub);

if(fileNum != curNum)   
{
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if(dir->d_name[0] != '.')
{
sprintf(pathFile,"cp -r %s%s %s", sub, dir->d_name, pathRep);
system(pathFile);
}
}
closedir(d);
}

// Print the changes and time to the terminal

printf("Last modified time: %s", ctime(&attr.st_mtime));
sprintf(pathFile, "ls -sR %s", sub);
system(pathFile);
printf("------------------------------------------------------------------------------\n\n");


snprintf(pathFile2, sizeof(pathFile2), "date >> %s && ls -sR %s >> %s && echo '----------\n' >> %s", pathLog, sub, pathLog, pathLog);
system(pathFile2);
}

   printf("\r\033[1A%c", system("date")); // \r: Set cursor to the first char of the line; \033[1A: move cursor up 1 line;
   fflush(stdout);   // Clear output buffer
   usleep((int)(100000));
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <dirent.h>

#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
using namespace std;

char *pathLog;
char *pathRep;
struct stat attr;
int fileNum, curNum;

char *create_logPath(char *directory);
void *funcChecker(void *pathSub);
void *funcprintTimeAndChanges(void *pathSub);

int main(int argc, char *argv[])
{
if(argc < 3)
{
cout<<argv[0]<<"must be executed with exactly two additional arguments (pathRep, pathSub)!\n";
cout<<"Typed count is "<<argc<<"\n";
cout<<"Aborted automatically...\n";
exit(-1);
}
else if(argc > 3)
{
cout<<"Too many arguments!."<<argv[0]<<" must be executed with exactly two additional arguments (pathRep, pathSub)!\n";
cout<<"Aborted automatically...\n";
exit(-1);
}

cout<<"Main function ->\n";
cout<<"pathRep:"<<argv[1]<<"\n";
cout<<"pathSub:"<<argv[2]<<"\n";

cout<<"\nfuncChecker ->\n";
cout<<"pathChk:"<<argv[2]<<"\n\n";

pathLog = create_logPath(argv[1]);

cout<<"funcprintTimeAndChanges ->\n";
cout<<"pathChk:"<<argv[2]<<"\n";
cout<<"pathLog:"<<pathLog<<"\n\n";

pathRep = argv[1];
char *pathSub = argv[2];

pthread_t threadChecker;
if(pthread_create(&threadChecker, NULL, funcChecker, (void *)pathSub))
{
cout<<"Error! Return code from pthread_create()\n";
exit(-1);
}

pthread_join(threadChecker, NULL);
exit(0);
return 0;
}

// Get the 0_repHist.log file path
char* create_logPath(char *directory)
{
char pathFile[100];

char* logname = "_0-repHist.log";
char *lpath=new char[strlen(directory) + strlen(logname)];
strcpy(lpath, directory);
strcat(lpath, logname);
return lpath;
}

// Check is there any change in FolderSubmission
void *funcChecker(void *pathSub)
{
pthread_t threadWorker_TimeAndChanges;
fileNum = curNum = 0;
char *sub = (char *) pathSub;

DIR *d;
struct dirent *dir;
d = opendir(sub);

if (d)
{
while ((dir = readdir(d)) != NULL)
{
if(dir->d_name[0] != '.')
fileNum++;
}
closedir(d);
}

while(1)
{
d = opendir(sub);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (dir->d_name[0] != '.')
curNum++;
}
closedir(d);

if(curNum != fileNum)
{
stat(sub, &attr);
}
if(pthread_create(&threadWorker_TimeAndChanges, NULL, funcprintTimeAndChanges, (void *)sub))
{
cout<<"Error! Return code from pthread_create()\n";
exit(-1);
}
pthread_join(threadWorker_TimeAndChanges, NULL);
fileNum = curNum;
}
curNum = 0;
sleep(1);
}
}

void *funcprintTimeAndChanges(void *pathSub)
{
char *sub = (char *) pathSub;
char pathFile[300];
char pathFile2[400];

DIR *d;
struct dirent *dir;
d = opendir(sub);

if(fileNum != curNum)
{
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if(dir->d_name[0] != '.')
{
sprintf(pathFile,"cp -r %s%s %s", sub, dir->d_name, pathRep);
system(pathFile);
}
}
closedir(d);
}

// Print the changes and time to the terminal

cout<<"Last modified time: "<<ctime(&attr.st_mtime);
sprintf(pathFile, "ls -sR %s", sub);
system(pathFile);
cout<<"------------------------------------------------------------------------------\n\n";


snprintf(pathFile2, sizeof(pathFile2), "date >> %s && ls -sR %s >> %s && echo '----------\n' >> %s", pathLog, sub, pathLog, pathLog);
system(pathFile2);
}

cout<<"\r\033[1A"<<system("date"); // \r: Set cursor to the first char of the line; \033[1A: move cursor up 1 line;
fflush(stdout); // Clear output buffer
usleep((int)(100000));
}

Add a comment
Know the answer?
Add Answer to:
can someone help me with changing this to c++ language #include <stdlib.h> #include <stdio.h> #include <pthread.h>...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Read the following code, threaded recursive calculation of Fibonacci number of n: #include <stdio.h> #include <stdlib.h>...

    Read the following code, threaded recursive calculation of Fibonacci number of n: #include <stdio.h> #include <stdlib.h> #include <pthread.h> void *fib(void *arg); int main(int argc, char **argv){     int n = atoi(argv[1]);     printf("%d\n", (int)fib(n)); } void *fib(void *arg){     int n;     pthread_t thread1;     pthread_t thread2;     void *a;     void *b;     int c;     n = (int)arg;     if (n <= 0) return 0;     if (n == 1) return 1;     pthread_create(&thread1, NULL, fib, n-1);    ...

  • I am supposed to write documentation and report for the code below but I am new...

    I am supposed to write documentation and report for the code below but I am new to operating system concepts I will appreciate if someone can help make a detailed comment on each line of code for better understanding. Thanks #include <pthread.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <ctype.h> #define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) struct thread_info...

  • so in this code, it computes the sum 1+2+....+n but i want it to compute 2*(1+2+....+n)...

    so in this code, it computes the sum 1+2+....+n but i want it to compute 2*(1+2+....+n) using semaphores implement solution to the critical section problem #include #include int sum; /* this data is shared by the thread(s) */ void *runner(void *param); /* threads call this function */ int main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of thread attributes */ if (argc != 2) { fprintf(stderr,"usage: a.out \n"); return -1; } if...

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • a multi-threaded producer / consumer program without any locking or signaling. It therefore has synchronization problems....

    a multi-threaded producer / consumer program without any locking or signaling. It therefore has synchronization problems. Add locks and signals so that it works correctly. /* Homework 5.X */ /* Robin Ehrlich */ /* compile: gcc Homework5.c -lpthread */ #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> struct class {    struct class *next;    int id;    int grade; }; #define SLEEP_TIME 1 #define MAX_PRODUCE 10 static struct class *classHead = NULL; static struct class *classTail = NULL; static...

  • i want to fix the solution and provide an explanation why the pthread_join is not working...

    i want to fix the solution and provide an explanation why the pthread_join is not working as intended? #include <stdio.h> /* standard I/O routines */ #include <pthread.h> /* pthread functions and data structures */ void* PrintHello(void* data) { pthread_t tid = (pthread_t)data; /* data received by thread */ pthread_join(tid, NULL); /* wait for thread tid */ printf("Hello from new thread %u - got %u\n", pthread_self(), data); pthread_exit(NULL); /* terminate the thread */ } /* like any C program, program's execution...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include...

    devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <termios.h> #include <sys/types.h> #include <sys/mman.h>    #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) #define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1) int main(int argc, char **argv) { int fd; void *map_base = NULL, *virt_addr = NULL; unsigned long read_result, writeval; off_t target; int access_type = 'w';    if(argc...

  • In C using the following 2 files to create a 3rd file that uses multiple threads...

    In C using the following 2 files to create a 3rd file that uses multiple threads to improve performance. Split the array into pieces and each piece is handled by a different thread. Use 8 threads. run and compile in linux. #include <stdio.h> #include <sys/time.h> #define BUFFER_SIZE 4000000 int countPrime=0; int numbers[BUFFER_SIZE]; int isPrime(int n) { int i; for(i=2;i<n;i++) if (n%i==0) return 0; return 1; } int main() { int i; // fill the buffer for(i=0;i<BUFFER_SIZE;i++) numbers[i] = (i+100)%100000; //...

  • #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <unistd.h> #include <pthread.h> pthread_mutex_t mtx; // used by each...

    #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <unistd.h> #include <pthread.h> pthread_mutex_t mtx; // used by each of the three threads to prevent other threads from accessing global_sum during their additions int global_sum = 0; typedef struct{ char* word; char* filename; }MyStruct; void *count(void*str) { MyStruct *struc; struc = (MyStruct*)str; const char *myfile = struc->filename; FILE *f; int count=0, j; char buf[50], read[100]; // myfile[strlen(myfile)-1]='\0'; if(!(f=fopen(myfile,"rt"))){ printf("Wrong file name"); } else printf("File opened successfully\n"); for(j=0; fgets(read, 10, f)!=NULL; j++){ if (strcmp(read[j],struc->word)==0)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT