Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream>
- #include <fstream>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <sys/uio.h>
- using namespace std;
- //помощь
- void help(){
- cout << "0 - Help" << endl << "1 - Copy file" << endl << "2 - Get info" << endl << "3 - Set rights" << endl << "4 - Exit"<< endl;
- }
- //копирование
- void copy() {
- string file_in, file_out;
- cout << "Input name copy file\n";
- cin >> file_in;
- int out, in;
- if((in = open(file_in.c_str(), O_RDONLY)) == -1){
- cout << "File can't be readed\n";
- return;
- }
- cout << "Input name copyed file\n";
- cin >> file_out;
- if((out = open(file_out.c_str(), O_WRONLY)) == -1){
- cout << "File can't be writed or not exist\n";
- return;
- }
- char *buf[64];
- int in_out = read(in, buf, 64);
- while (in_out == 64){
- write(out, buf, 64);
- in_out = read(in, buf, 64);
- }
- write(out, buf, in_out);
- }
- //узнать статус файла
- void file_status(){
- string filename;
- cout << "Input filename: ";
- cin >> filename;
- struct stat buf{};
- stat(filename.c_str(), &buf);
- //SUID или SGID бит
- if (buf.st_mode & S_ISUID || buf.st_mode & S_ISGID)
- cout << 's';
- else cout << '-';
- //создатель
- if (buf.st_mode & S_IWUSR)
- cout << 'r';
- else cout << '-';
- if (buf.st_mode &S_IWUSR)
- cout << 'w';
- else cout << '-';
- if (buf.st_mode &S_IXUSR)
- cout << 'x';
- else cout << '-';
- //группа
- if (buf.st_mode &S_IRGRP)
- cout << 'r';
- else cout << '-';
- if (buf.st_mode &S_IWGRP)
- cout << 'w';
- else cout << '-';
- if (buf.st_mode &S_IXGRP)
- cout << 'x';
- else cout << '-';
- //другие пользователи
- if (buf.st_mode &S_IROTH)
- cout << 'r';
- else cout << '-';
- if (buf.st_mode &S_IWOTH)
- cout << 'w';
- else cout << '-';
- if (buf.st_mode & S_IXOTH)
- cout << 'x';
- else cout << '-';
- //sticky-бит
- if (buf.st_mode & S_ISVTX)
- cout << 't';
- cout << endl;
- cout << filename << ' ' << buf.st_size << " " << "byte " << buf.st_blocks << " " << "blocks\n";
- }
- //функция задания прав
- void set_file_rights(){
- string filename;
- cout << "Input filename: ";
- cin >> filename;
- char y_n;
- cout << "Input new file rights r(Read) w(Write) x(eXecute) for owner, group and other\n";
- int rights = 0;
- cout << "PLEASE!!!, Enter '1', if you want agree\n";
- cout << "For owner\n";
- cout << "R\n";
- cin >> y_n;
- if(y_n == '1') rights += 256;
- cout << "W\n";
- cin >> y_n;
- if(y_n == '1') rights += 128;
- cout << "X\n";
- cin >> y_n;
- if(y_n == '1') rights += 64;
- cout << "For group" << endl;
- cout << "R\n";
- cin >> y_n;
- if(y_n == '1') rights += 32;
- cout << "W\n";
- cin >> y_n;
- if(y_n == '1') rights += 16;
- cout << "X\n";
- cin >> y_n;
- if(y_n == '1') rights += 8;
- cout << "For other" << endl;
- cout << "R\n";
- cin >> y_n;
- if(y_n == '1')rights += 4;
- cout << "W\n";
- cin >> y_n;
- if(y_n == '1') rights+= 2;
- cout << "X\n";
- cin >> y_n;
- if(y_n == '1') rights += 1;
- chmod(filename.c_str(), rights);
- }
- int main(int argc, char* argv[]) {
- int action;
- while (true) {
- cout << "Choose action ('0' for help)\n";
- if (!(cin >> action)) {
- }
- //помощь
- else if(action == 0) {
- help();
- break;
- }
- //копировать
- if(action == 1) {
- copy();
- break;
- }
- //узнать статус
- else if(action == 2) {
- file_status();
- break;
- }
- //задать права
- else if(action == 3) {
- set_file_rights();
- break;
- }
- //выйти
- else if(action == 4) {
- return 0;
- }
- else {
- cout << "Error!" << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement