-
Notifications
You must be signed in to change notification settings - Fork 0
Create lab3.2 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
lab3.2
Outdated
| perror("myls: openning dir"); | ||
| return 1; | ||
| } | ||
| struct dirent* ep; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Опять не понятно, что за ep)
Всё же, не стоит жертвовать читаемостью кода ради сокращения нескольких букв в названии переменной
| perror("mymkdir: removing dir"); | ||
| return 1; | ||
| } | ||
| return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
И снова какие-то непонятные 0 и 1)
| int mycat(char* name){ | ||
| FILE* file = fopen(name, "r"); | ||
| if (file == NULL){ | ||
| perror("mycat: openning file:"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А тут типо норм, файл не открылся - написали сообщение и пошли дальше?
lab3.2
Outdated
| size_t size; | ||
| char buffer[4096]; | ||
| while((size = fread(buffer, sizeof(char), 4096, file))){ | ||
| if (write(0,buffer,size )== 0){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ну, ты, вроде, везде пользовался буферизированным вводом/выводом с fread/fwrite. Почему вдруг тут решил напрямую системные вызовы дергать? Почему тот же fwrite() не использовать?
lab3.2
Outdated
| buffer[len] = '\n'; | ||
| buffer[len +1] = '\0'; | ||
| if (write(1, buffer, len + 1) == 0){ | ||
| perror("mycatvsl "); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Опять пробел в конце сообщения)
lab3.2
Outdated
| } | ||
|
|
||
| printf("%s info: number of hard links: %lu.", name, st.st_nlink); | ||
| char rights[10]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ты можешь при объявлении массива его сразу нулями инициализировать char rights[10] = {0};, тогда не придется потом последний символ занулять
lab3.2
Outdated
| perror("mychmod: getting info about file"); | ||
| return 1; | ||
| } | ||
| st.st_mode = (st.st_mode >> 8) << 8; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Очень странно, почему сдвиги на 8, когда бит используется 9..
Можно было еще написать что-то типа st.st_mode &= ~((mode_t) 0777);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
И если в своем коде используешь какие-то такие неочевидные шаги, лучше всегда оставлять комментарии, почему здесь именно так. Ибо ты сам будешь читать код через месяц и уже забудешь, о чем здесь вообще речь была, почему здесь 8/9/15, а не 379, например
lab3.2
Outdated
| } | ||
|
|
||
| for (int i = 0; i < 3; i++){ | ||
| st.st_mode = rights[3 * i] == 'r' ? (st.st_mode | (0400 >> (3 * i))) : st.st_mode; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Если хочется коротенько и красиво, то можно вот так сделать)
st.st_mode |= rights[3 * i] == 'r' ? (0400 >> (3 * i)) : 0;
lab3.2
Outdated
| // printf("argv[0] %s\n", argv[0]); | ||
| // } | ||
| char* exe_name = strrchr(argv[0], '/') + 1; | ||
| if (exe_name == (char* ) NULL + 1 ){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Выглядит это, конечно, страшновато)
Вообще, есть уже готовая функция, которая по пути вернет тебе имя файла. Советую поискать, посмотреть)
lab3.2
Outdated
|
|
||
| if (!strcmp(exe_name, "mymkdir")){ | ||
| if (argc != 2){ | ||
| fprintf(stderr, "ivalid number of arguments(requars 1 arg: dir name)"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Вообще, правильно пишется require, а не requar. Лучше будет написать required 1 arg
lab3.2
Outdated
| } | ||
|
|
||
|
|
||
| int myrm(char* name){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А вот твои изменения, в которых ты половину лабы удалил))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Красивое)
No description provided.