【IO】day4

#include<stdio.h>
#include <errno.h>
#include<sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include<unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>

char* change_mode(mode_t mode,char *res)
{
	
	char per[] = "rwx";
	for(int i = 0; i < 9; i ++)
	{
		if((mode&(0400>>i)) == 0)
		{
			res[i] = '-';
			continue;
		}
		res[i] = per[i%3];
	}
	return res;
}

int main(int argc, const char *argv[])
{
	DIR *dp = opendir("../day3/");
	if(NULL == dp)
	{
		perror("opendir");
		return -1;
	}

	int count = 1;
	struct dirent* rp = NULL;
	while(1)
	{
		rp = readdir(dp);
		if(NULL == rp)
		{
			if(0 == errno)
			{
				printf("readdir success\n");
				break;
			}
			else 
			{
				perror("readdir");
				return -1;
			}
		}
		if((rp->d_name)[0] != '.')
		{
			struct stat buf;
			if(stat(rp->d_name,&buf) < 0)
			{
				perror("stat");
				return -1;
			}
			char res[10] = "";
			change_mode(buf.st_mode,res);
			printf("%s",res);	
			//nlink
			printf(" %ld",buf.st_nlink);

			//uid gid
			struct passwd *pwd = getpwuid(buf.st_uid);
			if(NULL == pwd)
			{
				perror("getpwuid");
				return -1;
			}
			struct group *gwd = getgrgid(buf.st_gid);
			if(NULL == gwd)
			{
				perror("getgrgid");
				return -1;
			}
			printf(" %s %s",pwd->pw_name,gwd->gr_name);
			
			//size
			printf(" %7ld ",buf.st_size);

			//time
			time_t t = (time_t)buf.st_ctime;
			struct tm* st = localtime(&t);
			printf("%2d月%2d %d:%2d ",st->tm_mon+1,st->tm_mday,st->tm_hour,st->tm_min);
			printf("%s\n",rp->d_name);

		}
	}
	closedir(dp);

	return 0;
}

#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, const char *argv[])
{
	int a = 10;
	pid_t cpid = fork();
	
	if(cpid > 0)//father
	{
		printf("a father location %p\n",&a);
		a++;
		printf("a father = %d\n",a);
		int b = 1, c = 2;
		printf("b father location %p\n",&b);
		printf("b father = %d\n",b);
		printf("c father location %p\n",&c);
		printf("c father = %d\n",c);
		while(1);	
	}
	else if(0 == cpid)
	{
		int d = 3, e = 4;
		printf("a child location %p\n",&a);
		printf("a child = %d\n",a);
		printf("d child location %p\n",&d);
		printf("d child = %d\n",d);
		printf("e child location %p\n",&e);
		printf("e child = %d\n",e);
		while(1);
	}
	else 
	{
		perror("fork");
		return -1;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值