Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* exercise on UNIX file system */
- /* useful characters: [] # */
- # include <stdio.h>
- # include <string.h>
- # include <fcntl.h> // for bitflags
- # include <stdlib.h> // for exit()
- # define errormsg(x) { puts(x); exit(1); }
- int main(int argc, char * argv[])
- {
- // checking proper input format
- if(argc != 2) errormsg("Error: Invalid input length");
- // opening (and then closing) file
- int file = open(*(argv + 1), O_CREAT|O_TRUNC|O_WRONLY);
- if(file == -1) errormsg("Error: Incorrect file name");
- close(1); // closes stdout, why?
- dup(file); // why duplicate?
- // fork
- int pid, status;
- pid = fork();
- if(pid != 0)
- wait(&status); // waits for the child process
- execve("./writer", NULL, NULL); // calls the writer function
- errormsg("Error: execve() call failed");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement