Commit bcfe35f4 authored by Son Pham's avatar Son Pham

Lab 11.2 Completed

parent 164bafe2
/*
* CSCI 315 Operating Systems Design
* Original developed by CSCI 315 staff
* Modified by: L. Felipe Perrone
* Date: 2011-4-21
* Copyright (c) 2011 Bucknell University
*
* Permission is hereby granted, free of charge, to any individual or
* institution obtaining a copy of this software and associated
* documentation files (the "Software"), to use, copy, modify, and
* distribute without restriction, provided that this copyright and
* permission notice is maintained, intact, in all copies and supporting
* documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL BUCKNELL UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <grp.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <pwd.h>
#include <time.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
// pointer to file name string
char *name;
// allocate tm structure to store retrieved time
struct tm time;
// allocate space to hold a string with retrieved time (ASCII text)
char asctime_str[35];
// container for complete set of file permission bits (binary)
unsigned int mode;
// container for the three bits of user permissions
unsigned int umode;
// container for the three bits of group permissions
unsigned int gmode;
// container for the three bits of owner permission
unsigned int omode;
// human readable file permissions (ASCII text)
char perm_bits_str[] = "---------";
// file descriptor
unsigned int fd;
// structure to contain the result of the fstat call (info on this file)
struct stat file_info;
// structure to contain the result of the vfstat call (info on file system)
struct statvfs fs_info;
// used to save the return value of realpath
char resolved_path[PATH_MAX];
char* ret_path;
// check number of arguments for appropriate usage
if (2 != argc) {
printf(" usage: %s [file_name]\n", argv[0]);
exit(-11);
}
// post-condition: argv[1] contains name of file to use
// try to open file
fd = open(argv[1], O_RDONLY);
if (-1 == fd) {
perror("Failed to open read only file - ");
exit(-1);
}
// use fstatvfs to learn details about the file system
if (fstatvfs(fd, &fs_info) == 0) {
printf("== FILE SYSTEM INFO ============================\n");
printf(" file system fstatvfs() call successful\n");
printf(" file system block size: %ld\n", fs_info.f_bsize); // TO-DO
printf(" max. file name length: %ld\n", fs_info.f_namemax); // TO-DO
} else {
printf("%s: File system fstatvfs call failed\n", argv[0]);
exit(-1);
}
// post-condition: maximum length of file name string is known
// use calloc to allocate space for file name string
name = calloc(fs_info.f_namemax, 1);
if (NULL == name) {
perror("Problem in calloc - ");
exit(-1);
}
// copy file name into name variable using secure version of string copy
strncpy(name, argv[1], 500);
// use fstat to get information on specific file
if (fstat(fd, &file_info) == 0) {
printf("\n== FILE INFO ============================\n");
printf(" file fstat() call successful\n");
// mode comes from the lower 9 bits in file_info.st_mode
mode = file_info.st_mode & 0x1FF;
printf(" file protection bits = 0%o\n", mode);
// umode comes from the high 3 bits in mode
umode = (mode / 64) % 8;
// gmode comes from the middle 3 bits in mode
gmode = (mode / 8) % 8;
// omode comes from the low 3 bits in mode
omode = mode % 8;
// once you have set umode, gmode, and omode, the code below
// will construct the right string for you and display it
// construct string with file protection information
if (umode & 0x4) perm_bits_str[0] = 'r';
if (umode & 0x2) perm_bits_str[1] = 'w';
if (umode & 0x1) perm_bits_str[2] = 'x';
if (gmode & 0x4) perm_bits_str[3] = 'r';
if (gmode & 0x2) perm_bits_str[4] = 'w';
if (gmode & 0x1) perm_bits_str[5] = 'x';
if (omode & 0x4) perm_bits_str[6] = 'r';
if (omode & 0x2) perm_bits_str[7] = 'w';
if (omode & 0x1) perm_bits_str[8] = 'x';
printf(" file protection string = %s\n", perm_bits_str);
printf(" file protection mode (u:g:o) = %o:%o:%o\n",
umode, gmode, omode);
uid_t userid = file_info.st_uid;
uid_t groupid = file_info.st_gid;
char username[100];
char groupname[100];
struct passwd* user_info = getpwuid(userid);
strcpy(username, user_info -> pw_name);
struct group* group_info = getgrgid(groupid);
strcpy(groupname, group_info -> gr_name);
printf(" owner user name = %s\n", username); // TO-DO: man getpwuid
printf(" owner group name = %s\n", groupname); // TO-DO: man getgrgid
// TO-DO: print "mode = x", where x may be:
// "regular file"
// "directory"
// "character device"
// "block device"
// "symbolic link"
// "socket"
// "fifo"
// "unknown"
if (S_ISREG(file_info.st_mode)) {
printf(" mode = regular file\n");
} else { // see TO-DO above
}
// fill in the time the last write was made to file
localtime_r(&(file_info.st_mtime), &time);
asctime_r(&time, asctime_str);
printf(" time of last modification: %s", asctime_str);
// Complete for time of last access
localtime_r(&(file_info.st_atime), &time);
asctime_r(&time, asctime_str);
printf(" time of last access: %s", asctime_str);
// Complete for time of status change
localtime_r(&(file_info.st_ctime), &time);
asctime_r(&time, asctime_str);
printf(" time of last status change: %s", asctime_str);
fflush(stdout);
close(fd);
exit(0);
}
else
printf(" fstat call failed\n");
return 0;
}
# Name: Son Pham
# Class: CSCI 315
# Prof: Luiz Felipe Perrone
# Lab11
[2.2] The problem loops around its access, creating address such as:
../Lab11/loop/Lab11/loop/Lab11/loop/Lab11/loop
It will keep looping until the whole address go over the length limit and
the program can't loop anymore.
No preview for this file type
......@@ -92,8 +92,8 @@ int main(int argc, char *argv[]) {
if (fstatvfs(fd, &fs_info) == 0) {
printf("== FILE SYSTEM INFO ============================\n");
printf(" file system fstatvfs() call successful\n");
printf(" file system block size: %d\n", 0); // TO-DO
printf(" max. file name length: %d\n", 0); // TO-DO
printf(" file system block size: %ld\n", fs_info.f_bsize); // TO-DO
printf(" max. file name length: %ld\n", fs_info.f_namemax); // TO-DO
} else {
printf("%s: File system fstatvfs call failed\n", argv[0]);
exit(-1);
......@@ -123,13 +123,13 @@ int main(int argc, char *argv[]) {
printf(" file protection bits = 0%o\n", mode);
// umode comes from the high 3 bits in mode
umode = 0; // TO-DO
umode = (mode / 64) % 8;
// gmode comes from the middle 3 bits in mode
gmode = 0; // TO-DO
gmode = (mode / 8) % 8;
// omode comes from the low 3 bits in mode
omode = 0; // TO-DO
omode = mode % 8;
// once you have set umode, gmode, and omode, the code below
// will construct the right string for you and display it
......@@ -152,8 +152,19 @@ int main(int argc, char *argv[]) {
printf(" file protection mode (u:g:o) = %o:%o:%o\n",
umode, gmode, omode);
printf(" owner user name = %s\n",""); // TO-DO: man getpwuid
printf(" owner group name = %s\n", ""); // TO-DO: man getgrgid
uid_t userid = file_info.st_uid;
uid_t groupid = file_info.st_gid;
char username[100];
char groupname[100];
struct passwd* user_info = getpwuid(userid);
strcpy(username, user_info -> pw_name);
struct group* group_info = getgrgid(groupid);
strcpy(groupname, group_info -> gr_name);
printf(" owner user name = %s\n", username); // TO-DO: man getpwuid
printf(" owner group name = %s\n", groupname); // TO-DO: man getgrgid
// TO-DO: print "mode = x", where x may be:
// "regular file"
......@@ -164,11 +175,26 @@ int main(int argc, char *argv[]) {
// "socket"
// "fifo"
// "unknown"
if (S_ISREG(file_info.st_mode)) {
mode_t file_mode = file_info.st_mode;
if (S_ISREG(file_mode)) {
printf(" mode = regular file\n");
} else { // see TO-DO above
}
} else if (S_ISDIR(file_mode)) {
printf(" mode = directory");
} else if (S_ISCHR(file_mode)) {
printf(" mode = character device");
} else if (S_ISBLK(file_mode)) {
printf(" mode = block device");
} else if (S_ISFIFO(file_mode)) {
printf(" mode = fifo");
} else if (S_ISLNK(file_mode)) {
printf(" mode = symbolic link");
} else if (S_ISSOCK(file_mode)) {
printf(" mode = socket");
} else {
printf(" mode = unknown");
}
ret_path = realpath(name, resolved_path);
if (NULL != ret_path)
......@@ -177,12 +203,22 @@ int main(int argc, char *argv[]) {
perror(" couldn't resolve path");
exit(-1);
}
// fill in the time the last write was made to file
localtime_r(&(file_info.st_mtime), &time);
asctime_r(&time, asctime_str);
printf(" time of last modification: %s\n", asctime_str);
printf(" time of last modification: %s", asctime_str);
// Complete for time of last access
localtime_r(&(file_info.st_atime), &time);
asctime_r(&time, asctime_str);
printf(" time of last access: %s", asctime_str);
// Complete for time of status change
localtime_r(&(file_info.st_ctime), &time);
asctime_r(&time, asctime_str);
printf(" time of last status change: %s", asctime_str);
fflush(stdout);
close(fd);
exit(0);
......
No preview for this file type
..
\ No newline at end of file
No preview for this file type
......@@ -21,6 +21,8 @@
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <limits.h>
#include "read_dir.h"
// function prototypes
......@@ -55,6 +57,22 @@ void process(char *root)
char cname[MAXLENGTH];
char prefix[MAXLENGTH];
// Initialize some helpful variables for the assignment
int smallest_size = INT_MAX;
int largest_size = 0;
int total_size = 0;
double average_size = 0;
int num_directories = 0;
int num_regular = 0;
time_t most_recent = 0;
time_t least_recent = LONG_MAX;
char most_recent_file[MAXLENGTH];
char least_recent_file[MAXLENGTH];
// Temporary variables
off_t file_size;
time_t file_time;
struct dirent *dp;
DIR *dirp;
......@@ -64,10 +82,22 @@ void process(char *root)
while (true != queue_empty(nameq)) {
peek_front(dname,nameq);
dequeue(&nameq);
// Check for symbolic link
struct stat link_stat;
lstat(dname, &link_stat);
dirp = opendir(dname);
if (dirp != NULL) { // it is a directory
// Test of the path is a symbolic link
if (S_ISLNK(link_stat.st_mode)) {
printf("symbolic link : %s\n", dname);
}
// Test if the path is a directory.
else if ((dirp = opendir(dname)) != NULL) { // it is a directory
// Increment the number of directory
num_directories += 1;
// Explore that directory
printf("directory : %s\n",dname);
strncpy(prefix, dname, MAXLENGTH);
strncat(prefix,"/", MAXLENGTH);
......@@ -85,15 +115,52 @@ void process(char *root)
}
}
closedir (dirp);
} else {
// test if it is a regular file and not a device or link -- TO-DO
// if this is a regular file, then process it -- TO-DO
printf(" processing file: %s\n", dname);
} else {
unsigned int fd = open(dname, O_RDONLY);
struct stat file_info;
fstat(fd, &file_info); // wrapper?
mode_t file_mode = file_info.st_mode;
// Test if this is a regular file, then process it if it is
if (S_ISREG(file_mode)) {
// Increment necessary things
printf(" processing file: %s\n", dname);
// Do necessary things with file_size
file_size = file_info.st_size;
if (smallest_size > file_size) smallest_size = file_size;
if (largest_size < file_size) largest_size = file_size;
total_size += file_size;
num_regular += 1;
// Do necessary things with file_time
file_time = file_info.st_mtime;
if (file_time < least_recent) {
least_recent = file_time;
strcpy(least_recent_file, dname);
}
if (file_time > most_recent) {
most_recent = file_time;
strcpy(most_recent_file, dname);
}
}
numOfFiles++;
}
} // while
printf(" a total of %d files were counted\n",numOfFiles);
// After exploration, calculate the average file size
average_size = (double) total_size / num_regular;
// Print all confirmations
printf(" ========================\n");
printf(" Smallest file size: %d\n", smallest_size);
printf(" Largest file size: %d\n", largest_size);
printf(" Average file size: %lf\n", average_size);
printf(" Total number of directories: %d\n", num_directories);
printf(" Total number of regular files: %d\n", num_regular);
printf(" Most recently modified: %s\n", most_recent_file);
printf(" Least recently modified: %s\n", least_recent_file);
}
// initialize queue data structure
......
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment