Example #1
0
static int cdb_read_attrs(const char *filename, t_read_attr_func cb, void *data)
{
    cdbi_t eod, klen, vlen;
    cdbi_t pos = 0;
    const char *key;
    const char *val;
    unsigned char buf[2048];
    std::FILE *f;

    if ((f = std::fopen(filename, "rb")) == NULL) {
	eventlog(eventlog_level_error, __FUNCTION__, "got error opening file '%s'", filename);
	return -1;
    }

    if (fget(f, buf, 2048, &pos, 2048)) goto err_fd;
    eod = cdb_unpack(buf);
    while(pos < eod) {
	if (fget(f, buf, 8, &pos, eod)) goto err_fd;
	klen = cdb_unpack(buf);
	vlen = cdb_unpack(buf + 4);
	if ((key = fcpy(f, klen, &pos, eod, buf)) == NULL) {
	    eventlog(eventlog_level_error, __FUNCTION__, "error reading attribute key");
	    goto err_fd;
	}

	key = xstrdup(key);

	if ((val = fcpy(f, vlen, &pos, eod, buf)) == NULL) {
	    eventlog(eventlog_level_error, __FUNCTION__, "error reading attribute val");
	    goto err_key;
	}

//	eventlog(eventlog_level_trace, __FUNCTION__, "read atribute : '%s' -> '%s'", key, val);
	if (cb(key, val, data))
	    eventlog(eventlog_level_error, __FUNCTION__, "got error from callback on account file '%s'", filename);
	xfree((void *)key);
    }

    std::fclose(f);
    return 0;

err_key:
    xfree((void *)key);

err_fd:
    std::fclose(f);
    return -1;
}
Example #2
0
int main( int argc, char *argv[])
{
  //printf("%s %s\n\n", argv[0], argv[1]);
  //assert( strcmp( argv[0], "cat") == 0 && "cant find cat");
  /*********************************************
   *
   *argv[0] always = ./a.out( execute file)
   *
   ********************************************
   */
  //assert( FALSE will print) 
  assert( strcmp( argv[1], "cat") == 0 && "cant find cat");

  if( argc < 3)
  {
    perror("no file for cat");
    exit(0);
  }
  else
  {
    int i = 2;
    while( i < argc)
    {
      FILE *fp;
      assert( (fp = fopen( argv[i], "r")) != NULL);
      fcpy( fp, stdout);
      fclose(fp);
      i++;
    }
  }
  return 0;
}
Example #3
0
File: parse.c Project: dolfly/nmdb
static void parse_stats(struct req_info *req)
{
	int i;
	uint64_t response[STATS_REPLY_SIZE];

	/* The packet is just the request, there's no payload. We need to
	 * reply with the stats structure.
	 * The response structure is just several uint64_t packed together,
	 * each one corresponds to a single value of the stats structure. */

	/* We define a macro to do the assignment easily; it's not nice, but
	 * it's more portable than using a packed struct */
	i = 0;
	#define fcpy(field) \
		do { response[i] = htonll(stats.field); i++; } while(0)


	fcpy(cache_get);
	fcpy(cache_set);
	fcpy(cache_del);
	fcpy(cache_cas);
	fcpy(cache_incr);

	fcpy(db_get);
	fcpy(db_set);
	fcpy(db_del);
	fcpy(db_cas);
	fcpy(db_incr);

	fcpy(cache_hits);
	fcpy(cache_misses);

	fcpy(db_hits);
	fcpy(db_misses);

	fcpy(msg_tipc);
	fcpy(msg_tcp);
	fcpy(msg_udp);
	fcpy(msg_sctp);

	fcpy(net_version_mismatch);
	fcpy(net_broken_req);
	fcpy(net_unk_req);

	fcpy(db_firstkey);
	fcpy(db_nextkey);

	req->reply_long(req, REP_OK, (unsigned char *) response,
			sizeof(response));

	return;
}