⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usertest.c

📁 一个用于学习的操作系统
💻 C
字号:
#include <fairysky/types.h>
#include <fairysky/unistd.h>
#include <fairysky/fcntl.h>
#include <stdarg.h>

static inline _syscall0(int, setup)
static inline _syscall0(int, fork)
static inline _syscall3(int, write, int, fd, const char *, buf, off_t, count)
static inline _syscall3(int, read, int, fd, const char *, buf, off_t, count)
//static inline _syscall1(int, dup, int,fd)
static inline _syscall3(int, open, const char *, file, int, flag, int, mode)
static inline _syscall1(int, close, int, fd)

static char printbuf[1024];
static char scanbuf[1024];

char szbuf[1024] = {0};

typedef struct _tag_cmd {
    char *cmd;
    int (*funp)();
} CMD;



static int do_ps(void)
{
    long __res;
    __asm__ volatile ("int $0x80"
        : "=a" (__res)
        : "0" (8));
    if (__res >= 0) {
        return (int) __res;
    }
    errno = -__res;
    return -1;
}

static int printf(const char *fmt, ...)
{
    va_list args;
    int i;

    va_start(args, fmt);
    write(1, printbuf, i = vsprintf(printbuf, fmt, args));
    va_end(args);
    return i;
}

static int gets(char *buffer)
{
    va_list args;
    int i;

    //printf("%s", "gets start\n");

    i = read(0, buffer, 256);
    //printf("read count %d, %s\n", i, buffer);
    if (i > 0)
        buffer[i - 1] = '\0';

    return i;
}

int do_cmd(char *szcmd) ;

void init()
{    int index;
    //char szbuf[1024] = {0};
    //setup();
    (void) open("/dev/tty", O_RDWR, 0);
    (void) open("/dev/tty", O_RDWR, 0);
    (void) open("/dev/tty", O_RDWR, 0);

    if (fork() == 0) {
        while (1) {
            //if ((index % 100000) == 0) {
            //   printf("2-%d\n", index);
            //}
            ++index;
        }
    } else {
        while (1) {

            printf("%s", "fairysky$");
            gets(szbuf);
            do_cmd(szbuf);
        }
    }

}

int do_help()
{
    printf("uname----------print os info\n");
    printf("ps-------------show task info\n");
    printf("help-----------print os help\n");

    return 0;
}

int do_uname()
{
    printf("fairysky os version 0.01\n");

    return 0;
}

CMD cmdlist[] = {
        {"help", do_help},
        {"uname", do_uname},
        {"ps", do_ps}
};

int do_cmd(char *szcmd)
{
    int i;
    int ok = 0;
    if (szcmd) {
        if (strlen(szcmd) > 0) {
            printf("szcmd:%s \n", szcmd);
        }
    }
    for (i = 0; i < sizeof(cmdlist) / sizeof(CMD); ++i) {
        if (strcmp(cmdlist[i].cmd, szcmd) == 0) {
            if (cmdlist[i].funp) {
                cmdlist[i].funp();
                ok = 1;
            } else {
                printf("%d cmd is null\n", i);
            }
            break;
        }
    }
    if (! ok) {
        if (strcmp("", szcmd) != 0) {
            printf("Error command\n");
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -