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

问题描述 投票:2回答:1

我已经创建了一个粗略的程序,使用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
c printing symlink ls
1个回答
1
投票

在你的程序中,你看看st_nlink,但它涉及硬链接(ln ...),而不是符号链接(ln -s ...

查看readlink以了解您要编写的路径

概要

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

通过

    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] = '\0';
      printf(" -> %s\n", linkname);
    }
    else
      putchar('\n');
© www.soinside.com 2019 - 2024. All rights reserved.