int main(int argc, char *argv[]) { char *p; size_t len; while ((p = fgetln(stdin, &len)) != NULL) { (void)printf("%zu %s", len, p); free(p); } return 0; }
int storemail(char *from) { FILE *fp = NULL; time_t tval; int fd, eline; size_t len; char *line, *tbuf; if ((tbuf = strdup(_PATH_LOCTMP)) == NULL) merr(FATAL, "unable to allocate memory"); if ((fd = mkstemp(tbuf)) == -1 || !(fp = fdopen(fd, "w+"))) merr(FATAL, "unable to open temporary file"); (void)unlink(tbuf); free(tbuf); (void)time(&tval); (void)fprintf(fp, "From %s %s", from, ctime(&tval)); for (eline = 1, tbuf = NULL; (line = fgetln(stdin, &len));) { /* We have to NUL-terminate the line since fgetln does not */ if (line[len - 1] == '\n') line[len - 1] = '\0'; else { /* No trailing newline, so alloc space and copy */ if ((tbuf = malloc(len + 1)) == NULL) merr(FATAL, "unable to allocate memory"); memcpy(tbuf, line, len); tbuf[len] = '\0'; line = tbuf; } if (line[0] == '\0') eline = 1; else { if (eline && line[0] == 'F' && len > 5 && !memcmp(line, "From ", 5)) (void)putc('>', fp); eline = 0; } (void)fprintf(fp, "%s\n", line); if (ferror(fp)) break; } if (tbuf) free(tbuf); /* Output a newline; note, empty messages are allowed. */ (void)putc('\n', fp); (void)fflush(fp); if (ferror(fp)) merr(FATAL, "temporary file write error"); return(fd); }
static void head(FILE *fp, int cnt) { char *cp; size_t error, readlen; while (cnt && (cp = fgetln(fp, &readlen)) != NULL) { error = fwrite(cp, sizeof(char), readlen, stdout); if (error != readlen) err(1, "stdout"); cnt--; } }
/* * Read a sequence of <start>|<len> pairs from stdin, where the <start> * values are all >= than the previous <start>+<len> values, and copy * bytes from F0 to F1, replacing the indicated bytes with zeros. */ static void findstamps_binary(FILE * F0, FILE * F1) { int ch; int pos = 0, spos = 0, slen = 0; char * line; size_t linelen; do { /* Read a line from stdin */ if ((line = fgetln(stdin, &linelen)) == NULL) { if (feof(stdin)) { spos = INT_MAX; slen = 0; } else err(1, "fgetln"); } else { /* Convert to integer */ spos = strtol(line, &line, 0); line++; slen = strtol(line, NULL, 0); } /* Sanity check */ if (spos < pos) errx(1, "Stamp positions went backwards!"); /* Copy bytes */ do { /* Read a byte */ ch = getc(F0); /* Handle EOF / error */ if (ch == EOF) { if (feof(F0)) return; else err(1, "fgetc"); } /* Write the byte */ if (pos < spos) putc(ch, F1); else putc(0, F1); pos++; } while (pos < spos + slen); } while (1); }
ssize_t getline(char **lineptr, size_t *n, FILE *stream) { char *lptr; size_t len = 0; /* check for invalid arguments */ if (lineptr == NULL || n == NULL) { errno = EINVAL; return -1; } lptr = fgetln(stream, &len); if (lptr == NULL) { /* invalid stream */ errno = EINVAL; return -1; } /* * getline() returns a null byte ('\0') terminated C string, * but fgetln() returns characters without '\0' termination */ if (*lineptr == NULL) { *n = BUF_MIN; goto alloc_buf; } /* realloc the original pointer */ if (*n < len + 1) { free(*lineptr); *n = len + 1; alloc_buf: *lineptr = malloc(*n); if (*lineptr == NULL) { *n = 0; return -1; } } /* copy over the string */ memcpy(*lineptr, lptr, len); (*lineptr)[len] = '\0'; /* * getline() and fgetln() both return len including the * delimiter but without the null byte at the end */ return len; }
static char* lmtp_getline(FILE *fp) { char *buffer; size_t len; if ((buffer = fgetln(fp, &len)) != NULL) { if (len >= 2 && buffer[len-2] == '\r') buffer[len-2] = '\0'; buffer[len-1] = '\0'; } return buffer; }
/* * Read a sequence of increasing <line> values from stdin and * delete those lines from F0, writing the result to F1. */ static void unstamp_text(FILE * F0, FILE * F1) { int ch; /* Character read from F0 */ int linenum = 0; /* Current line number */ char * line; /* Line of text from stdin */ size_t linelen; /* Length of line from stdin */ int nextdelete; /* Next line to delete */ do { /* Read a line from stdin */ if ((line = fgetln(stdin, &linelen)) == NULL) { if (feof(stdin)) nextdelete = INT_MAX; else err(1, "fgetln"); } else { /* Convert to integer */ nextdelete = strtol(line, NULL, 0); } /* Sanity check */ if (nextdelete < linenum) errx(1, "Stamp positions went backwards!"); /* Read lines */ do { /* Handle one line */ do { /* Read a character */ ch = getc(F0); /* Handle EOF / error */ if (ch == EOF) { if (feof(F0)) return; else err(1, "fgetc"); } /* Write it, if we're not deleting the line */ if (linenum != nextdelete) putc(ch, F1); } while (ch != '\n'); linenum++; } while (linenum <= nextdelete); } while (1); }
static void showfile(int start) { FILE *f; char *p; size_t len; f = fopen("/tmp/status.txt", "r"); p = fgetln(f, &len); if (!p || !len) return; p[len - 1] = '\0'; drawstring(p, start, 0); fclose(f); }
/* * Eat all of the lines in the input file, attempting to categorize * them by their various flavors */ void eaterrors(int *r_errorc, Eptr **r_errorv) { Errorclass errorclass = C_SYNC; char *line; const char *inbuffer; size_t inbuflen; for (;;) { if ((inbuffer = fgetln(errorfile, &inbuflen)) == NULL) break; line = Calloc(inbuflen + 1, sizeof(char)); memcpy(line, inbuffer, inbuflen); line[inbuflen] = '\0'; wordvbuild(line, &cur_wordc, &cur_wordv); /* * for convenience, convert cur_wordv to be 1 based, instead * of 0 based. */ cur_wordv -= 1; if (cur_wordc > 0 && ((( errorclass = onelong() ) != C_UNKNOWN) || (( errorclass = cpp() ) != C_UNKNOWN) || (( errorclass = gcc45ccom() ) != C_UNKNOWN) || (( errorclass = pccccom() ) != C_UNKNOWN) || (( errorclass = richieccom() ) != C_UNKNOWN) || (( errorclass = lint0() ) != C_UNKNOWN) || (( errorclass = lint1() ) != C_UNKNOWN) || (( errorclass = lint2() ) != C_UNKNOWN) || (( errorclass = lint3() ) != C_UNKNOWN) || (( errorclass = make() ) != C_UNKNOWN) || (( errorclass = f77() ) != C_UNKNOWN) || ((errorclass = pi() ) != C_UNKNOWN) || (( errorclass = ri() )!= C_UNKNOWN) || (( errorclass = mod2() )!= C_UNKNOWN) || (( errorclass = troff() )!= C_UNKNOWN)) ) ; else errorclass = catchall(); if (cur_wordc) erroradd(cur_wordc, cur_wordv+1, errorclass, C_UNKNOWN); } #ifdef FULLDEBUG printf("%d errorentrys\n", nerrors); #endif arrayify(r_errorc, r_errorv, er_head); }
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__ */ }
static int grab_new_line_and_readchar(void) { size_t len; if (current->F) { current->ptr = fgetln(current->F, &len); if (current->ptr) { current->end = current->ptr + len; return *current->ptr++; } else { current->end = NULL; } } return EOF; }
static struct passwd * find_user(FILE *fp, char *uname) { size_t len; char *line; rewind(fp); while ((line = fgetln(fp, &len)) != NULL) { struct passwd *pw = parse_user(line, len); if (pw && strcmp(uname, pw->pw_name) == 0) { return pw; } } return NULL; }
static void read_zones(void) { FILE *fp; char *line; size_t len; int lineno; char *tlc, *coord, *file, *descr, *p; char contbuf[16]; struct continent *cont; fp = fopen(_PATH_ZONETAB, "r"); if (!fp) err(1, _PATH_ZONETAB); lineno = 0; while ((line = fgetln(fp, &len)) != NULL) { lineno++; if (line[len - 1] != '\n') errx(1, _PATH_ZONETAB ":%d: invalid format", lineno); line[len - 1] = '\0'; if (line[0] == '#') continue; tlc = strsep(&line, "\t"); if (strlen(tlc) != 2) errx(1, _PATH_ZONETAB ":%d: invalid country code `%s'", lineno, tlc); coord = strsep(&line, "\t"); file = strsep(&line, "\t"); p = strchr(file, '/'); if (p == NULL) errx(1, _PATH_ZONETAB ":%d: invalid zone name `%s'", lineno, file); contbuf[0] = '\0'; strncat(contbuf, file, p - file); cont = find_continent(contbuf); if (!cont) errx(1, _PATH_ZONETAB ":%d: invalid region `%s'", lineno, contbuf); descr = (line && *line) ? line : 0; add_zone_to_country(lineno, tlc, descr, file, cont); } fclose(fp); }
/* * Read the ISO 3166 country code database in _PATH_ISO3166 * (/usr/share/misc/iso3166). On error, exit via err(3). */ static void read_iso3166_table(void) { FILE *fp; char *s, *t, *name; size_t len; int lineno; struct country *cp; fp = fopen(_PATH_ISO3166, "r"); if (!fp) err(1, _PATH_ISO3166); lineno = 0; while ((s = fgetln(fp, &len)) != NULL) { lineno++; if (s[len - 1] != '\n') errx(1, _PATH_ISO3166 ":%d: invalid format", lineno); s[len - 1] = '\0'; if (s[0] == '#' || strspn(s, " \t") == len - 1) continue; /* Isolate the two-letter code. */ t = strsep(&s, "\t"); if (t == NULL || strlen(t) != 2) errx(1, _PATH_ISO3166 ":%d: invalid format", lineno); if (t[0] < 'A' || t[0] > 'Z' || t[1] < 'A' || t[1] > 'Z') errx(1, _PATH_ISO3166 ":%d: invalid code `%s'", lineno, t); name = s; cp = &countries[CODE2INT(t)]; if (cp->name) errx(1, _PATH_ISO3166 ":%d: country code `%s' multiply defined: %s", lineno, t, cp->name); cp->name = strdup(name); if (cp->name == NULL) errx(1, "malloc failed"); cp->tlc = strdup(t); if (cp->tlc == NULL) errx(1, "malloc failed"); } fclose(fp); }
static char * map_stdio_get_entry(void *hdl, char *key, size_t *len) { char *buf, *lbuf; size_t flen; char *keyp; char *valp; FILE *fp = hdl; char *result = NULL; lbuf = NULL; while ((buf = fgetln(fp, &flen))) { if (buf[flen - 1] == '\n') buf[flen - 1] = '\0'; else { if ((lbuf = malloc(flen + 1)) == NULL) err(1, NULL); memcpy(lbuf, buf, flen); lbuf[flen] = '\0'; buf = lbuf; } keyp = buf; while (isspace((int)*keyp)) ++keyp; if (*keyp == '\0' || *keyp == '#') continue; valp = keyp; strsep(&valp, " \t:"); if (valp == NULL || valp == keyp) continue; if (strcmp(keyp, key) != 0) continue; result = strdup(valp); if (result == NULL) err(1, NULL); *len = strlen(result); break; } free(lbuf); return result; }
int nombreDeNombresDuFichier(FILE *fichier) { int nbNombres = 0; char *ligne; size_t taille; char *pch; rewind(fichier); while ((ligne = fgetln(fichier, &taille))) { pch = strtok(ligne, " "); while (pch != NULL) { nbNombres++; pch = strtok (NULL, " "); } } return nbNombres; }
/* * Do it yourself getline() implementation */ ssize_t getline(char **lineptr, size_t *n, FILE *stream) { size_t len = 0; char *srcln = NULL; char *newlnptr = NULL; if (!(srcln = fgetln(stream, &len))) /* get line, bail on error */ return -1; if (len >= *n) { /* line is too big for buffer, must realloc */ if(!(newlnptr = realloc(*lineptr, len * 2))) /* double the buffer, bail on error */ return -1; *lineptr = newlnptr; *n = len * 2 ; } memcpy(*lineptr, srcln, len); return len; }
int LoadText_Direct(const char* path) { std::vector<double> items; int nrows = 0; FILE* fd = fopen(path, "r"); while (!feof(fd)) { size_t len = 0; const char* line = fgetln(fd, &len); if (line == nullptr) { break; } items.clear(); ProcessLine(items, line, len); ++nrows; } fclose(fd); return nrows; }
static void parse_pkg_install_conf(void) { struct config_variable *var; FILE *fp; char *line, *value; size_t len, var_len, i; fp = fopen(config_file, "r"); if (!fp) { if (errno != ENOENT) warn("Can't open '%s' for reading", config_file); return; } while ((line = fgetln(fp, &len)) != (char *) NULL) { if (line[len - 1] == '\n') --len; for (i = 0; (var = &config_variables[i])->name != NULL; ++i) { var_len = strlen(var->name); if (strncmp(var->name, line, var_len) != 0) continue; if (line[var_len] != '=') continue; line += var_len + 1; len -= var_len + 1; if (config_tmp_variables[i]) value = xasprintf("%s\n%.*s", config_tmp_variables[i], (int)len, line); else value = xasprintf("%.*s", (int)len, line); free(config_tmp_variables[i]); config_tmp_variables[i] = value; break; } } for (i = 0; (var = &config_variables[i])->name != NULL; ++i) { if (config_tmp_variables[i] == NULL) continue; *var->var = config_tmp_variables[i]; config_tmp_variables[i] = NULL; } fclose(fp); }
/* * Print the value of variable from the file fname to stdout. */ char * var_get(const char *fname, const char *variable) { FILE *fp; char *line; size_t len; size_t varlen; char *value; size_t valuelen; size_t thislen; const char *p; varlen = strlen(variable); if (varlen == 0) return NULL; fp = fopen(fname, "r"); if (!fp) { if (errno != ENOENT) warn("var_get: can't open '%s' for reading", fname); return NULL; } value = NULL; valuelen = 0; while ((line = fgetln(fp, &len)) != (char *) NULL) { if (line[len - 1] == '\n') --len; if ((p=var_cmp(line, len, variable, varlen)) == NULL) continue; thislen = line+len - p; if (value) { value = xrealloc(value, valuelen+thislen+2); value[valuelen++] = '\n'; } else { value = xmalloc(thislen+1); } sprintf(value+valuelen, "%.*s", (int)thislen, p); valuelen += thislen; } (void) fclose(fp); return value; }
int prompt() { puts("Choose an option:"); puts(""); puts("1. Function a()"); puts("2. Function b()"); puts("3. Function c()"); puts("4. Function d()"); puts("5. Function e()"); puts("Q. Quit."); printf(">> "); char *response; size_t numchars = 1; response = fgetln(stdin, &numchars); return jump(response); }
char * grep_fgetln(file_t *f, size_t *l) { switch (f->type) { case FILE_STDIO: return fgetln(f->f, l); case FILE_MMAP: return mmfgetln(f->mmf, l); #ifndef NOZ case FILE_GZIP: return gzfgetln(f->gzf, l); #endif default: /* can't happen */ errx(2, "invalid file type"); } }
int scan(FILE *fp, struct passwd *pw) { static int lcnt; size_t len; char *p; p = fgetln(fp, &len); if (p == NULL) return (0); ++lcnt; /* * ``... if I swallow anything evil, put your fingers down my * throat...'' * -- The Who */ if (len > 0 && p[len - 1] == '\n') len--; if (len >= sizeof(line) - 1) { warnx("line #%d too long", lcnt); goto fmt; } memcpy(line, p, len); line[len] = '\0'; /* * Ignore comments: ^[ \t]*# */ for (p = line; *p != '\0'; p++) if (*p != ' ' && *p != '\t') break; if (*p == '#' || *p == '\0') { is_comment = 1; return(1); } else is_comment = 0; if (!__pw_scan(line, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) { warnx("at line #%d", lcnt); fmt: errno = EFTYPE; /* XXX */ error(pname); } return (1); }
int read_configline(FILE *config) { char *buf; size_t len; if ((buf = fgetln(config, &len))) { if (buf[len - 1] == '\n') buf[len - 1] = '\0'; else return (-1); /* all valid lines end in \n */ parse_configline(buf); } else { syslog_r(LOG_DEBUG, &sdata, "read_configline: fgetln (%m)"); return (-1); } return (0); }
void executeCommandAndWaitForResponse (const char *command, const char *response) { command = completeCommand(command); int res = fputs(command, file); if (res == EOF) { perror("Could not send command"); fclose(file); return; } while ((line = fgetln(file, &length))) { if (strstr(line, response) != NULL) { // Got expected response break; } memset(line, 0, sizeof(char)*4096); } }
/* * Reads searching patterns from a file and adds them with add_pattern(). */ static void read_patterns(const char *fn) { struct stat st; FILE *f; char *line; size_t len; if ((f = fopen(fn, "r")) == NULL) err(2, "%s", fn); if ((fstat(fileno(f), &st) == -1) || (S_ISDIR(st.st_mode))) { fclose(f); return; } while ((line = fgetln(f, &len)) != NULL) add_pattern(line, line[0] == '\n' ? 0 : len); if (ferror(f)) err(2, "%s", fn); fclose(f); }
static int mta_check_loop(FILE *fp) { char *buf, *lbuf; size_t len; uint32_t rcvcount = 0; int ret = 0; lbuf = NULL; while ((buf = fgetln(fp, &len))) { if (buf[len - 1] == '\n') buf[len - 1] = '\0'; else { /* EOF without EOL, copy and add the NUL */ lbuf = xmalloc(len + 1, "mta_check_loop"); memcpy(lbuf, buf, len); lbuf[len] = '\0'; buf = lbuf; } if (strchr(buf, ':') == NULL && !isspace((unsigned char)*buf)) break; if (strncasecmp("Received: ", buf, 10) == 0) { rcvcount++; if (rcvcount == MAX_HOPS_COUNT) { ret = 1; break; } } if (lbuf) { free(lbuf); lbuf = NULL; } } if (lbuf) free(lbuf); fseek(fp, SEEK_SET, 0); return ret; }
void cvs_read_config(char *name, int (*cb)(char *, int)) { FILE *fp; size_t len; int lineno; char *p, *buf, *lbuf, fpath[PATH_MAX]; (void)xsnprintf(fpath, sizeof(fpath), "%s/%s", current_cvsroot->cr_dir, name); if ((fp = fopen(fpath, "r")) == NULL) return; lbuf = NULL; lineno = 0; while ((buf = fgetln(fp, &len)) != NULL) { lineno++; if (buf[len - 1] == '\n') { buf[len - 1] = '\0'; } else { lbuf = xmalloc(len + 1); memcpy(lbuf, buf, len); lbuf[len] = '\0'; buf = lbuf; } p = buf; while (*p == ' ' || *p == '\t') p++; if (p[0] == '#' || p[0] == '\0') continue; if (cb(p, lineno) < 0) break; } free(lbuf); (void)fclose(fp); }
/* * Scan for indexable paths. */ static void pathgen(struct req *req) { FILE *fp; char *dp; size_t dpsz; if (NULL == (fp = fopen("manpath.conf", "r"))) { fprintf(stderr, "%s/manpath.conf: %s\n", MAN_DIR, strerror(errno)); pg_error_internal(); exit(EXIT_FAILURE); } while (NULL != (dp = fgetln(fp, &dpsz))) { if ('\n' == dp[dpsz - 1]) dpsz--; req->p = mandoc_realloc(req->p, (req->psz + 1) * sizeof(char *)); dp = mandoc_strndup(dp, dpsz); if ( ! validate_urifrag(dp)) { fprintf(stderr, "%s/manpath.conf contains " "unsafe path \"%s\"\n", MAN_DIR, dp); pg_error_internal(); exit(EXIT_FAILURE); } if (NULL != strchr(dp, '/')) { fprintf(stderr, "%s/manpath.conf contains " "path with slash \"%s\"\n", MAN_DIR, dp); pg_error_internal(); exit(EXIT_FAILURE); } req->p[req->psz++] = dp; } if ( req->p == NULL ) { fprintf(stderr, "%s/manpath.conf is empty\n", MAN_DIR); pg_error_internal(); exit(EXIT_FAILURE); } }
static void test_fgetln_multi(void) { struct file files[FILE_COUNT]; int i, l; for (i = 0; i < FILE_COUNT; i++) { char *str; str = strdup("A\n"); str[0] += i; files[i].lines = reallocarray(NULL, LINE_COUNT, sizeof(char *)); files[i].lines[0] = str; files[i].lines[1] = str; files[i].fp = pipe_feed("%s", files[i].lines, LINE_COUNT); } for (l = 0; l < LINE_COUNT; l++) { for (i = 0; i < FILE_COUNT; i++) { size_t len; char *str; str = fgetln(files[i].fp, &len); assert(str); assert(len == LINE_LEN); files[i].got_len = len; files[i].got_buf = str; } for (i = 0; i < FILE_COUNT; i++) { assert(memcmp(files[i].lines[l], files[i].got_buf, files[i].got_len) == 0); } } for (i = 0; i < LINE_COUNT; i++) pipe_close(files[i].fp); }