Esempio n. 1
0
        Channel TIDELog::writeCHAN(const std::string& name, const std::string& type, const std::string& source,
                const Array& source_spec, const Array& fmt_spec) {
            // argument checking
            check_bounds("name", 256, name.length());
            check_bounds("type", 10, type.length());
            check_bounds("source", 256, source.length());
            check_bounds("source_spec", 256, source_spec.length);
            check_bounds("fmt_spec", UINT_MAX, fmt_spec.length);

            // header
            HEADER hdr(TAG_CHAN, 4 + 1 + name.length() + type.length() + 4 + 
                        source.length() + 4 + source_spec.length + 4 + fmt_spec.length + 4);

            write_checked<HEADER,HDR_SIZE>(hdr);

            // ID
            const uint32_t id = ++num_chans;
            check_io(1, fwrite(&id, sizeof(id), 1, logfile), "id");
            // name
            write_checked(SArray(name.c_str(), name.length()), "name");
            // type
            write_checked(Array(type.c_str(), type.length()), "type");
            // human-readable source description
            write_checked(Array(source.c_str(), source.length()), "source");
            // source string
            write_checked(source_spec, "spec");
            // format spec
            write_checked(fmt_spec, "format");

            fflush(logfile);

            return Channel(id, name, type, source, source_spec, fmt_spec);
        }
Esempio n. 2
0
void clr(char** empty)
{
  //fprintf(stdout,"HELLO!\n");
	
	//empty character array
	pid_t pid;
	int status;
	strcpy(empty[0],"clear");
	//printf("%s",empty[0]);
	switch(pid=fork()){
	case -1:
		//syserr("fork");
	case 0:
		//syserr("exec");
		
	  //fprintf(stdout,"arrives at exec: %s\n", empty[0]);
		//checks IO redirect
			check_io();
		if(execvp(empty[0],empty)==-1)	//executes file and tests for failure
			{
				fprintf(stdout,"Command not found\n");
				_exit(-1);
			}
	default:
		if(!dont_wait)
			{
		//fprintf(stdout,"%d",pid);
		waitpid(pid,&status,WUNTRACED);}
	}
}
Esempio n. 3
0
void help()
{
	fprintf(stdout,"\n");
	pid_t pid;
	int status;
	char *fstr= (char*) malloc(sizeof(char)*40);
	strcpy(fstr,getenv("SHELL"));
	strcat(fstr,"/help.txt");
	//printf("%s",fstr);
	char *help[]={"more",fstr,(char *) 0};
	switch(pid=fork()){				
	case -1:
		//syserr("fork");
	case 0:
		//syserr("exec");
		//checks IO redirect
			check_io();
		if(execvp(help[0],help)==-1)
			{
				fprintf(stdout,"Command not found\n");
				_exit(-1);
			}
		//syserr("exec");
	default:
		if(!dont_wait)
			{
		//fprintf(stdout,"%d",pid);
		waitpid(pid,&status,WUNTRACED);}
	}
	
}
Esempio n. 4
0
/************************
* Other command function handler
*
************************/
void other(char** command)
{
	
	pid_t pid;
	int status;
	
	//First Instance of the Switch case used for creating a forked process.
	//Giving it PID as a switch case, it can determine if the child process or the parent process
	//simply by checking for zero.
	                	
	switch(pid=fork()){
	case -1:
		//syserr("fork");
	case 0:
		//syserr("exec");
		//checks IO redirect
			check_io();
		//execute to "unknown" command
		if(execvp(command[0],command)==-1)
			{
				fprintf(stdout,"Command not found\n");
				_exit(-1);
			}
	default:
		if(!dont_wait)
			{
		//fprintf(stdout,"%d",pid);
		waitpid(pid,&status,WUNTRACED);}
	}
}
Esempio n. 5
0
File: reset.c Progetto: ceph/spdk
static int
work_fn(void *arg)
{
	uint64_t tsc_end = rte_get_timer_cycles() + g_time_in_sec * g_tsc_rate;
	struct worker_thread *worker = (struct worker_thread *)arg;
	struct ns_worker_ctx *ns_ctx = NULL;

	printf("Starting thread on core %u\n", worker->lcore);

	if (spdk_nvme_register_io_thread() != 0) {
		fprintf(stderr, "spdk_nvme_register_io_thread() failed on core %u\n", worker->lcore);
		return -1;
	}

	/* Submit initial I/O for each namespace. */
	ns_ctx = worker->ns_ctx;
	while (ns_ctx != NULL) {
		submit_io(ns_ctx, g_queue_depth);
		ns_ctx = ns_ctx->next;
	}

	while (1) {
		/*
		 * Check for completed I/O for each controller. A new
		 * I/O will be submitted in the io_complete callback
		 * to replace each I/O that is completed.
		 */
		ns_ctx = worker->ns_ctx;
		while (ns_ctx != NULL) {
			check_io(ns_ctx);
			ns_ctx = ns_ctx->next;
		}

		if (((tsc_end - rte_get_timer_cycles()) / g_tsc_rate) > (uint64_t)g_time_in_sec / 5 &&
		    ((tsc_end - rte_get_timer_cycles()) / g_tsc_rate) < (uint64_t)(g_time_in_sec / 5 + 10)) {
			ns_ctx = worker->ns_ctx;
			while (ns_ctx != NULL) {
				if (spdk_nvme_ctrlr_reset(ns_ctx->ctr_entry->ctrlr) < 0) {
					fprintf(stderr, "nvme reset failed.\n");
					return -1;
				}
				ns_ctx = ns_ctx->next;
			}
		}

		if (rte_get_timer_cycles() > tsc_end) {
			break;
		}
	}

	ns_ctx = worker->ns_ctx;
	while (ns_ctx != NULL) {
		drain_io(ns_ctx);
		ns_ctx = ns_ctx->next;
	}

	spdk_nvme_unregister_io_thread();

	return 0;
}
Esempio n. 6
0
/**
 * Returns the original file descriptor
 * Usage:
 * io:getfd ( )
 *
 * [+1, -0, e]
 */
