int main(int argc, char *argv[]) { int fd; struct stat sb; mode_t u; umask(UMASK_SETTING); fd = open(MYFILE, O_RDWR | O_CREAT | O_EXCL, FILE_PERMS); if (fd == -1) errExit("open-%s", MYFILE); if (mkdir(MYDIR, DIR_PERMS) == -1) errExit("mkdir-%s", MYDIR); u = umask(0); /* Retrieves (and clears) umask value */ if (stat(MYFILE, &sb) == -1) errExit("stat-%s", MYFILE); printf("Requested file perms: %s\n", filePermStr(FILE_PERMS, 0)); printf("Process umask: %s\n", filePermStr(u, 0)); printf("Actual file perms: %s\n\n", filePermStr(sb.st_mode, 0)); if (stat(MYDIR, &sb) == -1) errExit("stat-%s", MYDIR); printf("Requested dir. perms: %s\n", filePermStr(DIR_PERMS, 0)); printf("Process umask: %s\n", filePermStr(u, 0)); printf("Actual dir. perms: %s\n", filePermStr(sb.st_mode, 0)); if (unlink(MYFILE) == -1) errMsg("unlink-%s", MYFILE); if (rmdir(MYDIR) == -1) errMsg("rmdir-%s", MYDIR); exit(EXIT_SUCCESS); }
static void displayStatInfo(const struct stat *sb) { printf("File type: "); switch (sb->st_mode & S_IFMT) { case S_IFREG: printf("regular file\n"); break; case S_IFDIR: printf("directory\n"); break; case S_IFCHR: printf("character device\n"); break; case S_IFBLK: printf("block device\n"); break; case S_IFLNK: printf("symbolic (soft) link\n"); break; case S_IFIFO: printf("FIFO or pipe\n"); break; case S_IFSOCK: printf("socket\n"); break; default: printf("unknown file type?\n"); break; } printf("Device containing i-node: major=%ld minor=%ld\n", (long) major(sb->st_dev), (long) minor(sb->st_dev)); printf("I-node number: %ld\n", (long) sb->st_ino); printf("Mode: %lo (%s)\n", (unsigned long) sb->st_mode, filePermStr(sb->st_mode, 0)); if (sb->st_mode & (S_ISUID | S_ISGID | S_ISVTX)) printf(" special bits set: %s%s%s\n", (sb->st_mode & S_ISUID) ? "set-UID " : "", (sb->st_mode & S_ISGID) ? "set-GID " : "", (sb->st_mode & S_ISVTX) ? "sticky " : ""); printf("Number of (hard) links: %ld\n", (long) sb->st_nlink); printf("Ownership: UID=%ld GID=%ld\n", (long) sb->st_uid, (long) sb->st_gid); if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode)) printf("Device number (st_rdev): major=%ld; minor=%ld\n", (long) major(sb->st_rdev), (long) minor(sb->st_rdev)); printf("File size: %lld bytes\n", (long long) sb->st_size); printf("Optimal I/O block size: %ld bytes\n", (long) sb->st_blksize); printf("512B blocks allocated: %lld\n", (long long) sb->st_blocks); printf("Last file access: %s", ctime(&sb->st_atime)); printf("Last file modification: %s", ctime(&sb->st_mtime)); printf("Last status change: %s", ctime(&sb->st_ctime)); }
static void displayStatInfo(const struct stat *sb) { char tmArr[200]; time_t t; struct tm *tmp; struct passwd *pd = getpwuid(sb->st_uid); struct group *gd = getgrgid(sb->st_gid); printf(" Size: %-16lld", (long long) sb->st_size); printf("Blocks: %-11lld", (long long) sb->st_blocks); printf("IO Block: %-7ld", (long) sb->st_blksize); switch (sb->st_mode & S_IFMT) { case S_IFREG: printf("regular file\n"); break; case S_IFDIR: printf("directory\n"); break; case S_IFCHR: printf("character device\n"); break; case S_IFBLK: printf("block device\n"); break; case S_IFLNK: printf("symbolic (soft) link\n"); break; case S_IFIFO: printf("FIFO or pipe\n"); break; case S_IFSOCK: printf("socket\n"); break; default: printf("unknown file type?\n"); break; } char combine[50]; sprintf(combine, "%lxh/%ldd",(long)sb->st_dev, (long)sb->st_dev); printf("Device: %-16s", combine); printf("Inode: %-12ld", (long) sb->st_ino); printf("Links: %ld\n", (long) sb->st_nlink); printf("Access: (0%lo/-%s) ", (unsigned long) sb->st_mode & 000777, filePermStr(sb->st_mode, 0)); printf("Uid: (%5u/%8s) ", pd->pw_uid, pd->pw_name); printf("Gid: (%5u/%8s)\n", gd->gr_gid, gd->gr_name); tmp = localtime(&sb-> st_atime); strftime( tmArr, sizeof(tmArr), "%F %T %z" , tmp); printf("Access: %s\n", tmArr); tmp = localtime(&sb-> st_mtime); strftime( tmArr, sizeof(tmArr), "%F %T %z" , tmp); printf("Modify: %s\n", tmArr); tmp = localtime(&sb-> st_ctime); strftime( tmArr, sizeof(tmArr), "%F %T %z" , tmp); printf("Change: %s\n",tmArr); }
static void displayStatInfo(const struct stat *sb, char *fileName) { //printf("Size: %lld\t\t", (long long) sb->st_size); //printf("Blocks: %lld\t", sb->st_blocks); //printf("I/O block: %ld ", (long) sb->st_blksize); /* printf( "Device containing i-node: major=%ld minor=%ld\n", (long) major(sb->st_dev), (long) minor(sb->st_dev) ); */ /* printf( "Mode: (%lo/-%s)\n", (unsigned long) sb->st_mode, filePermStr(sb->st_mode, 0) ); */ /* printf( "Ownership: UID=%ld GID=%ld\n", (long)sb->st_uid, (long)sb->st_gid ); */ if(sb->st_mode & (S_ISUID | S_ISGID | S_ISVTX)) { printf( "Special bits set: %s%s%s\n", (sb->st_mode & S_ISUID) ? "set-UID " : "", (sb->st_mode & S_ISGID) ? "set-GID " : "", (sb->st_mode & S_ISVTX) ? "sticky " : "" ); } if(S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode)) { printf( "Device number (st_rdev): major=%ld; minor=%ld\n", (long)major(sb->st_rdev), (long) minor(sb->st_rdev) ); } // File and Size Blocks IO Block printf( " File: '%s'\n Size: %lld Blocks: %lld IO Block: %ld ", fileName, (long long) sb->st_size, (long long)sb->st_blocks, (long)sb->st_blksize ); // Filetype switch (sb->st_mode & S_IFMT) { case S_IFREG: printf("regular file\n"); break; case S_IFDIR: printf("directory\n"); break; case S_IFCHR: printf("character device\n"); break; case S_IFBLK: printf("block device\n"); break; case S_IFLNK: printf("symbolic (soft) link\n"); break; case S_IFIFO: printf("FIFO or pipe\n"); break; case S_IFSOCK: printf("socket\n"); break; default: printf("unknown file type?\n"); break; } // Device and Inode printf( "Device: %lxh/%ldd Inode: %ld Links: %ld\n", (long)sb->st_dev, (long)sb->st_dev, (long) sb->st_ino, (long)sb->st_nlink ); // Access Uid and Gid // See: http://linux.die.net/man/3/getpwuid for details // getpwuid returns a passwd struct based on the passed user id struct passwd *pwd; pwd = getpwuid(sb->st_uid); // getgrgid returns a group struct based on the passed group id struct group *gp; gp = getgrgid(sb->st_gid); printf( "Access: (%4o/%s%s) Uid: (%ld/%s) Gid: (%ld/%s)\n", sb->st_mode & 007777, //Bitshift the //(unsigned long) sb->st_mode, ((S_ISDIR(sb->st_mode)) ? "d" : "-"), // Check if it is a dir and change the filePermStr(sb->st_mode, 0), // Get the uid and gid from the respective structs // Note that sb->st_uid and sb->gid also contain these values // See: http://linux.die.net/man/3/getpwuid for details (long)pwd->pw_uid, pwd->pw_name, (long)gp->gr_gid, gp->gr_name ); // Print the access modify and change times char buf[200]; strftime(buf, sizeof(buf), "Access: %Y-%m-%d %T %z\n", localtime(&sb->st_atime)); printf("%s", buf); //buf[200]; strftime(buf, sizeof(buf), "Modify: %Y-%m-%d %T %z\n", localtime(&sb->st_mtime)); printf("%s", buf); //buf[200]; strftime(buf, sizeof(buf), "Change: %Y-%m-%d %T %z\n", localtime(&sb->st_ctime)); printf("%s", buf); /* printf("Last file access: %s", ctime(&sb->st_atime)); printf("Last file modification: %s", ctime(&sb->st_mtime)); printf("Last status change: %s", ctime(&sb->st_ctime)); */ }