Flying Mullet
04-19-2009, 09:09 PM
Mulley's brain is tired from looking at C code all day and he's ready to call for help.
I have two programs using system calls in C that I need to write and neither of them are working correctly. The first one takes two parameters, an existing folder and the name of a new folder. The program should create the new folder and copy the contents of the old folder (i.e. files) into the new folder. My program will create the new folder and create the files in the new folder, but it won't write the contents to the files. Here's what I have so far:
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main (int argc, char *argv[])
{
int count;
char buffer[1024];
int folderNew, fileNew, fileOld;
DIR *folderOld;
struct dirent * file;
// ensure the proper number of command line arguments are passed in
if (argc !=3) {
fprintf (stderr,"need 3 args\n");
return 1;
}
// create the new folder
folderNew = mkdir(argv[2],00644);
if (folderNew == -1) {
printf ("can not create: %s\n", argv[2]);
/* fprintf (stderr,"errstring is: %s\n",strerror(errno));; */
return 0;
}
// open the old folder
folderOld = opendir (argv[1]);
if (folderOld == NULL) {
fprintf (stderr,"can not open: %s\n", argv[1]);
/* fprintf (stderr,"errstring is: %s\n",strerror(errno)); */
return 0;
}
// copy the contents
/*
while ( (count = read(folderOld, buffer, sizeof(buffer))) > 0)
{
printf ("in loop\n");
write (folderNew, buffer, count);
}
*/
while (file = readdir(folderOld))
{
// printf ("inode: %ld file name: %s file type: %d offset: %ld length: %d\n",file->d_ino, file->d_name, file->d_type, file->d_off, file->d_reclen);
// only copy the proper file types
if(file->d_type != 4)
{
// navigate to the old folder
chdir(argv[1]);
// open the old file
fileOld = open ("dummy.txt",O_RDONLY);
if (fileOld == -1)
{
fprintf (stderr,"can not open: %s\n", file->d_name);
/* fprintf (stderr,"errstring is: %s\n",strerror(errno)); */
return 0;
}
// navigate to the new folder
chdir("..");
chdir(argv[2]);
// create the new file
fileNew = creat (file->d_name,00644);
if (fileNew == -1)
{
printf ("can not create: %s\n", file->d_name);
/* fprintf (stderr,"errstring is: %s\n",strerror(errno));; */
return 0;
}
while ( (count = read(fileOld, buffer, sizeof(buffer))) > 0)
{
write (fileNew, buffer, count);
}
// navigate back to the parent folder
chdir("..");
}
}
return 0;
}
My second program is supposed to create two child processes. Then the parent sends a message to each child, each child sends a message to the parent, then everything shuts down gracefully. When I'm in the child process/file, it says it there is no such file or directory when I tell it to open the pipe from the parent to receive the message. Here's what I have:
<i>parent.c</i>
/* a simple first example, showing output */
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
pid_t child1pid;
pid_t child2pid;
char message1[]="Hi Child1, this is your parent!!!";
char message2[]="Hi Child2, this is your parent!!!";
int fd1, fd2;
char buff[128];
int num;
/* create new named pipes in current directory with
permissions 777 (rwx for everyone) */
mknod("toChild1", 010777,0);
mknod("toChild2", 010777,0);
/* parent process here */
printf("Howdy from parent process %d\n", getpid());
/* split child 1 here */
if ((child1pid=fork()) == 0) { /* child */
execlp("./child", "./child", "1", "toChild1", NULL);
perror("oops, child had an error!");
/* write to child 1 on that named pipe */
fd1 = open("toChild1", O_WRONLY);
num = write(fd1, message1, sizeof(message1));
return 1;
}
else
{
/* split child 2 here */
if ((child2pid=fork()) == 0) { /* child */
execlp("./child", "./child", "2", "toChild2", NULL);
perror("oops, child had an error!");
/* write to child 2 on that named pipe */
fd2 = open("toChild2", O_WRONLY);
num = write(fd2, message2, sizeof(message2));
return 1;
}
// parent display
else { /* parent */
printf("parent: child1's process id is %d\n",child1pid);
printf("parent: child2's process id is %d\n",child2pid);
printf("parent is going to wait for children to end...\n");
wait();
}
}
/* remove the named pipes from the directory */
close(fd1);
close(fd2);
printf("Bye from the parent!!\n");
}
<i>child.c</i>
#include <stdio.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
if (argc < 2){
printf ("Must supply argument!\n");
return -1;
}
char message[]="Hi Parent!!!";
int fd;
char buff[128];
int num;
printf("Howdy, I'm child %s.\n", argv[1]);
printf("Child %s stream to read is %s.\n", argv[1], argv[2]);
/* read data from the pipe that the parent created */
if ((fd = open(argv[2], O_RDONLY)) == -1) {
perror ("on open of child stream");
// unlink(childName);
return 1;
}
num = read(fd, buff, 128);
printf ("\nChild %s received: %s\n\n", argv[1], buff);
close(fd);
// sleep(atoi(argv[1]));
printf("Bye from child %s!!!\n", argv[1]);
}
I have two programs using system calls in C that I need to write and neither of them are working correctly. The first one takes two parameters, an existing folder and the name of a new folder. The program should create the new folder and copy the contents of the old folder (i.e. files) into the new folder. My program will create the new folder and create the files in the new folder, but it won't write the contents to the files. Here's what I have so far:
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main (int argc, char *argv[])
{
int count;
char buffer[1024];
int folderNew, fileNew, fileOld;
DIR *folderOld;
struct dirent * file;
// ensure the proper number of command line arguments are passed in
if (argc !=3) {
fprintf (stderr,"need 3 args\n");
return 1;
}
// create the new folder
folderNew = mkdir(argv[2],00644);
if (folderNew == -1) {
printf ("can not create: %s\n", argv[2]);
/* fprintf (stderr,"errstring is: %s\n",strerror(errno));; */
return 0;
}
// open the old folder
folderOld = opendir (argv[1]);
if (folderOld == NULL) {
fprintf (stderr,"can not open: %s\n", argv[1]);
/* fprintf (stderr,"errstring is: %s\n",strerror(errno)); */
return 0;
}
// copy the contents
/*
while ( (count = read(folderOld, buffer, sizeof(buffer))) > 0)
{
printf ("in loop\n");
write (folderNew, buffer, count);
}
*/
while (file = readdir(folderOld))
{
// printf ("inode: %ld file name: %s file type: %d offset: %ld length: %d\n",file->d_ino, file->d_name, file->d_type, file->d_off, file->d_reclen);
// only copy the proper file types
if(file->d_type != 4)
{
// navigate to the old folder
chdir(argv[1]);
// open the old file
fileOld = open ("dummy.txt",O_RDONLY);
if (fileOld == -1)
{
fprintf (stderr,"can not open: %s\n", file->d_name);
/* fprintf (stderr,"errstring is: %s\n",strerror(errno)); */
return 0;
}
// navigate to the new folder
chdir("..");
chdir(argv[2]);
// create the new file
fileNew = creat (file->d_name,00644);
if (fileNew == -1)
{
printf ("can not create: %s\n", file->d_name);
/* fprintf (stderr,"errstring is: %s\n",strerror(errno));; */
return 0;
}
while ( (count = read(fileOld, buffer, sizeof(buffer))) > 0)
{
write (fileNew, buffer, count);
}
// navigate back to the parent folder
chdir("..");
}
}
return 0;
}
My second program is supposed to create two child processes. Then the parent sends a message to each child, each child sends a message to the parent, then everything shuts down gracefully. When I'm in the child process/file, it says it there is no such file or directory when I tell it to open the pipe from the parent to receive the message. Here's what I have:
<i>parent.c</i>
/* a simple first example, showing output */
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
pid_t child1pid;
pid_t child2pid;
char message1[]="Hi Child1, this is your parent!!!";
char message2[]="Hi Child2, this is your parent!!!";
int fd1, fd2;
char buff[128];
int num;
/* create new named pipes in current directory with
permissions 777 (rwx for everyone) */
mknod("toChild1", 010777,0);
mknod("toChild2", 010777,0);
/* parent process here */
printf("Howdy from parent process %d\n", getpid());
/* split child 1 here */
if ((child1pid=fork()) == 0) { /* child */
execlp("./child", "./child", "1", "toChild1", NULL);
perror("oops, child had an error!");
/* write to child 1 on that named pipe */
fd1 = open("toChild1", O_WRONLY);
num = write(fd1, message1, sizeof(message1));
return 1;
}
else
{
/* split child 2 here */
if ((child2pid=fork()) == 0) { /* child */
execlp("./child", "./child", "2", "toChild2", NULL);
perror("oops, child had an error!");
/* write to child 2 on that named pipe */
fd2 = open("toChild2", O_WRONLY);
num = write(fd2, message2, sizeof(message2));
return 1;
}
// parent display
else { /* parent */
printf("parent: child1's process id is %d\n",child1pid);
printf("parent: child2's process id is %d\n",child2pid);
printf("parent is going to wait for children to end...\n");
wait();
}
}
/* remove the named pipes from the directory */
close(fd1);
close(fd2);
printf("Bye from the parent!!\n");
}
<i>child.c</i>
#include <stdio.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
if (argc < 2){
printf ("Must supply argument!\n");
return -1;
}
char message[]="Hi Parent!!!";
int fd;
char buff[128];
int num;
printf("Howdy, I'm child %s.\n", argv[1]);
printf("Child %s stream to read is %s.\n", argv[1], argv[2]);
/* read data from the pipe that the parent created */
if ((fd = open(argv[2], O_RDONLY)) == -1) {
perror ("on open of child stream");
// unlink(childName);
return 1;
}
num = read(fd, buff, 128);
printf ("\nChild %s received: %s\n\n", argv[1], buff);
close(fd);
// sleep(atoi(argv[1]));
printf("Bye from child %s!!!\n", argv[1]);
}