Linux System Programming Guide - Chapter 4 exercises to practice reading and recording

Problem 1: achieve some tee, -a achieve additional options

#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "tlpi_hdr.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define MAX_READ 1024

int readLine(int fd, char *buf, int size)
{
        int count = 0, nbytes;
        char *ptr;

        ptr = buf;
        while (count < size) {
                nbytes = read(fd, ptr, 1);
                if (nbytes == -1)
                        errExit("read");
                if (nbytes == 0)
                        break;
                if (*ptr == '\n')
                        break;
                ptr++;
                count++;
        }

        return count;
}

int main(int argc, char **argv)
{
        int opt, flags, fd, nbytes;
        char buf[MAX_READ + 1], *file;

        if (argc > 3) {
                usageErr("%s [-a] <file>\n", argv[0]);
        }
        opt = getopt(argc, argv , "a:");
        if (opt == ':' || opt == '?')
                usageErr("%s [-a] <file>\n", argv[0]);
        switch(opt) {
                case 'a':
                        flags = O_APPEND | O_WRONLY | O_CREAT;
                        file = optarg;
                        break;
                default:
                        flags = O_WRONLY | O_TRUNC | O_CREAT;
                        file = argv[1];
                        break;
        }
        if (file == NULL)
                fd = STDOUT_FILENO;
        else
                fd = open(file, flags, 0644);

        while (1) {
                if (fd == -1)
                        errExit("open");
                nbytes = readLine(STDIN_FILENO, buf, MAX_READ);
                buf[nbytes] = '\0';
                if (write(fd, buf, nbytes) == -1)
                        errExit("write");
                if (write(fd, "\n", 1) == -1)
                        errExit("write");
        }
        close(fd);

        exit(EXIT_SUCCESS);
}

   Summary: practiced getopt, and after a correct misconceptions --close, the data will be written to the file, in fact after the write, the data has been written to the drive cache, that data is written to the file has nothing to do with the close, close just released resource file descriptors, file descriptors due to limited resources, the need for timely release.

Problem 2: Implement cp command can copy the empty file

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "tlpi_hdr.h"


int main(int argc, char **argv)
{
        int src_fd, des_fd, src_size, i, nbytes;
        struct stat src_st;
        char ch;


        if (argc != 3)
                usageErr("%s <source file> <des file>\n", argv[0]);
        src_fd = open(argv[1], O_RDONLY);
        if (src_fd == -1)
                errExit("open src");
        des_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
        if (des_fd == -1)
                errExit("open des");
        if (stat(argv[1], &src_st) == -1)
                errExit("stat src");
        src_size = src_st.st_size;
        for (i = 0; i < src_size; i++) {
                lseek(src_fd, i, SEEK_SET);
                lseek(des_fd, i, SEEK_SET);
                nbytes = read(src_fd, &ch, sizeof(ch));
                if (nbytes == -1)
                        errExit("read src");
                if (nbytes == 0)
                        continue;
                nbytes = write(des_fd, &ch, sizeof(ch));
                if (nbytes == -1)
                        errExit("write des");
        }

        exit(EXIT_SUCCESS);
}

Summary: The practice of using lower lseek, intuitive feelings and write memory as another file offset is zero, and the memory of the same design.

Guess you like

Origin www.cnblogs.com/yangxinrui/p/11409626.html