#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct Director
{
int parent;
int child;
int brother;
int dir; /* 1:dir 0:file */
char name[100];
};
int root = 0;
int work_dir = 0;
struct Director dir[10000];
int index = 1;
int get_dir(char *p)
{
int start;
if (*p == '/')
start = root;
else
start = work_dir;
return 0;
}
int get_path(int w, char *path)
{
int i, point, len, list_point;
char *p = path;
len = strlen(path);
point = w;
while ((p - path) < len && point != -1)
{
if (*p == '/')
{
++p;
point = root;
continue;
}
list_point = dir[point].child;
while (list_point != -1)
{
i = 0;
while (dir[list_point].name[i] && p[i] && p[i] != '/' && (dir[list_point].name[i] == p[i]))
++i;
if (!dir[list_point].name[i] && (!p[i] || p[i] == '/'))
{
p = p + i + 1;
break;
}
list_point = dir[list_point].brother;
}
if (list_point == -1)
return -1;
if (p[-2] == '.' && p[-3] == '.')
{
point = dir[point].parent;
continue;
}
if (p[-2] == '.')
{
point = point;
continue;
}
point = list_point;
}
return point;
}
int del_path(int index)
{
int point;
point = dir[index].parent;
point = dir[point].child;
while ((dir[point].brother != -1) && (dir[point].brother != index))
point = dir[point].brother;
if(dir[point].brother != -1)
dir[point].brother = dir[dir[point].brother].brother;
return 0;
}
int insert_file_dir(int w, char *name, int d)
{
int tmp, point, pre_point;
tmp = get_path(w, name);
if (tmp != -1)
del_path(tmp);
dir[index].parent = w;
dir[index].dir = d;
strcpy(dir[index].name, name);
dir[index].brother = -1;
dir[index].child = -1;
index++;
point = dir[w].child;
if (dir[w].child == -1)
{
dir[w].child = index - 1;
return index - 1;
}
pre_point = w;
while (1)
{
if ((point == -1) || (strcmp(dir[point].name, dir[index - 1].name) > 0))
{
dir[index - 1].brother = dir[pre_point].brother;
dir[pre_point].brother = index - 1;
break;
}
pre_point = point;
point = dir[point].brother;
}
return index - 1;
}
int copy_dir(int source, int dmt)
{
int tmp, point;
tmp = insert_file_dir(dmt, dir[source].name, dir[source].dir);
if (dir[source].dir == 0)
return 0;
point = dir[source].child;
insert_file_dir(tmp, ".", 1);
insert_file_dir(tmp, "..", 1);
while (point != -1)
{
if (strcmp(dir[point].name, ".") == 0);
else if (strcmp(dir[point].name, "..") == 0);
else
{
if (dir[point].dir == 1)
copy_dir(point, tmp);
else
insert_file_dir(tmp, dir[point].name, dir[point].dir);
}
point = dir[point].brother;
}
return 0;
}
int main()
{
int point, tmp, tmp1;
char str[1000];
char cmd[10], file[100], file1[100];
dir[0].dir = 1;
dir[0].child = -1;
dir[0].brother = -1;
dir[index].brother = -1;
dir[index].child = -1;
strcpy(dir[index].name, ".");
dir[index].dir = 1;
dir[index].parent = 0;
dir[0].child = index;
++index;
while (gets(str))
{
sscanf(str, "%s", cmd);
if (strcmp(cmd, "ls") == 0)
{
point = dir[work_dir].child;
while (point != -1)
{
printf("%s", dir[point].name);
if (dir[point].dir == 1)
printf(" d");
printf("\n");
point = dir[point].brother;
}
printf("\n");
}
else if (strcmp(cmd, "mv") == 0)
{
if (sscanf(str, "%s %s %s", cmd, file, file1) != 3)
{
printf("\n");
continue;
}
tmp = get_path(work_dir, file);
tmp1 = get_path(work_dir, file1);
copy_dir(tmp, tmp1);
tmp1 = work_dir;
while (tmp1 != 0)
{
if (tmp1 == tmp)
{
work_dir = root;
break;
}
tmp1 = dir[tmp1].parent;
}
del_path(tmp);
}
else if (strcmp(cmd, "cp") == 0)
{
if (sscanf(str, "%s %s %s", cmd, file, file1) != 3)
{
printf("\n");
continue;
}
tmp = get_path(work_dir, file);
tmp1 = get_path(work_dir, file1);
copy_dir(tmp, tmp1);
}
else if (strcmp(cmd, "mkdir") == 0)
{
if (sscanf(str, "%s %s", cmd, file) != 2)
{
printf("\n");
continue;
}
tmp = get_path(work_dir, file);
if (tmp != -1)
continue;
tmp = insert_file_dir(work_dir, file, 1);
insert_file_dir(tmp, ".", 1);
insert_file_dir(tmp, "..", 1);
}
else if (strcmp(cmd, "create") == 0)
{
if (sscanf(str, "%s %s", cmd, file) != 2)
{
printf("\n");
continue;
}
tmp = get_path(work_dir, file);
if (tmp != -1)
continue;
insert_file_dir(work_dir, file, 0);
}
else if (strcmp(cmd, "del") == 0)
{
if (sscanf(str, "%s %s", cmd, file) != 2)
{
printf("\n");
continue;
}
tmp = get_path(work_dir, file);
del_path(tmp);
tmp1 = work_dir;
while (tmp1 != 0)
{
if (tmp1 == tmp)
{
work_dir = root;
break;
}
tmp1 = dir[tmp1].parent;
}
}
else if (strcmp(cmd, "cd") == 0)
{
if (sscanf(str, "%s %s", cmd, file) != 2)
{
printf("\n");
continue;
}
work_dir = get_path(work_dir, file);
}
}
return 0;
}
|