源程序代码如下:
因为解决乱码问题,所以小巫把注释都改为了英文,我的英文有点粗,希望有人能看懂。
/**source file: cmd.cpp
@stunum: 201038889071
@classnum: 31
@author:wwj
@date:2012/12/16
@decription:Linux Operation System
**/
#include<iostream>
#include<cstring>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<stdlib.h>
#include<fcntl.h>
#include<time.h>
#include<queue>
#include<ftw.h>
using namespace std;
//declare method
void pwd(); //show current absolute path
void dir(); //show directory and file
void cd(); //change directory
void newdir(); //make new directory
void deldir(); //delete directory
void rename(); //rename the directory's name
void find(); //find the assign file in the assign directory
void date(); //show the date of now
int fn(const char *file, const struct stat *sb, int flag);
/**
* main method
* return 0
**/
int main(int argc, char *argv[])
{
cout<<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
cout<<">>>>>>Welcome to wwj's cmd line<<<<<<"<<endl;
cout<<"You can use the commands as follows:"<<endl;
cout<<"1. pwd "<<endl;
cout<<"2. dir <dirname>"<<endl;
cout<<"3. cd <dirname or path> "<<endl;
cout<<"4. newdir <dirname> "<<endl;
cout<<"5. deldir <dirname> "<<endl;
cout<<"6. rename <old filename> <new filename> "<<endl;
cout<<"7. find <dirname>"<<endl;
cout<<"8. date "<<endl;
cout<<"9. exit "<<endl;
cout<<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
string str;
//when the str is equal with the string "exit",break the cycling
while(str != "exit") {
//shell prompt
cout<<"[wuwenjie@]$";
cin>>str; //enter the command
if(str == "pwd"){
// if str is equal with the string "pwd", execute the pwd() method
pwd();
}
if(str == "dir") {
// if str is equal with the string "dir", execute the dir() method;
dir();
}
if(str == "cd") {
// if str is equal with the string "dir", execute the cd() method;
cd();
}
if(str == "newdir"){
// if str is equal with the string "newdir", execute the newdir() meth
newdir();
}
if(str == "deldir"){
// if str is equal with the string "deldir", execute the deldir() mte
deldir();
}
if(str == "rename") {
// if str is equal with the string "rename", execute the rename() meth
rename();
}
if(str == "date") {
// if str is equal with the string "date", execute the date() method;
date();
}
if(str == "find") {
// if str is equal with the string "find",execute the find() method
find();
}
}
return 0;
}
/**
* fuction:show current directory path
* return : void
**/
void pwd()
{
char ptr[80]; // create a character array with the size of 80
getcwd(ptr,sizeof(ptr)); //invoke the getcwd() method
cout<<ptr<<endl; //print the directory path
}
/**
* fuction: show current directory's directory or file
* return : void
*/
void dir()
{
DIR * dir; //The DIR data type represents a directory stream
struct dirent* ptr;
int count = 0;
char *dirname;
cin>>dirname;
dir = opendir(dirname);
if(dir == NULL)
{
cout<<"cannot open directory"<<endl;
}
//The opendir function opens and returns a directory stream for reading the
rectory whose file name is dirname
//This readdir function reads the next entry from the directory.
while((ptr = readdir(dir)) != NULL)
{ // if the d_name is equal with "." or ".." ,do nothing
if(strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0){}
// if not , print the ptr->d_name
else
cout<<ptr->d_name<<" ";
// count the d_name
count++;
// if count % 8 == 0, line feed
if(count % 8 == 0)
cout<<endl;
}
// close directory stream.
closedir(dir);
cout<<endl;
}
/**
* function; change the directory path
* return: void
*/
void cd()
{
char dirname[20];
cin>>dirname;
//if change the directory successful
if(chdir(dirname) == -1)
{
cout<<"the directory is not exit!!!"<<endl;
}
else
{
cout<<"change directory success!!!"<<endl;
}
}
/**
* function : make a new directory
* return: void
*/
void newdir()
{
char filename[20];
cin >> filename;
//S_IFDIR is the mode, the meaning is the file'type is a dirtectory
if(mkdir(filename, 0777) == 0)
{
cout<<filename<<" indecates successful!!!"<<endl;
}
else
{ cout<<filename<<" indecates failure!!!"<<endl;
}
}
/**
* function : delete a directory
* return: void
*/
void deldir()
{
char filename[20];
cin >> filename;
// if delete the dirtectory successful return 0
if(rmdir(filename) == 0)
{
cout<<filename<<" delete successful!!!"<<endl;
}
else
cout<<filename<<" delete failure!!!"<<endl;
}
/**
* function: rename the diretory'name
* return :void
*/
void rename()
{
char filename1[20], filename2[20];
cin>>filename1>>filename2;
// if the directory rename successful it will return 0
if(rename(filename1, filename2) == 0)
{
cout<<filename1<< " success change to "<<filename2<<endl;
}
else
cout<<filename1<< " failure change to "<<filename2<<endl;
}
/**
* function: find the assign directory and child directory's assign file
* return: void
*/
void find()
{
char dirname[50];
cin>>dirname;
// ftw(const char *dir, int(*fn)(const *file, const struct stat *sb,int fl
,int depth)
ftw(dirname, fn,500);
}
/**
* function: ergodic every layer directory and print the files
* @param *file
* @param struct stat *sb
* @param flag
* return int
*/
int fn(const char *file, const struct stat *sb, int flag)
{
if(flag == FTW_D)
cout << file <<"-- directory"<<endl;
else if(flag == FTW_F)
cout << file <<"-- file"<<endl;
return 0;
}
/**
* function: show the current date
* return : void
*/
void date()
{
time_t timeval;
(void)time(&timeval);
string timestr;
// invoke the ctime fuction and return the string
timestr = ctime(&timeval);
cout<<"The date is: "<<timestr<<endl;
}
