Exemplo n.º 1
0
static int getStat (int pid, stat_t *s)
{
  char f_status[64];
  char f_psinfo[64];
  char *buffer;

  pstatus_t *myStatus;
  psinfo_t *myInfo;

  sprintf (f_status, "/proc/%d/status", pid);
  sprintf (f_psinfo, "/proc/%d/psinfo", pid);

  buffer = malloc (sizeof (pstatus_t));
  memset (buffer, 0, sizeof (pstatus_t));
  read_file_contents (f_status, buffer, sizeof (pstatus_t));
  myStatus = (pstatus_t *) buffer;

  buffer = malloc (sizeof (psinfo_t));
  memset (buffer, 0, sizeof (psinfo_t));
  read_file_contents (f_psinfo, buffer, sizeof (psinfo_t));
  myInfo = (psinfo_t *) buffer;

  s->pid = myInfo->pr_pid;
  s->ppid = myInfo->pr_ppid;
  s->rss = myInfo->pr_rssize * 1024;
  s->stime = myStatus -> pr_stime.tv_sec;
  s->utime = myStatus -> pr_utime.tv_sec;

  sfree (myStatus);
  sfree (myInfo);

  return (1);
}
Exemplo n.º 2
0
static int thermal_sysfs_device_read (const char *dir, const char *name,
		void *user_data)
{
	char filename[256];
	char data[1024];
	int len;
	int ok = 0;

	if (device_list && ignorelist_match (device_list, name))
		return -1;

	len = snprintf (filename, sizeof (filename), "%s/%s/temp", dirname_sysfs, name);
	if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
		return -1;

	len = read_file_contents (filename, data, sizeof(data));
	if (len > 1 && data[--len] == '\n') {
		char *endptr = NULL;
		double temp;

		data[len] = 0;
		errno = 0;
		temp = strtod (data, &endptr) / 1000.0;

		if (endptr == data + len && errno == 0) {
			thermal_submit(name, TEMP, temp);
			++ok;
		}
	}

	len = snprintf (filename, sizeof (filename), "%s/%s/cur_state", dirname_sysfs, name);
	if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
		return -1;

	len = read_file_contents (filename, data, sizeof(data));
	if (len > 1 && data[--len] == '\n') {
		char *endptr = NULL;
		double state;

		data[len] = 0;
		errno = 0;
		state = strtod (data, &endptr);

		if (endptr == data + len && errno == 0) {
			thermal_submit(name, COOLING_DEV, state);
			++ok;
		}
	}

	return ok ? 0 : -1;
}
Exemplo n.º 3
0
/*
 * Render the layout with the file content passed as a partial with the key
 * "content".
 */