static int io_getfd(lua_State *L) {
    ev_io*       io  = check_io(L, 1);

    lua_pushinteger ( L , io->fd );

    return 1;
}
Esempio n. 7
0
int mcast_poll(char *data, int size)
{
	int ret;
	unsigned int len;
	struct sockaddr_in6 addr;

	len = sizeof(addr);
	ret = recvfrom(sock,
		       buf, sizeof(buf),
		       0,
		       (struct sockaddr *)&addr, &len);
	if (ret < 0) {
		perror("socket");
		exit(1);
	}

	if (memcmp(MSG_IO, buf, sizeof(MSG_IO)) == 0) {
		dbg("recv: io", &addr);
		return check_io(buf, ret, data, size);
	} else {
		dbg("recv: UNKNOWN", &addr);
	}

	return -EAGAIN;
}
Esempio n. 8
0
/*
* Paste text file to console/serial line. Avoid ascii-xfer problem of 
* swallowing up status messages returned via the serial line.
* This is especially useful for Embedded Microprocessor Development Kits
* that use raw file transfer mode (no protocols) to download text encoded
* executable files (eg., in S-Record or Intel Hex formats)
*
* TC Wan <*****@*****.**> 2003-10-18
*/
int paste_file(void)
{
  FILE *fp;
  char line[1024];
  char *s;
  const int dotrans = 0;
  const int ldelay = 1;      /* hardcoded 1 ms */
  char buf[128] = "";
  char *ptr;
  int bytes_read;
  unsigned long bdone = 0;
  int x;

  if ((s = filedir(1, 0)) == NULL)
    return 0;
  if ((fp = fopen(s, "r")) == NULL) {
    perror(s);
    return -1;
  }

  while (fgets(line, sizeof(line), fp)) {
    /* Check for I/O or timer. */
    x = check_io(portfd_connected, 0, 1000, buf, sizeof(buf), &bytes_read);

    /*  Send data from the modem to the screen. */
    if ((x & 1)) {
      ptr = buf;
      while (bytes_read-- > 0) {
	if (P_PARITY[0] == 'M' || P_PARITY[0] == 'S')
	  *ptr &= 0x7f;
	vt_out(*ptr++);
      }
      mc_wflush();
    }

    if (dotrans && (s = strrchr(line, '\n')) != NULL) {
      if (s > line && *(s - 1) == '\r')
	s--;
      *s = 0;
      s = line;
      for (s = line; *s; s++)
	vt_send(*s);
      vt_send('\r');
      vt_send('\n');
      bdone += strlen(line) + 2;
    } else {
      for (s = line; *s; s++)
	vt_send(*s);
      bdone += strlen(s);
    }
    if (ldelay) {
#ifdef HAVE_USLEEP
      usleep(ldelay * 1000);
#endif
    }
  }
  fclose(fp);
  return 0;
}
Esempio n. 9
0
static void
drain_io(struct ns_entry *entry)
{
	entry->is_draining = true;
	while (entry->current_queue_depth > 0) {
		check_io(entry);
	}
}
Esempio n. 10
0
File: perf.c Progetto: ninataki/spdk
static void
drain_io(struct ns_worker_ctx *ns_ctx)
{
    ns_ctx->is_draining = true;
    while (ns_ctx->current_queue_depth > 0) {
        check_io(ns_ctx);
    }
}
Esempio n. 11
0
File: overhead.c Progetto: spdk/spdk
static void
drain_io(void)
{
	g_ns->is_draining = true;
	while (g_ns->current_queue_depth > 0) {
		check_io();
	}
}
Esempio n. 12
0
void IOOUTCALL
cpu_out_d(UINT port, UINT32 data)
{

	if (CPU_STAT_PM && (CPU_STAT_VM86 || (CPU_STAT_CPL > CPU_STAT_IOPL))) {
		check_io(port, 4);
	}
	iocore_out32(port, data);
}
Esempio n. 13
0
void IOOUTCALL
cpu_out_w(UINT port, UINT16 data)
{

	if (CPU_STAT_PM && (CPU_STAT_VM86 || (CPU_STAT_CPL > CPU_STAT_IOPL))) {
		check_io(port, 2);
	}
	iocore_out16(port, data);
}
Esempio n. 14
0
UINT16 IOINPCALL
cpu_in_w(UINT port)
{

	if (CPU_STAT_PM && (CPU_STAT_VM86 || (CPU_STAT_CPL > CPU_STAT_IOPL))) {
		check_io(port, 2);
	}
	return iocore_inp16(port);
}
Esempio n. 15
0
UINT32 IOINPCALL
cpu_in_d(UINT port)
{

	if (CPU_STAT_PM && (CPU_STAT_VM86 || (CPU_STAT_CPL > CPU_STAT_IOPL))) {
		check_io(port, 4);
	}
	return iocore_inp32(port);
}
Esempio n. 16
0
void IOOUTCALL
cpu_out(UINT port, UINT8 data)
{

	if (CPU_STAT_PM && (CPU_STAT_VM86 || (CPU_STAT_CPL > CPU_STAT_IOPL))) {
		check_io(port, 1);
	}
	iocore_out8(port, data);
}
Esempio n. 17
0
/**
 * Stops the io so it won't be called by the specified event loop.
 *
 * Usage:
 *     io:stop(loop)
 *
 * [+0, -0, e]
 */
static int io_stop(lua_State *L) {
    ev_io*          io     = check_io(L, 1);
    struct ev_loop* loop   = *check_loop_and_init(L, 2);

    loop_stop_watcher(L, 2, 1);
    ev_io_stop(loop, io);

    return 0;
}
Esempio n. 18
0
UINT8 IOINPCALL
cpu_in(UINT port)
{

	if (CPU_STAT_PM && (CPU_STAT_VM86 || (CPU_STAT_CPL > CPU_STAT_IOPL))) {
		check_io(port, 1);
	}
	return iocore_inp8(port);
}
Esempio n. 19
0
        void TIDELog::writeTIDE() {
            const off_t pos = ftello(logfile);
            fseeko(logfile, 0, SEEK_SET);
            HEADER hdr(TAG_TIDE, pos);

            write_checked<HEADER,HDR_SIZE>(hdr, "TIDE header");
            write_checked<TIDE,TIDE_SIZE>(TIDE(1, 0, num_chans, num_chunks), "TIDE block");

            check_io(0, fflush(logfile), "flush");
        }
Esempio n. 20
0
/**
 * Starts the io so it won't be called by the specified event loop.
 *
 * Usage:
 *     io:start(loop [, is_daemon])
 *
 * [+0, -0, e]
 */
static int io_start(lua_State *L) {
    ev_io*       io  = check_io(L, 1);
    struct ev_loop* loop   = *check_loop_and_init(L, 2);
    int is_daemon          = lua_toboolean(L, 3);

    ev_io_start(loop, io);
    loop_start_watcher(L, 2, 1, is_daemon);

    return 0;
}
Esempio n. 21
0
static int
work_fn(void *arg)
{
	uint64_t tsc_end;
	struct worker_thread *worker = (struct worker_thread *)arg;
	struct ns_worker_ctx *ns_ctx = NULL;

	printf("Starting thread on core %u\n", worker->lcore);

	/* Allocate a queue pair for each namespace. */
	ns_ctx = worker->ns_ctx;
	while (ns_ctx != NULL) {
		if (init_ns_worker_ctx(ns_ctx) != 0) {
			printf("ERROR: init_ns_worker_ctx() failed\n");
			return 1;
		}
		ns_ctx = ns_ctx->next;
	}

	tsc_end = rte_get_timer_cycles() + g_time_in_sec * g_tsc_rate;

	/* Submit initial I/O for each namespace. */
	ns_ctx = worker->ns_ctx;
	while (ns_ctx != NULL) {
		submit_io(ns_ctx, g_queue_depth);
		ns_ctx = ns_ctx->next;
	}

	while (1) {
		/*
		 * Check for completed I/O for each controller. A new
		 * I/O will be submitted in the io_complete callback
		 * to replace each I/O that is completed.
		 */
		ns_ctx = worker->ns_ctx;
		while (ns_ctx != NULL) {
			check_io(ns_ctx);
			ns_ctx = ns_ctx->next;
		}

		if (rte_get_timer_cycles() > tsc_end) {
			break;
		}
	}

	ns_ctx = worker->ns_ctx;
	while (ns_ctx != NULL) {
		drain_io(ns_ctx);
		cleanup_ns_worker_ctx(ns_ctx);
		ns_ctx = ns_ctx->next;
	}

	return 0;
}
Esempio n. 22
0
static int
work_fn(void *arg)
{
	uint64_t tsc_end = rte_get_timer_cycles() + g_time_in_sec * g_tsc_rate;
	struct worker_thread *worker = (struct worker_thread *)arg;
	struct ns_entry *entry = NULL;

	printf("Starting thread on core %u\n", worker->lcore);

	nvme_register_io_thread();

	/* Submit initial I/O for each namespace. */
	entry = worker->namespaces;
	while (entry != NULL) {

		submit_io(entry, g_queue_depth);
		entry = entry->next;
	}

	while (1) {
		/*
		 * Check for completed I/O for each controller. A new
		 * I/O will be submitted in the io_complete callback
		 * to replace each I/O that is completed.
		 */
		entry = worker->namespaces;
		while (entry != NULL) {
			check_io(entry);
			entry = entry->next;
		}

		if (rte_get_timer_cycles() > tsc_end) {
			break;
		}
	}

	entry = worker->namespaces;
	while (entry != NULL) {
		drain_io(entry);
		entry = entry->next;
	}

	nvme_unregister_io_thread();

	return 0;
}
Esempio n. 23
0
/************************
*	Environment function
*	Purpose: Displays all environment data that is pointed to by 
* external environment pointer.
************************/
void envi(){
	//checks IO redirect
	check_io();
	//prints out file
	fprintf(stdout,"%s",getenv("PWD"));
	int f=0;
	while(*environ!=NULL)
	{
		fprintf(stdout,"%s\n",*environ);
		environ++;
		f++;
	}
	for(;f>0;f--)
	{
		environ--;
	}
}
Esempio n. 24
0
/************************
*Directory function implementation
************************/
void dir(char** directory,int dircount)
{	
	
	int status;


	//add ls -al to our arguments
	//add only one and overwrite dir
	directory[dircount+1]=(char*) malloc(sizeof(directory[dircount])); //malloc a new array location to 
	//fprintf(stdout,"%d",dircount);																	//shift all array data to the right 1
	strcpy(directory[0],"-al");
	while(dircount>=0)
	{
		
		directory[dircount+1]=directory[dircount];
		dircount--;
	}
	directory[0]="ls";

	
	//fprintf(stdout,"%s",directory[0]);
	pid_t pid;
	switch(pid=fork()){
	case -1:
		//syserr("fork");
	case 0:
		//fprintf(stdout,"Executing: %s",directory[0]);
		//checks IO redirect
			check_io();
		if(execvp(directory[0],directory)==-1) //executes file and tests for failure
			{
				fprintf(stdout,"Command not found\n");
				_exit(-1);
			}
		//syserr("exec");

	default:
		if(!dont_wait)
			{
		//fprintf(stdout,"%d",pid);
		waitpid(pid,&status,WUNTRACED);}
	}
	

}
Esempio n. 25
0
void echoarg(char** echoarg,int echocount)
{
	int i;
	int j;
	//checks IO redirect
	check_io();
	for(j=1;j<echocount;j++)
	{
		for(i=0;i<strlen(echoarg[j]);i++)
		{
			if(echoarg[j][i]!='"')
			{			
				fprintf(stdout,"%c",echoarg[j][i]);
			}
			
		}
		fprintf(stdout," ");
	}
	fprintf(stdout,"\n"); //clears line to throw
	
}
Esempio n. 26
0
File: overhead.c Progetto: spdk/spdk
static int
work_fn(void)
{
	uint64_t tsc_end;

	printf("Starting work_fn on core %u\n", rte_lcore_id());

	/* Allocate a queue pair for each namespace. */
	if (init_ns_worker_ctx() != 0) {
		printf("ERROR: init_ns_worker_ctx() failed\n");
		return 1;
	}

	tsc_end = spdk_get_ticks() + g_time_in_sec * g_tsc_rate;

	/* Submit initial I/O for each namespace. */
	submit_single_io();
	g_complete_tsc_start = spdk_get_ticks();

	while (1) {
		/*
		 * Check for completed I/O for each controller. A new
		 * I/O will be submitted in the io_complete callback
		 * to replace each I/O that is completed.
		 */
		check_io();

		if (spdk_get_ticks() > tsc_end) {
			break;
		}
	}

	drain_io();
	cleanup_ns_worker_ctx();

	return 0;
}
Esempio n. 27
0
int main(int argc, char *argv[])
{
    mrp_mainloop_t *ml;

    mrp_clear(&cfg);
    parse_cmdline(&cfg, argc, argv);

    mrp_log_set_mask(cfg.log_mask);
    mrp_log_set_target(cfg.log_target);

    ml = mainloop_create(&cfg);

    if (ml == NULL)
        fatal("failed to create main loop.");

    dbus_test.ml = ml;
    setup_dbus_tests(ml);
    ml = dbus_test.ml;

    setup_timers(ml);
    setup_io(ml);
    setup_signals(ml);
    MRP_UNUSED(setup_deferred);   /* XXX TODO: add deferred tests... */

#ifdef GLIB_ENABLED
    if (cfg.mainloop_type != MAINLOOP_GLIB && cfg.mainloop_type != MAINLOOP_QT) {
        if (cfg.ngio > 0 || cfg.ngtimer > 0)
            glib_pump_setup(ml);
    }

    setup_glib_io();
    setup_glib_timers();
#endif

    if (mrp_add_timer(ml, 1000, check_quit, NULL) == NULL)
        fatal("failed to create quit-check timer");

    setup_wakeup(ml);

    mainloop_run(&cfg);

    check_io();
    check_timers();
    check_signals();

#ifdef GLIB_ENABLED
    check_glib_io();
    check_glib_timers();
#endif

    if (dbus_test.client != 0)
        close(dbus_test.pipe[1]);   /* let the client continue */

    check_dbus();

#ifdef GLIB_ENABLED
    if (cfg.mainloop_type != MAINLOOP_GLIB) {
        if (cfg.ngio > 0 || cfg.ngtimer > 0)
            glib_pump_cleanup();
    }
#endif

    cleanup_wakeup();

    mainloop_cleanup(&cfg);
}
Esempio n. 28
0
 void TIDELog::write_checked<timeval>(const timeval& tv, const char* name) {
     uint64_t timestamp = ((uint64_t) tv.tv_sec * 10e6) + (uint64_t) tv.tv_usec;
     assert(sizeof(uint64_t) == 8);
     check_io(1, fwrite(&timestamp, sizeof (timestamp), 1, logfile), "timeval");
 }
Esempio n. 29
0
static int eat(const char *filename)
{

#define fail_if(cond) \
  while (cond) \
  { \
    show_error(filename); \
    if (fd != -1) \
      close(fd); \
    return -1; \
  }

#define check_io(i, j) \
  while ((size_t) (i) != (size_t) (j)) \
  { \
    if ((i) >= 0) \
      errno = EIO; \
    fail_if(1); \
  }

  const int fd = open(filename, O_RDWR);
  fail_if(fd == -1);
  const off_t file_size = lseek(fd, 0, SEEK_END);
  fail_if(file_size == -1);

  int rc;
  off_t offset;
  ssize_t r_bytes, w_bytes;

  offset = lseek(fd, 0, SEEK_SET);
  fail_if(offset == -1);

  if (block_size == 0)
  {
    struct statvfs st;
    rc = fstatvfs(fd, &st);
    fail_if(rc == -1);
    if (st.f_bsize == 0 || st.f_bsize >= block_size_limit)
    {
      errno = ERANGE;
      fail_if(1);
    }
    block_size = st.f_bsize;
  }

  const off_t n_blocks = (file_size + block_size - 1) / block_size;
  const size_t tail_size = (size_t) (1 + (file_size - 1) % block_size);

  switch (n_blocks)
  {
  case 2:
    r_bytes = read(fd, buffer, block_size);
    check_io(r_bytes, block_size);
    w_bytes = write(STDOUT_FILENO, buffer, block_size);
    check_io(w_bytes, block_size);
  case 1:
    r_bytes = read(fd, buffer, tail_size);
    check_io(r_bytes, tail_size);
    w_bytes = write(STDOUT_FILENO, buffer, (size_t) r_bytes);
    check_io(w_bytes, r_bytes);
  case 0:
    goto done;
  default:
    break;
  }

  struct stat stat;
  rc = fstat(fd, &stat);
  fail_if(rc == -1);
  if (stat.st_nlink > 1 && !opt_force)
  {
    errno = EMLINK;
    fail_if(1);
  }

  for (off_t i = 0; i < n_blocks / 2; i++)
  {
    r_bytes = read(fd, buffer, block_size);
    check_io(r_bytes, block_size);
    w_bytes = write(STDOUT_FILENO, buffer, block_size);
    check_io(w_bytes, block_size);
    if (i == 0 && opt_punch)
    {
#if HAVE_FALLOC_FL_PUNCH_HOLE
      rc = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, offset, block_size);
      if (rc == 0)
      {
        offset = 0;
        while (1)
        {
          r_bytes = read(fd, buffer, block_size);
          if (r_bytes == 0)
            break;
          offset += r_bytes;
          fail_if(r_bytes == -1);
          w_bytes = write(STDOUT_FILENO, buffer, (size_t) r_bytes);
          check_io(w_bytes, r_bytes);
          rc = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, offset);
          fail_if(rc != 0);
        }
        goto done;
      }
      else
#else
      errno = ENOTSUP;
#endif
      {
        show_error(filename);
        if (opt_punch > 1)
        {
          fprintf(stderr, "hungrycat: %s: fallocate() failed\n", filename);
          close(fd);
          return 1;
        }
        else
          fprintf(stderr, "hungrycat: %s: fallocate() failed; falling back to ftruncate()\n", filename);
      }
    }
    offset = (n_blocks - i - 1) * block_size;
    r_bytes = pread(fd, buffer, block_size, offset);
    check_io(r_bytes, (i == 0 ? tail_size : block_size));
    w_bytes = pwrite(fd, buffer, (size_t) r_bytes, i * block_size);
    check_io(r_bytes, w_bytes);
    rc = ftruncate(fd, offset);
    fail_if(rc == -1);
  }

  if ((n_blocks & 1) == 1)
  {
    r_bytes = read(fd, buffer, block_size);
    check_io(r_bytes, block_size);
    w_bytes = write(STDOUT_FILENO, buffer, block_size);
    check_io(w_bytes, block_size);
    rc = ftruncate(fd, (n_blocks / 2) * block_size);
    fail_if(rc == -1);
  }

  for (off_t i = (n_blocks / 2) - 1; i > 0; i--)
  {
    r_bytes = pread(fd, buffer, block_size, i * block_size);
    check_io(r_bytes, block_size);
    w_bytes = write(STDOUT_FILENO, buffer, block_size);
    check_io(w_bytes, block_size);
    rc = ftruncate(fd, i * block_size);
    fail_if(rc == -1);
  }

  assert(n_blocks > 0);
  r_bytes = pread(fd, buffer, tail_size, 0);
  check_io(r_bytes, tail_size);
  w_bytes = write(STDOUT_FILENO, buffer, (size_t) r_bytes);
  check_io(w_bytes, r_bytes);

