Exemplo n.º 1
0
void
dbop3_update(DBOP *dbop, const char *key, const char *dat) {
	int rc;
	char *errmsg = 0;
	STRBUF *sql = strbuf_open_tempbuf();

	strbuf_sprintf(sql, "update %s set dat = '%s' where key = '%s'",
					dbop->tblname, dbop3_quote((char *)dat), key);
	rc = sqlite3_exec(dbop->db3, strbuf_value(sql), NULL, NULL, &errmsg);
       	if (rc != SQLITE_OK) {
		sqlite3_close(dbop->db3);
		die("dbop3_update failed: %s", errmsg);
	}
	if (sqlite3_changes(dbop->db3) == 0) {
		strbuf_clear(sql);
		strbuf_sprintf(sql, "insert into %s values ('%s', '%s', NULL)",
					dbop->tblname, key, dbop3_quote((char *)dat));
		rc = sqlite3_exec(dbop->db3, strbuf_value(sql), NULL, NULL, &errmsg);
		if (rc != SQLITE_OK) {
			sqlite3_close(dbop->db3);
			die("dbop3_updated failed: %s", errmsg);
		}
	}
	strbuf_release_tempbuf(sql);
}
Exemplo n.º 2
0
/**
 * Generate list begin tag.
 */
const char *
gen_list_begin(void)
{
    STATIC_STRBUF(sb);

    if (strbuf_empty(sb)) {
        strbuf_clear(sb);
        if (table_list) {
            if (enable_xhtml) {
                strbuf_sprintf(sb, "%s\n%s%s%s%s",
                               table_begin,
                               "<tr><th class='tag'>tag</th>",
                               "<th class='line'>line</th>",
                               "<th class='file'>file</th>",
                               "<th class='code'>source code</th></tr>");
            } else {
                strbuf_sprintf(sb, "%s\n%s%s%s%s",
                               table_begin,
                               "<tr><th nowrap='nowrap' align='left'>tag</th>",
                               "<th nowrap='nowrap' align='right'>line</th>",
                               "<th nowrap='nowrap' align='center'>file</th>",
                               "<th nowrap='nowrap' align='left'>source code</th></tr>");
            }
        } else {
            strbuf_puts(sb, verbatim_begin);
        }
    }
    return strbuf_value(sb);
}
Exemplo n.º 3
0
int cf_fmt_uint32_time_interval(const char **textp, const uint32_t *uintp)
{
  strbuf b = strbuf_alloca(60);
  uint32_t seconds = *uintp;
  if (seconds >= 7 * 24 * 60 * 60) {
    unsigned weeks = seconds / (7 * 24 * 60 * 60);
    seconds = seconds - weeks * (7 * 24 * 60 * 60);
    strbuf_sprintf(b, "%uw", weeks);
  }
  if (seconds >= 24 * 60 * 60) {
    unsigned days = seconds / (24 * 60 * 60);
    seconds = seconds - days * (24 * 60 * 60);
    strbuf_sprintf(b, "%ud", days);
  }
  if (seconds >= 60 * 60) {
    unsigned hours = seconds / (60 * 60);
    seconds = seconds - hours * (60 * 60);
    strbuf_sprintf(b, "%uh", hours);
  }
  if (seconds >= 60) {
    unsigned minutes = seconds / 60;
    seconds = seconds - minutes * 60;
    strbuf_sprintf(b, "%um", minutes);
  }
  if (seconds)
    strbuf_sprintf(b, "%us", seconds);
  if (strbuf_overrun(b))
    return CFINVALID;
  *textp = str_edup(strbuf_str(b));
  return CFOK;
}
Exemplo n.º 4
0
static int
overlay_queue_dump(overlay_txqueue *q)
{
  strbuf b = strbuf_alloca(8192);
  struct overlay_frame *f;
  strbuf_sprintf(b,"overlay_txqueue @ 0x%p\n",q);
  strbuf_sprintf(b,"  length=%d\n",q->length);
  strbuf_sprintf(b,"  maxLenght=%d\n",q->maxLength);
  strbuf_sprintf(b,"  latencyTarget=%d milli-seconds\n",q->latencyTarget);
  strbuf_sprintf(b,"  first=%p\n",q->first);
  f=q->first;
  while(f) {
    strbuf_sprintf(b,"    %p: ->next=%p, ->prev=%p\n",
		   f,f->next,f->prev);
    if (f==f->next) {
      strbuf_sprintf(b,"        LOOP!\n"); break;
    }
    f=f->next;
  }
  strbuf_sprintf(b,"  last=%p\n",q->last);
  f=q->last;
  while(f) {
    strbuf_sprintf(b,"    %p: ->next=%p, ->prev=%p\n",
		   f,f->next,f->prev);
    if (f==f->prev) {
      strbuf_sprintf(b,"        LOOP!\n"); break;
    }
    f=f->prev;
  }
  DEBUG(strbuf_str(b));
  return 0;
}
Exemplo n.º 5
0
/**
 * Generate beginning of generic page
 *
 *	@param[in]	title	title of this page
 *	@param[in]	place	#SUBDIR: this page is in sub directory <br>
 *			#TOPDIR: this page is in the top directory
 *	@param[in]	use_frameset
 *			use frameset document type or not
 *	@param[in]	header_item
 *			item which should be inserted into the header
 */
