日期:2014-05-16  浏览次数:20773 次

哪位linux高手帮我修改下下面的C程序,错误提示我已经贴出来,谢谢。
/*
[root@localhost tellfile]# ls
aa makefile tell tell.c
[root@localhost tellfile]# ./tell
please input the file name :
aa
Segmentation fault
[root@localhost tellfile]# 

*/

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

//int stat(const char *path, struct stat *buf);
//int fstat(int fd, struct stat *buf);
// open(const char *pathname, int flags);
// open(const char *pathname, int flags, mode_t mode);
int main()
{
char *filename;
int fd;
struct stat buf;
printf("please input the file name :\n");
scanf("%s",filename);

if((fd = open(filename,O_RDONLY || O_WRONLY || O_CREAT)) < 0)
{
perror("open");
exit(EXIT_FAILURE);
}

  if((fstat(fd,&buf)) < 0)
{
perror("fstat");
exit(EXIT_FAILURE);
}
printf("the file is:%s,and it is size is:%ld\n",filename,buf.st_size);
printf(" MODE:%#o\n",buf.st_mode);
  switch(buf.st_mode)
{
case S_ISUID: printf("04000 set user ID on execution\n"); break;
case S_ISGID: printf("02000 set group ID on execution\n"); break;
case S_ISVTX: printf("01000 sticky bit\n"); break;
case S_IRUSR: printf("00400 read by owner\n"); break;
case S_IWUSR: printf("00200 write by owner\n"); break;
  case S_IXUSR: printf("00100 execute/search by owner\n"); break;
case S_IRGRP: printf("00040 read by group\n"); break;
case S_IWGRP: printf("00020 write by group\n"); break;
case S_IXGRP: printf("00010 execute/search by group\n"); break;
case S_IROTH: printf("00004 read by others\n"); break;
case S_IWOTH: printf("00002 write by others\n"); break;
case S_IXOTH: printf("00001 execute/search by others\n"); break;
  default:printf("0000 Have no jurisdiction\n");
}

if(close(fd) < 0)
{
perror("close");
exit(EXIT_FAILURE);
}

return 0;

}









------解决方案--------------------
char *filename;
你要给filename分配内存啊。
你这个指针很危险啊