GMS Linux: start?
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#define MAX_LINES 1000
|
||||
#define MAX_LEN 128
|
||||
|
||||
char buffer[MAX_LINES][MAX_LEN];
|
||||
int line_count = 0;
|
||||
char filename[64];
|
||||
|
||||
// 화면 지우기 (ANSI Code)
|
||||
void clear_screen() {
|
||||
printf("\033[2J\033[H");
|
||||
}
|
||||
|
||||
// 현재 버퍼 내용 출력
|
||||
void print_buffer() {
|
||||
clear_screen();
|
||||
printf("\033[1;33m=== %s ===\033[0m\n", filename);
|
||||
for (int i = 0; i < line_count; i++) {
|
||||
printf("\033[1;34m%3d |\033[0m %s\n", i + 1, buffer[i]);
|
||||
}
|
||||
printf("---------------------------------------------------\n");
|
||||
printf("[Type text to append] [Cmds: :w (save), :q (quit), :d <num> (del)]\n");
|
||||
}
|
||||
|
||||
// 파일 불러오기
|
||||
void load_file() {
|
||||
int fd = open(filename, O_RDONLY);
|
||||
if (fd < 0) return; // 파일 없으면 새로 생성
|
||||
|
||||
char ch;
|
||||
int buf_idx = 0;
|
||||
while (read(fd, &ch, 1) > 0) {
|
||||
if (ch == '\n') {
|
||||
buffer[line_count][buf_idx] = '\0';
|
||||
line_count++;
|
||||
buf_idx = 0;
|
||||
if (line_count >= MAX_LINES) break;
|
||||
} else {
|
||||
if (buf_idx < MAX_LEN - 1) {
|
||||
buffer[line_count][buf_idx++] = ch;
|
||||
}
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
// 파일 저장하기
|
||||
void save_file() {
|
||||
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (fd < 0) {
|
||||
perror("save failed");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < line_count; i++) {
|
||||
write(fd, buffer[i], strlen(buffer[i]));
|
||||
write(fd, "\n", 1);
|
||||
}
|
||||
close(fd);
|
||||
printf("Saved to %s\n", filename);
|
||||
sleep(1); // 저장 메시지 확인용
|
||||
}
|
||||
|
||||
// 라인 삭제
|
||||
void delete_line(int line_num) {
|
||||
if (line_num < 1 || line_num > line_count) {
|
||||
printf("Invalid line number\n");
|
||||
sleep(1);
|
||||
return;
|
||||
}
|
||||
|
||||
// 뒤의 라인들을 앞으로 당김
|
||||
for (int i = line_num - 1; i < line_count - 1; i++) {
|
||||
strcpy(buffer[i], buffer[i+1]);
|
||||
}
|
||||
line_count--;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
printf("Usage: medit <filename>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
strncpy(filename, argv[1], 63);
|
||||
load_file();
|
||||
|
||||
char input[MAX_LEN + 10]; // 명령어 포함 여유분
|
||||
|
||||
while (1) {
|
||||
print_buffer();
|
||||
printf("> ");
|
||||
|
||||
if (fgets(input, sizeof(input), stdin) == NULL) break;
|
||||
input[strcspn(input, "\n")] = 0; // 개행 제거
|
||||
|
||||
// 명령어 처리 (:)
|
||||
if (input[0] == ':') {
|
||||
if (strcmp(input, ":q") == 0) {
|
||||
break;
|
||||
} else if (strcmp(input, ":w") == 0) {
|
||||
save_file();
|
||||
} else if (strncmp(input, ":d", 2) == 0) {
|
||||
int ln = atoi(input + 3);
|
||||
delete_line(ln);
|
||||
} else {
|
||||
printf("Unknown command. :w, :q, :d <num>\n");
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
// 일반 텍스트 입력 (추가)
|
||||
else {
|
||||
if (line_count < MAX_LINES) {
|
||||
strncpy(buffer[line_count], input, MAX_LEN - 1);
|
||||
buffer[line_count][MAX_LEN - 1] = '\0';
|
||||
line_count++;
|
||||
} else {
|
||||
printf("Buffer full!\n");
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user