RSS Forums RSS
Please support our C advertiser: Programming Forums
Views: 2128 | Replies: 30
Reply
Join Date: Jan 2007
Posts: 58
Reputation: jobs is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
jobs jobs is offline Offline
Junior Poster in Training

Question Re: Calling two different C program via OS command line

  #21  
Dec 26th, 2007
Originally Posted by Salem View Post
execl doesn't understand ">" as meaning redirection.
How I can get the gzipping: execl("/bin/gzip","/bin/gzip","-c","../data/data_.txt",">","./data/data.gz",0);
to work from a C-program?
Also, it appears AFTER the execl() call, so it can only ever execute if the execl FAILS (I've said this before remember).
I understand it now.
Oh, and one more thing, your indentation sucks. Configure your IDE to only use 4 spaces for indentation (any decent code editor will allow you to do this somewhere). Your IDE may be able to do the right thing with a mix of space and tab characters, but forums as a rule make a mess of it.

Use the "preview" feature of the forum post to make sure your code looks exactly like what you're seeing in your IDE, indentation wise at least.
I am just using default Eclipse CDT setup. I was just not tabbing properly.

Also I think I need to pass an array of string to execv like so?
  1. #define SIZE 256
  2. char f1[SIZE],f2[SIZE];
  3.  
  4. i =0;
  5. sprintf(f1, "../data/data%d_.txt", i);
  6. sprintf(f2, "../data/data%d.gz", i);
  7.  
  8. char *argv[6], *c1, *c2,*c3,*c5;
  9. strcpy(c1, "/bin/gzip");
  10. strcpy(c2, "/bin/gzip");
  11. strcpy(c3, "-c");
  12. strcpy(c5, ">");
  13.  
  14. argv[0]=c2;
  15. argv[1]=c3;
  16. argv[2]=f1;
  17. argv[3]=c5;
  18. argv[4]=f2;
  19. argv[5]=(char*)0;
  20.  
and call execv like this:
execv(c1, argv);

can someone tell me how I can call gzip from C?
Reply With Quote  
Join Date: Dec 2005
Posts: 3,923
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 452
Colleague
Salem's Avatar
Salem Salem is offline Offline
Senior Poster

Re: Calling two different C program via OS command line

  #22  
Dec 26th, 2007
I think something like this (untested) would work.
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main ( ) {
    pid_t   pid;
    pid = fork();
    if ( pid == 0 ) {
        char *argv[4];
        int fd = open("bar.zip",O_CREAT | O_TRUNC | O_WRONLY);
        dup2(fd,STDOUT_FILENO);
        argv[0] = "/bin/gzip";
        argv[1] = "-c";
        argv[2] = "test.txt";
        argv[3] = NULL;
        execv( argv[0], argv );
    } else if ( pid != (pid_t)-1 ) {
        int status;
        pid_t   waitpid = wait(&status);
    } else {
        fprintf(stderr,"Fork failed\n" );
    }
    return 0;
}
In order to achieve the redirection, the desired output file is opened for writing, then dup2 is called to replace the existing stdout with the file descriptor of the opened file.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Join Date: Jan 2007
Posts: 58
Reputation: jobs is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
jobs jobs is offline Offline
Junior Poster in Training

Question Re: Calling two different C program via OS command line

  #23  
Dec 27th, 2007
I ran it. But I have problem with the output file. I can't gunzip it. I changed bar.zip on the second run to bar.gz. What is cap T in permission for the file?
--wx-wx--T 1 jobs jobs  432940 Dec 27 13:37 bar.gz
-r----x--T 1 jobs jobs  432940 Dec 27 13:34 bar.zip
[jobs@localhost proj1]$ gunzip bar.gz 
gunzip: bar.gz: Permission denied
Last edited by jobs : Dec 27th, 2007 at 2:50 pm.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,923
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 452
Colleague
Salem's Avatar
Salem Salem is offline Offline
Senior Poster

Re: Calling two different C program via OS command line

  #24  
Dec 27th, 2007
> gunzip bar.gz
Your file seems to lack read permissions.
Since it needs to read the file, and it doesn't have read permissions, it's going to fail.

There are some very strange permissions on those files. Have you been messing with the umask() function?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Join Date: Jan 2007
Posts: 58
Reputation: jobs is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
jobs jobs is offline Offline
Junior Poster in Training

Re: Calling two different C program via OS command line

  #25  
Dec 27th, 2007
> There are some very strange permissions on those files. Have you been messing with the umask() function?
I didn't play with umask()
I just cut and pasted it and complied with gdb option and ran it inside gdb. Do I have to do setuid inside the c program when it's forking a process that creates a file?

If I compile it without gdb and ran it normal, I get the file permission as:
--wx-wx--T bar.gz
Reply With Quote  
Join Date: Dec 2005
Posts: 3,923
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 452
Colleague
Salem's Avatar
Salem Salem is offline Offline
Senior Poster

Re: Calling two different C program via OS command line

  #26  
Dec 27th, 2007
It's going to be a good few days before I can get back to a Linux box to try this out
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Join Date: Jan 2007
Posts: 58
Reputation: jobs is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
jobs jobs is offline Offline
Junior Poster in Training

Re: Calling two different C program via OS command line

  #27  
Dec 27th, 2007
On the linux terminal, I changed the permission to 770 and gunzipped. Bar contents looks good. And the permisson T disappears after chmod 770. I will quote this from this site: capital T sticky Bit
Using the sicky bit, the administrator could ensure that once a given student created content in their respective folder, no other student could come in and delete it.

Here's how it looks:

drwxr-xr-t 1 alice alice 4.4K 2007-01-01 09:21 Alice

Remember that if "everyone" (other) doesn't have execute permissions on the directory it'll be a capital T instead of lowercase.

Everything looks good to me. I can just do the gzipping in a process and have the main process wait for it to complete. Then fork another process to change zip file permission.

This technique should work. I will give it a try. Salem, thank you very much for the help.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,923
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 452
Colleague
Salem's Avatar
Salem Salem is offline Offline
Senior Poster

Re: Calling two different C program via OS command line

  #28  
Dec 27th, 2007
> Then fork another process to change zip file permission.
Or just use the chmod() API call instead of running the chmod command.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Join Date: Jan 2007
Posts: 58
Reputation: jobs is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
jobs jobs is offline Offline
Junior Poster in Training

Re: Calling two different C program via OS command line

  #29  
Dec 27th, 2007
Salem, in the above code you do: (pid_t)-1, what does this do? Type cast the -1 to pid_t?
Can I run fork in a function other than main? Many example on google show it main.
Can I do parent (programA) create a process, then that child (programB) process creates another child process, Grand Parent(programA spawn a process to run programB)->Parent(programB process)->Child(programB spawn a process)?
Last edited by jobs : Dec 27th, 2007 at 8:42 pm.
Reply With Quote  
Join Date: Jan 2007
Posts: 58
Reputation: jobs is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
jobs jobs is offline Offline
Junior Poster in Training

Re: Calling two different C program via OS command line

  #30  
Dec 27th, 2007
I got it to work. Whoo hoo! Salem, you're C SuperMan. Thanks.
Last edited by jobs : Dec 27th, 2007 at 9:26 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:55 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC