From 0f2deb8b32b968824a008a7c8cfc71dcd2079dbe Mon Sep 17 00:00:00 2001 From: pajjilykk Date: Sat, 4 Oct 2025 18:44:43 +0700 Subject: [PATCH] add examples to lab1 --- lab_1/examples/copy.c | 53 +++++++++++++++++++++++++++++++++++++++++++ lab_1/examples/file.c | 32 ++++++++++++++++++++++++++ lab_1/task12.c | 0 3 files changed, 85 insertions(+) create mode 100644 lab_1/examples/copy.c create mode 100644 lab_1/examples/file.c create mode 100644 lab_1/task12.c diff --git a/lab_1/examples/copy.c b/lab_1/examples/copy.c new file mode 100644 index 0000000..8513113 --- /dev/null +++ b/lab_1/examples/copy.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#define BUF_SIZE 1024 +// пример программы обработки текстового файла средствами системых вызовов Linux +// учебник "Системное программирование в среде Linux", Гунько А.В., стр. 22 +int main (int argc, char * argv [ ]) +{ +int inputFd, outputFd, openFlags; +mode_t filePerms ; +ssize_t numRead; +char buf[BUF_SIZE]; +if (argc != 3) + { + printf("Usage: %s old-file new-file \n", argv[0]); exit(-1); + } +/* Открытие файлов ввода и вывода */ +inputFd = open (argv[1], O_RDONLY); +if (inputFd == -1) + { + printf ("Error opening file %s\n", argv[1]) ; exit(-2); + } +openFlags = O_CREAT | O_WRONLY | O_TRUNC; +filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; /* rw - rw - rw - */ +outputFd = open (argv [2], openFlags, filePerms); +if (outputFd == -1) + { + printf ("Error opening file %s\n ", argv[2]) ; exit(-3); + } +/* Перемещение данных до достижения конца файла ввода или возникновения ошибки */ +while ((numRead = read (inputFd, buf, BUF_SIZE)) > 0) + { + if (write (outputFd, buf, numRead) != numRead) + { + printf ("couldn't write whole buffer\n "); exit(-4); + } + if (numRead == -1) + { + printf ("read error\n "); exit(-5); + } + if (close (inputFd ) == -1 ) + { + printf ("close input error\n"); exit(-6); + } + if (close (outputFd ) == -1 ) + { + printf ("close output error\n"); exit(-7); + } + } +exit(EXIT_SUCCESS); +} diff --git a/lab_1/examples/file.c b/lab_1/examples/file.c new file mode 100644 index 0000000..75a0129 --- /dev/null +++ b/lab_1/examples/file.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include +// пример программы обработки текстового файла средствами системых вызовов Linux +// учебник "Системное программирование в среде Linux", Гунько А.В. +int main(int argc, char *argv[]) { +int handle, numRead, total= 0; +char buf; +if (argc<2) { + printf("Usage: file textfile\n"); + exit(-1); + } +// низкоуровневое открытие файла на чтение +handle = open( argv[1], O_RDONLY); +if (handle<0) { + printf("Error %d (%s) while open file: %s!\n",errno, strerror(errno),argv[1]); + exit(-2); + } +// цикл до конца файла +do { +// посимвольное чтение из файла +numRead = read( handle, &buf, 1); +if (buf == 0x20) total++; } +while (numRead > 0); +// Закрытие файла +close( handle); +printf("(PID: %d), File %s, spaces = %d\n", getpid(), argv[1], total); +// возвращаемое программой значение +return( total); } diff --git a/lab_1/task12.c b/lab_1/task12.c new file mode 100644 index 0000000..e69de29