【问题标题】:Aligning columns in ls在 ls 中对齐列
【发布时间】:2019-05-01 23:56:56
【问题描述】:

我正在编写 ls 命令的实现,但我发现了一个问题 列,我想像真正的 ls -l 一样对齐它们

drwx------@   3   haxor123 candidate  102  Oct  3 14:43 Applications
drwxr-xr-x    21   haxor123 candidate  714  Nov 29 21:07 Desktop
drwxr-xr-x    4   haxor123 candidate  136  Nov  6 19:54 Documents
drwx------    9   haxor123 candidate  306  Nov 28 22:28 Downloads
drwxr-xr-x@   396   haxor123 candidate  13464  Nov 29 19:52 Library
drwx------+   3   haxor123 candidate  102  Aug  9 16:38 Movies
drwx------+   4   haxor123 candidate  136  Oct  5 14:13 Music
drwxr-xr-x    3   haxor123 candidate  102  Oct  4 23:23 PicineRe
drwxr-xr-x    4   haxor123 candidate  136  Oct  4 23:52 PicineRee
drwxr-xr-x    3   haxor123 candidate  102  Oct  4 22:32 PicineReloaded
drwx------+   4   haxor123 candidate  136  Nov 11 16:46 Pictures
drwxr-xr-x    6   haxor123 candidate  204  Nov 12 21:38 exam-basedir
lrwxr-xr-x    1   haxor123 candidate  34  Jul 16 10:12 goinfre -> 
/Volumes/Storage/goinfre/haxor123/
drwxr-xr-x    4   haxor123 candidate  136  Oct  3 15:14 s

这是 ls -l 函数的一部分

temp = list;
ft_putstr("total ");
printblocks(list);
ft_putchar('\n');
while (temp != NULL)
{
    lstat(temp->full_path, &fstat);
    ft_permissions(temp, fstat);
    ft_putstr("  ");
    bytes1 = ft_itoa(fstat.st_nlink);
    ft_putstr(bytes1);
    ft_putstr(get_user(fstat));
    bytes = ft_itoa(fstat.st_size);
    len = ft_strlen(bytes);
    ft_putstr(ft_strjoin(bytes, "  "));
    get_time(fstat, temp);
    temp = temp->next;
    if (temp != NULL)
        ft_putchar('\n');

【问题讨论】:

  • 是什么阻止你这样做?
  • 我尝试了很多解决方案,但没有一个有效,我需要像精确的 ls 一样对齐每一列

标签: c ls


【解决方案1】:

如果您想直接阅读 GNU 命令的源代码,您可以这样做……这可能是一次很好的学习体验:

Where can I find source code for Linux core commands?

特别是ls.c:

http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/ls.c

准确知道“ls 是如何做到的”的唯一方法来自该文件。我们不是通灵者(至少我不是),所以如果你要询问任何其他编程方法,它需要在你的问题中独立包含你具体想要实现的目标以及为什么你不能实现它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 2021-12-15
    • 1970-01-01
    • 2022-11-08
    • 2011-11-09
    • 2017-01-11
    • 2016-02-10
    相关资源
    最近更新 更多