static void
render_with_layout(FILE *in_fp,
                   FILE *out_fp,
                   struct layout *layouts,
                   int num_layouts,
                   ctache_data_t *file_data)
{
    char *content = read_file_contents(in_fp);
    size_t content_len = strlen(content);
    ctache_data_t *content_data;
    content_data = ctache_data_create_string(content, content_len);
    ctache_data_hash_table_set(file_data, "content", content_data);
    ctache_data_t *layout_data = ctache_data_hash_table_get(file_data, LAYOUT);
    char *str = strdup(ctache_data_string_buffer(layout_data));
    char *layout_name = string_trim(str);
    free(str);
    char *layout = get_layout_content(layouts, num_layouts, layout_name);
    if (layout == NULL) {
        fprintf(stderr, "ERROR: Layout not found: \"%s\"\n", layout_name);
        abort();
    }
    size_t layout_len = strlen(layout);
    ctache_render_string(layout,
                         layout_len,
                         out_fp,
                         file_data,
                         ESCAPE_HTML,
                         DELIM_BEGIN,
                         DELIM_END);
    free(content);
    free(layout_name);
}
Exemplo n.º 4
0
std::vector<ScopePtr> Indexer::index_file(const unicode &path) {
    /*
     *  Indexes a file by running a parser over it and storing the
     *  resulting scopes in the database
     */

    return index_file(path, read_file_contents(path));
}
Exemplo n.º 5
0
static int thermal_procfs_device_read (const char *dir, const char *name,
		void *user_data)
{
	const char str_temp[] = "temperature:";
	char filename[256];
	char data[1024];
	int len;

	if (device_list && ignorelist_match (device_list, name))
		return -1;

	/**
	 * rechot ~ # cat /proc/acpi/thermal_zone/THRM/temperature
	 * temperature:             55 C
	 */
	
	len = snprintf (filename, sizeof (filename), "%s/%s/temperature", dirname_procfs, name);
	if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
		return -1;

	len = read_file_contents (filename, data, sizeof(data));
	if (len > sizeof(str_temp) && data[--len] == '\n' && !strncmp(data, str_temp, sizeof(str_temp)-1)) {
		char *endptr = NULL;
		double temp;
		double celsius, add;
		
		if (data[--len] == 'C') {
			add = 0;
			celsius = 1;
		} else if (data[len] == 'F') {
			add = -32;
			celsius = 5/9;
		} else if (data[len] == 'K') {
			add = -273.15;
			celsius = 1;
		} else
			return -1;

		while (len > 0 && data[--len] == ' ')
			;
		data[len + 1] = 0;

		while (len > 0 && data[--len] != ' ')
			;
		++len;

		errno = 0;
		temp = (strtod (data + len, &endptr) + add) * celsius;

		if (endptr != data + len && errno == 0) {
			thermal_submit(name, TEMP, temp);
			return 0;
		}
	}

	return -1;
}
Exemplo n.º 6
0
Arquivo: fork.c Projeto: VanL/zrt
int main(int argc, char **argv)
{
    int res;
    int datalen, datalen1;
    char testpath[PATH_MAX];
    snprintf(testpath, sizeof(testpath), "/test/%s", FILENAME_WITH_DYNAMIC_CONTENTS );
    fprintf(stderr, "%s\n", testpath);

    /*Read files at mountpoint*/
    char* contents1 = read_file_contents( testpath, &datalen1 );
    CMP_MEM_DATA(MOUNT_CONTENTS, contents1, strlen(MOUNT_CONTENTS) );

    char* contents = read_file_contents( FILENAME_WITH_DYNAMIC_CONTENTS, &datalen );
    CMP_MEM_DATA(MOUNT_CONTENTS, contents, strlen(MOUNT_CONTENTS) );

    TEST_OPERATION_RESULT(strcmp("1", getenv("new")), &res, res==0);

    struct stat st;
    TEST_OPERATION_RESULT( lstat("/dev/read-write", &st), &res, res==0 );
    TEST_OPERATION_RESULT( (st.st_mode&S_IFCHR)==S_IFCHR, &res, res==1 );
    
    res = zfork();
    free(contents);
    free(contents1);
    /*After fork it is expected that this file remains unchanged*/
    contents1 = read_file_contents( testpath, &datalen1 );
    CMP_MEM_DATA(MOUNT_CONTENTS, contents1, strlen(MOUNT_CONTENTS) );

    /*After fork it is expected that this file is changed*/
    contents = read_file_contents( FILENAME_WITH_DYNAMIC_CONTENTS, &datalen );
    CMP_MEM_DATA(REMOUNT_CONTENTS, contents, strlen(REMOUNT_CONTENTS) );

    /*After fork env var changed*/
    TEST_OPERATION_RESULT(strcmp("2", getenv("new")), &res, res==0);

    /*After fork channels mapping is changed*/
    TEST_OPERATION_RESULT( lstat("/dev/read-write", &st), &res, res==0 );
    TEST_OPERATION_RESULT( (st.st_mode&S_IFREG)==S_IFREG, &res, res==1 );

    free(contents);
    free(contents1);
    return 0;
}
void unit_test_decode_uuencode()
{
    delete_file("1489406.jpg");

    nf::download download({"alt.binaries.foobar"}, {"1", "2", "3"}, "", "test");
    nf::session session;
    session.on_send = [&](const std::string&) {};

    auto cmdlist = download.create_commands();

    cmdlist->submit_data_commands(session);
    cmdlist->receive_data_buffer(read_file_buffer("test_data/1489406.jpg-003.uuencode"));
    cmdlist->receive_data_buffer(read_file_buffer("test_data/1489406.jpg-001.uuencode"));
    cmdlist->receive_data_buffer(read_file_buffer("test_data/1489406.jpg-002.uuencode"));

    std::vector<std::unique_ptr<nf::action>> actions1;
    std::vector<std::unique_ptr<nf::action>> actions2;
    download.complete(*cmdlist, actions1);

    while (!actions1.empty())
    {
        for (auto& it : actions1)
        {
            it->perform();
            download.complete(*it, actions2);
        }
        actions1 = std::move(actions2);
        actions2 = std::vector<std::unique_ptr<nf::action>>();
    }

    download.commit();

    const auto& jpg = read_file_contents("1489406.jpg");
    const auto& ref = read_file_contents("test_data/1489406.jpg");
    BOOST_REQUIRE(jpg == ref);

    delete_file("1489406.jpg");

}
Exemplo n.º 8
0
/* Loads the authz config into *AUTHZ from the file at AUTHZ_FILE
   in repository at REPOS_PATH from the transaction TXN_NAME.  If GROUPS_FILE
   is set, the resulting *AUTHZ will be constructed from AUTHZ_FILE with
   global groups taken from GROUPS_FILE.  Using POOL for allocations. */
