예제 #1
0
static char *sysctl_read_string(char *name, char *buf, size_t size)
{
	char fname[64];

	sysctl_fname(name, fname, sizeof(fname));
	return file_read_string(fname, buf, size);
}
예제 #2
0
int hw_version_string_get(char *hw_version_string, size_t size)
{
  char raw_hw_ver_string[DEVICE_HARDWARE_VERSION_MAX_LENGTH];
  u64 hw_version = 0;
  if (file_read_string(DEVICE_HARDWARE_VERSION_FILE_PATH,
                       raw_hw_ver_string,
                       sizeof(raw_hw_ver_string))
      == 0) {
    hw_version = (u64)strtod(raw_hw_ver_string, NULL);
    if (hw_version == 0 && errno == EINVAL) {
      piksi_log(LOG_ERR, "Error converting hardware version string: EINVAL");
      return -1;
    }
  } else {
    piksi_log(LOG_ERR,
              "Error reading hardware version string (buffer size: %d)",
              sizeof(raw_hw_ver_string));
    return -1;
  }
  u16 major_ver = (u16)((hw_version >> 16) & 0xFFFF);
  u16 minor_ver = (u16)((hw_version >> 0) & 0xFFFF);
  int written = snprintf(hw_version_string, size, "%d.%d", major_ver, minor_ver);
  if (written < 0) {
    piksi_log(LOG_ERR, "Error writing hardware version string to buffer");
    return -1;
  }
  if (int_to_sizet(written) >= size) {
    piksi_log(
      LOG_ERR,
      "Hardware version string truncated when writing to buffer (size of intended string: %d)",
      written);
    return -1;
  }
  return 0;
}
예제 #3
0
static char *modinfo_read_string(char *name, char *buf, size_t size)
{
	char fname[64];

	modinfo_fname(name, fname, sizeof(fname));
	return file_read_string(fname, buf, size);
}
예제 #4
0
u64 system_uptime_ms_get(void)
{
  char uptime_string[UPTIME_READ_MAX_LENGTH];
  u64 uptime_ms = 0;
  if (file_read_string(PROC_UPTIME_FILE_PATH, uptime_string, sizeof(uptime_string)) == 0) {
    uptime_ms = (u64)(1e3 * strtod(uptime_string, NULL));
  }
  return uptime_ms;
}
예제 #5
0
bool file_read_value(char *file_path)
{
  /* Accommodate the terminating null char fgets always adds and a newline */
  char val_char[3];
  if (file_read_string(file_path, val_char, sizeof(val_char)) != 0) {
    return false;
  }

  return ('1' == val_char[0]);
}
예제 #6
0
void file_read_header(FILE *archive, codes_array_t *codes, int64_t *data_size){
	unsigned char i;
	int j;
	short codes_count;
	fread(&codes_count, sizeof(codes_count),1,archive);
	memset(codes->code, 0, sizeof(codes->code)); 
	for (j=0;j< codes_count; j++){
		fread(&i, sizeof(i), 1, archive);
		codes->code[i] = file_read_string(archive);
	}
	fread(data_size, sizeof(*data_size),1,archive);
}
예제 #7
0
파일: main.c 프로젝트: af-inet/RCS
int main(int argc, char *argv[]){	
	char *filename = argv[1];
	if(filename){
		int l = strlen(filename);
		char *input = file_read_string(filename);
		if(input){
			eval(input);	
		}else{
			printf("Could not read file '%s'. \n",filename);
		}
	}else{
		printf("No filename given, try 'rcs <filename>'. \n");
	}
    return 0;
}
예제 #8
0
int get_value_main(void *parent, const char *controller, const char *cgroup,
		const char *key, struct ucred p, struct ucred r, char **value)
{
	char path[MAXPATHLEN];

	if (!sane_cgroup(cgroup)) {
		nih_error("%s: unsafe cgroup", __func__);
		return -1;
	}

	if (!compute_pid_cgroup(r.pid, controller, cgroup, path, NULL)) {
		nih_error("%s: Could not determine the requested cgroup", __func__);
		return -1;
	}

	/* Check access rights to the cgroup directory */
	if (!may_access(r.pid, r.uid, r.gid, path, O_RDONLY)) {
		nih_error("%s: Pid %d may not access %s\n", __func__, r.pid, path);
		return -1;
	}

	/* append the filename */
	if (strlen(path) + strlen(key) + 2 > MAXPATHLEN) {
		nih_error("%s: filename too long for cgroup %s key %s", __func__, path, key);
		return -1;
	}

	strncat(path, "/", MAXPATHLEN-1);
	strncat(path, key, MAXPATHLEN-1);

	/* Check access rights to the file itself */
	if (!may_access(r.pid, r.uid, r.gid, path, O_RDONLY)) {
		nih_error("%s: Pid %d may not access %s\n", __func__, r.pid, path);
		return -1;
	}

	/* read and return the value */
	*value = file_read_string(parent, path);
	if (!*value) {
		nih_error("%s: Failed to read value from %s", __func__, path);
		return -1;
	}

	nih_info(_("Sending to client: %s"), *value);
	return 0;
}
예제 #9
0
u16 sbp_sender_id_get(void)
{
  u16 sbp_sender_id = SBP_SENDER_ID;

  char sbp_sender_id_string[32];
  if (file_read_string(SBP_SENDER_ID_FILE_PATH, sbp_sender_id_string, sizeof(sbp_sender_id_string))
      == 0) {
    unsigned long ul_value = strtoul(sbp_sender_id_string, NULL, 10);
    if (ul_value > UINT16_MAX) {
      piksi_log(LOG_WARNING,
                "invalid value for SBP sender id: %s (from file '%s')",
                sbp_sender_id_string,
                SBP_SENDER_ID_FILE_PATH);
    }
    sbp_sender_id = (u16)ul_value;
  }

  return sbp_sender_id;
}
예제 #10
0
int hw_revision_string_get(char *hw_revision_string, size_t size)
{
  const char *s = NULL;

  char raw_hw_rev_string[DEVICE_HARDWARE_REVISION_MAX_LENGTH];
  u16 hw_revision = 0;
  if (file_read_string(DEVICE_HARDWARE_REVISION_FILE_PATH,
                       raw_hw_rev_string,
                       sizeof(raw_hw_rev_string))
      == 0) {
    hw_revision = (u16)strtod(raw_hw_rev_string, NULL);
    if (hw_revision == 0 && errno == EINVAL) {
      piksi_log(LOG_ERR, "Error converting hardware revision string: EINVAL");
      return -1;
    }
  } else {
    piksi_log(LOG_ERR,
              "Error reading hardware revision string (buffer size: %d)",
              sizeof(raw_hw_rev_string));
    return -1;
  }

  switch (hw_revision) {
  case IMAGE_HARDWARE_UNKNOWN: s = "Unknown"; break;
  case IMAGE_HARDWARE_V3_MICROZED: s = "Piksi Multi MicroZed"; break;
  case IMAGE_HARDWARE_V3_EVT1: s = "Piksi Multi EVT1"; break;
  case IMAGE_HARDWARE_V3_EVT2: s = "Piksi Multi EVT2"; break;
  case IMAGE_HARDWARE_V3_PROD: s = "Piksi Multi"; break;
  default: s = "Invalid"; break;
  }

  if (strlen(s) >= size) {
    piksi_log(LOG_ERR,
              "Hardware revision string too large for buffer (size of intended string: %d)",
              strlen(s));
    return -1;
  }
  strncpy(hw_revision_string, s, size);
  return 0;
}
예제 #11
0
/* Read the items in a Vorbis comment packet. For Ogg files, the file must
 * be located on a page start, for other files, the beginning of the comment
 * data (i.e., the vendor string length). Returns total size of the
 * comments, or 0 if there was a read error.
 */
long read_vorbis_tags(int fd, struct mp3entry *id3, 
    long tag_remaining)
{
    struct file file;
    char *buf = id3->id3v2buf;
    int32_t comment_count;
    int32_t len;
    long comment_size = 0;
    int buf_remaining = sizeof(id3->id3v2buf) + sizeof(id3->id3v1buf);
    int i;
    
    if (!file_init(&file, fd, id3->codectype, tag_remaining))
    {
        return 0;
    }
    
    /* Skip vendor string */

    if (!file_read_int32(&file, &len) || (file_read(&file, NULL, len) < 0))
    {
        return 0;
    }

    if (!file_read_int32(&file, &comment_count))
    {
        return 0;
    }
    
    comment_size += 4 + len + 4;

    for (i = 0; i < comment_count && file.packet_remaining > 0; i++)
    {
        char name[TAG_NAME_LENGTH];
        int32_t read_len;

        if (!file_read_int32(&file, &len))
        {
            return 0;
        }
        
        comment_size += 4 + len;
        read_len = file_read_string(&file, name, sizeof(name), '=', len);
        
        if (read_len < 0)
        {
            return 0;
        }

        len -= read_len;
        read_len = file_read_string(&file, id3->path, sizeof(id3->path), -1, len);

        if (read_len < 0)
        {
            return 0;
        }

        logf("Vorbis comment %d: %s=%s", i, name, id3->path);

        /* Is it an embedded cuesheet? */
        if (!strcasecmp(name, "CUESHEET"))
        {
            id3->has_embedded_cuesheet = true;
            id3->embedded_cuesheet.pos = lseek(file.fd, 0, SEEK_CUR) - read_len;
            id3->embedded_cuesheet.size = len;
            id3->embedded_cuesheet.encoding = CHAR_ENC_UTF_8;
        }
        else
        {
            len = parse_tag(name, id3->path, id3, buf, buf_remaining,
                TAGTYPE_VORBIS);
        }

        buf += len;
        buf_remaining -= len;
    }

    /* Skip to the end of the block (needed by FLAC) */
    if (file.packet_remaining)
    {
        if (file_read(&file, NULL, file.packet_remaining) < 0)
        {
            return 0;
        }
    }

    return comment_size;
}
예제 #12
0
int device_uuid_get(char *str, size_t str_size)
{
  return file_read_string(DEVICE_UUID_FILE_PATH, str, str_size);
}