Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- int main(int argc, char *args[]) {
- if(argc != 3) {
- printf("Usage: %s <file> <output directory>\n", args[0]);
- exit(1);
- }
- struct stat st;
- char buffer[20];
- char *output_dir;
- output_dir = (char *) malloc((sizeof(args[2])+sizeof(args[1]))*sizeof(char));
- strcat(output_dir, args[2]);
- strcat(output_dir, "/");
- strcat(output_dir, args[1]);
- int file = open(args[1], O_RDONLY, S_IRUSR|S_IWUSR);
- if(file == -1) {
- printf("Could not open input file\n");
- }
- int digit_count = 0;
- int even_digit_count = 0;
- while(read(file, buffer, sizeof(buffer))) {
- int i;
- for(i=0; buffer[i]; i++) {
- if(isdigit(buffer[i])) {
- digit_count++;
- if((int)(buffer[i]-'0') % 2 == 0) {
- even_digit_count++;
- }
- }
- }
- }
- char report_string[128];
- if (stat(args[1], &st) == -1) {
- printf("Error while getting stat!\n");
- exit(1);
- }
- sprintf(report_string, "File owner GID: %d | Digit count: %d | Even digit count: %d\n", (int) st.st_gid, digit_count, even_digit_count);
- close(file);
- file = open(output_dir, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
- if(file == -1) {
- printf("Error while opening output file!\n");
- exit(1);
- }
- if(write(file, report_string, strlen(report_string)/sizeof(char)) == -1) {
- printf("Error while writing report!\n");
- exit(1);
- }
- char cur_dir[128];
- if(getcwd(cur_dir, sizeof(cur_dir)) == NULL) {
- printf("Error while getting CWD!\n");
- exit(1);
- }
- strcat(cur_dir, "/slink-");
- strcat(cur_dir, args[1]);
- unlink(cur_dir);
- if(symlink(output_dir, cur_dir) == -1) {
- printf("Error while creating symlink!\n");
- exit(1);
- }
- if (stat(cur_dir, &st) == -1) {
- printf("Error while getting stat!\n");
- exit(1);
- }
- printf("Size for simlink: %d\n", st.st_size);
- printf("Report generated\n");
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement