我正在try 用C语言编写一个类似于Ubuntu终端的应用程序,它支持命令Touch、ls和Clear(在某种程度上).(在run函数中)当我使用Touch命令创建文件时,命令被解析,名称被放入new_thing char[]中.它将文件名分配给new_thing.在每个命令之后,我使用Memset重置变量new_thing.然而,当我使用ls命令时,名称是空白的,我不知道为什么.
我try 更改重置变量的方式,但无济于事.在我看来,无论我将new_thing设置为什么,每个文件名都被设置为new_thing,即使我只设置了一次文件.代码如下.
手C
#include "file_explorer.h"
int main(void)
{
run();
return 0;
}
file_explorer.h
#ifndef FILE_EXPLORER_H_
#define FILE_EXPLORER_H_
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#define READ_PERMISSIONS 0x04
#define WRITE_PERMISSIONS 0x02
#define EXECUTE_PERMISSIONS 0x01
#define ADMIN_PERMISSIONS 0x07
#define LS_COMMAND "ls"
#define CD_COMMAND "cd"
#define PWD_COMMAND "pwd"
#define MKDIR_COMMAND "mkdir"
#define TOUCH_COMMAND "touch"
typedef struct File File;
typedef struct Folder Folder;
struct File {
char *file_name;
char *file_path;
uint8_t file_permissions;
char *contents;
};
struct Folder {
char *folder_name;
char *folder_path;
File files[5];
Folder *folders[2];
};
File new_file( char * , uint8_t );
char *add_contents(char * );
Folder new_folder( char * );
void change_directory(char * );
void print_file(char * );
void run();
#endif
文件资源管理器.c
#include "file_explorer.h"
char current_path[100] = "";
char input[100] = "";
char command[10] = "";
char new_thing[90] = "";
Folder home;
File new_file(char *file_name, uint8_t file_permissions)
{
File file = {
.file_name = file_name,
.file_path = current_path,
.file_permissions = file_permissions,
.contents = NULL
};
return file;
}
char * add_contents(char * contents)
{
return contents;
}
Folder new_folder(char *folder_name)
{
Folder folder = {
.folder_name = folder_name,
.folder_path = current_path,
.files = NULL,
.folders = NULL
};
return folder;
}
void change_directory(char *directory)
{
strcat(current_path, "/");
strcat(current_path, directory);
}
void print_file(char *contents)
{
if (contents == NULL)
{
printf("ERROR: File empty\n");
} else
{
printf("%s\n", contents);
}
}
void run()
{
system("clear");
home = new_folder("home");
int counter = 0;
while (true)
{
printf("parker@PARKERMASON%s$ ", current_path);
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = 0;
memset(command,0,strlen(command));
memset(new_thing,0,strlen(new_thing));
if (input[0] == 't' && input[1] == 'o' && input[2] == 'u' && input[3] == 'c' && input[4] == 'h' && input[5] == ' ')
{
int i = strlen(TOUCH_COMMAND) + 1;
strcpy(command, TOUCH_COMMAND);
for (int j = 0; j < strlen(input) - strlen(TOUCH_COMMAND) - 1; j++)
{
new_thing[j] = input[i];
i++;
}
if (counter == 0)
{
home.files[0] = new_file(new_thing, ADMIN_PERMISSIONS);
printf("%s\n", home.files[0].file_name);
} else if (counter == 1)
{
home.files[1] = new_file(new_thing, ADMIN_PERMISSIONS);
} else if (counter == 2)
{
home.files[2] = new_file(new_thing, ADMIN_PERMISSIONS);
} else if (counter == 3)
{
home.files[3] = new_file(new_thing, ADMIN_PERMISSIONS);
} else if (counter == 4)
{
home.files[4] = new_file(new_thing, ADMIN_PERMISSIONS);
} else
{
printf("Folder full\n");
}
counter++;
} else if (input[0] == 'l' && input[1] == 's' && input[2] == '\0')
{
for (int j = 0; j < sizeof(home.files) / sizeof(home.files[0]); j++)
{
if (home.files[j].file_name != NULL)
{
printf("%s ", home.files[j].file_name);
} else
{
printf("EMPTY ");
}
}
printf("\n");
} else if (input[0] == 'c' && input[1] == 'd' && input[2] == ' ')
{
int i = strlen(CD_COMMAND) + 1;
strcpy(command, CD_COMMAND);
for (int j = 0; j < strlen(input) - strlen(CD_COMMAND) - 1; j++)
{
new_thing[j] = input[i];
i++;
}
}
else if (input[0] == 'c' && input[1] == 'l' && input[2] == 'e' && input[3] == 'a' && input[4] == 'r' && input[5] == '\0')
{
system("clear");
}
}
}