如何显示像 LS -al 这样的符号链接?

How to show symbolic links like LS -al?

我创建了一个粗略的程序,它使用 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

在你的程序中你查看 st_nlink 但它涉及 hard link (ln ...),而不是 符号links (ln -s ...)

readlink想写的路径

SYNOPSIS

  #include <unistd.h>

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

DESCRIPTION

  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");
   } 

来自

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

    char linkname[PATH_MAX];
    ssize_t r = readlink(myfile->d_name, linkname, PATH_MAX);

    if (r != -1) {
      linkname[r] = '[=13=]';
      printf(" -> %s\n", linkname);
    }
    else
      putchar('\n');