linux命令源码

pwd

#include
#include
#include

int main()
{
    char path[256]={0};
    getcwd(path,256);
    printf("%s\n",path);
}

ls

#include
#include
#include
#include
#include
#include

int main()
{
    char path[256]={0};
    if(getcwd(path,256)==NULL)
    {
        printf ("ls err:cuo");
        exit(0);
    }
    DIR *ptr=opendir(path);
    if(ptr==NULL)
    {
        printf("err\n");
        exit(0);
    }
    struct stat st;
    struct dirent *p =NULL;
    while((p = readdir(ptr)) != NULL)
    {
        if ( strncmp( p->d_name, ".", 1) == 0)
        {
            continue;
        }
        lstat(p->d_name,&st);
        if(S_ISDIR(st.st_mode) )
        {
            printf("\033[1;34m%s \033[0m",p->d_name);
        }
        else
        {
            
            if(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH ) )
            {
                printf("\033[1;32m%s \033[0m",p->d_name);
            }
            printf("%s  ",p->d_name);
        }
    }
    printf("\n");
    closedir(ptr);
    exit(0);
}

你可能感兴趣的:(Linux,linux)