Write a UNIX command compsize that takes two files as input, compares their size
ID: 3862634 • Letter: W
Question
Write a UNIX command compsize that takes two files as input, compares their sizes and determines which fie has bigger size than the other by printing out a message: “file1 is greater than file2.”
please write full C lanugae code using unix command and use command line argument.
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* file type and mode */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
……..
};
Explanation / Answer
stat.c
#include <sys/stat.h>
#include <acess/sys.h>
// === CODE ===
int stat(const char *path, struct stat *buf)
{
int fd = _SysOpen(path, 0);
if( !fd ) {
// errno is set by _SysOpen
return -1;
}
int rv = fstat(fd, buf);
_SysClose(fd);
return rv;
}
int lstat(const char *path, struct stat *buf)
{
int fd = _SysOpen(path, OPENFLAG_NOLINK);
if( !fd ) {
// errno is set by _SysOpen
return -1;
}
int rv = fstat(fd, buf);
_SysClose(fd);
return rv;
}
int fstat(int fd, struct stat *buf)
{
t_sysFInfo info;
int rv = _SysFInfo(fd, &info, 0);
if( rv == -1 ) {
return -1;
}
_SysDebug("fstat(fd=%i,buf=%p)", fd, buf);
// TODO: Populate buf
buf->st_dev = info.mount;
buf->st_ino = info.inode;
if( info.flags & FILEFLAG_SYMLINK )
buf->st_mode = S_IFLNK;
else if( info.flags & FILEFLAG_DIRECTORY )
buf->st_mode = S_IFDIR;
else
buf->st_mode = S_IFREG;
// TODO: User modes (assume 660)
buf->st_mode |= 0660;
buf->st_nlink = 1;
buf->st_uid = info.uid;
buf->st_gid = info.gid;
buf->st_rdev = NULL;
buf->st_size = info.size;
buf->st_blksize = 512;
buf->st_blocks = (info.size+511)/512;
buf->st_atime = info.atime;
buf->st_mtime = info.mtime;
buf->st_ctime = info.ctime;
return 0;
}
stat.h
#ifndef _SYS_STAT_H_
#define _SYS_STAT_H_
#include <stdint.h>
#include "sys/types.h" // off_t
#ifdef __cplusplus
extern "C" {
#endif
typedef void *dev_t; /* TODO: How to identify a device with Acess */
typedef uint64_t ino_t;
typedef unsigned int blksize_t;
typedef uint64_t blkcnt_t;
typedef unsigned int nlink_t;
typedef uint32_t mode_t;
#define S_IFMT 0170000 /* type of file */
#define S_IFDIR 0040000 /* directory */
#define S_IFCHR 0020000 /* character special */
#define S_IFBLK 0060000 /* block special */
#define S_IFREG 0100000 /* regular */
#define S_IFLNK 0120000 /* symbolic link */
#define S_IFSOCK 0140000 /* socket */
#define S_IFIFO 0010000 /* fifo */
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
struct stat
{
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
extern int stat(const char *path, struct stat *buf);
extern int lstat(const char *path, struct stat *buf);
extern int fstat(int fd, struct stat *buf);
extern int mkdir(const char *pathname, mode_t mode);
#ifdef __cplusplus
}
#endif
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.