Advertisement
Leeen

Linux_cmd_lab #2

Apr 15th, 2020
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9. #include <sys/uio.h>
  10.  
  11. using namespace std;
  12.  
  13. //помощь
  14. void help(){
  15. cout << "0 - Help" << endl << "1 - Copy file" << endl << "2 - Get info" << endl << "3 - Set rights" << endl << "4 - Exit"<< endl;
  16. }
  17.  
  18. //копирование
  19. void copy() {
  20.         string file_in, file_out;
  21.         cout << "Input name copy file\n";
  22.         cin >> file_in;
  23.         int out, in;
  24.         if((in = open(file_in.c_str(), O_RDONLY)) == -1){
  25.             cout << "File can't be readed\n";
  26.              return;
  27.              }
  28.         cout << "Input name copyed file\n";
  29.         cin >> file_out;
  30.         if((out = open(file_out.c_str(), O_WRONLY)) == -1){
  31.             cout << "File can't be writed or not exist\n";
  32.               return;
  33.             }
  34.         char *buf[64];
  35.         int in_out = read(in, buf, 64);
  36.         while (in_out == 64){
  37.             write(out, buf, 64);
  38.             in_out = read(in, buf, 64);
  39.         }
  40.         write(out, buf, in_out);
  41. }
  42.  
  43. //узнать статус файла
  44. void file_status(){
  45.     string filename;
  46.     cout << "Input filename: ";
  47.     cin >> filename;
  48.    
  49.     struct stat buf{};
  50.     stat(filename.c_str(), &buf);
  51. //SUID или SGID бит
  52.     if (buf.st_mode & S_ISUID || buf.st_mode & S_ISGID)
  53.         cout << 's';
  54.     else cout << '-';
  55. //создатель
  56.     if (buf.st_mode & S_IWUSR)
  57.         cout << 'r';
  58.     else cout << '-';
  59.     if (buf.st_mode &S_IWUSR)
  60.         cout << 'w';
  61.     else cout << '-';
  62.     if (buf.st_mode &S_IXUSR)
  63.         cout << 'x';
  64.     else cout << '-';
  65.  
  66. //группа
  67.     if (buf.st_mode &S_IRGRP)
  68.         cout << 'r';
  69.     else cout << '-';
  70.     if (buf.st_mode &S_IWGRP)
  71.         cout << 'w';
  72.     else cout << '-';
  73.     if (buf.st_mode &S_IXGRP)
  74.         cout << 'x';
  75.     else cout << '-';
  76.  
  77. //другие пользователи
  78.     if (buf.st_mode &S_IROTH)
  79.         cout << 'r';
  80.     else cout << '-';
  81.     if (buf.st_mode &S_IWOTH)
  82.         cout << 'w';
  83.     else cout << '-';
  84.     if (buf.st_mode & S_IXOTH)
  85.         cout << 'x';
  86.     else cout << '-';
  87. //sticky-бит
  88.     if (buf.st_mode & S_ISVTX)
  89.         cout << 't';
  90.  
  91.     cout << endl;
  92.     cout << filename << ' ' << buf.st_size << " " << "byte " << buf.st_blocks << " " << "blocks\n";
  93. }
  94.  
  95.  
  96. //функция задания прав
  97. void set_file_rights(){
  98.     string filename;
  99.         cout << "Input filename: ";
  100.         cin >> filename;
  101.        
  102.         char y_n;
  103.         cout << "Input new file rights r(Read) w(Write) x(eXecute) for owner, group and other\n";
  104.         int rights = 0;
  105.         cout << "PLEASE!!!, Enter '1', if you want agree\n";
  106.         cout << "For owner\n";
  107.         cout << "R\n";
  108.         cin >> y_n;
  109.         if(y_n == '1') rights += 256;
  110.         cout << "W\n";
  111.         cin >> y_n;
  112.         if(y_n == '1') rights += 128;
  113.         cout << "X\n";
  114.         cin >> y_n;
  115.         if(y_n == '1') rights += 64;
  116.        
  117.         cout << "For group" << endl;
  118.         cout << "R\n";
  119.         cin >> y_n;
  120.         if(y_n == '1') rights += 32;
  121.         cout << "W\n";
  122.         cin >> y_n;
  123.         if(y_n == '1') rights += 16;
  124.         cout << "X\n";
  125.         cin >> y_n;
  126.         if(y_n == '1') rights += 8;
  127.        
  128.         cout << "For other" << endl;
  129.         cout << "R\n";
  130.         cin >> y_n;
  131.         if(y_n == '1')rights += 4;
  132.         cout << "W\n";
  133.         cin >> y_n;
  134.         if(y_n == '1') rights+= 2;
  135.         cout << "X\n";
  136.         cin >> y_n;
  137.         if(y_n == '1') rights += 1;
  138.        
  139.     chmod(filename.c_str(), rights);
  140. }
  141.  
  142.  
  143.  
  144. int main(int argc, char* argv[]) {
  145.  
  146.         int action;
  147.     while (true) {
  148.             cout << "Choose action ('0' for help)\n";
  149.             if (!(cin >> action)) {
  150.             }
  151.            
  152.             //помощь
  153.             else if(action == 0) {
  154.                 help();
  155.                 break;
  156.  
  157.             }
  158.             //копировать 
  159.             if(action == 1) {
  160.                 copy();
  161.                 break;
  162.  
  163.             }
  164.             //узнать статус
  165.             else if(action == 2) {
  166.                 file_status();
  167.                 break;
  168.  
  169.             }
  170.            
  171.             //задать права
  172.             else if(action == 3) {
  173.                 set_file_rights();
  174.                 break;
  175.  
  176.             }
  177.            
  178.             //выйти
  179.             else if(action == 4) {
  180.                 return 0;
  181.  
  182.             }
  183.  
  184.             else {
  185.                 cout << "Error!" << endl;
  186.  
  187.             }
  188.            
  189.         }
  190.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement