Repository navigation
Expand file tree
/
Copy pathshare_task.c
More file actions
110 lines (87 loc) · 2.85 KB
/
Copy pathshare_task.c
File metadata and controls
110 lines (87 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#define CUR_PATH "./"
char* extract_filename(const char* string) {
const char* start = strrchr(string, '/') + 1; // 파일 이름 시작 위치
const char* end = strrchr(string, '.'); // 파일 확장자 위치
// 파일 이름의 길이 계산
size_t length = (end != NULL) ? (end - start) : strlen(start);
// 파일 이름을 새로운 문자열로 복사
char* result = (char*)malloc(length + 1);
strncpy(result, start, length);
result[length] = '\0';
return result;
}
void remove_last_newline(char* string) {
size_t length = strlen(string);
if (length > 0 && string[length - 1] == '\n') {
string[length - 1] = '\0';
}
}
int create_link(char *username, char *taskpath) {
char new_line_content[256];
char *usr_dir = username;
char newlink[256];
char old_path[100];
strcpy(old_path, taskpath);
printf("%s", old_path);
char* extracted_filename = extract_filename(taskpath);
printf("%s\n", extracted_filename);
fflush(stdout);
struct stat st;
if (stat(username, &st) == -1) {
// 디렉토리가 존재하지 않는 경우에만 생성
if (mkdir(username, 0777) == -1) {
perror("mkdir error");
exit(EXIT_FAILURE);
}
}
strcpy(newlink, CUR_PATH);
strcat(newlink, username);
strcat(newlink, "/");
strcat(newlink, extracted_filename);
printf("%s\n", newlink);
// /* add user to the group */
FILE *file = fopen(old_path, "r+");
char line[1000];
int current_line = 1;
strcpy(new_line_content, " ");
strcat(new_line_content, username);
int target_line_number = 3;
// 임시 파일을 생성하여 수정된 내용을 기록
FILE* temp_file = fopen("temp.txt", "w");
if (temp_file == NULL) {
// 임시 파일 생성 실패 처리
perror("Failed to create temporary file");
return -1;
}
while (fgets(line, sizeof(line), file) != NULL) {
if (current_line == target_line_number) {
remove_last_newline(line);
fprintf(temp_file, "%s%s\n", line, new_line_content); // 수정된 줄 내용 쓰기
} else {
fprintf(temp_file, "%s", line); // 그대로 임시 파일에 기록
}
current_line++;
}
fclose(file);
fclose(temp_file);
// 기존 파일을 삭제하고 임시 파일을 이름을 변경하여 원본 파일로 대체
remove(old_path);
rename("temp.txt", old_path);
// /* create hard link */
if (link(old_path, newlink) != 0) {
perror("Error creating hard link");
}
return 0;
}
int main() {
char *usr_name = "js";
char *task_path = "./Today/Friend/test.txt";
create_link(usr_name, task_path);
return 0;
}