Linux环境下模拟实现命令解释器(3)

1.遇到的困难及解决的问题

一段时间没有使用C++/C语言,有些函数的使用不怎么清晰,用惯了java的思考模式和API,转换过来用C++语言来编程,感觉有点不怎么舒服。后来,简单复习了C++的输入输出还有头文件包含等知识,还是把以前的底子给捡起来了。整个课程设计,遇到的问题并不多,难度是如果去理解Linux c函数库的调用。需要花时间去理解每一个函数的作用和相关参数的作用。刚开始做的时候,也是不知道怎么开始,之前把问题想得复杂化了,后来通过一步一步实现每个指令,才慢慢找到解决的办法。

2.总结与感想

总的来说,整个课程设计还算比较顺利,因为对Linux操作系统接触得并不多,对一些命令行的实现还不怎么清楚,后来通过查阅资料,自己用虚拟机运行了Linux Ubuntu发行版来研究了一下。发现Linux确实非常简洁好用,我非常喜欢这样的系统。历时两天的时间,把程序设计出来了,也认真写了下这次的文档。感觉通过一段时间的学习,自己的编程能力确实变强了,但还是有许多不足。在程序设计过程中,要学会查看文档,因为很多文档都是英文,这就需要英文好一点。我也在克服查看英文文档的障碍,努力提升自己的英文阅读能力。通过这次课程设计,也让自己捡起了C++这门语言,熟悉了Linux环境下的一些命令操作,并且让我有了极大兴趣去研究Linux,我在接下来的时间里也会使用和学习Linux程序设计。总的来说,感觉不错。

 

七.参考文献

Linux 程序设计4版》,The GUN C Library Manual

源程序代码如下:

因为解决乱码问题,所以小巫把注释都改为了英文,我的英文有点粗,希望有人能看懂。

/**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;
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/20432.html