Пример #1
0
int
main(int argc, char *argv[])
{
	char	**pargv;
	int	  error, pargc;

	pargv = NULL;
	error = pargc = 0;

	if (argc < 2)
		usage(MAIN_USAGE);

	/* torture test called with no options. */
	if (argc == 2) {
		pargc = 0;
		pargv = NULL;
	} else {
		/* Pass the program name to. */
		pargc = argc - 1;
		pargv = &argv[1];
	}
	if (strncmp("swap", argv[1], 4) == 0)
		error = swapkill(pargc, pargv);
	else if (strncmp("iogen", argv[1], 5) == 0)
		error = iogen(pargc, pargv);
	else if (strncmp("filbuf", argv[1], 6) == 0)
		error = filbuf(pargc, pargv);
	else if (strncmp("coredumper", argv[1], 10) == 0)
		error = coredumper(pargc, pargv);
	else
		usage(MAIN_USAGE);
	return (error);
}
Пример #2
0
static int wctx(struct zm_fileinfo *zi)
{
    register size_t thisblklen;
    register int sectnum, attempts, firstch;

    firstsec = TRUE;
    thisblklen = blklen;

    while ((firstch = READLINE_PF(Rxtimeout)) != NAK && firstch != WANTCRC && firstch != WANTG && firstch != TIMEOUT && firstch != CAN);
    if (firstch == CAN) {
        return ERROR;
    }
    if (firstch == WANTCRC)
        Crcflg = TRUE;
    if (firstch == WANTG)
        Crcflg = TRUE;
    sectnum = 0;
    for (;;) {
        if (zi->bytes_total <= (zi->bytes_sent + 896L))
            thisblklen = 128;
        if (!filbuf(txbuf, thisblklen))
            break;
        if (wcputsec(txbuf, ++sectnum, thisblklen) == ERROR)
            return ERROR;
        zi->bytes_sent += thisblklen;
    }
    fclose(input_f);
    attempts = 0;
    do {
        purgeline(io_mode_fd);
        sendline(EOT);
        flushmo();
        ++attempts;
    } while ((firstch = (READLINE_PF(Rxtimeout)) != ACK) && attempts < RETRYMAX);
    if (attempts == RETRYMAX) {
        return ERROR;
    } else
        return OK;
}
Пример #3
0
size_t fread(void *buffer, size_t size, size_t num, FILE *stream) {
  char *data;                     // Point to where should be read next
  unsigned total;                 // Total bytes to read
  unsigned count;                 // Num bytes left to read
  unsigned bufsize;               // Size of stream buffer
  unsigned nbytes;                // How much to read now
  unsigned nread;                 // How much we did read
  int c;                          // A temp char

  // Initialize local vars
  data = buffer;

  if ((count = total = size * num) == 0) return 0;

  if (anybuf(stream)) {
    // Already has buffer, use its size
    bufsize = stream->bufsiz;
  } else {
    // Assume will get BUFSIZ buffer
    bufsize = BUFSIZ;
  }

  // Here is the main loop -- we go through here until we're done
  while (count != 0) {
    // if the buffer exists and has characters, copy them to user buffer
    if (anybuf(stream) && stream->cnt != 0) {
      // How much do we want?
      nbytes = (count < (unsigned) stream->cnt) ? count : stream->cnt;
      memcpy(data, stream->ptr, nbytes);

      // Update stream and amount of data read
      count -= nbytes;
      stream->cnt -= nbytes;
      stream->ptr += nbytes;
      data += nbytes;
    } else if (count >= bufsize) {
      // If we have more than bufsize chars to read, get data
      // by calling read with an integral number of bufsiz
      // blocks.

      // Calc chars to read -- (count / bufsize) * bufsize
      nbytes = bufsize ? count - count % bufsize : count;

      nread = read(fileno(stream), data, nbytes);
      if (nread == 0) {
        // End of file -- out of here
        stream->flag |= _IOEOF;
        return (total - count) / size;
      } else if ((int) nread < 0) {
        // Error -- out of here
        stream->flag |= _IOERR;
        return (total - count) / size;
      }

      // Update count and data to reflect read
      count -= nread;
      data += nread;
    } else {
      // Less than bufsize chars to read, so call filbuf to fill buffer
      if ((c = filbuf(stream)) == EOF) {
        // Error or eof, stream flags set by filbuf
        return (total - count) / size;
      }

      // filbuf returned a char -- store it
      *data++ = (char) c;
      count--;

      // Update buffer size
      bufsize = stream->bufsiz;
    }
  }

  // We finished successfully, so just return num
  return num;
}