Exemple #1
1
char * rb_proc_pid_cmdline(int pid) {
  char buffer[512];
  FILE *fp;
  char *cmdline;
  size_t size = 0;
  sprintf(buffer, "/proc/%d/cmdline", pid);
  fp = fopen(buffer, "rb");
  if (!fp)
    return NULL;
  getdelim(&cmdline, &size, 0 , fp);
  fclose(fp);
  return cmdline;
}
TEST(stdio, getdelim_invalid) {
  FILE* fp = tmpfile();
  ASSERT_TRUE(fp != NULL);

  char* buffer = NULL;
  size_t buffer_length = 0;

  // The first argument can't be NULL.
  errno = 0;
  ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
  ASSERT_EQ(EINVAL, errno);

  // The second argument can't be NULL.
  errno = 0;
  ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
  ASSERT_EQ(EINVAL, errno);

  // The stream can't be closed.
  fclose(fp);
  errno = 0;
  ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
  // glibc sometimes doesn't set errno in this particular case.
#if defined(__BIONIC__)
  ASSERT_EQ(EBADF, errno);
#endif // __BIONIC__
}
TEST(stdio, getdelim) {
  FILE* fp = tmpfile();
  ASSERT_TRUE(fp != NULL);

  const char* line_written = "This  is a test";
  int rc = fprintf(fp, "%s", line_written);
  ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));

  rewind(fp);

  char* word_read = NULL;
  size_t allocated_length = 0;

  const char* expected[] = { "This ", " ", "is ", "a ", "test" };
  for (size_t i = 0; i < 5; ++i) {
    ASSERT_FALSE(feof(fp));
    ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
    ASSERT_GE(allocated_length, strlen(expected[i]));
    ASSERT_STREQ(word_read, expected[i]);
  }
  // The last read should have set the end-of-file indicator for the stream.
  ASSERT_TRUE(feof(fp));
  clearerr(fp);

  // getdelim returns -1 but doesn't set errno if we're already at EOF.
  // It should set the end-of-file indicator for the stream, though.
  errno = 0;
  ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
  ASSERT_EQ(errno, 0);
  ASSERT_TRUE(feof(fp));

  free(word_read);
  fclose(fp);
}
int
main (int argc, char **argv)
{
  FILE *f;
  char *line = NULL;
  size_t len = 0;
  ssize_t result;

  /* Create test file.  */
  f = fopen ("test-getdelim.txt", "wb");
  if (!f || fwrite ("anbcnd\0f", 1, 8, f) != 8 || fclose (f) != 0)
    {
      fputs ("Failed to create sample file.\n", stderr);
      remove ("test-getdelim.txt");
      return 1;
    }
  f = fopen ("test-getdelim.txt", "rb");
  if (!f)
    {
      fputs ("Failed to reopen sample file.\n", stderr);
      remove ("test-getdelim.txt");
      return 1;
    }

  /* Test initial allocation, which must include trailing NUL.  */
  result = getdelim (&line, &len, 'n', f);
  ASSERT (result == 2);
  ASSERT (strcmp (line, "an") == 0);
  ASSERT (2 < len);

  /* Test growth of buffer.  */
  free (line);
  line = malloc (1);
  len = 1;
  result = getdelim (&line, &len, 'n', f);
  ASSERT (result == 3);
  ASSERT (strcmp (line, "bcn") == 0);
  ASSERT (3 < len);

  /* Test embedded NULs and EOF behavior.  */
  result = getdelim (&line, &len, 'n', f);
  ASSERT (result == 3);
  ASSERT (memcmp (line, "d\0f", 4) == 0);
  ASSERT (3 < len);

  result = getdelim (&line, &len, 'n', f);
  ASSERT (result == -1);

  free (line);
  fclose (f);
  remove ("test-getdelim.txt");
  return 0;
}
Exemple #5
0
int
lr_next (struct linereader *lr)
{
  int n;

  n = getdelim (&lr->buf, &lr->bufsize, '\n', lr->fp);
  if (n < 0)
    return -1;

  ++lr->lineno;

  if (n > 1 && lr->buf[n - 2] == lr->escape_char && lr->buf[n - 1] == '\n')
    {
#if 0
      /* XXX Is this correct?  */
      /* An escaped newline character is substituted with a single <SP>.  */
      --n;
      lr->buf[n - 1] = ' ';
#else
      n -= 2;
#endif
    }

  lr->buf[n] = '\0';
  lr->bufact = n;
  lr->idx = 0;

  return 0;
}
Exemple #6
0
// ----------------------------------------------------------------
static int do_stream(char* file_name) {
	FILE* input_stream  = stdin;
	FILE* output_stream = stdout;

	if (strcmp(file_name, "-")) {
		input_stream = fopen(file_name, "r");
		if (input_stream == NULL) {
			perror(file_name);
			return 0;
		}
	}

	while (1) {
		char* line = NULL;
		size_t linecap = 0;
		ssize_t linelen = getdelim(&line, &linecap, '\n', input_stream);
		if (linelen <= 0) {
			break;
		}
		fputs(line, output_stream);
		free(line);
	}
	if (input_stream != stdin)
		fclose(input_stream);

	return 1;
}
Exemple #7
0
int main (int argc, char** argv) {
  //FILE* fp = popen(FROM("/sys/devices") SELECT SUBSYSTEM("tty"), "r");
  FILE* fp = popen(FROM("/sys/devices") SELECT SUBSYSTEM("usb") SELECTUP SUBSYSTEM("pci"), "r");

  if (!fp) {
    printf("popen() failed\n");
    return 1;
  }

  char* device = NULL;
  size_t n = 0;
  while (-1 != getdelim(&device, &n, '\0', fp)) {
    printf("%s\n", device);
  }

  free(device);

  if (ferror(fp)) {
    printf("error while reading\n");
    return 1;
  }

  if (-1 == pclose(fp)) {
    printf("pclose() failed\n");
    return 1;
  }
}
Exemple #8
0
/* 
 * get_free_count - get number of free bytes on a node
 */
long long get_free_count(int node)
{
  size_t len = 0;
  char *line = NULL;
  char fn[64];
  long long size = -1;
  FILE *f;

  sprintf(fn, "/sys/devices/system/node/node%d/meminfo", node);
  f = fopen(fn, "r");
  if (!f)
    return -1;
  while (getdelim(&line, &len, '\n', f) > 0) {
    char *end;
    char *s = strstr(line, "kB");
    if (!s)
      continue;
    --s;
    while (s > line && isspace(*s))
      --s;
    while (s > line && isdigit(*s))
      --s;
    if (strstr(line, "MemFree")) {
      size = strtoull(s, &end, 0) << 10;
      if (end == s)
        size = -1;
    }
  }
  fclose(f);
  free(line);
  return size;
}
Exemple #9
0
static int parse_arg(csiebox_client* client, int argc, char** argv) {
    if (argc != 2) {
        return 0;
    }
    FILE* file = fopen(argv[1], "r");
    if (!file) {
        return 0;
    }
    fprintf(stderr, "reading config...\n");
    size_t keysize = 20, valsize = 20;
    char* key = (char*)malloc(sizeof(char) * keysize);
    char* val = (char*)malloc(sizeof(char) * valsize);
    ssize_t keylen, vallen;
    int accept_config_total = 5;
    int accept_config[5] = {0, 0, 0, 0, 0};
    while ((keylen = getdelim(&key, &keysize, '=', file) - 1) > 0) {
        key[keylen] = '\0';
        vallen = getline(&val, &valsize, file) - 1;
        val[vallen] = '\0';
        fprintf(stderr, "config (%zd, %s)=(%zd, %s)\n", keylen, key, vallen, val);
        if (strcmp("name", key) == 0) {
            if (vallen <= sizeof(client->arg.name)) {
                strncpy(client->arg.name, val, vallen);
                accept_config[0] = 1;
            }
        } else if (strcmp("server", key) == 0) {
            if (vallen <= sizeof(client->arg.server)) {
                strncpy(client->arg.server, val, vallen);
                accept_config[1] = 1;
            }
        } else if (strcmp("user", key) == 0) {
            if (vallen <= sizeof(client->arg.user)) {
                strncpy(client->arg.user, val, vallen);
                accept_config[2] = 1;
            }
        } else if (strcmp("passwd", key) == 0) {
            if (vallen <= sizeof(client->arg.passwd)) {
                strncpy(client->arg.passwd, val, vallen);
                accept_config[3] = 1;
            }
        } else if (strcmp("path", key) == 0) {
            if (vallen <= sizeof(client->arg.path)) {
                strncpy(client->arg.path, val, vallen);
                accept_config[4] = 1;
            }
        }
    }
    free(key);
    free(val);
    fclose(file);
    int i, test = 1;
    for (i = 0; i < accept_config_total; ++i) {
        test = test & accept_config[i];
    }
    if (!test) {
        fprintf(stderr, "config error\n");
        return 0;
    }
    return 1;
}
Exemple #10
0
/*
 * get_largest_anon_set - return @pid's largest anonymous segment of memory
 */
int get_largest_anon_seg(int pid, void **va, unsigned long *ct)
{
  size_t len = 0;
  char *line = NULL;
  char fn[64];
  FILE *f;
  unsigned long long size;
  int ps = getpagesize();

  *ct = 0;

  sprintf(fn, "/proc/%d/numa_maps", pid);
  f = fopen(fn, "r");
  if (!f)
    return -1;
  while (getdelim(&line, &len, '\n', f) > 0) {
    char *end;
    char *s = strstr(line, "anon");
    if (!s)
      continue;
    while (!isdigit(*s))
      ++s;
    size = strtoull(s, &end, 0);
    if (end == s)
      size = 0;
    if (size > *ct) {
      *ct = size;
      *va = (void *)strtoull(line, NULL, 16);
    }
  }
  if (*ct == 0)
    return -1;
  return 0;
}
Exemple #11
0
char *
argv_iter (struct argv_iterator *ai, enum argv_iter_err *err)
{
    if (ai->fp)
    {
        ssize_t len = getdelim (&ai->tok, &ai->buf_len, '\0', ai->fp);
        if (len < 0)
        {
            *err = feof (ai->fp) ? AI_ERR_EOF : AI_ERR_READ;
            return NULL;
        }

        *err = AI_ERR_OK;
        ai->item_idx++;
        return ai->tok;
    }
    else
    {
        if (*(ai->p) == NULL)
        {
            *err = AI_ERR_EOF;
            return NULL;
        }
        else
        {
            *err = AI_ERR_OK;
            return *(ai->p++);
        }
    }
}
Exemple #12
0
void getdatacommand::run()
{
    FILE *fpipe;
    char* line = 0;
    size_t len = 0;
    QString string;

    if ( inRun != TRUE )
    {
        inRun = TRUE;

        fpipe = popen(theQuery.toUtf8(), "r");
        if(fpipe != NULL)
        {
            int findfile = 0;
            // delimiter is null character.
            while( getdelim(&line, &len, 0, fpipe) != -1 )
            {
                findfile++;
                string = QString::fromUtf8(line);
                theListBoxResults->addItem( string );
            }
            delete(line);
            pclose(fpipe);
            emit queryComplete(findfile);
        }

        inRun = FALSE;
    }
}
Exemple #13
0
bool files_parseDictionary(honggfuzz_t * hfuzz)
{
    FILE *fDict = fopen(hfuzz->dictionaryFile, "rb");
    if (fDict == NULL) {
        PLOG_W("Couldn't open '%s' - R/O mode", hfuzz->dictionaryFile);
        return false;
    }
    defer {
        fclose(fDict);
    };

    for (;;) {
        char *lineptr = NULL;
        size_t n = 0;
        ssize_t len = getdelim(&lineptr, &n, '\n', fDict);
        if (len == -1) {
            break;
        }
        if (n > 1 && lineptr[len - 1] == '\n') {
            lineptr[len - 1] = '\0';
            len--;
        }

        struct strings_t *str = (struct strings_t *)util_Malloc(sizeof(struct strings_t));
        str->len = util_decodeCString(lineptr);
        str->s = lineptr;
        hfuzz->dictionaryCnt += 1;
        TAILQ_INSERT_TAIL(&hfuzz->dictionaryq, str, pointers);

        LOG_D("Dictionary: loaded word: '%s' (len=%zu)", str->s, str->len);
    }
    LOG_I("Loaded %zu words from the dictionary", hfuzz->dictionaryCnt);
    return true;
}
Exemple #14
0
TEST(stdio, getdelim_directory) {
  FILE* fp = fopen("/proc", "r");
  ASSERT_TRUE(fp != NULL);
  char* word_read;
  size_t allocated_length;
  ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
  fclose(fp);
}
Exemple #15
0
static int read_distance_table(void)
{
	int nd, len;
	char *line = NULL;
	size_t linelen = 0;
	int maxnode = numa_max_node() + 1;
	int *table = NULL;
	int err = -1;

	for (nd = 0;; nd++) {
		char fn[100];
		FILE *dfh;
		sprintf(fn, "/sys/devices/system/node/node%d/distance", nd);
		dfh = fopen(fn, "r");
		if (!dfh) {
			if (errno == ENOENT)
				err = 0;
			if (!err && nd<maxnode)
				continue;
			else
				break;
		}
		len = getdelim(&line, &linelen, '\n', dfh);
		fclose(dfh);
		if (len <= 0)
			break;

		if (!table) {
			table = calloc(maxnode * maxnode, sizeof(int));
			if (!table) {
				errno = ENOMEM;
				break;
			}
		}

		parse_numbers(line, table + nd * maxnode);
	}
	free(line);
	if (err)  {
		numa_warn(W_distance,
			  "Cannot parse distance information in sysfs: %s",
			  strerror(errno));
		free(table);
		return err;
	}
	/* Update the global table pointer.  Race window here with
	   other threads, but in the worst case we leak one distance
	   array one time, which is tolerable. This avoids a
	   dependency on pthreads. */
	if (distance_table) {
		free(table);
		return 0;
	}
	distance_numnodes = maxnode;
	distance_table = table;
	return 0;
}
Exemple #16
0
static int parse_arg(csiebox_server* server, int argc, char** argv) {
	if (argc < 2) {
		return 0;
	}
	FILE* file = fopen(argv[1], "r");
	if (!file) {
		return 0;
	}
	fprintf(stderr, "reading config...\n");
	size_t keysize = 20, valsize = 20;
	char* key = (char*)malloc(sizeof(char) * keysize);
	char* val = (char*)malloc(sizeof(char) * valsize);
	ssize_t keylen, vallen;
	int accept_config_total = 4;
	int accept_config[4] = {0};
	while ((keylen = getdelim(&key, &keysize, '=', file) - 1) > 0) {
		key[keylen] = '\0';
		vallen = getline(&val, &valsize, file) - 1;
		val[vallen] = '\0';
		fprintf(stderr, "config (%zd, %s)=(%zd, %s)\n", keylen, key, vallen, val);
		if (strcmp("path", key) == 0) {
			if (vallen <= sizeof(server->arg.path)) {
				strncpy(server->arg.path, val, vallen);
				accept_config[0] = 1;
			}
		} else if (strcmp("account_path", key) == 0) {
			if (vallen <= sizeof(server->arg.account_path)) {
				strncpy(server->arg.account_path, val, vallen);
				accept_config[1] = 1;
			}
		} else if (strcmp("thread", key) == 0) {
			server->arg.thread_max = atoi(val);
			accept_config[2] = 1;
		} else if (strcmp("run_path", key) == 0) {
			if (vallen <= sizeof(server->arg.run_path)) {
				strncpy(server->arg.run_path, val, vallen);
				accept_config[3] = 1;
			}
		}
	}
	free(key);
	free(val);
	fclose(file);
	int i, test = 1;
	for (i = 0; i < accept_config_total; ++i) {
		test = test & accept_config[i];
	}
	if (!test) {
		fprintf(stderr, "config error\n");
		return 0;
	}
	// daemonize
	if (argc > 2 && strcmp(argv[2], "-d") == 0) server->arg.daemonize = 1;
	else server->arg.daemonize = 0;
	return 1;
}
Exemple #17
0
int main(int, char**) {
    ssize_t read;
    char* line = NULL;
    size_t len = 0;
    while((read = getdelim(&line, &len, '\0', stdin)) != -1) {
        puts(line);
    }
    free(line);
    return 0;
}
Exemple #18
0
void load_halos_from_file(char* fname, struct Catalogue *cat){
    /*
    Load catalogue of DM halos from specified file.
    */
    int success;
    char *buf = NULL;
    size_t len;
    FILE *f;
    
    // Count number of rows in file
    f = fopen(fname, "r");
    cat->nhalos = 0;
    while(getline(&buf, &len, f) != -1){
        (cat->nhalos)++;
    }
    printf("Found %d halos in %s.\n", cat->nhalos, fname);
    rewind(f); // Return to start of file
    
    // Allocate halo arrays
    cat->mhalo = malloc(sizeof(float) * (cat->nhalos));
    cat->z = malloc(sizeof(float) * (cat->nhalos));
    
    // Load catalogue into arrays (assumes only two columns, space-separated)
    for(int i=0; i < cat->nhalos; i++){
        // Halo mass (column 0)
        success = getdelim(&buf, &len, ' ', f);
        if(success){
            (cat->mhalo)[i] = strtof(buf, NULL);
        }
        
        // Redshift (column 1)
        success = getdelim(&buf, &len, '\n', f);
        if(success){
            (cat->z)[i] = strtof(buf, NULL);
        }
    }
    
    // Close file and free buffers
    free(buf);
    fclose(f);
}
Exemple #19
0
TEST(stdio, getdelim_invalid) {
  FILE* fp = tmpfile();

  char* buffer = NULL;
  size_t buffer_length = 0;

  // The first argument can't be NULL.
  errno = 0;
  ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
  ASSERT_EQ(errno, EINVAL);

  // The second argument can't be NULL.
  errno = 0;
  ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
  ASSERT_EQ(errno, EINVAL);

  // The stream can't be closed.
  fclose(fp);
  errno = 0;
  ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
  ASSERT_EQ(errno, EBADF);
}
Exemple #20
0
I getdelim_(S *s,I *n,I d,FILE *f)
{
  I m; S z;size_t o=*n;
  if(getdelim(s,&o,d,f)==-1){*n=0; R -1;}
  *n=o;
  m=strlenn(*s,*n);
  if(1<m && '\n'==(*s)[m-1] && '\r'==(*s)[m-2]) {
    (*s)[--m]='\0'; (*s)[m-1]='\n'; }
  z=strdupn(*s,m);
  free(*s);
  *s=z;
  R *n=m;
}
Exemple #21
0
// ----------------------------------------------------------------
char* mlr_get_line(FILE* input_stream, char rs) {
	char* line = NULL;
	size_t linecap = 0;
	ssize_t linelen = getdelim(&line, &linecap, rs, input_stream);
	if (linelen <= 0) {
		return NULL;
	}
	if (line[linelen-1] == '\n') { // chomp
		line[linelen-1] = 0;
		linelen--;
	}

	return line;
}
Exemple #22
0
void readconf (void)
{
    char *line;

    size_t linel;

    int linenum;

    FILE *ifh;

    char *p;

    int n;

    ifh = fopen (fname, "r");
    if (!ifh)
        complain (_("opening configuration file %s: %s"), fname, strerror (errno));

    line = NULL;
    linel = 0;
    linenum = 1;
    while (getdelim (&line, &linel, '\n', ifh) > 0)
    {
        struct change *ch = xmalloc (sizeof (struct change));

        char pos[20];

        sprintf (pos, _("line %d"), linenum);

        if ((p = strchr (line, '#')) != NULL)
            *p = '\0';
        p = line;
        while (isspace (*p))
            ++p;
        if (*p == '\0')
            continue;
        n = strcspn (p, " \t");
        if (n > IFNAMSIZ)
            complain (_("interface name too long at line %d"), line);
        memcpy (ch->ifname, p, n);
        ch->ifname[n] = 0;
        p += n;
        p += strspn (p, " \t");
        n = strspn (p, "0123456789ABCDEFabcdef:");
        p[n] = 0;
        addchange (p, ch, pos);
        linenum++;
    }
    fclose (ifh);
}
Exemple #23
0
// ----------------------------------------------------------------
char* mlr_get_cline(FILE* fp, char irs) {
	char* line = NULL;
	size_t linecap = 0;
	ssize_t linelen = getdelim(&line, &linecap, irs, fp);
	if (linelen <= 0) {
		return NULL;
	}
	if (line[linelen-1] == '\n') { // chomp
		line[linelen-1] = 0;
		linelen--;
	}

	return line;
}
Exemple #24
0
int test_getdelim()
{
    char buf[BUF_SIZE];
    FILE *f;
    int rc;
    int i;

    if (make_test_file(DATA_2, LINE_NUM_2) != 0) {
        printf("test_getdelim: failed to make test file\n");
        return 1;
    }

    f = fopen(TEST_FILE_NAME, "r");
    if (f == NULL) {
        rc = errno;
        printf("test_getdelim: failed to open temp file; error %i; %s\n", rc, strerror(rc));
        return 1;
    }

    for (i=0; i<LINE_NUM_2; ++i) {
        char *s = NULL;
        int num = 0;
        if (getdelim(&s, &num, ' ', f) == -1) {
            rc = errno;
            printf("test_getdelim: failed to read temp file; error %i; %s\n", rc, strerror(rc));
            return 1;
        }
        if (strcmp(s, DATA_2[i]) != 0) {
            printf("test_getdelim: expected: '%s'\n", DATA_2[i]);
            printf("test_getdelim: read:     '%s'\n", s);
            return 1;
        }
    }

    if (fclose(f) != 0) {
        rc = errno;
        printf("failed to close test file; error %i; %s\n", rc, strerror(rc));
        return 1;
    }

    if (unlink(TEST_FILE_NAME) != 0) {
        rc = errno;
        printf("failed to close test file; error %i; %s\n", rc, strerror(rc));
        return 1;
    }

    printf("getdelim - ok\n");
    return 0;
}
Exemple #25
0
TEST(stdio, getdelim_invalid) {
  FILE* fp = tmpfile();
  ASSERT_TRUE(fp != NULL);

  char* buffer = NULL;
  size_t buffer_length = 0;

  // The first argument can't be NULL.
  errno = 0;
  ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
  ASSERT_EQ(EINVAL, errno);

  // The second argument can't be NULL.
  errno = 0;
  ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
  ASSERT_EQ(EINVAL, errno);

  // The underlying fd can't be closed.
  ASSERT_EQ(0, close(fileno(fp)));
  errno = 0;
  ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
  ASSERT_EQ(EBADF, errno);
  fclose(fp);
}
Exemple #26
0
ssize_t mygetline(char **lineptr, size_t * n, FILE * stream)
{
	ssize_t mread;
	char *line = NULL;
	char *head = NULL;

	line = *lineptr;




	if ((mread = getline(&line, n, stream)) > 0) {
		head = line;
		line = line + mread - 1;


		if (head[0] == '\n') {
			*lineptr = head;
			return mread;
		}


		if (head[0] == '.' && head[1] == 'q') {
			return -1;
		}
		if (head[0] == '.' && head[1] == 'e') {
			return -1;
		}


		if (strcspn(head, ";") < (size_t) mread) {
			*lineptr = head;
			return mread;

		}


		if ((mread = getdelim(&line, n, ';', stream)) < 0) {
			fprintf(stderr, "ERROR in mygetline getdelm \n");
		}


	}


	*lineptr = head;
	return mread;
}
Exemple #27
0
ssize_t apol_getline(char **lineptr, size_t *n, FILE *stream)
{
#ifdef __ANDROID__

    char *ptr;
    size_t len;

    if (lineptr == NULL || n == NULL) {
        errno = EINVAL;
        return -1;
    }

    ptr = fgetln(stream, n);
    if (ptr == NULL) {
        return -1;
    }

    /* Free the original ptr */
    if (*lineptr != NULL) free(*lineptr);

    /* Add one more space for '\0' */
    len = n[0] + 1;

    /* Update the length */
    n[0] = len;

    /* Allocate a new buffer */
    *lineptr = malloc(len);
    if (*lineptr == NULL) {
        errno = ENOMEM;
        return -1;
    }

    /* Copy over the string */
    memcpy(*lineptr, ptr, len-1);

    /* Write the NULL character */
    (*lineptr)[len-1] = '\0';

    /* Return the length of the new buffer */
    return (ssize_t)len;

#else /* __ANDROID__ */

    return getdelim(lineptr, n, '\n', stream);

#endif /* __ANDROID__ */
}
Exemple #28
0
int main (int argc, char ** argv)
{
    FILE * cpuinfo = fopen("/proc/cpuinfo", "rb");
    char * arg = 0;
    size_t size = 0;

    if (!cpuinfo)
        return errno;

    while(getdelim(&arg, &size, 0, cpuinfo) != -1)
        puts(arg);

    free(arg);
    fclose(cpuinfo);
    return 0;
}
Exemple #29
0
static int _cs_analyze_content( struct state *s ) {

	if( s->analysis->utf8[0] == s->final_line_separator /* we've finished another line */ ) {

		ssize_t llen;
		s->lines  ++;

		rewind( s->cache );

		if( (llen = getdelim( &_line, &_blen, s->final_line_separator, s->cache )) > 0 )
			_analyze_line( s->analysis, _line, llen ); // ...whether empty or not!

		rewind( s->cache );
	}
	return ASTAT_CONTINUE;
}
Exemple #30
0
static int
handle_existing_file(char **path)
{
	size_t alen;
	ssize_t len;
	char buf[4];

	for (;;) {
		fprintf(stderr,
		    "replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ",
		    *path);
		if (fgets(buf, sizeof(buf), stdin) == NULL) {
			clearerr(stdin);
			printf("NULL\n(EOF or read error, "
			    "treating as \"[N]one\"...)\n");
			n_opt = 1;
			return -1;
		}
		switch (*buf) {
		case 'A':
			o_opt = 1;
			/* FALLTHROUGH */
		case 'y':
		case 'Y':
			(void)unlink(*path);
			return 1;
		case 'N':
			n_opt = 1;			
			/* FALLTHROUGH */
		case 'n':
			return -1;
		case 'r':
		case 'R':
			printf("New name: ");
			fflush(stdout);
			free(*path);
			*path = NULL;
			alen = 0;
			len = getdelim(path, &alen, '\n', stdin);
			if ((*path)[len - 1] == '\n')
				(*path)[len - 1] = '\0';
			return 0;
		default:
			break;
		}
	}
}