1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
// Copyright (c) 2003 David Muse
// See the file COPYING for more information
#include <rudiments/groupentry.h>
#include <rudiments/passwdentry.h>
#include <rudiments/file.h>
#include <rudiments/permissions.h>
#include <rudiments/datetime.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, const char **argv) {
// remove the file (in case it already exists)
unlink("testfile");
// create a new file called "testfile" with rw-rw---- permissions
// and initial contents "hello"
file fl;
fl.create("testfile",permissions::evalPermString("rw-rw----"),"hello");
printf("testfile:\n");
// check for existence
if (file::exists("testfile")) {
printf(" exists\n");
} else {
printf(" does not exist\n");
}
// display the permissions of the file
mode_t mode=fl.getPermissions();
printf(" permissions: %s\n",permissions::evalPermOctal(mode));
// display the name of the user that owns the file
uid_t uid=fl.getOwnerUserId();
char *username;
passwdentry::getName(uid,&username);
printf(" user : %s\n",username);
delete[] username;
// display the name of the group that owns the file
gid_t gid=fl.getOwnerGroupId();
char *groupname;
groupentry::getName(gid,&groupname);
printf(" group : %s\n",groupname);
delete[] groupname;
// display the size of the file in bytes
off_t size=fl.getSize();
printf(" size : %ld\n",size);
// display the size of the file in blocks
blkcnt_t blocks=fl.getBlockCount();
printf(" blocks : %ld\n",blocks);
// display the file type
printf(" is a socket: %d\n",fl.isSocket());
printf(" is a symlink: %d\n",fl.isSymbolicLink());
printf(" is a regular file: %d\n",fl.isRegularFile());
printf(" is a block device: %d\n",fl.isBlockDevice());
printf(" is a directory: %d\n",fl.isDirectory());
printf(" is a character device: %d\n",fl.isCharacterDevice());
printf(" is a fifo: %d\n",fl.isFifo());
// display the last time the file was accessed
time_t atime=fl.getLastAccessTime();
char *atimestr=datetime::getString(atime);
printf(" last access : %s\n",atimestr);
delete[] atimestr;
// display the last time the file was modified
time_t mtime=fl.getLastModificationTime();
char *mtimestr=datetime::getString(mtime);
printf(" last modification: %s\n",mtimestr);
delete[] mtimestr;
// display the last time the file was changed
time_t ctime=fl.getLastChangeTime();
char *ctimestr=datetime::getString(ctime);
printf(" last change : %s\n",ctimestr);
delete[] ctimestr;
// display the device that the file resides on
dev_t dev=fl.getDevice();
printf(" device : %d\n",dev);
// display the type of the device that the file resides on
dev_t devtype=fl.getDeviceType();
printf(" device type : %d\n",devtype);
// display the file's first inode
ino_t inode=fl.getInode();
printf(" inode : %d\n",inode);
// display the number of hard links to the file
nlink_t nlink=fl.getNumberOfHardLinks();
printf(" hard links : %ld\n",nlink);
char *path="/usr/local/firstworks/include/rudiments/file.h";
char *dirname=file::dirname(path);
printf("dirname(%s)=%s\n",path,dirname);
delete[] dirname;
char *basename=file::basename(path);
printf("basename(%s)=%s\n",path,basename);
delete[] basename;
basename=file::basename(path,".h");
printf("basename(%s,\".h\")=%s\n",path,basename);
delete[] basename;
printf("key=%d\n",file::generateKey("/",1));
printf("maxLinks(%s)=%d\n",path,file::maxLinks(path));
printf("canChangeOwner(%s)=%d\n",path,file::canChangeOwner(path));
}
|