写了个简易版的 ls -l 哪里有错?

软件和网站开发以及相关技术探讨
回复
Debian大法好
帖子: 5
注册时间: 2019-07-03 9:08

写了个简易版的 ls -l 哪里有错?

#1

帖子 Debian大法好 » 2019-07-03 10:49

代码: 全选

#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
	DIR *a=opendir(argv[1]);
	struct dirent *b=NULL;
	struct stat* c=malloc(sizeof(struct stat));
	struct passwd *d=NULL;
	struct group *e=NULL;
	while(1)
	{
		b=readdir(a);	
		if(!b)
			break;
		stat(b->d_name,c);

		if(S_ISSOCK(c->st_mode))
			printf("socker file");
		else if(S_ISLNK(c->st_mode))
			printf("symbolic link");
		else if(S_ISREG(c->st_mode))
			printf("regular file");
		else if(S_ISBLK(c->st_mode))
			printf("block device");
		else if(S_ISDIR(c->st_mode))
			printf("directory");
		printf(" ");
		
		if(c->st_mode & S_IRUSR)
			printf("r");
		else
			printf("-");
		if(c->st_mode & S_IWUSR)
			printf("w");
		else
			printf("-");
		if(c->st_mode & S_IXUSR)
			printf("x");
		else
			printf("-");

		if(c->st_mode & S_IRGRP)
			printf("r");
		else
			printf("-");
		if(c->st_mode & S_IWGRP)
			printf("w");
		else
			printf("-");
		if(c->st_mode & S_IXGRP)
			printf("x");
		else
			printf("-");

		if(c->st_mode & S_IROTH)
			printf("r");
		else
			printf("-");
		if(c->st_mode & S_IWOTH)
			printf("w");
		else
			printf("-");
		if(c->st_mode & S_IXOTH)
			printf("x");
		else
			printf("-");
		printf(" ");
	
		d=getpwuid(c->st_uid);
		printf("%s ",d->pw_name);

		e=getgrgid(c->st_gid);
		printf("%s ",e->gr_name);

		printf("%s %s",c->st_size,b->d_name);
		printf("\n");
	}
}
上次由 Debian大法好 在 2019-07-04 8:33,总共编辑 1 次。
头像
astolia
论坛版主
帖子: 6460
注册时间: 2008-09-18 13:11

Re: 写了个简易版的 ls -l 哪里有错?

#2

帖子 astolia » 2019-07-03 19:59

如果你运行遇到了错误请直接说出来。大家没有那个闲工夫帮你一行行审查代码
Debian大法好
帖子: 5
注册时间: 2019-07-03 9:08

Re: 写了个简易版的 ls -l 哪里有错?

#3

帖子 Debian大法好 » 2019-07-04 8:37

astolia 写了: 2019-07-03 19:59 如果你运行遇到了错误请直接说出来。大家没有那个闲工夫帮你一行行审查代码
编译过程没有报错
运行 ./a.out .
结果是 Segmentation fault
运行 ./a.out ~
结果是
--------- root root (null) .nemiver
--------- root root (null) Downloads
--------- root root (null) .viminfo
--------- root root (null) .pki
--------- root root (null) C
--------- root root (null) shadowsocks.json~
--------- root root (null) .bashrc
--------- root root (null) .xsession-errors.old
--------- root root (null) .weechat
--------- root root (null) shell
--------- root root (null) .config
--------- root root (null) .bash_history
--------- root root (null) .gnupg
--------- root root (null) .xfce4-session.verbose-log
--------- root root (null) Desktop
--------- root root (null) .bash_logout
--------- root root (null) .protonvpn-cli
--------- root root (null) .xsession-errors
--------- root root (null) .dbus
Segmentation fault
头像
astolia
论坛版主
帖子: 6460
注册时间: 2008-09-18 13:11

Re: 写了个简易版的 ls -l 哪里有错?

#4

帖子 astolia » 2019-07-04 10:05

Debian大法好 写了: 2019-07-04 8:37 编译过程没有报错
:What :What :What

代码: 全选

$ gcc a.c
a.c: In function ‘main’:
a.c:81:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘__off_t {aka long int}’ [-Wformat=]
   printf("%s %s",c->st_size,b->d_name);
           ~^     ~~~~~~~~~~
           %ld
如果你用的gcc的默认配置没有报警,你可以自行添加参数-Wall

代码: 全选

gcc -Wall a.c
更进一步还可以加上-Wextra
Debian大法好
帖子: 5
注册时间: 2019-07-03 9:08

Re: 写了个简易版的 ls -l 哪里有错?

#5

帖子 Debian大法好 » 2019-07-04 12:29

astolia 写了: 2019-07-04 10:05
Debian大法好 写了: 2019-07-04 8:37 编译过程没有报错
:What :What :What

代码: 全选

$ gcc a.c
a.c: In function ‘main’:
a.c:81:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘__off_t {aka long int}’ [-Wformat=]
   printf("%s %s",c->st_size,b->d_name);
           ~^     ~~~~~~~~~~
           %ld
如果你用的gcc的默认配置没有报警,你可以自行添加参数-Wall

代码: 全选

gcc -Wall a.c
更进一步还可以加上-Wextra
谢谢
回复