From ce8b6cc0093b21ccd1c01f6536b8181372e03423 Mon Sep 17 00:00:00 2001 From: qkrwjdgus333 Date: Wed, 21 May 2025 00:14:08 +0900 Subject: [PATCH] =?UTF-8?q?rm=EB=AA=85=EB=A0=B9=EC=96=B4=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81:=20=EC=A0=84=EC=97=AD=ED=8A=B8?= =?UTF-8?q?=EB=A6=AC=20=EC=9D=B8=EC=9E=90=EB=A1=9C=20=EC=A0=84=EB=8B=AC=20?= =?UTF-8?q?ls=EB=8F=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/command.h | 4 +- include/ls.h | 2 +- src/ls.c | 57 ++++++++++++++++++++-------- src/rm.c | 95 +++++++++++++++++++++++++++++++---------------- 4 files changed, 109 insertions(+), 49 deletions(-) diff --git a/include/command.h b/include/command.h index 5f60973..7f071da 100644 --- a/include/command.h +++ b/include/command.h @@ -3,10 +3,10 @@ #include "include/osproject.h" -void command_ls(int argc, char** argv); +void command_ls(DTree* tree, int argc, char** argv); void command_mkdir(int argc, char** argv); void command_cat(DTree* tree, const char* filename); void command_alias(AliasTable* table, int argc, char** argv); -void command_rm(int argc, char** argv); +void command_rm(DTree* tree, int argc, char** argv); #endif diff --git a/include/ls.h b/include/ls.h index 5ac0b51..af6d12a 100644 --- a/include/ls.h +++ b/include/ls.h @@ -3,6 +3,6 @@ #include "osproject.h" -void command_ls(int argc, char** argv); +void command_ls(DTree* tree, int argc, char** argv); #endif diff --git a/src/ls.c b/src/ls.c index a680da8..b6502d0 100644 --- a/src/ls.c +++ b/src/ls.c @@ -2,8 +2,6 @@ #include #include "../include/ls.h" -extern DTree* tree;//ÀüüƮ¸® - void print_permissions(int permission[9]) { const char* rwx = "rwx"; for (int i = 0; i < 9; i++) { @@ -11,9 +9,24 @@ void print_permissions(int permission[9]) { } } -void command_ls(int argc, char** argv) {//Â÷º°Á¡ ÇÔ¼ö¸¦ ls,ls-a,ls-al·Î µû·Î ±¸ÇöÇÏÁö ¾Ê°í - int show_all = 0, long_format = 0;//Çѹø¿¡ ±¸Çö + ¼ø¼­°¡ ls-la¿©µµ °¡´É +void print_long_format(TNode* node) { + printf("%c", node->type); + print_permissions(node->permission); + printf(" %d %d %5d %02d-%02d %02d:%02d %s\n", + node->UID, node->GID, node->SIZE, + node->month, node->day, node->hour, node->minute, + node->name); +} + +void print_short_format(TNode* node) { + if (strlen(node->name) < 8) + printf("%s\t\t", node->name); + else + printf("%s\t", node->name); +} +void command_ls(DTree* tree, int argc, char** argv) { + int show_all = 0, long_format = 0; for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-a") == 0) show_all = 1; else if (strcmp(argv[i], "-l") == 0) long_format = 1; @@ -21,27 +34,41 @@ void print_permissions(int permission[9]) { show_all = long_format = 1; } + int count = 0; + + if (show_all) { + // "." Ãâ·Â + TNode* current = tree->current; + if (long_format) print_long_format(current); + else print_short_format(current); + count++; + + // ".." Ãâ·Â + TNode* parent = current->Parent; + if (parent) { + if (long_format) print_long_format(parent); + else print_short_format(parent); + count++; + } + + if (!long_format && count % 5 == 0) printf("\n"); + } + TNode* node = tree->current->left; - while (node != NULL) { + while (node) { if (!show_all && node->name[0] == '.') { node = node->right; continue; } - if (long_format) { - printf("%c", node->type); - print_permissions(node->permission); - printf(" %d %d %5d %02d-%02d %02d:%02d %s\n", - node->UID, node->GID, node->SIZE, - node->month, node->day, node->hour, node->minute, - node->name); - } + if (long_format) print_long_format(node); else { - printf("%s ", node->name); + print_short_format(node); + if (++count % 5 == 0) printf("\n"); } node = node->right; } - if (!long_format) printf("\n"); + if (!long_format && count % 5 != 0) printf("\n"); } diff --git a/src/rm.c b/src/rm.c index 0d90135..7c90df5 100644 --- a/src/rm.c +++ b/src/rm.c @@ -4,46 +4,79 @@ #include "../include/osproject.h" #include "../include/command.h" -extern DTree* tree; +void delete_node(DTree* dTree, const char* path, int recursive, int force, int verbose) { + TNode* currentN = dTree->current; + TNode* target = NULL; + TNode* tmpDir = NULL; + char tmp[MAXDIRECTORY]; + char tmp2[MAXDIRECTORY]; + char tmp3[MAXDIRECTORY]; + char* str; + int val; -void delete_node(TNode* parent, const char* name, int recursive, int force, int verbose) { - TNode* prev = NULL; - TNode* curr = parent->left; + strncpy(tmp, path, MAXDIRECTORY); - while (curr) { - if (strcmp(curr->name, name) == 0) { - if (curr->type == 'd' && !recursive) { - if (!force) - printf("rm: µð·ºÅ丮 '%s'¸¦ Áö¿ì·Á¸é -r ¿É¼ÇÀÌ ÇÊ¿äÇÕ´Ï´Ù.\n", name); - return; - } + // °æ·Î°¡ Æ÷ÇÔµÈ °æ¿ì + if (strchr(path, '/')) { + strncpy(tmp2, getDir(path), MAXDIRECTORY); + val = MovePath(dTree, tmp2); + if (val != 0) { + if (!force) + printf("rm: '%s': ±×·± ÆÄÀÏÀ̳ª µð·ºÅ͸®°¡ ¾ø½À´Ï´Ù.\n", tmp2); + return; + } - if (curr->type == 'd' && recursive) { - while (curr->left) - delete_node(curr, curr->left->name, recursive, force, verbose); - } + str = strtok(tmp, "/"); + while (str != NULL) { + strncpy(tmp3, str, MAXNAME); + str = strtok(NULL, "/"); + } + } + else { + strncpy(tmp3, path, MAXNAME); + } - if (verbose) - printf("»èÁ¦µÊ: %s\n", curr->name); + // µð·ºÅ͸®ÀÎÁö ÆÄÀÏÀÎÁö ÆÇº° + target = ExistDir(dTree, tmp3, 'd'); + if (target && !recursive) { + if (!force) + printf("rm: µð·ºÅ丮 '%s'¸¦ Áö¿ì·Á¸é -r ¿É¼ÇÀÌ ÇÊ¿äÇÕ´Ï´Ù.\n", tmp3); + dTree->current = currentN; + return; + } + if (!target) { + target = ExistDir(dTree, tmp3, 'f'); + } - if (prev) - prev->right = curr->right; - else - parent->left = curr->right; + if (!target) { + if (!force) + printf("rm: '%s': ±×·± ÆÄÀÏÀ̳ª µð·ºÅ͸®°¡ ¾ø½À´Ï´Ù.\n", tmp3); + dTree->current = currentN; + return; + } - free(curr); - return; - } + // ±ÇÇÑ È®ÀÎ + if (!force && (IsPermission(dTree->current, 'w') != 0 || IsPermission(target, 'w') != 0)) { + printf("rm: '%s'À»(¸¦) Áö¿ï ¼ö ¾ø½À´Ï´Ù: Permission denied\n", tmp3); + dTree->current = currentN; + return; + } - prev = curr; - curr = curr->right; + // Àç±Í »èÁ¦ ó¸® + if (target->type == 'd' && recursive) { + while (target->left) + delete_node(dTree, target->left->name, recursive, force, verbose); } - if (!force) - printf("rm: '%s': ±×·± ÆÄÀÏÀ̳ª µð·ºÅ͸®°¡ ¾ø½À´Ï´Ù.\n", name); + // ½ÇÁ¦ »èÁ¦ + if (verbose) + printf("»èÁ¦µÊ: %s\n", tmp3); + RemoveDir(dTree, tmp3); + + dTree->current = currentN; // ¿øÀ§Ä¡ º¹±Í } -void command_rm(int argc, char** argv) { +void command_rm(DTree* tree, int argc, char** argv) { int recursive = 0, force = 0, verbose = 0; int i = 1; @@ -53,13 +86,13 @@ void command_rm(int argc, char** argv) { else if (argv[i][j] == 'f') force = 1; else if (argv[i][j] == 'v') verbose = 1; else { - printf("¾Ë ¼ö ¾ø´Â ¿É¼Ç: -%c\n", argv[i][j]); + printf("rm: ¾Ë ¼ö ¾ø´Â ¿É¼Ç -- '%c'\n", argv[i][j]); return; } } } for (; i < argc; i++) { - delete_node(tree->current, argv[i], recursive, force, verbose); + delete_node(tree, argv[i], recursive, force, verbose); } }