PROGRAM 3
/*******************************************************/
/* program name: program3.c */
/* brief description: directory browser */
/* name: anthony f. ortiz */
/* assignment #: 3 */
/* class: cs3560-01 */
/* quarter: spring 97 */
/* date completed: may 22, 1997 */
/* date due: may 22, 1997 */
/* version of compiler: csuh compiler */
/*******************************************************/
/*****************************************************************************/
/* this program gets and displays the files in the current directory. the */
/* user can perform any one of the following actions: quit the program, */
/* move cursor up to view a file, move cursor down to view a file, page */
/* up through list of files, page down through list of files, go to parent */
/* directory, or go to selected sub-directory. */
/*****************************************************************************/
/*****************************************************************************/
/* note: the last three permissions are the set-user id bit, the set-group */
/* id bit, and the sticky bit. */
/*****************************************************************************/
/* header files */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/* file variable */
struct file
{
char file_name [15];
char user [15];
char group [15];
char permissions [12];
char mod_time [30];
}
files [500];
int i;
/* cursor position (x, y) */
short int x = 1;
short int y = 7;
/* part of array to print */
int min = 0;
int max = 15;
/* program procedures */
void print_name ();
void print_instructions ();
void print_pathname ();
void get_files (char adirectory []);
void print_firstscreen ();
void do_action (char action);
void move_up ();
void move_down ();
void page_up ();
void page_down ();
void goto_parentdir ();
void goto_selecteddir ();
void clear_screen ();
void clear_screen2 ();
void initialize_files ();
/******************/
/* main program */
/******************/
void main ()
{
char action;
initialize_files ();
print_name ();
print_instructions ();
print_pathname ();
get_files (".");
print_firstscreen ();
do
{
action = getch ();
do_action (action);
}
while (action != 'q');
endwin ();
}
/***********************/
/* initialize files. */
/***********************/
void initialize_files ()
{
int j, k;
for (j = 0; j < 500; j++)
{
strcpy (files [j].file_name, "error") ;
strcpy (files [j].user, "error");
strcpy (files [j].group, "error");
strcpy (files [j].permissions, "error");
strcpy (files [j].mod_time, "error ");
}
}
/*****************************/
/* print the program name. */
/*****************************/
void print_name ()
{
initscr ();
crmode ();
noecho ();
nonl ();
clear ();
mvaddstr (1, 1,"-----------------------------------");
refresh ();
mvaddstr (1,36,"-----------------------------------");
refresh ();
mvaddstr (2, 1, "directory browser");
refresh ();
mvaddstr (3,1, "-----------------------------------");
refresh ();
mvaddstr (3,36,"-----------------------------------");
refresh ();
}
/**********************************/
/* print the help instructions. */
/**********************************/
void print_instructions ()
{
mvaddstr (4, 1,"q-quit, w-cursor up, z-cursor down, e-page up, ");
refresh ();
mvaddstr (4, 48, "x-page down, a-first page");
refresh ();
mvaddstr (5,1 ,"r-go to parent directory, -go to selected subdirectory");
refresh ();
mvaddstr (6, 1,"-----------------------------------");
refresh ();
mvaddstr (6,36,"-----------------------------------");
}
/**************************/
/* print the path name. */
/**************************/
void print_pathname ()
{
mvaddstr (22, 1,"-----------------------------------");
refresh ();
mvaddstr (22,36,"-----------------------------------");
refresh ();
mvaddstr (23, 1, "pathname: ");
refresh ();
move (23, 15);
printw ("%c", system ("pwd"));
refresh ();
mvaddstr (24,1, "-----------------------------------");
refresh ();
mvaddstr (24, 36,"-----------------------------------");
refresh ();
}
/*********************************************/
/* get the files in the current directory. */
/*********************************************/
void get_files (char adirectory [])
{
DIR * dp;
struct dirent * dirp;
struct stat buf;
i = 0;
if ((dp = opendir (adirectory)) == NULL)
{
mvaddstr (7, 1, "can't open this directory ");
refresh ();
}
else
{
while ((dirp = readdir (dp)) != NULL)
{
strcpy (files [i].file_name, dirp ->d_name);
if (lstat (files [i].file_name, &buf) < 0)
{
/* initialize_files will take care of message. */
}
else
{
strcpy (files [i].user, getpwuid (buf.st_uid) -> pw_name);
strcpy (files [i].group, getgrgid (buf.st_gid) -> gr_name);
if (S_IRUSR & buf.st_mode)
files [i].permissions [0] = 'r';
else
files [i].permissions [0] = '-';
if (S_IWUSR & buf.st_mode)
files [i].permissions [1] = 'w';
else
files [i].permissions [1] = '-';
if (S_IXUSR & buf.st_mode)
files [i].permissions [2] = 'x';
else
files [i].permissions [2] = '-';
if (S_IRGRP & buf.st_mode)
files [i].permissions [3] = 'r';
else
files [i].permissions [3] = '-';
if (S_IWGRP & buf.st_mode)
files [i].permissions [4] = 'w';
else
files [i].permissions [4] = '-';
if (S_IXGRP & buf.st_mode)
files [i].permissions [5] = 'x';
else
files [i].permissions [5] = '-';
if (S_IROTH & buf.st_mode)
files [i].permissions [6] = 'r';
else
files [i].permissions [6] = '-';
if (S_IWOTH & buf.st_mode)
files [i].permissions [7] = 'w';
else
files [i].permissions [7] = '-';
if (S_IXOTH & buf.st_mode)
files [i].permissions [8] = 'x';
else
files [i].permissions [8] = '-';
if (S_ISVTX & buf.st_mode)
files [i].permissions [9] = 's';
else
files [i].permissions [9] = '-';
if (S_ISUID & buf.st_mode)
files [i].permissions [10] = 's';
else
files [i].permissions [10] = '-';
if (S_ISGID & buf.st_mode)
files [i].permissions [11] = 's';
else
files [i].permissions [11] = '-';
strcpy (files [i].mod_time, ctime(&buf.st_ctime));
files [i].mod_time [20] = '\0';
}
i = i + 1;
}
}
closedir (dp);
}
/*************************/
/* print first screen. */
/*************************/
void print_firstscreen ()
{
int j;
int k = 0;
for (j = min; j < max; j++)
{
if (j >= i || j < 0)
break;
move (7 + k, 1);
printw ("%s", files [j].file_name);
refresh ();
move (7 + k, 16);
printw ("%s", files [j].user);
refresh ();
move (7 + k, 31);
printw ("%s", files [j].group);
refresh ();
move (7 + k, 46);
printw ("%s", files [j].permissions);
refresh ();
move (7 + k, 58);
printw ("%s", files [j].mod_time);
refresh ();
k = k + 1;
}
move (y, x);
refresh ();
}
/*************************************/
/* do one of the keyboard actions. */
/*************************************/
void do_action (char action)
{
switch (action)
{
case 'q': break;
case 'w': move_up ();
break;
case 'z': move_down ();
break;
case 'e': page_up ();
break;
case 'x': page_down ();
break;
case 'a': x = 1;
y = 7;
min = 0;
max = 15;
clear_screen ();
print_firstscreen ();
break;
case 'r': goto_parentdir ();
break;
case 13: goto_selecteddir ();
break;
}
}
/*********************/
/* move cursor up. */
/*********************/
void move_down ()
{
if (y < 21)
{
y++;
move (y, x);
refresh ();
}
else if (y >= 21)
{
y = 7;
x = 1;
min = min + 15;
max = max + 15;
clear_screen ();
print_firstscreen ();
}
}
/***********************/
/* move cursor down. */
/***********************/
void move_up ()
{
if (y > 7)
{
y--;
move (y, x);
refresh ();
}
else if (y == 7)
{
y = 21;
x = 1;
min = min - 15;
max = max - 15;
clear_screen ();
print_firstscreen ();
}
}
/**************/
/* page up. */
/**************/
void page_down ()
{
x = 1;
y = 7;
min = min + 15;
max = max + 15;
clear_screen ();
print_firstscreen ();
}
/****************/
/* page down. */
/****************/
void page_up ()
{
x = 1;
y = 7;
min = min - 15;
max = max - 15;
clear_screen ();
print_firstscreen ();
}
/****************************/
/* goto parent directory. */
/****************************/
void goto_parentdir ()
{
i = 0;
x = 1;
y = 7;
min = 0;
max = 15;
chdir ("..");
get_files (".");
clear_screen ();
clear_screen2 ();
print_pathname ();
print_firstscreen ();
}
/******************************/
/* goto selected directory. */
/******************************/
void goto_selecteddir ()
{
short int y2 = y;
int min2 = min;
DIR * dp;
struct dirent * dirp;
if (chdir (files [min + y - 7].file_name) < 0)
{
clear_screen ();
mvaddstr (7, 1, "can't open directory");
refresh ();
}
else
{
i = 0;
x = 1;
y = 7;
min = 0;
max = 15;
get_files (".");
clear_screen ();
clear_screen2 ();
print_pathname ();
print_firstscreen ();
}
}
/*********************************/
/* clear screen (main window). */
/*********************************/
void clear_screen ()
{
int k;
for (k = 0; k < 15; k++)
{
mvaddstr (7 + k, 1, " ");
mvaddstr (7 + k, 41, " ");
refresh ();
}
}
/*******************************************/
/* clear screen (bottom part of window). */
/*******************************************/
void clear_screen2 ()
{
int k;
for (k = 0; k < 1; k++)
{
mvaddstr (23, 1, " ");
mvaddstr (23, 41, " ");
refresh ();
}
}
/* outfile: prog3.out */
----------------------------------------------------------------------
directory browser
----------------------------------------------------------------------
q-quit, w-cursor up, z-cursor down, e-page up, x-page down, a-first page
r-go to parent directory, -go to selected subdirectory
----------------------------------------------------------------------
pathname: /students/w99/cs3590-01/cs218-01
. cs218-01 student rwx---------Fri Mar 12 14:56:14
.. root other rwxr-xr-x---Thu Jan 7 19:49:53
.dt cs218-01 student rwxr-xr-x---Thu Jan 7 20:34:16
.fm cs218-01 student rwx---------Thu Jan 7 20:48:23
.solregis cs218-01 student rwx---------Thu Jan 7 21:00:50
.wastebasket cs218-01 student rwx---------Thu Jan 7 21:02:41
.OWfontpath cs218-01 student rw----------Thu Jan 7 17:45:38
.Xauthority cs218-01 student rw----------Thu Jan 7 17:45:38
.Xdefaults.X11Rcs218-01 student rwx---------Thu Jan 7 17:45:38
.Xdefaults.opencs218-01 student rwx---------Thu Jan 7 17:45:38
.cshrc cs218-01 student rwx---------Thu Jan 7 17:45:39
.desksetdefaultcs218-01 student rw----------Thu Jan 7 17:45:39
.dtprofile cs218-01 student rwx---------Thu Jan 7 17:45:39
.emacs cs218-01 student rwx---------Thu Jan 7 17:45:39
BACK TO CS3560 PAGE.