LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 705|回复: 5

实现命令" ls -l"是遇到的问题

[复制链接]
发表于 2007-5-14 12:53:00 | 显示全部楼层 |阅读模式
#include <cstdio>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <ctime>
#include <iostream>
using namespace std;
const int SIZE=1024;
int main()
{
        char pwd[SIZE];
        getcwd(pwd,SIZE);
        DIR* dir=opendir(pwd);
        if(dir==NULL)
        {
                cout<<"Opendir "<<pwd<<" error"<<endl;
                exit(0);
        }
        struct dirent *direntp;
        while((direntp=readdir(dir))!=NULL)
        {
                struct stat statbuf;
                stat(direntp->d_name,&statbuf);
                cout<<statbuf.st_mode<<"   "<<statbuf.st_nlink<<"   "<<statbuf.st_uid<<"   "
                        <<statbuf.st_gid<<"   "<<statbuf.st_size<<"   "<<ctime(&statbuf.st_ctime)
                        <<"   "<<direntp->d_name<<endl;
        }
        return 0;

得到的输出结果格式是这样的:
33188   1   0   0   1146   Sun May 13 22:56:21 2007
   mv.cpp
我想要的是这样的:
-rw-r--r-- 1 root    root    1146 05-13 22:56 mv.cpp
我是新手,不知道有没有什么系统函数可以把结果转换成我想要的。
thanks a million!
发表于 2007-5-14 14:25:20 | 显示全部楼层
自己用 位或 算出来呗,
$ man 2 stat
不是说的很清楚,哪些位代表什么么?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-5-14 15:15:53 | 显示全部楼层
权限可以用位域算出来,可是如何把uid转换为用户名,有没有系统函数实现这功能的,如果没的话我就要去读passwd文件和group文件了。
谢谢!
回复 支持 反对

使用道具 举报

发表于 2007-5-14 18:07:17 | 显示全部楼层
Post by capable
权限可以用位域算出来,可是如何把uid转换为用户名,有没有系统函数实现这功能的,如果没的话我就要去读passwd文件和group文件了。
谢谢!

man getpwnam
回复 支持 反对

使用道具 举报

发表于 2007-5-14 18:08:56 | 显示全部楼层
getgrgid (gid)
getpwuid (uid)

其实,这个也是读 passwd 文件得出来的。

如:
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <grp.h>
  4. #include <pwd.h>
  5. int main ()
  6. {
  7.         gid_t   gid = 500;
  8.         struct group *grp = getgrgid (gid);
  9.         puts (grp->gr_name);
  10.         uid_t   uid = 504;
  11.         struct passwd *pwd = getpwuid (uid);
  12.         puts (pwd->pw_name);
  13.         return 0;
  14. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-5-14 18:54:19 | 显示全部楼层
呵呵,从名字看的出来是从passwd和group文件读出来的。
谢谢楼上的两位。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表