static const char *
gen_page_generic_begin(const char *title, int place, int use_frameset, const char *header_item)
{
    STATIC_STRBUF(sb);
    const char *dir = NULL;

    switch (place) {
    case TOPDIR:
        dir = "";
        break;
    case SUBDIR:
        dir = "../";
        break;
    case CGIDIR:
        dir = "$basedir/";	/* decided by the CGI script */
        break;
    }
    strbuf_clear(sb);
    if (enable_xhtml) {
        /*
         * Since some browser cannot treat "<?xml...>", we don't
         * write the declaration as long as XHTML1.1 is not required.
         */
        if (strict_xhtml) {
            strbuf_puts_nl(sb, "<?xml version='1.0' encoding='ISO-8859-1'?>");
            strbuf_sprintf(sb, "<?xml-stylesheet type='text/css' href='%sstyle.css'?>\n", dir);
        }
        /*
         * If the --frame option are specified then we take
         * 'XHTML 1.0 Frameset' for index.html
         * and 'XHTML 1.0 Transitional' for other files,
         * else if the config variable 'xhtml_version' is
         * set to '1.1' then we take 'XHTML 1.1',
         * else 'XHTML 1.0 Transitional'.
         */
        if (use_frameset)
            strbuf_puts_nl(sb, "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Frameset//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd'>");
        else if (!Fflag && strict_xhtml)
            strbuf_puts_nl(sb, "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>");
        else
            strbuf_puts_nl(sb, "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
    }
    strbuf_puts_nl(sb, html_begin);
    strbuf_puts_nl(sb, html_head_begin);
    strbuf_puts(sb, html_title_begin);
    strbuf_puts(sb, title);
    strbuf_puts_nl(sb, html_title_end);
    strbuf_sprintf(sb, "<meta name='robots' content='noindex,nofollow'%s>\n", empty_element);
    strbuf_sprintf(sb, "<meta name='generator' content='GLOBAL-%s'%s>\n", get_version(), empty_element);
    if (enable_xhtml) {
        strbuf_sprintf(sb, "<meta http-equiv='Content-Style-Type' content='text/css'%s>\n", empty_element);
        strbuf_sprintf(sb, "<link rel='stylesheet' type='text/css' href='%sstyle.css'%s>\n", dir, empty_element);
    }
    if (header_item)
        strbuf_puts(sb, header_item);		/* internal use */
    if (html_header)
        strbuf_puts(sb, html_header);		/* --html-header=file */
    strbuf_puts(sb, html_head_end);
    return strbuf_value(sb);
}
Exemplo n.º 6
0
static strbuf strbuf_build_http_response(strbuf sb, const struct http_response *h)
{
  strbuf_sprintf(sb, "HTTP/1.0 %03u %s\r\n", h->result_code, httpResultString(h->result_code));
  strbuf_sprintf(sb, "Content-type: %s\r\n", h->content_type);
  strbuf_sprintf(sb, "Content-length: %llu\r\n", h->content_length);
  strbuf_puts(sb, "\r\n");
  if (h->body)
    strbuf_puts(sb, h->body);
  return sb;
}
Exemplo n.º 7
0
/**
 * Generate beginning of form (@CODE{\<form\>})
 *
 *	@param[in]	target	target attribute or @VAR{NULL} for no target.
 */
const char *
gen_form_begin(const char *target)
{
    STATIC_STRBUF(sb);

    strbuf_clear(sb);
    strbuf_sprintf(sb, "<form method='get' action='%s'", fix_attr_value(action));
    if (Fflag && target)
        strbuf_sprintf(sb, " target='%s'", fix_attr_value(target));
    strbuf_puts(sb, ">");
    return strbuf_value(sb);
}
Exemplo n.º 8
0
void _debug_cli_parsed(struct __sourceloc __whence, const struct cli_parsed *parsed)
{
  DEBUG_argv("command", parsed->argc, parsed->args);
  strbuf b = strbuf_alloca(1024);
  int i;
  for (i = 0; i < parsed->labelc; ++i) {
    const struct labelv *lab = &parsed->labelv[i];
    strbuf_sprintf(b, " %s=%s", alloca_toprint(-1, lab->label, lab->len), alloca_str_toprint(lab->text));
  }
  if (parsed->varargi >= 0)
    strbuf_sprintf(b, " varargi=%d", parsed->varargi); 
  DEBUGF("parsed%s", strbuf_str(b));
}
Exemplo n.º 9
0
/**
 * Generate image tag (@CODE{\<img\>})
 *
 *	@param[in]	where	Where is the icon directory? <br>
 *			#CURRENT: current directory <br>
 *			#PARENT: parent directory
 *	@param[in]	file	icon file without suffix.
 *	@param[in]	alt	alt string (the @CODE{alt} attribute is always added)
 *
 *	@note Images are assumed to be in the @FILE{icons} or @FILE{../icons} directory, only.
 */
const char *
gen_image(int where, const char *file, const char *alt)
{
    STATIC_STRBUF(sb);
    const char *dir = (where == PARENT) ? "../icons" : "icons";

    strbuf_clear(sb);
    if (enable_xhtml)
        strbuf_sprintf(sb, "<img class='icon' src='%s/%s.%s' alt='[%s]'%s>",
                       dir, file, icon_suffix, fix_attr_value(alt), empty_element);
    else
        strbuf_sprintf(sb, "<img src='%s/%s.%s' alt='[%s]' %s%s>",
                       dir, file, icon_suffix, fix_attr_value(alt), icon_spec, empty_element);
    return strbuf_value(sb);
}
Exemplo n.º 10
0
char *http_response_to_string(const struct http_response *response, size_t *n)
{
    char *buf = NULL;
    size_t size = 0, offset = 0;

    if (response->version == HTTP_09) {
        /* HTTP/0.9 doesn't have a Status-Line or headers. See
           http://www.w3.org/Protocols/HTTP/AsImplemented.html. */
        return Strdup("");
    } else {
        const char *version;
        char *header_str;

        if (response->version == HTTP_10)
            version = "HTTP/1.0";
        else
            version = "HTTP/1.1";

        header_str = http_header_to_string(response->header, NULL);
        strbuf_sprintf(&buf, &size, &offset, "%s %d %s\r\n%s\r\n",
            version, response->code, response->phrase, header_str);
        free(header_str);
    }

    if (n != NULL)
        *n = offset;

    return buf;
}
Exemplo n.º 11
0
static char *make_nonce(const struct timeval *tv)
{
    char *buf = NULL;
    size_t size = 0, offset = 0;
    MD5_CTX md5;
    unsigned char hashbuf[MD5_DIGEST_LENGTH];
    char hash_hex[MD5_DIGEST_LENGTH * 2 + 1];
    char time_buf[32];

    /* Crash if someone forgot to call http_digest_init_secret. */
    if (!secret_initialized)
        bye("Server secret not initialized for Digest authentication. Call http_digest_init_secret.");

    Snprintf(time_buf, sizeof(time_buf), "%lu.%06lu",
        (long unsigned) tv->tv_sec, (long unsigned) tv->tv_usec);

    MD5_Init(&md5);
    MD5_Update(&md5, secret, sizeof(secret));
    MD5_Update(&md5, ":", 1);
    MD5_Update(&md5, time_buf, strlen(time_buf));
    MD5_Final(hashbuf, &md5);
    enhex(hash_hex, hashbuf, sizeof(hashbuf));

    strbuf_sprintf(&buf, &size, &offset, "%s-%s", time_buf, hash_hex);

    return buf;
}
Exemplo n.º 12
0
/**
 * print directory name.
 *
 *	@param[in]	level	0,1,2...
 *	@param[in]	path	path of the directory
 *	@param[in]	count	number of files in this directory
 */
static const char *
print_directory_name(int level, const char *path, int count)
{
	STATIC_STRBUF(sb);
	char tips[80];

	if (count > 1)
		snprintf(tips, sizeof(tips), "%d files", count);
	else
		snprintf(tips, sizeof(tips), "%d file", count);
	path = removedotslash(path);
	strbuf_clear(sb);
	if (table_flist)
		strbuf_puts(sb, fitem_begin);
	else if (!no_order_list)
		strbuf_puts(sb, item_begin);
	strbuf_puts(sb, gen_href_begin_with_title(level == 0 ? "files" : NULL,
			path2fid(path), HTML, NULL, tips));
	if (Iflag) {
		strbuf_puts(sb, gen_image(level == 0 ? CURRENT : PARENT, dir_icon, appendslash(path)));
		strbuf_puts(sb, quote_space);
	}
	strbuf_sprintf(sb, "%s/%s", lastpart(path), gen_href_end());
	if (table_flist)
		strbuf_puts(sb, fitem_end);
	else if (!no_order_list)
		strbuf_puts(sb, item_end);
	else
		strbuf_puts(sb, br);
	strbuf_putc(sb, '\n');
	return (const char *)strbuf_value(sb);
}
Exemplo n.º 13
0
int ssl_load_default_ca_certs(SSL_CTX *ctx)
{
    char buf[1024];
    char *bundlename;
    int n, rc;
    size_t size, offset;

    /* Get the executable's filename. */
    n = GetModuleFileName(GetModuleHandle(0), buf, sizeof(buf));
    if (n == 0 || n == sizeof(buf))
        return -1;

    bundlename = path_get_dirname(buf);
    bundlename = (char *) safe_realloc(bundlename, 1024);
    offset = strlen(bundlename);
    size = offset + 1;
    strbuf_sprintf(&bundlename, &size, &offset, "\\%s", NCAT_CA_CERTS_FILE);

    if (o.debug)
        logdebug("Using trusted CA certificates from %s.\n", bundlename);
    rc = SSL_CTX_load_verify_locations(ctx, bundlename, NULL);
    if (rc != 1) {
        if (o.debug)
            logdebug("Unable to load trusted CA certificates from %s: %s\n",
                bundlename, ERR_error_string(ERR_get_error(), NULL));
    }
    free(bundlename);

    return rc == 1 ? 0 : -1;
}
Exemplo n.º 14
0
/* Send a 407 Proxy Authenticate Required response. */
static int send_proxy_authenticate(struct fdinfo *fdn, int stale)
{
    char *buf = NULL;
    size_t size = 0, offset = 0;
    int n;

    strbuf_append_str(&buf, &size, &offset, "HTTP/1.0 407 Proxy Authentication Required\r\n");
    strbuf_append_str(&buf, &size, &offset, "Proxy-Authenticate: Basic realm=\"Ncat\"\r\n");
#if HAVE_HTTP_DIGEST
    {
        char *hdr;

        hdr = http_digest_proxy_authenticate("Ncat", stale);
        strbuf_sprintf(&buf, &size, &offset, "Proxy-Authenticate: %s\r\n", hdr);
        free(hdr);
    }
#endif
    strbuf_append_str(&buf, &size, &offset, "\r\n");

    if (o.debug > 1)
        logdebug("RESPONSE:\n%s", buf);

    n = send_string(fdn, buf);
    free(buf);

    return n;
}
Exemplo n.º 15
0
/**
 * Generate beginning of frame (@CODE{\<frame\>})
 *
 *	@param[in]	name	target (value for @CODE{name} and @CODE{id} attributes)
 *	@param[in]	src	value for @CODE{src} attribute
 */
const char *
gen_frame(const char *name, const char *src)
{
    STATIC_STRBUF(sb);

    strbuf_clear(sb);
    strbuf_sprintf(sb, "<frame name='%s' id='%s' src='%s'%s>", name, name, src, empty_element);
    return strbuf_value(sb);
}
Exemplo n.º 16
0
/**
 * Generate upper directory.
 *
 * Just returns the parent path of @a dir. (Adds @FILE{../} to it).
 */
const char *
upperdir(const char *dir)
{
    STATIC_STRBUF(sb);

    strbuf_clear(sb);
    strbuf_sprintf(sb, "../%s", dir);
    return strbuf_value(sb);
}
Exemplo n.º 17
0
/**
 * Generate beginning of frameset (@CODE{\<frameset\>})
 *
 *	@param[in]	contents	target
 */
const char *
gen_frameset_begin(const char *contents)
{
    STATIC_STRBUF(sb);

    strbuf_clear(sb);
    strbuf_sprintf(sb, "<frameset %s>", contents);
    return strbuf_value(sb);
}
Exemplo n.º 18
0
strbuf strbuf_append_exit_status(strbuf sb, int status)
{
  if (WIFEXITED(status))
    strbuf_sprintf(sb, "exited normally with status %u", WEXITSTATUS(status));
  else if (WIFSIGNALED(status)) {
    strbuf_sprintf(sb, "terminated by signal %u (%s)", WTERMSIG(status), strsignal(WTERMSIG(status)));
#ifdef WCOREDUMP
    if (WCOREDUMP(status))
      strbuf_puts(sb, " and dumped core");
#endif
  } else if (WIFSTOPPED(status))
    strbuf_sprintf(sb, "stopped by signal %u (%s)", WSTOPSIG(status), strsignal(WSTOPSIG(status)));
#ifdef WIFCONTINUED
  else if (WIFCONTINUED(status))
    strbuf_sprintf(sb, "continued by signal %u (SIGCONT)", SIGCONT);
#endif
  return sb;
}
Exemplo n.º 19
0
static int _log_prepare(int level, struct __sourceloc where)
{
    if (level == LOG_LEVEL_SILENT)
        return 0;
    if (strbuf_is_empty(&logbuf))
        strbuf_init(&logbuf, _log_buf, sizeof _log_buf);
    open_logging(); // Put initial INFO message at start of log file
#ifndef ANDROID
    const char *levelstr = "UNKWN:";
    switch (level) {
    case LOG_LEVEL_FATAL:
        levelstr = "FATAL:";
        break;
    case LOG_LEVEL_ERROR:
        levelstr = "ERROR:";
        break;
    case LOG_LEVEL_INFO:
        levelstr = "INFO:";
        break;
    case LOG_LEVEL_WARN:
        levelstr = "WARN:";
        break;
    case LOG_LEVEL_DEBUG:
        levelstr = "DEBUG:";
        break;
    }
    strbuf_sprintf(&logbuf, "%-6.6s ", levelstr);
#endif
    if (show_pid())
        strbuf_sprintf(&logbuf, "[%5u] ", getpid());
    if (show_time()) {
        struct timeval tv;
        if (gettimeofday(&tv, NULL) == -1) {
            strbuf_puts(&logbuf, "NOTIME______ ");
        } else {
            struct tm tm;
            char buf[20];
            if (strftime(buf, sizeof buf, "%T", localtime_r(&tv.tv_sec, &tm)) == 0)
                strbuf_puts(&logbuf, "EMPTYTIME___ ");
            else
                strbuf_sprintf(&logbuf, "%s.%03u ", buf, tv.tv_usec / 1000);
        }
    }
    if (where.file) {
        strbuf_sprintf(&logbuf, "%s", _trimbuildpath(where.file));
        if (where.line)
            strbuf_sprintf(&logbuf, ":%u", where.line);
        if (where.function)
            strbuf_sprintf(&logbuf, ":%s()", where.function);
        strbuf_putc(&logbuf, ' ');
    } else if (where.function) {
        strbuf_sprintf(&logbuf, "%s() ", where.function);
    }
    strbuf_putc(&logbuf, ' ');
    return 1;
}
Exemplo n.º 20
0
/**
 * Generate name tag (@CODE{\<a name='xxx'\>}).
 *
 * Uses attribute @CODE{'id'}, if is @NAME{XHTML}.
 */
const char *
gen_name_string(const char *name)
{
    STATIC_STRBUF(sb);

    strbuf_clear(sb);
    if (enable_xhtml) {
        /*
         * Since some browser cannot understand "<a id='xxx' />",
         * we put both of 'id=' and 'name=' as long as XHTML1.1
         * is not required. XHTML1.1 prohibit 'name='.
         */
        if (strict_xhtml)
            strbuf_sprintf(sb, "<a id='%s'></a>", name);
        else
            strbuf_sprintf(sb, "<a id='%s' name='%s'></a>", name, name);
    } else {
        strbuf_sprintf(sb, "<a name='%s'></a>", name);
    }
    return strbuf_value(sb);
}
Exemplo n.º 21
0
int logDump(int level, struct __sourceloc where, char *name, const unsigned char *addr, size_t len)
{
    char buf[100];
    size_t i;
    if (name)
        logMessage(level, where, "Dump of %s", name);
    for(i = 0; i < len; i += 16) {
        strbuf b = strbuf_local(buf, sizeof buf);
        strbuf_sprintf(b, "  %04x :", i);
        int j;
        for (j = 0; j < 16 && i + j < len; j++)
            strbuf_sprintf(b, " %02x", addr[i + j]);
        for (; j < 16; j++)
            strbuf_puts(b, "   ");
        strbuf_puts(b, "    ");
        for (j = 0; j < 16 && i + j < len; j++)
            strbuf_sprintf(b, "%c", addr[i+j] >= ' ' && addr[i+j] < 0x7f ? addr[i+j] : '.');
        logMessage(level, where, "%s", strbuf_str(b));
    }
    return 0;
}
Exemplo n.º 22
0
/**
 * Generate anchor begin tag (@CODE{\<a href='dir/file.suffix\#key'\>}).
 * (complete format)
 *
 *	@param[in]	dir	directory
 *	@param[in]	file	file
 *	@param[in]	suffix	suffix (file extension e.g. @CODE{'.txt'}). A @CODE{'.'} (dot) will be added.
 *	@param[in]	key	key
 *	@param[in]	title	@CODE{title='xxx'} attribute; if @VAR{NULL}, doesn't add it.
 *	@param[in]	target	@CODE{target='xxx'} attribute; if @VAR{NULL}, doesn't add it.
 *	@return		generated anchor tag
 *
 *	@note @a dir, @a file, @a suffix, @a key, @a target and @a title may be @VAR{NULL}.
 *	@note Single quote (@CODE{'}) characters are used with the attribute values.
 */
const char *
gen_href_begin_with_title_target(const char *dir, const char *file, const char *suffix, const char *key, const char *title, const char *target)
{
    STATIC_STRBUF(sb);

    strbuf_clear(sb);
    /*
     * Construct URL.
     * href='dir/file.suffix#key'
     */
    strbuf_puts(sb, "<a href='");
    if (file) {
        if (dir) {
            strbuf_puts(sb, dir);
            strbuf_putc(sb, '/');
        }
        strbuf_puts(sb, file);
        if (suffix) {
            strbuf_putc(sb, '.');
            strbuf_puts(sb, suffix);
        }
    }
    if (key) {
        strbuf_putc(sb, '#');
        /*
         * If the key starts with a digit, it assumed line number.
         * XHTML 1.1 profibits number as an anchor.
         */
        if (isdigit(*key))
            strbuf_putc(sb, 'L');
        strbuf_puts(sb, key);
    }
    strbuf_putc(sb, '\'');
    if (Fflag && target)
        strbuf_sprintf(sb, " target='%s'", fix_attr_value(target));
    if (title)
        strbuf_sprintf(sb, " title='%s'", fix_attr_value(title));
    strbuf_putc(sb, '>');
    return strbuf_value(sb);
}
Exemplo n.º 23
0
/*
 * Update tag files.
 */
static void
update(void)
{
	STATIC_STRBUF(command);

	strbuf_clear(command);
	strbuf_sprintf(command, "%s -u", global_path);
	if (vflag) {
		strbuf_putc(command, 'v');
		fprintf(stderr, "gscope: %s\n", strbuf_value(command));
	}
	system(strbuf_value(command));
}
Exemplo n.º 24
0
bool futil_generate_filename(const char *base_fmt, StrBuf *str)
{
  int i;

  for(i = 0; i < 10000; i++)
  {
    strbuf_reset(str);
    strbuf_sprintf(str, base_fmt, i);
    struct stat st;
    if(stat(str->b, &st) != 0) return true;
  }

  return false;
}
Exemplo n.º 25
0
char *http_request_to_string(const struct http_request *request, size_t *n)
{
    const char *path;
    char *buf = NULL;
    size_t size = 0, offset = 0;

    /* RFC 2616, section 5.1.2: "the absolute path cannot be empty; if none is
       present in the original URI, it MUST be given as "/" (the server
       root)." */
    path = request->uri.path;
    if (path[0] == '\0')
        path = "/";

    if (request->version == HTTP_09) {
        /* HTTP/0.9 doesn't have headers. See
           http://www.w3.org/Protocols/HTTP/AsImplemented.html. */
        strbuf_sprintf(&buf, &size, &offset, "%s %s\r\n", request->method, path);
    } else {
        const char *version;
        char *header_str;

        if (request->version == HTTP_10)
            version = " HTTP/1.0";
        else
            version = " HTTP/1.1";

        header_str = http_header_to_string(request->header, NULL);
        strbuf_sprintf(&buf, &size, &offset, "%s %s%s\r\n%s\r\n",
            request->method, path, version, header_str);
        free(header_str);
    }

    if (n != NULL)
        *n = offset;

    return buf;
}
Exemplo n.º 26
0
/**
 * Generate input radio tag (@CODE{\<input\>})
 *
 *	@note @a name, @a value, @a type and @a title may be @VAR{NULL}, thus only those
 *		with a non-@VAR{NULL} value will have there attribute added. <br>
 *		The argument names are the same as the corresponding HTML attribute names.
 *	@note Single quote (@CODE{'}) characters are used with the attribute values.
 */
const char *
gen_input_with_title_checked(const char *name, const char *value, const char *type, int checked, const char *title)
{
    STATIC_STRBUF(sb);

    strbuf_clear(sb);
    strbuf_puts(sb, "<input");
    if (type)
        strbuf_sprintf(sb, " type='%s'", type);
    if (name)
        strbuf_sprintf(sb, " name='%s' id='%s'", name, name);
    if (value)
        strbuf_sprintf(sb, " value='%s'", fix_attr_value(value));
    if (checked) {
        if (enable_xhtml)
            strbuf_puts(sb, " checked='checked'");
        else
            strbuf_puts(sb, " checked");
    }
    if (title)
        strbuf_sprintf(sb, " title='%s'", fix_attr_value(title));
    strbuf_sprintf(sb, "%s>", empty_element);
    return strbuf_value(sb);
}
Exemplo n.º 27
0
void vcf_misc_hdr_add_cmd(bcf_hdr_t *hdr, const char *cmdline, const char *cwd)
{
  char keystr[8], timestr[100];
  time_t tnow;
  time(&tnow);
  strftime(timestr, sizeof(timestr), "%Y%m%d-%H:%M:%S", localtime(&tnow));
  StrBuf sbuf;
  strbuf_alloc(&sbuf, 1024);
  strbuf_sprintf(&sbuf, "##mccortex_%s=<prev=\"NULL\",cmd=\"%s\",cwd=\"%s\","
                        "datetime=\"%s\",version="CTX_VERSION">\n",
                 hex_rand_str(keystr, sizeof(keystr)),
                 cmdline, cwd, timestr);
  bcf_hdr_append(hdr, sbuf.b);
  strbuf_dealloc(&sbuf);
}
Exemplo n.º 28
0
/**
 * completion_idutils: print completion list of specified @a prefix
 *
 *	@param[in]	dbpath	dbpath directory
 *	@param[in]	root	root directory
 *	@param[in]	prefix	prefix of primary key
 */
void
completion_idutils(const char *dbpath, const char *root, const char *prefix)
{
	FILE *ip;
	STRBUF *sb = strbuf_open(0);
	const char *lid = usable("lid");
	char *line, *p;

	if (prefix && *prefix == 0)	/* In the case global -c '' */
		prefix = NULL;
	/*
	 * make lid command line.
	 * Invoke lid with the --result=grep option to generate grep format.
	 */
	if (!lid)
		die("lid(idutils) not found.");
	strbuf_puts(sb, lid);
	strbuf_sprintf(sb, " --file=%s/ID", quote_shell(dbpath));
	strbuf_puts(sb, " --key=token");
	if (iflag)
		strbuf_puts(sb, " --ignore-case");
	if (prefix) {
		strbuf_putc(sb, ' ');
		strbuf_putc(sb, '"');
		strbuf_putc(sb, '^');
		strbuf_puts(sb, prefix);
		strbuf_putc(sb, '"');
	}
	if (debug)
		fprintf(stderr, "completion_idutils: %s\n", strbuf_value(sb));
	if (chdir(root) < 0)
		die("cannot move to '%s' directory.", root);
	if (!(ip = popen(strbuf_value(sb), "r")))
		die("cannot execute '%s'.", strbuf_value(sb));
	while ((line = strbuf_fgets(sb, ip, STRBUF_NOCRLF)) != NULL) {
		for (p = line; *p && *p != ' '; p++)
			;
		if (*p == '\0') {
			warning("Invalid line: %s", line);
			continue;
		}
		*p = '\0';
		puts(line);
	}
	if (pclose(ip) != 0)
		die("terminated abnormally (errno = %d).", errno);
	strbuf_close(sb);
}
Exemplo n.º 29
0
void
dbop3_put(DBOP *dbop, const char *p1, const char *p2, const char *p3) {
	int rc, len;
	char *errmsg = 0;
	STRBUF *sql = strbuf_open_tempbuf();

	if (!(len = strlen(p1)))
		die("primary key size == 0.");
	if (len > MAXKEYLEN)
		die("primary key too long.");
	if (dbop->stmt_put3 == NULL) {
		strbuf_sprintf(sql, "insert into %s values (?, ?, ?)", dbop->tblname);
		rc = sqlite3_prepare_v2(dbop->db3, strbuf_value(sql), -1, &dbop->stmt_put3, NULL);
		if (rc != SQLITE_OK) {
			die("dbop3_put prepare failed. (rc = %d, sql = %s)", rc, strbuf_value(sql));
		}
	}
	rc = sqlite3_bind_text(dbop->stmt_put3, 1, p1, -1, SQLITE_STATIC);
       	if (rc != SQLITE_OK) {
		die("dbop3_put 1 failed. (rc = %d)", rc);
	}
	rc = sqlite3_bind_text(dbop->stmt_put3, 2, p2, -1, SQLITE_STATIC);
       	if (rc != SQLITE_OK) {
		die("dbop3_put 2 failed. (rc = %d)", rc);
	}
	rc = sqlite3_bind_text(dbop->stmt_put3, 3, p3, -1, SQLITE_STATIC);
       	if (rc != SQLITE_OK) {
		die("dbop3_put 3 failed. (rc = %d)", rc);
	}
	rc = sqlite3_step(dbop->stmt_put3);
       	if (rc != SQLITE_DONE) {
		die("dbop3_put failed. (rc = %d)", rc);
	}
	rc = sqlite3_reset(dbop->stmt_put3);
       	if (rc != SQLITE_OK) {
		die("dbop3_put reset failed. (rc = %d)", rc);
	}
	if (dbop->writecount++ > DBOP_COMMIT_THRESHOLD) {
		dbop->writecount = 0;
		rc = sqlite3_exec(dbop->db3, "end transaction", NULL, NULL, &errmsg);
		if (rc != SQLITE_OK)
			die("end transaction error: %s", errmsg);
		rc = sqlite3_exec(dbop->db3, "begin transaction", NULL, NULL, &errmsg);
		if (rc != SQLITE_OK)
			die("begin transaction error: %s", errmsg);
	}
	strbuf_release_tempbuf(sql);
}
Exemplo n.º 30
0
char *http_header_to_string(const struct http_header *header, size_t *n)
{
    const struct http_header *p;
    char *buf = NULL;
    size_t size = 0, offset = 0;

    strbuf_append_str(&buf, &size, &offset, "");

    for (p = header; p != NULL; p = p->next)
        strbuf_sprintf(&buf, &size, &offset, "%s: %s\r\n", p->name, p->value);

    if (n != NULL)
        *n = offset;

    return buf;
}