Please support our C advertiser: Programming Forums
Views: 2128 | Replies: 30
![]() |
•
•
Join Date: Jan 2007
Posts: 58
Reputation:
Rep Power: 2
Solved Threads: 0
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?
I understand it now.
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?
and call execv like this:
execv(c1, argv);
can someone tell me how I can call gzip from C?
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).
•
•
•
•
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.
Also I think I need to pass an array of string to execv like so?
c Syntax (Toggle Plain Text)
#define SIZE 256 char f1[SIZE],f2[SIZE]; i =0; sprintf(f1, "../data/data%d_.txt", i); sprintf(f2, "../data/data%d.gz", i); char *argv[6], *c1, *c2,*c3,*c5; strcpy(c1, "/bin/gzip"); strcpy(c2, "/bin/gzip"); strcpy(c3, "-c"); strcpy(c5, ">"); argv[0]=c2; argv[1]=c3; argv[2]=f1; argv[3]=c5; argv[4]=f2; argv[5]=(char*)0;
execv(c1, argv);
can someone tell me how I can call gzip from C?
I think something like this (untested) would work.
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.
#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;
} If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
•
•
Join Date: Jan 2007
Posts: 58
Reputation:
Rep Power: 2
Solved Threads: 0
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.
> 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?
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.
•
•
Join Date: Jan 2007
Posts: 58
Reputation:
Rep Power: 2
Solved Threads: 0
> 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
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
•
•
Join Date: Jan 2007
Posts: 58
Reputation:
Rep Power: 2
Solved Threads: 0
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
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.
•
•
•
•
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.
•
•
Join Date: Jan 2007
Posts: 58
Reputation:
Rep Power: 2
Solved Threads: 0
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)?
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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode