Ejemplo n.º 1
0
void split_window (zword height)
{
    zword stat_height = 0;

    flush_buffer ();

    /* Calculate height of status line and upper window */

    if (h_version != V6)
        height *= hi (wp[1].font_size);

    if (h_version <= V3)
        stat_height = hi (wp[7].font_size);

    /* Cursor of upper window mustn't be swallowed by the lower window */

    wp[1].y_cursor += wp[1].y_pos - 1 - stat_height;

    wp[1].y_pos = 1 + stat_height;
    wp[1].y_size = height;

    if ((short) wp[1].y_cursor > (short) wp[1].y_size)
        reset_cursor (1);

    /* Cursor of lower window mustn't be swallowed by the upper window */

    wp[0].y_cursor += wp[0].y_pos - 1 - stat_height - height;

    wp[0].y_pos = 1 + stat_height + height;
    wp[0].y_size = h_screen_height - stat_height - height;

    if ((short) wp[0].y_cursor < 1)
        reset_cursor (0);

    /* Erase the upper window in V3 only */

    if (h_version == V3 && height != 0)
        erase_window (1);

}/* split_window */
Ejemplo n.º 2
0
static void
cockpit_channel_real_close (CockpitChannel *self,
                            const gchar *problem)
{
  JsonObject *object;
  GBytes *message;

  if (self->priv->sent_close)
    return;

  self->priv->sent_close = TRUE;

  if (!self->priv->transport_closed)
    {
      flush_buffer (self);

      if (self->priv->close_options)
        {
          object = self->priv->close_options;
          self->priv->close_options = NULL;
        }
      else
        {
          object = json_object_new ();
        }

      json_object_set_string_member (object, "command", "close");
      json_object_set_string_member (object, "channel", self->priv->id);
      if (problem)
        json_object_set_string_member (object, "problem", problem);

      message = cockpit_json_write_bytes (object);
      json_object_unref (object);

      cockpit_transport_send (self->priv->transport, NULL, message);
      g_bytes_unref (message);
    }

  g_signal_emit (self, cockpit_channel_sig_closed, 0, problem);
}
Ejemplo n.º 3
0
int git_filebuf_commit(git_filebuf *file)
{
    int error;

    /* temporary files cannot be committed */
    assert(file && file->path_original);

    file->flush_mode = Z_FINISH;
    if ((error = flush_buffer(file)) < GIT_SUCCESS)
        goto cleanup;

    gitfo_close(file->fd);
    file->fd = -1;

    error = gitfo_mv(file->path_lock, file->path_original);

cleanup:
    git_filebuf_cleanup(file);
    if (error < GIT_SUCCESS)
        return git__rethrow(error, "Failed to commit locked file from buffer");
    return GIT_SUCCESS;
}
Ejemplo n.º 4
0
int git_filebuf_commit(git_filebuf *file, mode_t mode)
{
	/* temporary files cannot be committed */
	assert(file && file->path_original);

	file->flush_mode = Z_FINISH;
	flush_buffer(file);

	if (verify_last_error(file) < 0)
		goto on_error;

	file->fd_is_open = false;

	if (p_close(file->fd) < 0) {
		giterr_set(GITERR_OS, "Failed to close file at '%s'", file->path_lock);
		goto on_error;
	}

	file->fd = -1;

	if (p_chmod(file->path_lock, mode)) {
		giterr_set(GITERR_OS, "Failed to set attributes for file at '%s'", file->path_lock);
		goto on_error;
	}

	p_unlink(file->path_original);

	if (p_rename(file->path_lock, file->path_original) < 0) {
		giterr_set(GITERR_OS, "Failed to rename lockfile to '%s'", file->path_original);
		goto on_error;
	}

	git_filebuf_cleanup(file);
	return 0;

on_error:
	git_filebuf_cleanup(file);
	return -1;
}
Ejemplo n.º 5
0
void z_erase_picture (void)
{
    int height, width;

    zword y = zargs[1];
    zword x = zargs[2];

    flush_buffer ();

    if (y == 0)         /* use cursor line if y-coordinate is 0 */
        y = cwp->y_cursor;
    if (x == 0)         /* use cursor column if x-coordinate is 0 */
        x = cwp->x_cursor;

    os_picture_data (zargs[0], &height, &width);

    y += cwp->y_pos - 1;
    x += cwp->x_pos - 1;

    os_erase_area (y, x, y + height - 1, x + width - 1);

}/* z_erase_picture */
Ejemplo n.º 6
0
Archivo: stream.c Proyecto: epa/lrzip
/* flush and close down a stream. return -1 on failure */
int close_stream_out(void *ss)
{
	struct stream_info *sinfo = ss;
	int i;

	/* reallocate buffers to try and save space */
	for (i = 0; i < sinfo->num_streams; i++) {
		if (sinfo->s[i].buflen != 0) {
			if (!realloc(sinfo->s[i].buf, sinfo->s[i].buflen))
				fatal("Error Reallocating Output Buffer %d\n", i);
		}
	}
	for (i = 0; i < sinfo->num_streams; i++) {
		if (sinfo->s[i].buflen != 0 && flush_buffer(sinfo, i) != 0)
			return -1;
		if (sinfo->s[i].buf)
			free(sinfo->s[i].buf);
	}
	free(sinfo->s);
	free(sinfo);
	return 0;
}
Ejemplo n.º 7
0
size_t gut_file_write(gut_file *f, const void *ptr, size_t len) {
	gut_buffer *buf = f->buf;
	size_t written = 0;
	const unsigned char *cptr = ptr; // for convenience

	do {
		if (gut_buffer_availw(buf) == 0) {
			int success = flush_buffer(f);
			if (!success) break;
		}
		// Should have space for at least one byte now.

		written += gut_buffer_write(
		    buf,
		    &cptr[written],
		    len - written
		);

	} while (written < len);

	return written;
}
Ejemplo n.º 8
0
Archivo: err.c Proyecto: 4nykey/rockbox
void runtime_error (int errnum)
{
    int wasfirst;
    
    if (errnum <= 0 || errnum > ERR_NUM_ERRORS)
	return;

    if (f_setup.err_report_mode == ERR_REPORT_FATAL
	|| (!f_setup.ignore_errors && errnum <= ERR_MAX_FATAL)) {
	flush_buffer ();
	os_fatal (err_messages[errnum - 1]);
	return;
    }

    wasfirst = (error_count[errnum - 1] == 0);
    error_count[errnum - 1]++;
    
    if ((f_setup.err_report_mode == ERR_REPORT_ALWAYS)
	|| (f_setup.err_report_mode == ERR_REPORT_ONCE && wasfirst)) {
	long pc;

	GET_PC (pc);
	print_string ("Warning: ");
	print_string (err_messages[errnum - 1]);
	print_string (" (PC = ");
	print_long (pc, 16);
	print_char (')');
        
	if (f_setup.err_report_mode == ERR_REPORT_ONCE) {
	    print_string (" (will ignore further occurrences)");
	} else {
	    print_string (" (occurence ");
	    print_long (error_count[errnum - 1], 10);
	    print_char (')');
	}
	new_line ();
    }

} /* report_error */
Ejemplo n.º 9
0
/* If folding is on, check the position in the line and break it if necessary
 * so there is room to insert another graphic character.
 */
static void check_position(int fd)
{
   if (fold && position == width)
   {
      // Must break the line.
      // Insert a newline.
      if (buffer_full == BUFFER_SIZE)
      {
         flush_buffer(fd);
      }
      buffer[buffer_full++] = '\n';
      position = 0;
      // Insert a tab if the line we are breaking had an added tab.
      if (line_tabbed)
      {
         if (buffer_full == BUFFER_SIZE)
         {
            flush_buffer(fd);
         }
         buffer[buffer_full++] = '\t';
         position = 8;
      }
      // Insert 4 spaces.
      if (buffer_full == BUFFER_SIZE)
      {
         flush_buffer(fd);
      }
      buffer[buffer_full++] = ' ';
      if (buffer_full == BUFFER_SIZE)
      {
         flush_buffer(fd);
      }
      buffer[buffer_full++] = ' ';
      if (buffer_full == BUFFER_SIZE)
      {
         flush_buffer(fd);
      }
      buffer[buffer_full++] = ' ';
      if (buffer_full == BUFFER_SIZE)
      {
         flush_buffer(fd);
      }
      buffer[buffer_full++] = ' ';
      position += 4;
   }
}
Ejemplo n.º 10
0
void
#line 268 "./cwebdir/ctang-w2c.ch"
 output_defs P1H(void)
#line 608 "./cwebdir/ctangle.w"
{
sixteen_bits a;
push_level(NULL);
for(cur_text= text_info+1;cur_text<text_ptr;cur_text++)
if(cur_text->text_link==0){
cur_byte= cur_text->tok_start;
cur_end= (cur_text+1)->tok_start;
C_printf("%s","#define ");
out_state= normal;
protect= 1;
while(cur_byte<cur_end){
a= *cur_byte++;
if(cur_byte==cur_end&&a=='\n')break;
if(out_state==verbatim&&a!=string&&a!=constant&&a!='\n')
C_putc(a);

else if(a<0200)out_char(a);
else{
a= (a-0200)*0400+*cur_byte++;
if(a<024000){
cur_val= a;out_char(identifier);
}
else if(a<050000){confusion("macro defs have strange char");}
else{
cur_val= a-050000;cur_section= cur_val;out_char(section_number);
}

}
}
protect= 0;
flush_buffer();
}
pop_level(0);
}
Ejemplo n.º 11
0
Archivo: e.c Proyecto: 8l/ted
void disp_page()
{
int row;
if (batch) return;
clrscr_w();
if (c_comment) {
/* if there are more than 300 lines in a comment, this will fail */
	scan_comment(cursor_row-300,cursor_row+page_len);
}
for(row=page_row;row<page_row+page_len && row < Lines; row++) {
	if (LangType>=0)
		disp_row(row);
	else
		disp_row_raw(row);
}
if (row-page_row < page_len) {
	set_att(0);
	disp_eof(row-page_row);
}
flush_buffer();
draw_scr();
set_att(0);
}
Ejemplo n.º 12
0
int git_filebuf_reserve(git_filebuf *file, void **buffer, size_t len)
{
	size_t space_left = file->buf_size - file->buf_pos;

	*buffer = NULL;

	ENSURE_BUF_OK(file);

	if (len > file->buf_size) {
		file->last_error = BUFERR_MEM;
		return -1;
	}

	if (space_left <= len) {
		if (flush_buffer(file) < 0)
			return -1;
	}

	*buffer = (file->buffer + file->buf_pos);
	file->buf_pos += len;

	return 0;
}
Ejemplo n.º 13
0
int git_filebuf_printf(git_filebuf *file, const char *format, ...)
{
	va_list arglist;
	size_t space_left = file->buf_size - file->buf_pos;
	int len, error;

	va_start(arglist, format);

	len = vsnprintf((char *)file->buffer + file->buf_pos, space_left, format, arglist);

	if (len < 0 || (size_t)len >= space_left) {
		if ((error = flush_buffer(file)) < GIT_SUCCESS)
			return error;

		len = vsnprintf((char *)file->buffer + file->buf_pos, space_left, format, arglist);
		if (len < 0 || (size_t)len > file->buf_size)
			return GIT_ENOMEM;
	}

	file->buf_pos += len;
	return GIT_SUCCESS;

}
Ejemplo n.º 14
0
Archivo: screen.c Proyecto: BAM-X/frotz
/*
 * z_buffer_mode, turn text buffering on/off.
 *
 *	zargs[0] = new text buffering flag (0 or 1)
 *
 */
void z_buffer_mode (void)
{
    /* Infocom's V6 games rarely use the buffer_mode opcode. If they do
       then only to print text immediately, without any delay. This was
       used to give the player some sign of life while the game was
       spending much time on parsing a complicated input line. (To turn
       off word wrapping, V6 games use the window_style opcode instead.)
       Today we can afford to ignore buffer_mode in V6. */

    if (h_version != V6) {

	flush_buffer ();

	wp[0].attribute &= ~8;

	if (zargs[0] != 0)
	    wp[0].attribute |= 8;

	update_attributes ();

    }

}/* z_buffer_mode */
Ejemplo n.º 15
0
zchar Processor::stream_read_input(int max, zchar *buf, zword timeout, zword routine,
			  bool hot_keys, bool no_scripting) {
	zchar key = ZC_BAD;
	flush_buffer();

	// Remove initial input from the transscript file or from the screen
	if (ostream_script && enable_scripting && !no_scripting)
		script_erase_input(buf);

	// Read input line from current input stream
continue_input:

	do {
		if (istream_replay)
			key = replay_read_input(buf);
		else
			key = console_read_input(max, buf, timeout, key != ZC_BAD);
		if (shouldQuit())
			return ZC_BAD;
	} while (key == ZC_BAD);

	// Copy input line to the command file
	if (ostream_record && !istream_replay)
		record_write_input(buf, key);

	// Handle timeouts
	if (key == ZC_TIME_OUT)
	if (direct_call(routine) == 0)
		goto continue_input;

	// Copy input line to transscript file or to the screen
	if (ostream_script && enable_scripting && !no_scripting)
		script_write_input(buf, key);

	// Return terminating key
	return key;
}
Ejemplo n.º 16
0
size_t
mm_write_buffer_io_c::_write(const void *buffer,
                             size_t size) {
  size_t avail;
  const char *buf = static_cast<const char *>(buffer);
  size_t remain   = size;

  // whole blocks
  while (remain >= (avail = m_size - m_fill)) {
    if (m_fill) {
      // Fill the buffer in an attempt to defeat potentially
      // lousy OS I/O scheduling
      memcpy(m_buffer + m_fill, buf, avail);
      m_fill = m_size;
      flush_buffer();
      remain -= avail;
      buf    += avail;

    } else {
      // write whole blocks, skipping the buffer
      avail = mm_proxy_io_c::_write(buf, m_size);
      if (avail != m_size)
        throw mtx::mm_io::insufficient_space_x();

      remain -= avail;
      buf    += avail;
    }
  }

  if (remain) {
    memcpy(m_buffer + m_fill, buf, remain);
    m_fill += remain;
  }

  return size;
}
Ejemplo n.º 17
0
static int
msf_sas_handle_pipe_notify(msf_sas_handle_t *handle, msf_sas_op_t *op, int event_type)
{
	static int update_count = 0;
    int res;

    switch (event_type)
    {
    case MSF_PIPE_EVENT_READ:
        res = msf_sas_read_pipe_data(handle, op);
		if(res == MSF_PACKAGE_OPERATION_COMPLETE || (++update_count % 10 == 0)) {
			msf_sas_update_progressbar(handle, op);
		}
        break;
    case MSF_PIPE_EVENT_CLOSED:
        flush_buffer(handle, op);
        MSF_PIPE_CLOSE(op->pipe_handle);
        MSF_PIPE_DELETE(op->pathname);
        op->state = MSF_SAS_STATE_FINISHED;
        op->status = MSF_SAS_RETURN_OK;
        res = MSF_PACKAGE_OPERATION_COMPLETE;
        break;
    case MSF_PIPE_EVENT_CLOSED_ERROR:
        MSF_PIPE_CLOSE(op->pipe_handle);
        MSF_PIPE_DELETE(op->pathname);
        msf_sas_create_error_dialog (handle, op, MSF_WIDGET_STRING_GET_PREDEFINED(BRA_STR_ID_CONNECTION_FAILED));
        op->status = MSF_SAS_RETURN_ERROR;
        op->state = MSF_SAS_STATE_MESSAGE_WAIT;
        res = MSF_PACKAGE_SIGNAL_HANDLED;
        break;
    default:
        res = MSF_PACKAGE_SIGNAL_NOT_HANDLED;
        break;
    }
    return res;
}
Ejemplo n.º 18
0
void DiveLogExportDialog::export_depths(const char *filename, const bool selected_only)
{
	FILE *f;
	struct dive *dive;
	depth_t depth;
	int i;
	const char *unit = NULL;

	struct membuffer buf = {};

	for_each_dive (i, dive) {
		if (selected_only && !dive->selected)
			continue;

		FOR_EACH_PICTURE (dive) {
			int n = dive->dc.samples;
			struct sample *s = dive->dc.sample;
			depth.mm = 0;
			while (--n >= 0 && (int32_t)s->time.seconds <= picture->offset.seconds) {
				depth.mm = s->depth.mm;
				s++;
			}
			put_format(&buf, "%s\t%.1f", picture->filename, get_depth_units(depth.mm, NULL, &unit));
			put_format(&buf, "%s\n", unit);
		}
	}

	f = subsurface_fopen(filename, "w+");
	if (!f) {
		report_error(tr("Can't open file %s").toUtf8().data(), filename);
	} else {
		flush_buffer(&buf, f); /*check for writing errors? */
		fclose(f);
	}
	free_buffer(&buf);
}
Ejemplo n.º 19
0
void Processor::z_output_stream() {
	flush_buffer();

	switch ((short) zargs[0]) {
	case  1: ostream_screen = true;
		 break;
	case -1: ostream_screen = false;
		 break;
	case  2: if (!ostream_script) script_open();
		 break;
	case -2: if (ostream_script) script_close();
		 break;
	case  3: memory_open(zargs[1], zargs[2], zargc >= 3);
		 break;
	case -3: memory_close();
		 break;
	case  4: if (!ostream_record) record_open();
		 break;
	case -4: if (ostream_record) record_close();
		 break;
	default:
		break;
	}
}
Ejemplo n.º 20
0
void z_get_wind_prop (void)
{

    flush_buffer ();

    if (zargs[1] < 16)
        store (((zword *) (wp + winarg0 ())) [zargs[1]]);

    else if (zargs[1] == 16)
        store (os_to_true_colour (lo (wp [winarg0 ()].colour)));

    else if (zargs[1] == 17) {

        zword bg = hi (wp [winarg0 ()].colour);

        if (bg == TRANSPARENT_COLOUR)
            store ((zword) -4);
        else
            store (os_to_true_colour (bg));

    } else
        runtime_error (ERR_ILL_WIN_PROP);

}/* z_get_wind_prop */
Ejemplo n.º 21
0
void avio_flush(AVIOContext *s)
{
    flush_buffer(s);
    s->must_flush = 0;
}
Ejemplo n.º 22
0
/* Called only from hputc(), when our buffer is already full.  */
int hputc2(int c, hFILE *fp)
{
    if (flush_buffer(fp) < 0) return EOF;
    *(fp->begin++) = c;
    return c;
}
Ejemplo n.º 23
0
void avio_w8(AVIOContext *s, int b)
{
    *(s->buf_ptr)++ = b;
    if (s->buf_ptr >= s->buf_end)
        flush_buffer(s);
}
Ejemplo n.º 24
0
/*
 * tries to (re)open log file, nomove flag is used with -x switch
 * returns 0: success, 1: retry (log moved), -1: error
 */
int
try_reset_dump(int nomove)
{
	struct pcap_file_header hdr;
	struct stat st;
	int fd;
	FILE *fp;

	if (hpcap == NULL)
		return (-1);

	if (dpcap) {
		flush_buffer(dpcap);
		fclose(dpcap);
		dpcap = NULL;
	}

	/*
	 * Basically reimplement pcap_dump_open() because it truncates
	 * files and duplicates headers and such.
	 */
	fd = priv_open_log();
	if (fd < 0)
		return (-1);

	fp = fdopen(fd, "a+");

	if (fp == NULL) {
		logmsg(LOG_ERR, "Error: %s: %s", filename, strerror(errno));
		close(fd);
		return (-1);
	}
	if (fstat(fileno(fp), &st) == -1) {
		logmsg(LOG_ERR, "Error: %s: %s", filename, strerror(errno));
		fclose(fp);
		return (-1);
	}

	/* set FILE unbuffered, we do our own buffering */
	if (setvbuf(fp, NULL, _IONBF, 0)) {
		logmsg(LOG_ERR, "Failed to set output buffers");
		fclose(fp);
		return (-1);
	}

#define TCPDUMP_MAGIC 0xa1b2c3d4

	if (st.st_size == 0) {
		if (snaplen != cur_snaplen) {
			logmsg(LOG_NOTICE, "Using snaplen %d", snaplen);
			if (set_snaplen(snaplen))
				logmsg(LOG_WARNING,
				    "Failed, using old settings");
		}
		hdr.magic = TCPDUMP_MAGIC;
		hdr.version_major = PCAP_VERSION_MAJOR;
		hdr.version_minor = PCAP_VERSION_MINOR;
		hdr.thiszone = hpcap->tzoff;
		hdr.snaplen = hpcap->snapshot;
		hdr.sigfigs = 0;
		hdr.linktype = hpcap->linktype;

		if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
			fclose(fp);
			return (-1);
		}
	} else if (scan_dump(fp, st.st_size)) {
		fclose(fp);
		if (nomove || priv_move_log()) {
			logmsg(LOG_ERR,
			    "Invalid/incompatible log file, move it away");
			return (-1);
		}
		return (1);
	}

	dpcap = fp;

	set_suspended(0);
	flush_buffer(fp);

	return (0);
}
Ejemplo n.º 25
0
partition_snapshooter::~partition_snapshooter() {
        flush_buffer();
}
Ejemplo n.º 26
0
int hflush(hFILE *fp)
{
    if (flush_buffer(fp) < 0) return EOF;
    if (fp->backend->flush(fp) < 0) { fp->has_errno = errno; return EOF; }
    return 0;
}
Ejemplo n.º 27
0
int imap_append_message (CONTEXT *ctx, MESSAGE *msg)
{
  IMAP_DATA* idata;
  FILE *fp;
  char buf[LONG_STRING];
  char mbox[LONG_STRING];
  char mailbox[LONG_STRING];
  char internaldate[IMAP_DATELEN];
  char imap_flags[SHORT_STRING];
  size_t len;
  progress_t progressbar;
  size_t sent;
  int c, last;
  IMAP_MBOX mx;
  int rc;

  idata = (IMAP_DATA*) ctx->data;

  if (imap_parse_path (ctx->path, &mx))
    return -1;

  imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
  if (!*mailbox)
    strfcpy (mailbox, "INBOX", sizeof (mailbox));

  if ((fp = fopen (msg->path, "r")) == NULL)
  {
    mutt_perror (msg->path);
    goto fail;
  }

  /* currently we set the \Seen flag on all messages, but probably we
   * should scan the message Status header for flag info. Since we're
   * already rereading the whole file for length it isn't any more
   * expensive (it'd be nice if we had the file size passed in already
   * by the code that writes the file, but that's a lot of changes.
   * Ideally we'd have a HEADER structure with flag info here... */
  for (last = EOF, len = 0; (c = fgetc(fp)) != EOF; last = c)
  {
    if(c == '\n' && last != '\r')
      len++;

    len++;
  }
  rewind (fp);

  mutt_progress_init (&progressbar, _("Uploading message..."),
		      MUTT_PROGRESS_SIZE, NetInc, len);

  imap_munge_mbox_name (idata, mbox, sizeof (mbox), mailbox);
  imap_make_date (internaldate, msg->received);

  imap_flags[0] = imap_flags[1] = 0;
  if (msg->flags.read)
    safe_strcat (imap_flags, sizeof (imap_flags), " \\Seen");
  if (msg->flags.replied)
    safe_strcat (imap_flags, sizeof (imap_flags), " \\Answered");
  if (msg->flags.flagged)
    safe_strcat (imap_flags, sizeof (imap_flags), " \\Flagged");
  if (msg->flags.draft)
    safe_strcat (imap_flags, sizeof (imap_flags), " \\Draft");

  snprintf (buf, sizeof (buf), "APPEND %s (%s) \"%s\" {%lu}", mbox,
            imap_flags + 1,
	    internaldate,
	    (unsigned long) len);

  imap_cmd_start (idata, buf);

  do
    rc = imap_cmd_step (idata);
  while (rc == IMAP_CMD_CONTINUE);

  if (rc != IMAP_CMD_RESPOND)
  {
    char *pc;

    dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
		idata->buf));

    pc = idata->buf + SEQLEN;
    SKIPWS (pc);
    pc = imap_next_word (pc);
    mutt_error ("%s", pc);
    mutt_sleep (1);
    safe_fclose (&fp);
    goto fail;
  }

  for (last = EOF, sent = len = 0; (c = fgetc(fp)) != EOF; last = c)
  {
    if (c == '\n' && last != '\r')
      buf[len++] = '\r';

    buf[len++] = c;

    if (len > sizeof(buf) - 3)
    {
      sent += len;
      flush_buffer(buf, &len, idata->conn);
      mutt_progress_update (&progressbar, sent, -1);
    }
  }

  if (len)
    flush_buffer(buf, &len, idata->conn);

  mutt_socket_write (idata->conn, "\r\n");
  safe_fclose (&fp);

  do
    rc = imap_cmd_step (idata);
  while (rc == IMAP_CMD_CONTINUE);

  if (!imap_code (idata->buf))
  {
    char *pc;

    dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
		idata->buf));
    pc = idata->buf + SEQLEN;
    SKIPWS (pc);
    pc = imap_next_word (pc);
    mutt_error ("%s", pc);
    mutt_sleep (1);
    goto fail;
  }

  FREE (&mx.mbox);
  return 0;

 fail:
  FREE (&mx.mbox);
  return -1;
}
Ejemplo n.º 28
0
void put_flush_packet(ByteIOContext *s)
{
    flush_buffer(s);
    s->must_flush = 0;
}
Ejemplo n.º 29
0
void put_byte(ByteIOContext *s, int b)
{
    *(s->buf_ptr)++ = b;
    if (s->buf_ptr >= s->buf_end)
        flush_buffer(s);
}
Ejemplo n.º 30
0
int
main(int argc, char **argv)
{
	int ch, np, ret, Xflag = 0;
	pcap_handler phandler = dump_packet;
	const char *errstr = NULL;
	char *pidf = NULL;

	ret = 0;

	closefrom(STDERR_FILENO + 1);

	while ((ch = getopt(argc, argv, "Dxd:f:i:p:s:")) != -1) {
		switch (ch) {
		case 'D':
			Debug = 1;
			break;
		case 'd':
			delay = strtonum(optarg, 5, 60*60, &errstr);
			if (errstr)
				usage();
			break;
		case 'f':
			filename = optarg;
			break;
		case 'i':
			interface = optarg;
			break;
		case 'p':
			pidf = optarg;
			break;
		case 's':
			snaplen = strtonum(optarg, 0, PFLOGD_MAXSNAPLEN,
			    &errstr);
			if (snaplen <= 0)
				snaplen = DEF_SNAPLEN;
			if (errstr)
				snaplen = PFLOGD_MAXSNAPLEN;
			break;
		case 'x':
			Xflag++;
			break;
		default:
			usage();
		}

	}

	log_debug = Debug;
	argc -= optind;
	argv += optind;

	/* does interface exist */
	if (!if_exists(interface)) {
		warn("Failed to initialize: %s", interface);
		logmsg(LOG_ERR, "Failed to initialize: %s", interface);
		logmsg(LOG_ERR, "Exiting, init failure");
		exit(1);
	}

	if (!Debug) {
		openlog("pflogd", LOG_PID | LOG_CONS, LOG_DAEMON);
		if (daemon(0, 0)) {
			logmsg(LOG_WARNING, "Failed to become daemon: %s",
			    strerror(errno));
		}
		pidfile(pidf);
	}

	tzset();
	(void)umask(S_IRWXG | S_IRWXO);

	/* filter will be used by the privileged process */
	if (argc) {
		filter = copy_argv(argv);
		if (filter == NULL)
			logmsg(LOG_NOTICE, "Failed to form filter expression");
	}

	/* initialize pcap before dropping privileges */
	if (init_pcap()) {
		logmsg(LOG_ERR, "Exiting, init failure");
		exit(1);
	}

	/* Privilege separation begins here */
	if (priv_init()) {
		logmsg(LOG_ERR, "unable to privsep");
		exit(1);
	}

	setproctitle("[initializing]");
	/* Process is now unprivileged and inside a chroot */
	signal(SIGTERM, sig_close);
	signal(SIGINT, sig_close);
	signal(SIGQUIT, sig_close);
	signal(SIGALRM, sig_alrm);
	signal(SIGUSR1, sig_usr1);
	signal(SIGHUP, sig_hup);
	alarm(delay);

	buffer = malloc(PFLOGD_BUFSIZE);

	if (buffer == NULL) {
		logmsg(LOG_WARNING, "Failed to allocate output buffer");
		phandler = dump_packet_nobuf;
	} else {
		bufleft = buflen = PFLOGD_BUFSIZE;
		bufpos = buffer;
		bufpkt = 0;
	}

	if (reset_dump(Xflag) < 0) {
		if (Xflag)
			return (1);

		logmsg(LOG_ERR, "Logging suspended: open error");
		set_suspended(1);
	} else if (Xflag)
		return (0);

	while (1) {
		np = pcap_dispatch(hpcap, PCAP_NUM_PKTS,
		    phandler, (u_char *)dpcap);
		if (np < 0) {
			if (!if_exists(interface) == -1) {
				logmsg(LOG_NOTICE, "interface %s went away",
				    interface);
				ret = -1;
				break;
			}
			logmsg(LOG_NOTICE, "%s", pcap_geterr(hpcap));
		}

		if (gotsig_close)
			break;
		if (gotsig_hup) {
			if (reset_dump(0)) {
				logmsg(LOG_ERR,
				    "Logging suspended: open error");
				set_suspended(1);
			}
			gotsig_hup = 0;
		}

		if (gotsig_alrm) {
			if (dpcap)
				flush_buffer(dpcap);
			else 
				gotsig_hup = 1;
			gotsig_alrm = 0;
			alarm(delay);
		}

		if (gotsig_usr1) {
			log_pcap_stats();
			gotsig_usr1 = 0;
		}
	}

	logmsg(LOG_NOTICE, "Exiting");
	if (dpcap) {
		flush_buffer(dpcap);
		fclose(dpcap);
	}
	purge_buffer();

	log_pcap_stats();
	pcap_close(hpcap);
	if (!Debug)
		closelog();
	return (ret);
}