main(argc,argv) int argc; char *argv[]; { int fd; struct stat statbuf; if (argc!=2){ printf("usage: statfile filename!\n"); exit(1); } if ((fd = fopen(argv[1], O_RDONLY)) == -1) fprintf(stderr, "Cannot open %s!\n", argv[1]); if (unlink(argv[1]) == -1) fprintf(stderr,"Cannot unlink %s!\n", argv[1]); if (stat(argv[1], &statbuf) == -1) /* by file name */ fprintf(stderr, "stat %s fails as it should !\n"); else printf("stat %s succeed!\n", argv[1]); if (fstat(fd, &statbuf) == -1) /* by file descriptor */ fprintf(stderr, "fstat %s fails!\n", argv[1]); else printf("fstat %s succeeds as it should!\n", argv[1]); }
stat("hello", &statbuf); if (statbuf.st_mod & S_IFMT) == S_IFDIR) printf("This is a directory file!\n"); else if (statbuf.st_mod & S_IFMT) == S_IFREG) printf("This is a regular file !\n"); else …