如何显示符号链接,如LS -al?

时间:2019-04-04 17:20:32

标签: c printing symlink ls

我创建了一个粗糙的程序,使用C打印出与ls -al类似的提示符。尽管它几乎可以完美地工作,除了不能动态打印出由于没有参数而无法打开的目录之外,鉴于它只是在当前目录中运行,所以我认为.就足够了,但是如果有更好的动态方式进行此操作,请告诉我。我想知道如果下面显示类似内容,如何打印出指向文件的符号链接。

代码

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <grp.h>
#include <time.h>
#include <pwd.h>
#include <errno.h>
#include <string.h>

int main(void)
{
    DIR *mydir;
    DIR *thedir;
    struct dirent *myfile;
    struct stat myStat;
    struct passwd *pwd;
    int size = 0;
    struct tm lt;
    char buf[1024];
    char length[100];
    long width;
    struct group *gf;
    int len = 0;
    mydir = opendir("./");
    thedir = opendir("./");
    if(mydir!= NULL){
    while((myfile = readdir(thedir)) != NULL){
        lstat(myfile->d_name, &myStat);
        size += myStat.st_blocks;
        width = (long)myStat.st_size;
        sprintf(length, "%ld", width);
        if(len < strlen(length)){
        len = strlen(length);
     }

    }

    printf("total %d\n", size/2);
    closedir(thedir);

    while((myfile = readdir(mydir)) != NULL)
    {

        lstat(myfile->d_name, &myStat);
        if((lstat(myfile->d_name, &myStat) ) == 0){
            pwd = getpwuid(myStat.st_uid);
        }
        gf = getgrgid(myStat.st_gid);
        time_t t = myStat.st_mtime;
        localtime_r(&t, &lt);
        char timebuf[80];
        char timebuf2[80];

        strftime(timebuf, sizeof(timebuf), "%F", &lt);
        strftime(timebuf2, sizeof(timebuf2), "%R", &lt);
        printf( (S_ISDIR(myStat.st_mode)) ? "d" : "-");
        printf( (myStat.st_mode & S_IRUSR) ? "r" : "-");
        printf( (myStat.st_mode & S_IWUSR) ? "w" : "-");
        printf( (myStat.st_mode & S_IXUSR) ? "x" : "-");
        printf( (myStat.st_mode & S_IRGRP) ? "r" : "-");
        printf( (myStat.st_mode & S_IWGRP) ? "w" : "-");
        printf( (myStat.st_mode & S_IXGRP) ? "x" : "-");
        printf( (myStat.st_mode & S_IROTH) ? "r" : "-");
        printf( (myStat.st_mode & S_IWOTH) ? "w" : "-");
        printf( (myStat.st_mode & S_IXOTH) ? "x" : "-");
        printf(" ");
        printf("%ld ", myStat.st_nlink);
        if(pwd != 0){
            printf("%s %s %*ld %s %s %s\n", pwd->pw_name, gf->gr_name, len, (long)myStat.st_size, timebuf, timebuf2, myfile->d_name);
        }else  {

            printf("%d %s %*ld %s %s %s\n", myStat.st_uid, gf->gr_name, len, (long)myStat.st_size, timebuf, timebuf2, myfile->d_name);
            printf("\n");
        } 
        }
    closedir(mydir);
    }else{
        printf("ls: cannot open directory .: Permission denied");

    }


}

当前输出

total 8
-rw-rw-r-- 1 travis travis    0 2019-04-04 17:11 file2.txt
-rw-rw-r-- 1 travis travis    0 2019-04-04 17:11 file1.txt
drwxrwxr-x 4 travis travis 4096 2019-04-04 17:11 ..
drwxrwxr-x 2 travis travis 4096 2019-04-04 17:11 .
-rwxrwxrwx 1 travis travis    9 2019-04-04 17:11 link1
-rwxrwxrwx 1 travis travis    9 2019-04-04 17:11 link2

所需的输出

total 8
-rw-rw-r-- 1 travis travis    0 2019-04-04 17:11 file2.txt
-rw-rw-r-- 1 travis travis    0 2019-04-04 17:11 file1.txt
drwxrwxr-x 4 travis travis 4096 2019-04-04 17:11 ..
drwxrwxr-x 2 travis travis 4096 2019-04-04 17:11 .
lrwxrwxrwx 1 travis travis    9 2019-04-04 17:11 link1 -> file1.txt
lrwxrwxrwx 1 travis travis    9 2019-04-04 17:11 link2 -> file2.txt

1 个答案:

答案 0 :(得分:1)

在您的程序中,您查看st_nlink,但它涉及到硬链接ln ...),而不是符号链接({{1 }})

readlink 中查找要写入的路径

  

简介

ln -s ...
     

说明

  #include <unistd.h>

  ssize_t readlink(const char *pathname, char *buf, size_t bufsiz);

例如替换

  readlink()  places  the  contents  of the symbolic link pathname in the
  buffer buf, which has size bufsiz.  readlink() does not append  a  null
  byte  to buf.  It will (silently) truncate the contents (to a length of
  bufsiz characters), in case the buffer is too small to hold all of  the
   contents.

作者

   if(pwd != 0){
       printf("%s %s %*ld %s %s %s\n", pwd->pw_name, gf->gr_name, len, (long)myStat.st_size, timebuf, timebuf2, myfile->d_name);
   }else  {

       printf("%d %s %*ld %s %s %s\n", myStat.st_uid, gf->gr_name, len, (long)myStat.st_size, timebuf, timebuf2, myfile->d_name);
       printf("\n");
   }