done:
  rc = unlink(filename);
  fail_if(rc == -1);
  rc = close(fd);
  {
    const int fd = -1;
    fail_if(rc == -1);
  }
  return 0;

#undef fail_if

}
Esempio n. 30
0
/*
 * The main terminal loop:
 *	- If there are characters received send them
 *	  to the screen via the appropriate translate function.
 */
int do_terminal(void)
{
  char buf[128];
  int buf_offset = 0;
  int c;
  int x;
  int blen;
  int zauto = 0;
  static const char zsig[] = "**\030B00";
  int zpos = 0;
  const char *s;
  dirflush = 0;
  WIN *error_on_open_window = NULL;

dirty_goto:
  /* Show off or online time */
  update_status_time();

  /* If the status line was shown temporarily, delete it again. */
  if (tempst) {
    tempst = 0;
    mc_wclose(st, 1);
    st = NULL;
  }


  /* Auto Zmodem? */
  if (P_PAUTO[0] >= 'A' && P_PAUTO[0] <= 'Z')
    zauto = P_PAUTO[0];
  /* Set the terminal modes */
  setcbreak(2); /* Raw, no echo */

  keyboard(KSTART, 0);

  /* Main loop */
  while (1) {
    /* See if window size changed */
    if (size_changed) {
      size_changed = 0;
      wrapln = us->wrap;
      /* I got the resize code going again! Yeah! */
      mc_wclose(us, 0);
      us = NULL;
      if (st)
        mc_wclose(st, 0);
      st = NULL;
      mc_wclose(stdwin, 0);
      if (win_init(tfcolor, tbcolor, XA_NORMAL) < 0)
        leave(_("Could not re-initialize window system."));
      /* Set the terminal modes */
      setcbreak(2); /* Raw, no echo */
      init_emul(terminal, 0);
    }
    /* Update the timer. */
    timer_update();

    /* check if device is ok, if not, try to open it */
    if (!get_device_status(portfd_connected)) {
      /* Ok, it's gone, most probably someone unplugged the USB-serial, we
       * need to free the FD so that a replug can get the same device
       * filename, open it again and be back */
      int reopen = portfd == -1;
      close(portfd);
      lockfile_remove();
      portfd = -1;
      if (open_term(reopen, reopen, 1) < 0) {
        if (!error_on_open_window)
          error_on_open_window = mc_tell(_("Cannot open %s!"), dial_tty);
      } else {
        if (error_on_open_window) {
          mc_wclose(error_on_open_window, 1);
          error_on_open_window = NULL;
        }
      }
    }

    /* Check for I/O or timer. */
    x = check_io(portfd_connected, 0, 1000,
                 buf + buf_offset, sizeof(buf) - buf_offset, &blen);
    blen += buf_offset;
    buf_offset = 0;

    /* Data from the modem to the screen. */
    if ((x & 1) == 1) {
      char obuf[sizeof(buf)];
      char *ptr;

      if (using_iconv()) {
        char *otmp = obuf;
        size_t output_len = sizeof(obuf);
        size_t input_len = blen;

        ptr = buf;
        do_iconv(&ptr, &input_len, &otmp, &output_len);

        // something happened at all?
        if (output_len < sizeof(obuf))
          {
            if (input_len)
              { // something remained, we need to adapt buf accordingly
                memmove(buf, ptr, input_len);
                buf_offset = input_len;
              }

            blen = sizeof(obuf) - output_len;
            ptr = obuf;
          }
	else
	  ptr = buf;
      } else {
        ptr = buf;
      }

      while (blen-- > 0) {
        /* Auto zmodem detect */
        if (zauto) {
          if (zsig[zpos] == *ptr)
            zpos++;
          else
            zpos = 0;
        }
        if (P_PARITY[0] == 'M' || P_PARITY[0] == 'S')
          *ptr &= 0x7f;
        if (display_hex) {
          unsigned char c = *ptr++;
          unsigned char u = c >> 4;
          c &= 0xf;
          vt_out(u > 9 ? 'a' + (u - 10) : '0' + u);
          vt_out(c > 9 ? 'a' + (c - 10) : '0' + c);
          vt_out(' ');
        } else
          vt_out(*ptr++);
        if (zauto && zsig[zpos] == 0) {
          dirflush = 1;
          keyboard(KSTOP, 0);
          updown('D', zauto - 'A');
          dirflush = 0;
          zpos = 0;
          blen = 0;
          goto dirty_goto;
        }
      }
      mc_wflush();
    }