自用

来源:互联网 发布:拼多多上款软件 编辑:程序博客网 时间:2024/05/17 07:25
#include <stdio.h>#include <string.h>#include <windows.h>#include <stdlib.h>#include <sys/types.h>#include <errno.h>#include <sys/stat.h>#include <io.h>#pragma warning(disable: 4996)void promptInput();void checkInput(char*);void showError();char* formatStr(char*);int fileExist(char*);int main(int argc, char* argv[]) {char cmd[1024];while (TRUE){memset(cmd, '\0', sizeof(cmd));promptInput();gets(cmd);char* tmp = formatStr(cmd);checkInput(tmp);}return 0;}void promptInput() {char dir[MAX_PATH];DWORD res = GetCurrentDirectory(MAX_PATH, dir);if (res == 0){printf("something must be wrong\n");}else {printf("%s> ", dir);}return;}void checkInput(char* str) {if (*str == '\0'){return;}char* cmd[3] = {"echo", "date", "type"};char* res[2] = { NULL, NULL };for (int i = 0; i < 3; i++){res[i] = strstr(str, cmd[i]);}if (res[0] == str) {str += 5;char sign[2] = ">";if (strstr(str, sign)){char files[2][200];const char* sep = " ";char* p = strtok(str, sep);strcpy(files[0], p);p = strtok(NULL, sep);p = strtok(NULL, sep);strcpy(files[1], p);if (fileExist(files[1]) == 0){FILE* fp = fopen(files[1], "wb+");fwrite(files[0], sizeof(char), strlen(files[0]), fp);fclose(fp);printf("write successfully\n");}else {printf("File don't exist\n");}}else{printf("%s\n", str);}}else if (res[1] == str) {if (str[4] == '>') {char* file = str + 6;if (fileExist(file) == 0){printf("Error! File exists\n");}else{FILE* fp = fopen(file, "wb+");fclose(fp);}}else {str = str + 4;char sign[2] = ">";if (strstr(str, sign) == NULL){char* file = str;if (fileExist(file) == 0){FILE* fp = fopen(file, "r");char con[4096];printf("%s content >>>\n", file);memset(con, '\0', sizeof(con));while (fgets(con, 4096, fp)){printf("%s", con);memset(con, '\0', sizeof(con));}printf("\n");fclose(fp);}else{printf("File don't exist;\n");}}else {char* p;const char* sep = " ";char files[10][20];int index = 0;p = strtok(str, sep);strcpy(files[index++], p);while (p = strtok(NULL, sep)) {if (*p == '>'){continue;}strcpy(files[index++], p);}if (fileExist(files[index - 1]) == 0){printf("File already exists\n", files[index - 1]);}else {int len = index - 1;FILE* fp = fopen(files[index - 1], "a+");for (int i = 0; i < len; i++){if (fileExist(files[i]) != 0){printf("File don't exist\n");return;}else {FILE* subFp = fopen(files[i], "r");char con[4096];memset(con, '\0', sizeof(con));while (fgets(con, 4096, subFp)){fwrite(con, sizeof(char), strlen(con), fp);memset(con, '\0', sizeof(con));}fclose(subFp);}}fclose(fp);}}}}}char* formatStr(char* str) {while (*str != '\0' && *str == ' '){str++;}return str;}int fileExist(char* file) {char dir[MAX_PATH];GetCurrentDirectory(MAX_PATH, dir);strncat(dir, "\\", 3);strncat(dir, file, strlen(file));return access(dir, 0);}

原创粉丝点击