diff --git a/include/command.h b/include/command.h index 58fc5d9..6cf2c91 100644 --- a/include/command.h +++ b/include/command.h @@ -1,24 +1,26 @@ #ifndef COMMAND_H #define COMMAND_H -#include "include/osproject.h" +#include "include/osproject.h" -void command_ls(int argc, char** argv); -void command_mkdir(int argc, char** argv); +void command_ls(DTree* tree, int argc, char** argv); +void command_mkdir(DTree* tree, 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); -int command_cd (DTree *tree, UList *users, int argc, char **argv); -int command_pwd (DTree *tree, Stack *stack, UList *users); -int command_cp (DTree *tree, int argc, char **argv); +void command_rm(DTree* tree, int argc, char** argv); + +int command_cd(DTree* tree, UList* users, int argc, char** argv); +int command_pwd(DTree* tree, Stack* stack, UList* users); +int command_cp(DTree* tree, int argc, char** argv); + +int cd(DTree* tree, char* command); +int MovePath(DTree* tree, char* path); +int Movecurrent(DTree* tree, char* segment); -int cd (DTree *tree, char *command); -int MovePath (DTree *tree, char *path); -int Movecurrent(DTree *tree, char *segment); +int pwd(DTree* tree, Stack* stack, char* opt); +int PrintPath(DTree* tree, Stack* stack); +int cp(DTree* tree, char* src, char* dst); -int pwd (DTree *tree, Stack *stack, char *opt); -int PrintPath (DTree *tree, Stack *stack); -int cp (DTree *tree, char *src, char *dst); +void UpdateUserDir(UList* users, DTree* tree); -void UpdateUserDir(UList *users, DTree *tree); #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 command_ls(int argc, char** argv) {// 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); } }