what is the name of this piece of code

while ((numRead = read(inputFd, buf, BUF_SIZE)) > 0)
if (write(outputFd, buf, numRead) != numRead)
fatal("couldn't write whole buffer");

if (numRead == -1)
errExit("read");

if (close(inputFd) == -1)
errExit("close input");

if (close(outputFd) == -1)
errExit("close output");

exit(EXIT_SUCCESS);

since:

#ifndef BUF_SIZE 
#define BUF_SIZE 1024
#endif
ssize_t numRead;
char buf[BUF_SIZE];
inputFd = open(argv[1], O_RDONLY);
outputFd = open(argv[2], openFlags, filePerms);

I need to understand the while loop :smiley:

A simple translation from C to English is:

  1. Read no more than BUF_SIZE bytes from the file associated with the file descriptor inputFd into the array of characters at the address specified by buf and save the number of bytes actually read in numRead.
  2. If numRead is greater than 0, write those bytes from buf to the file file associated with the file descriptor outputFd and return the number of bytes written.
  3. If the number of bytes written is not the same as the number of bytes read do whatever the function fatal() does.
  4. Repeat steps 1, 2, and 3 until read has no more data to read (in which case it returns 0) or detects an error (in which case it returns -1).
1 Like