static svn_error_t *
get_authz_from_txn(svn_authz_t **authz, const char *repos_path,
                   const char *authz_file, const char *groups_file,
                   const char *txn_name, apr_pool_t *pool)
{
  svn_repos_t *repos;
  svn_fs_t *fs;
  svn_fs_txn_t *txn;
  svn_fs_root_t *root;
  svn_stream_t *authz_contents;
  svn_stream_t *groups_contents;
  svn_error_t *err;

  /* Open up the repository and find the transaction root */
  SVN_ERR(svn_repos_open3(&repos, repos_path, NULL, pool, pool));
  fs = svn_repos_fs(repos);
  SVN_ERR(svn_fs_open_txn(&txn, fs, txn_name, pool));
  SVN_ERR(svn_fs_txn_root(&root, txn, pool));

  /* Get the authz file contents. */
  SVN_ERR(read_file_contents(&authz_contents, authz_file, root, pool));

  /* Get the groups file contents if needed. */
  if (groups_file)
    SVN_ERR(read_file_contents(&groups_contents, groups_file, root, pool));
  else
    groups_contents = NULL;

  err = svn_repos_authz_parse(authz, authz_contents, groups_contents, pool);

  /* Add the filename to the error stack since the parser doesn't have it. */
  if (err != SVN_NO_ERROR)
    return svn_error_createf(err->apr_err, err,
                             "Error parsing authz file: '%s':", authz_file);

  return SVN_NO_ERROR;
}
// unit test case for Issue #32
void unit_test_decode_yenc_bug_32()
{
    delete_file("02252012paul-10w(WallPaperByPaul)[1280X800].jpg");

    nf::download download({"alt.binaries.wallpaper"}, {"1", "2"}, "", "test");
    nf::session session;
    session.on_send = [&](const std::string&) {};

    auto cmdlist = download.create_commands();

    cmdlist->submit_data_commands(session);
    cmdlist->receive_data_buffer(read_file_buffer("test_data/wallpaper.jpg-002.yenc"));
    cmdlist->receive_data_buffer(read_file_buffer("test_data/wallpaper.jpg-001.yenc"));

    std::vector<std::unique_ptr<nf::action>> actions1;
    std::vector<std::unique_ptr<nf::action>> actions2;
    download.complete(*cmdlist, actions1);

    while (!actions1.empty())
    {
        for (auto& it : actions1)
        {
            it->perform();
            download.complete(*it, actions2);
        }
        actions1 = std::move(actions2);
        actions2 = std::vector<std::unique_ptr<nf::action>>();
    }

    download.commit();

    const auto& jpg = read_file_contents("02252012paul-10w(WallPaperByPaul)[1280X800].jpg");
    const auto& ref = read_file_contents("test_data/wallpaper.jpg");
    BOOST_REQUIRE(jpg == ref);

    delete_file("02252012paul-10w(WallPaperByPaul)[1280X800].jpg");
}
Exemplo n.º 10
0
static void test_index_for_algorithm(struct index_algorithm *alg, const char *name)
{
	struct index *index;
	char *text;
	int ok;
	int nd;

	test_index_naive_callback_called = 0;

	index = index_create(alg, name);
	CU_ASSERT(index != NULL);

	ok = index_put_document(index,4,"This is a very long text.");
	CU_ASSERT(ok != 0);

	ok = index_put_document(index,12,"This is a short text.");
	CU_ASSERT(ok != 0);

	nd = index_find_documents(index,test_index_naive_callback,NULL,1,"very");
	CU_ASSERT(test_index_naive_callback_called == 1);
	CU_ASSERT(nd == 1);

	ok = index_put_document(index,20,zauberlehrling);
	CU_ASSERT(ok != 0);

	test_index_contains_all_suffixes(index, "This is a very long text.", 4);
	test_index_contains_all_suffixes(index, zauberlehrling, 20);

	ok = index_remove_document(index,4);
	CU_ASSERT(ok != 0);

	nd = index_find_documents(index,test_index_naive_callback2,NULL,1,"very");
	CU_ASSERT(nd == 0);

	text = read_file_contents("of-human-bondage.txt");
	CU_ASSERT(text != NULL);

	ok = index_put_document(index,32,text);
	CU_ASSERT(ok != 0);

	test_index_contains_all_suffixes(index, zauberlehrling, 20);
	test_index_contains_all_suffixes(index, text, 32);

	index_dispose(index);
	free(text);
}
Exemplo n.º 11
0
/* Reads a file which contains only a number (and optionally a trailing
 * newline) and parses that number. */
static int sysfs_file_to_buffer(char const *dir, /* {{{ */
                                char const *power_supply, char const *basename,
                                char *buffer, size_t buffer_size) {
  char filename[PATH_MAX];
  int status;

  ssnprintf(filename, sizeof(filename), "%s/%s/%s", dir, power_supply,
            basename);

  status = (int)read_file_contents(filename, buffer, buffer_size - 1);
  if (status < 0)
    return status;

  buffer[status] = '\0';

  strstripnewline(buffer);
  return 0;
} /* }}} int sysfs_file_to_buffer */
Exemplo n.º 12
0
void test_zlib(int argc, char **argv)
{
  std::string s;
  int level = 1;
  if (argc > 0) {
    if (argc > 1) {
      level = std::atoi(argv[1]);
    }
    gtcpp::string_buffer buf;
    gtcpp::posix_error_callback ec;
    read_file_contents(argv[0], buf, ec);
    s = std::string(buf.begin(), buf.end());
  } else {
    s = std::string(
      "abcdef-------------------------------------------------");
  }
  test_zlib_one(s, level);
}
Exemplo n.º 13
0
int main(int argc, char** argv) {
    
    char* buf = mem_alloc(1024*sizeof(char));
    if (!buf)
        die("falha ao alocar memória");
    
    struct status st = read_file_contents("d:\\toni\\temp\\EXTRACTs.txt", buf, 1024);
    if (!st.ok)
    	die(st.err_msg);

    buf[1024] = '\0';

    printf(buf);

    mem_free(buf);

    check_mem();
    
    return 0;

}
Exemplo n.º 14
0
static int getStatus (int pid, status_t *s)
{

  char f_psinfo[64];
  char *buffer; 


  psinfo_t *myInfo;

  sprintf (f_psinfo, "/proc/%d/psinfo", pid);

  buffer = malloc (sizeof (psinfo_t));
  memset (buffer, 0, sizeof (psinfo_t));
  read_file_contents (f_psinfo, buffer, sizeof (psinfo_t));
  myInfo = (psinfo_t *) buffer;

  sstrncpy (s->Name, myInfo->pr_fname, sizeof (myInfo->pr_fname));  
  s->Uid[1] = myInfo->pr_euid;
  s->Gid[1] = myInfo->pr_egid;
  
  sfree (myInfo);
  return (1);
}