int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term) { int ch; if (feof(fp)) return EOF; strbuf_reset(sb); flockfile(fp); while ((ch = getc_unlocked(fp)) != EOF) { if (!strbuf_avail(sb)) strbuf_grow(sb, 1); sb->buf[sb->len++] = ch; if (ch == term) break; } funlockfile(fp); if (ch == EOF && sb->len == 0) return EOF; sb->buf[sb->len] = '\0'; return 0; }
/* * 0: ok * -1: parse error * -2: line is just "-" */ static int ldif_read_ad(FILE *s, GString *lhs) { int c; for (;;) { switch ( c = getc_unlocked(s)) { case ':': if (ferror(s)) syserr(); return 0; case EOF: fputs("Error: Unexpected EOF.\n", stderr); return -1; case '\r': if (fgetc(s) != '\n') return -1; /* fall through */ case '\n': if (lhs->len) { if ( (c = fgetc(s)) == ' ') /* folded line */ break; ungetc(c, s); if (lhs->len == 1 && lhs->str[0] == '-') return -2; } fputs("Error: Unexpected EOL.\n", stderr); return -1; case 0: fputs("Error: Null byte not allowed.\n", stderr); return -1; default: fast_g_string_append_c(lhs, c); } } }
/* * Read and return the next char from a stream reader * - update pos, line, column */ static int file_reader_next_char(reader_t *reader) { assert(reader->is_stream); if (reader->current == EOF) { return EOF; } if (reader->current == '\n') { reader->line ++; reader->column = 0; } // getc_unlocked is unsafe in multithreading applications // but it's much faster. #if defined(MINGW) reader->current = getc(reader->input.stream); #else reader->current = getc_unlocked(reader->input.stream); #endif reader->pos ++; reader->column ++; return reader->current; }
static int framing_format_uncompress(FILE *infp, FILE *outfp, int skip_magic) { const size_t max_data_len = MAX_DATA_LEN; const size_t max_uncompressed_data_len = MAX_UNCOMPRESSED_DATA_LEN; size_t data_len; size_t uncompressed_data_len; char *data = malloc(max_data_len); char *uncompressed_data = malloc(max_uncompressed_data_len); int err = 1; if (data == NULL || uncompressed_data == NULL) { print_error("out of memory\n"); goto cleanup; } if (!skip_magic) { /* read the steam header */ if (read_data(data, sizeof(stream_header), infp) != 0) { goto cleanup; } if (memcmp(data, stream_header, sizeof(stream_header)) != 0) { print_error("Invalid stream identfier\n"); goto cleanup; } } for (;;) { int id = getc_unlocked(infp); if (id == EOF) { break; } data_len = getc_unlocked(infp); data_len |= getc_unlocked(infp) << 8; if (data_len == (size_t)EOF) { print_error("Unexpected end of file\n"); goto cleanup; } if (id == COMPRESSED_DATA_IDENTIFIER) { /* 4.2. Compressed data (chunk type 0x00) */ if (data_len < 4) { print_error("too short data length %lu\n", data_len); goto cleanup; } if (read_data(data, data_len, infp) != 0) { goto cleanup; } uncompressed_data_len = max_uncompressed_data_len; if (snappy_uncompress(data + 4, data_len - 4, uncompressed_data, &uncompressed_data_len)) { print_error("Invalid data: snappy_uncompress failed\n"); goto cleanup; } if (check_crc32c(uncompressed_data, uncompressed_data_len, data) != 0) { goto cleanup; } if (fwrite_unlocked(uncompressed_data, uncompressed_data_len, 1, outfp) != 1) { break; } } else if (id == UNCOMPRESSED_DATA_IDENTIFIER) { /* 4.3. Uncompressed data (chunk type 0x01) */ if (data_len < 4) { print_error("too short data length %lu\n", data_len); goto cleanup; } if (read_data(data, data_len, infp) != 0) { goto cleanup; } if (check_crc32c(data + 4, data_len - 4, data) != 0) { goto cleanup; } if (fwrite_unlocked(data + 4, data_len - 4, 1, outfp) != 1) { break; } } else if (id < 0x80) { /* 4.4. Reserved unskippable chunks (chunk types 0x02-0x7f) */ print_error("Unsupported identifier 0x%02x\n", id); goto cleanup; } else { /* 4.5. Reserved skippable chunks (chunk types 0x80-0xfe) */ fseek(infp, data_len, SEEK_CUR); } } /* check stream errors */ if (ferror_unlocked(infp)) { print_error("Failed to read a file: %s\n", strerror(errno)); goto cleanup; } if (ferror_unlocked(outfp)) { print_error("Failed to write a file: %s\n", strerror(errno)); goto cleanup; } err = 0; cleanup: free(data); free(uncompressed_data); return err; }
static int do_test (void) { const char blah[] = "BLAH"; char buf[strlen (blah) + 1]; FILE *fp, *f; const char *cp; char *wp; if ((fp = fdopen (fd, "w+")) == NULL) exit (1); flockfile (fp); f = fp; cp = blah; /* These tests deliberately use fwrite_unlocked with the size argument specified as 0, which results in "division by zero" warnings from the expansion of that macro (in code that is not evaluated for a size of 0). This applies to the tests of fread_unlocked below as well. */ DIAG_PUSH_NEEDS_COMMENT; DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdiv-by-zero"); if (ftello (fp) != 0 || fwrite_unlocked (blah, blah - blah, strlen (blah), f++) != 0 || f != fp + 1 || fwrite_unlocked ("", 5.0, 0, --f) != 0 || f != fp || fwrite_unlocked (cp++, 16, 0.25, fp) != 0 || cp != blah + 1 || fwrite_unlocked (--cp, 0.25, 16, fp) != 0 || cp != blah || fwrite_unlocked (blah, 0, -0.0, fp) != 0 || ftello (fp) != 0) { puts ("One of fwrite_unlocked tests failed"); exit (1); } DIAG_POP_NEEDS_COMMENT; if (fwrite_unlocked (blah, 1, strlen (blah) - 2, fp) != strlen (blah) - 2) { puts ("Could not write string into file"); exit (1); } if (putc_unlocked ('A' + 0x1000000, fp) != 'A') { puts ("putc_unlocked failed"); exit (1); } f = fp; cp = blah + strlen (blah) - 1; if (putc_unlocked (*cp++, f++) != 'H' || f != fp + 1 || cp != strchr (blah, '\0')) { puts ("fputc_unlocked failed"); exit (1); } if (ftello (fp) != (off_t) strlen (blah)) { printf ("Failed to write %zd bytes to temporary file", strlen (blah)); exit (1); } rewind (fp); f = fp; wp = buf; memset (buf, ' ', sizeof (buf)); /* See explanation above. */ DIAG_PUSH_NEEDS_COMMENT; DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdiv-by-zero"); if (ftello (fp) != 0 || fread_unlocked (buf, buf - buf, strlen (blah), f++) != 0 || f != fp + 1 || fread_unlocked (buf, 5.0, 0, --f) != 0 || f != fp || fread_unlocked (wp++, 16, 0.25, fp) != 0 || wp != buf + 1 || fread_unlocked (--wp, 0.25, 16, fp) != 0 || wp != buf || fread_unlocked (buf, 0, -0.0, fp) != 0 || ftello (fp) != 0 || memcmp (buf, " ", sizeof (buf)) != 0) { puts ("One of fread_unlocked tests failed"); exit (1); } DIAG_POP_NEEDS_COMMENT; if (fread_unlocked (buf, 1, strlen (blah) - 2, fp) != strlen (blah) - 2) { puts ("Could not read string from file"); exit (1); } if (getc_unlocked (fp) != 'A') { puts ("getc_unlocked failed"); exit (1); } f = fp; if (fgetc_unlocked (f++) != 'H' || f != fp + 1 || fgetc_unlocked (--f) != EOF || f != fp) { puts ("fgetc_unlocked failed"); exit (1); } if (ftello (fp) != (off_t) strlen (blah)) { printf ("Failed to read %zd bytes from temporary file", strlen (blah)); exit (1); } funlockfile (fp); fclose (fp); return 0; }
int ruserpass(const char *host, const char **aname, const char **apass) { char *hdir, *buf, *tmp; char myname[1024], *mydomain; int t, usedefault = 0; struct stat stb; /* Give up when running a setuid or setgid app. */ if ((getuid() != geteuid()) || getgid() != getegid()) return -1; hdir = getenv("HOME"); if (hdir == NULL) { /* If we can't get HOME, fail instead of trying ".", which is no improvement. */ return -1; } buf = alloca (strlen(hdir) + 8); strcpy(buf, hdir); strcat(buf, "/.netrc"); cfile = fopen(buf, "r"); if (cfile == NULL) { if (errno != ENOENT) printf("%s", buf); return (0); } /* No threads use this stream. */ #ifdef __UCLIBC_HAS_THREADS__ __fsetlocking (cfile, FSETLOCKING_BYCALLER); #endif if (gethostname(myname, sizeof(myname)) < 0) myname[0] = '\0'; mydomain = strchr(myname, '.'); if (mydomain==NULL) { mydomain=myname + strlen(myname); } next: while ((t = token())) switch(t) { case DEFAULT: usedefault = 1; /* FALL THROUGH */ case MACHINE: if (!usedefault) { if (token() != ID) continue; /* * Allow match either for user's input host name * or official hostname. Also allow match of * incompletely-specified host in local domain. */ if (strcasecmp(host, tokval) == 0) goto match; if ((tmp = strchr(host, '.')) != NULL && strcasecmp(tmp, mydomain) == 0 && strncasecmp(host, tokval, tmp - host) == 0 && tokval[tmp - host] == '\0') goto match; continue; } match: while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) { case LOGIN: if (token()) { if (*aname == 0) { char *newp; newp = malloc((unsigned) strlen(tokval) + 1); if (newp == NULL) { printf(_("out of memory")); goto bad; } *aname = strcpy(newp, tokval); } else { if (strcmp(*aname, tokval)) goto next; } } break; case PASSWD: if (strcmp(*aname, "anonymous") && fstat(fileno(cfile), &stb) >= 0 && (stb.st_mode & 077) != 0) { printf(_("Error: .netrc file is readable by others.")); printf(_("Remove password or make file unreadable by others.")); goto bad; } if (token() && *apass == 0) { char *newp; newp = malloc((unsigned) strlen(tokval) + 1); if (newp == NULL) { printf(_("out of memory")); goto bad; } *apass = strcpy(newp, tokval); } break; case ACCOUNT: #if 0 if (fstat(fileno(cfile), &stb) >= 0 && (stb.st_mode & 077) != 0) { printf("Error: .netrc file is readable by others."); printf("Remove account or make file unreadable by others."); goto bad; } if (token() && *aacct == 0) { *aacct = malloc((unsigned) strlen(tokval) + 1); (void) strcpy(*aacct, tokval); } #endif break; case MACDEF: #if 0 if (proxy) { (void) fclose(cfile); return (0); } while ((c=getc_unlocked(cfile)) != EOF && c == ' ' || c == '\t'); if (c == EOF || c == '\n') { printf("Missing macdef name argument.\n"); goto bad; } if (macnum == 16) { printf("Limit of 16 macros have already been defined\n"); goto bad; } tmp = macros[macnum].mac_name; *tmp++ = c; for (i=0; i < 8 && (c=getc_unlocked(cfile)) != EOF && !isspace(c); ++i) { *tmp++ = c; } if (c == EOF) { printf("Macro definition missing null line terminator.\n"); goto bad; } *tmp = '\0'; if (c != '\n') { while ((c=getc_unlocked(cfile)) != EOF && c != '\n'); } if (c == EOF) { printf("Macro definition missing null line terminator.\n"); goto bad; } if (macnum == 0) { macros[macnum].mac_start = macbuf; } else { macros[macnum].mac_start = macros[macnum-1].mac_end + 1; } tmp = macros[macnum].mac_start; while (tmp != macbuf + 4096) { if ((c=getc_unlocked(cfile)) == EOF) { printf("Macro definition missing null line terminator.\n"); goto bad; } *tmp = c; if (*tmp == '\n') { if (*(tmp-1) == '\0') { macros[macnum++].mac_end = tmp - 1; break; } *tmp = '\0'; } tmp++; } if (tmp == macbuf + 4096) { printf("4K macro buffer exceeded\n"); goto bad; } #endif break; default: printf(_("Unknown .netrc keyword %s"), tokval); break; } goto done; } done: (void) fclose(cfile); return (0); bad: (void) fclose(cfile); return (-1); }
static int parse_shadow(struct request *request) { char *fields[SPFIELDS]; register char *cp; int i; struct spwd *spwd; //Null everything out first memset(fields, 0, sizeof(fields)); if ((cp = strchr(request->buffer, '\n')) == NULL) { int tc; /* eat the entire line */ while ((tc = getc_unlocked(request->dbase->fp)) != '\n' && tc != EOF) { /* nothing to do */ } return ERANGE; } *cp = '\0'; for(cp = request->buffer, i = 0; *cp && i < sizeof fields / sizeof *fields; i++) { fields[i] = cp; while(*cp && *cp != ':') { cp++; } if(*cp) { *cp++ = '\0'; } } /* Not all shadow passwords are this big, so fill extra space w/ zeros Just make sure that we have at least a user and a password field Ideally: if (i != sizeof fields / sizeof *fields) */ if (i < 2) { return(EINVAL); } spwd = &request->record->spwd; spwd->sp_namp = fields[0]; spwd->sp_pwdp = fields[1] ? fields[1] : "xx"; spwd->sp_lstchg = fields[2] ? strtol(fields[2], 0, 10) : 0; spwd->sp_max = fields[3] ? strtol(fields[3], 0, 10) : 0; spwd->sp_min = fields[4] ? strtol(fields[4], 0, 10) : 0; spwd->sp_warn = fields[5] ? strtol(fields[5], 0, 10) : 0; spwd->sp_inact = fields[6] ? strtol(fields[6], 0, 10) : 0; spwd->sp_expire = fields[7] ? strtol(fields[7], 0, 10) : 0; spwd->sp_flag = fields[8] ? strtol(fields[8], 0, 10) : 0; DPRINTF(printf("Returning entry\n\tName: %s\n\tPasswd: %s\n\t", spwd->sp_namp, spwd->sp_pwdp)); DPRINTF(printf("Last Chng: %ld\n\tMax: %ld\n\tMin: %ld\n\tWarn: %ld\n\t", spwd->sp_lstchg, spwd->sp_max, spwd->sp_min, spwd->sp_warn)); DPRINTF(printf("Inact: %ld\n\tExpire: %ld\n\tFlag: %ld\n", spwd->sp_inact, spwd->sp_expire, spwd->sp_flag)); return EOK; }
int getc(FILE* file) { flockfile(file); int result = getc_unlocked(file); funlockfile(file); return result; }
/* Headers are of the format: * [ indent ] ">" start-offset "=" value * [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n" */ static XdgMimeMagicState _xdg_mime_magic_parse_magic_line (FILE *magic_file, XdgMimeMagicMatch *match) { XdgMimeMagicMatchlet *matchlet; int c; int end_of_file; int indent = 0; int bytes_read; assert (magic_file != NULL); /* Sniff the buffer to make sure it's a valid line */ c = getc_unlocked (magic_file); if (c == EOF) return XDG_MIME_MAGIC_EOF; else if (c == '[') { ungetc (c, magic_file); return XDG_MIME_MAGIC_SECTION; } else if (c == '\n') return XDG_MIME_MAGIC_MAGIC; /* At this point, it must be a digit or a '>' */ end_of_file = FALSE; if (isdigit (c)) { ungetc (c, magic_file); indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file); if (end_of_file) return XDG_MIME_MAGIC_EOF; if (indent == -1) return XDG_MIME_MAGIC_ERROR; c = getc_unlocked (magic_file); if (c == EOF) return XDG_MIME_MAGIC_EOF; } if (c != '>') return XDG_MIME_MAGIC_ERROR; matchlet = _xdg_mime_magic_matchlet_new (); matchlet->indent = indent; matchlet->offset = _xdg_mime_magic_read_a_number (magic_file, &end_of_file); if (end_of_file) { _xdg_mime_magic_matchlet_free (matchlet); return XDG_MIME_MAGIC_EOF; } if (matchlet->offset == -1) { _xdg_mime_magic_matchlet_free (matchlet); return XDG_MIME_MAGIC_ERROR; } c = getc_unlocked (magic_file); if (c == EOF) { _xdg_mime_magic_matchlet_free (matchlet); return XDG_MIME_MAGIC_EOF; } else if (c != '=') { _xdg_mime_magic_matchlet_free (matchlet); return XDG_MIME_MAGIC_ERROR; } /* Next two bytes determine how long the value is */ matchlet->value_length = 0; c = getc_unlocked (magic_file); if (c == EOF) { _xdg_mime_magic_matchlet_free (matchlet); return XDG_MIME_MAGIC_EOF; } matchlet->value_length = c & 0xFF; matchlet->value_length = matchlet->value_length << 8; c = getc_unlocked (magic_file); if (c == EOF) { _xdg_mime_magic_matchlet_free (matchlet); return XDG_MIME_MAGIC_EOF; } matchlet->value_length = matchlet->value_length + (c & 0xFF); matchlet->value = malloc (matchlet->value_length); /* OOM */ if (matchlet->value == NULL) { _xdg_mime_magic_matchlet_free (matchlet); return XDG_MIME_MAGIC_ERROR; } bytes_read = fread (matchlet->value, 1, matchlet->value_length, magic_file); if (bytes_read != matchlet->value_length) { _xdg_mime_magic_matchlet_free (matchlet); if (feof (magic_file)) return XDG_MIME_MAGIC_EOF; else return XDG_MIME_MAGIC_ERROR; } c = getc_unlocked (magic_file); if (c == '&') { matchlet->mask = malloc (matchlet->value_length); /* OOM */ if (matchlet->mask == NULL) { _xdg_mime_magic_matchlet_free (matchlet); return XDG_MIME_MAGIC_ERROR; } bytes_read = fread (matchlet->mask, 1, matchlet->value_length, magic_file); if (bytes_read != matchlet->value_length) { _xdg_mime_magic_matchlet_free (matchlet); if (feof (magic_file)) return XDG_MIME_MAGIC_EOF; else return XDG_MIME_MAGIC_ERROR; } c = getc_unlocked (magic_file); } if (c == '~') { matchlet->word_size = _xdg_mime_magic_read_a_number (magic_file, &end_of_file); if (end_of_file) { _xdg_mime_magic_matchlet_free (matchlet); return XDG_MIME_MAGIC_EOF; } if (matchlet->word_size != 0 && matchlet->word_size != 1 && matchlet->word_size != 2 && matchlet->word_size != 4) { _xdg_mime_magic_matchlet_free (matchlet); return XDG_MIME_MAGIC_ERROR; } c = getc_unlocked (magic_file); } if (c == '+') { matchlet->range_length = _xdg_mime_magic_read_a_number (magic_file, &end_of_file); if (end_of_file) { _xdg_mime_magic_matchlet_free (matchlet); return XDG_MIME_MAGIC_EOF; } if (matchlet->range_length == -1) { _xdg_mime_magic_matchlet_free (matchlet); return XDG_MIME_MAGIC_ERROR; } c = getc_unlocked (magic_file); } if (c == '\n') { /* We clean up the matchlet, byte swapping if needed */ if (matchlet->word_size > 1) { #if LITTLE_ENDIAN int i; #endif if (matchlet->value_length % matchlet->word_size != 0) { _xdg_mime_magic_matchlet_free (matchlet); return XDG_MIME_MAGIC_ERROR; } /* FIXME: need to get this defined in a <config.h> style file */ #if LITTLE_ENDIAN for (i = 0; i < matchlet->value_length; i = i + matchlet->word_size) { if (matchlet->word_size == 2) *((xdg_uint16_t *) matchlet->value + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->value + i))); else if (matchlet->word_size == 4) *((xdg_uint32_t *) matchlet->value + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->value + i))); if (matchlet->mask) { if (matchlet->word_size == 2) *((xdg_uint16_t *) matchlet->mask + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->mask + i))); else if (matchlet->word_size == 4) *((xdg_uint32_t *) matchlet->mask + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->mask + i))); } } #endif } matchlet->next = match->matchlet; match->matchlet = matchlet; return XDG_MIME_MAGIC_MAGIC; } _xdg_mime_magic_matchlet_free (matchlet); if (c == EOF) return XDG_MIME_MAGIC_EOF; return XDG_MIME_MAGIC_ERROR; }
void checker_read_file_by_line_ex( FILE *f, checker_error_func_t error_func, const char *name, char ***out_lines, size_t *out_lines_num) { unsigned char **lines = 0, **new_l = 0; size_t lines_u = 0, lines_a = 0, new_a = 0; unsigned char *buf = 0; size_t buf_u = 0, buf_a = 0; int c; *out_lines = 0; *out_lines_num = 0; if (!name) name = ""; while ((c = getc_unlocked(f)) != EOF) { if (!isspace(c) && c < ' ') { error_func(_("%s: invalid control character with code %d"), name, c); } if (buf_u == buf_a) { if (!buf_a) buf_a = 16; buf = xrealloc(buf, buf_a *= 2); } buf[buf_u++] = c; if (c == '\n') { if (buf_u == buf_a) { if (!buf_a) buf_a = 16; buf = xrealloc(buf, buf_a *= 2); } buf[buf_u] = 0; if (lines_u == lines_a) { if(!(new_a = lines_a * 2)) new_a = 16; XCALLOC(new_l, new_a); if (lines_a > 0) { memcpy(new_l, lines, lines_a * sizeof(new_l[0])); } free(lines); lines_a = new_a; lines = new_l; } lines[lines_u++] = xstrdup(buf); buf_u = 0; } } if (ferror_unlocked(f)) { fatal_CF(_("%s: input error"), name); } if (buf_u > 0) { if (buf_u == buf_a) { buf = xrealloc(buf, buf_a *= 2); } buf[buf_u] = 0; if (lines_u == lines_a) { if(!(new_a = lines_a * 2)) new_a = 16; XCALLOC(new_l, new_a); if (lines_a > 0) { memcpy(new_l, lines, lines_a * sizeof(new_l[0])); } free(lines); lines_a = new_a; lines = new_l; } lines[lines_u++] = buf; buf = 0; buf_u = buf_a = 0; } *out_lines = (char**) lines; *out_lines_num = lines_u; }
int main(int argc, char **argv) { FILE *fp; char bufchar; input_state_t input_state = IS_NORMAL; int exit_code = 0; int remove_whitespace = 0; int arg; if(argc < 2) usage(argv); for(arg=2; arg<argc; arg++) { switch(argv[arg][0]) { case 'r': remove_whitespace++; break; default: usage(argv); } } fp = fopen(argv[1], "r"); if(!fp) { fprintf(stderr, "Error opening %s\n", argv[1]); perror("fopen"); exit(1); } for(;;) { bufchar = getc_unlocked(fp); if (bufchar == EOF) break; switch(input_state) { case IS_NORMAL: if(bufchar == '/') { /* * Might be start of a comment. */ input_state = IS_SLASH; } else { if(!(remove_whitespace && isspace(bufchar))) { putchar_unlocked(bufchar); } } break; case IS_SLASH: switch(bufchar) { case '*': /* * Start of normal comment. */ input_state = IS_IN_COMMENT; break; case '/': /* * Start of 'to-end-of-line' comment. */ input_state = IS_IN_END_COMMENT; break; default: /* * Not the start of comment. Emit the '/' * we skipped last char in case we were * entering a comment this time, then the * current char. */ putchar_unlocked('/'); if(!(remove_whitespace && isspace(bufchar))) { putchar_unlocked(bufchar); } input_state = IS_NORMAL; break; } break; case IS_IN_COMMENT: if(bufchar == '*') { /* * Maybe ending comment... */ input_state = IS_STAR; } break; case IS_STAR: switch(bufchar) { case '/': /* * End of normal comment. */ input_state = IS_NORMAL; break; case '*': /* * Still could be one char away from end * of comment. */ break; default: /* * Still inside comment, no end in sight. */ input_state = IS_IN_COMMENT; break; } break; case IS_IN_END_COMMENT: if(bufchar == '\n') { /* * End of comment. Emit the newline if * appropriate. */ if(!remove_whitespace) { putchar_unlocked(bufchar); } input_state = IS_NORMAL; } break; } /* switch input_state */ } /* main read loop */ /* * Done. */ return(exit_code); }
int main(int argc, char **argv) { int c; int i; buffer_t buffer; boolean_t first = B_TRUE; boolean_t doall = B_FALSE; boolean_t same = B_FALSE; boolean_t newarg = B_FALSE; off_t offset = 0; off_t skip = 0; char *eptr; char *offstr = 0; input = stdin; (void) setlocale(LC_ALL, ""); (void) textdomain(TEXT_DOMAIN); while ((c = getopt(argc, argv, "A:bCcdDfFj:N:oOsSxXvt:")) != EOF) { switch (c) { case 'A': newarg = B_TRUE; if (strlen(optarg) > 1) { afmt = NULL; } switch (*optarg) { case 'o': afmt = "%07llo"; cfmt = " "; break; case 'd': afmt = "%07lld"; cfmt = " "; break; case 'x': afmt = "%07llx"; cfmt = " "; break; case 'n': /* * You could argue that the code should * use the same 7 spaces. Legacy uses 8 * though. Oh well. Better to avoid * gratuitous change. */ afmt = " "; cfmt = " "; break; default: afmt = NULL; break; } if (strlen(optarg) != 1) { afmt = NULL; } if (afmt == NULL) warnx(_("invalid address base, " "must be o, d, x, or n")); break; case 'b': add_out(&output_oct_b); break; case 'c': case 'C': add_out(&output_char); break; case 'f': add_out(&output_float); break; case 'F': add_out(&output_double); break; case 'd': add_out(&output_dec_w); break; case 'D': add_out(&output_dec_d); break; case 't': newarg = B_TRUE; do_type_string(optarg); break; case 'o': add_out(&output_oct_w); break; case 'O': add_out(&output_oct_d); break; case 's': add_out(&output_sig_w); break; case 'S': add_out(&output_sig_d); break; case 'x': add_out(&output_hex_w); break; case 'X': add_out(&output_hex_d); break; case 'v': doall = B_TRUE; break; case 'j': newarg = B_TRUE; skip = strtoll(optarg, &eptr, 0); if (*eptr == 'b') { skip <<= 9; /* 512 bytes */ eptr++; } else if (*eptr == 'k') { skip <<= 10; /* 1k */ eptr++; } else if (*eptr == 'm') { skip <<= 20; /* 1m */ eptr++; } else if (*eptr == 'g') { skip <<= 30; /* 1g */ eptr++; } if ((skip < 0) || (eptr[0] != 0)) { warnx(_("invalid skip count '%s' specified"), optarg); exit(1); } break; case 'N': newarg = B_TRUE; limit = strtoll(optarg, &eptr, 0); /* * POSIX doesn't specify this, but I think these * may be helpful. */ if (*eptr == 'b') { limit <<= 9; eptr++; } else if (*eptr == 'k') { limit <<= 10; eptr++; } else if (*eptr == 'm') { limit <<= 20; eptr++; } else if (*eptr == 'g') { limit <<= 30; eptr++; } if ((limit < 0) || (eptr[0] != 0)) { warnx(_("invalid byte count '%s' specified"), optarg); exit(1); } break; default: usage(); break; } } /* this finds the smallest power of two size we can use */ buffer.mask = (1 << (ffs(blocksize * 3) + 1)) - 1; buffer.data = memalign(16, buffer.mask + 1); if (buffer.data == NULL) { err(1, "memalign"); } /* * Wow. This option parsing is hideous. * * If the we've not seen a new option, and there is just one * operand, if it starts with a "+", then treat it as an * offset. Otherwise if two operands, and the second operand * starts with + or a digit, then it is an offset. */ if (!newarg) { if (((argc - optind) == 1) && (argv[optind][0] == '+')) { offstr = argv[optind]; argc--; } else if (((argc - optind) == 2) && (strchr("+0123456789", (argv[optind + 1][0])) != NULL)) { offstr = argv[optind + 1]; argc--; } } if (offstr) { int base = 0; int mult = 1; int l; if (*offstr == '+') { offstr++; } l = strlen(offstr); if ((strncmp(offstr, "0x", 2) == 0)) { afmt = "%07llx"; base = 16; offstr += 2; if (offstr[l - 1] == 'B') { offstr[l - 1] = 0; l--; mult = 512; } } else { base = 8; afmt = "%07llo"; if ((offstr[l - 1] == 'B') || (offstr[l - 1] == 'b')) { offstr[l - 1] = 0; l--; mult = 512; } if (offstr[l - 1] == '.') { offstr[l - 1] = 0; base = 10; afmt = "%07lld"; } } skip = strtoll(offstr, &eptr, base); if (*eptr != '\0') { errx(1, _("invalid offset string specified")); } skip *= mult; offset += skip; } /* * Allocate an array for all the input files. */ if (argc > optind) { files = calloc(sizeof (char *), argc - optind); for (i = 0; i < argc - optind; i++) { files[i] = argv[optind + i]; numfiles++; } input = next_input(); } else { input = stdin; } /* * We need to seek ahead. fseek would be faster. */ while (skip && (input != NULL)) { struct stat sbuf; /* * Only fseek() on regular files. (Others * we have to read(). */ if (fstat(fileno(input), &sbuf) < 0) { warn("fstat: %s", files[curfile-1]); input = next_input(); continue; } if (S_ISREG(sbuf.st_mode)) { /* * No point in seeking a file that is too * short to begin with. */ if (sbuf.st_size < skip) { skip -= sbuf.st_size; input = next_input(); continue; } if (fseeko(input, skip, SEEK_SET) < 0) { err(1, "fseek:%s", files[curfile-1]); } /* Done seeking. */ skip = 0; break; } /* * fgetc seems like it would be slow, but it uses * buffered I/O, so it should be fast enough. */ flockfile(input); while (skip) { if (getc_unlocked(input) == EOF) { funlockfile(input); if (ferror(input)) { warn("read: %s", files[curfile-1]); } input = next_input(); if (input != NULL) { flockfile(input); } break; } skip--; } if (input != NULL) funlockfile(input); } if (head == NULL) { add_out(&output_oct_w); } buffer.navail = 0; buffer.prod = 0; buffer.cons = 0; for (refill(&buffer); buffer.navail > 0; refill(&buffer)) { output_t *out; int mx; int j, k; /* * If this buffer was the same as last, then just * dump an asterisk. */ if ((!first) && (buffer.navail >= blocksize) && (!doall)) { j = buffer.cons; k = j - blocksize; for (i = 0; i < blocksize; i++) { if (buffer.data[j & buffer.mask] != buffer.data[k & buffer.mask]) { break; } j++; k++; } if (i == blocksize) { if (!same) { (void) fputs("*\n", stdout); same = B_TRUE; } buffer.navail -= blocksize; offset += blocksize; buffer.cons += blocksize; buffer.cons &= buffer.mask; continue; } } first = B_FALSE; same = B_FALSE; mx = (buffer.navail > blocksize) ? blocksize : buffer.navail; for (out = head; out != NULL; out = out->next) { if (out == head) { /*LINTED E_SEC_PRINTF_VAR_FMT*/ (void) printf(afmt, offset); } else { (void) fputs(cfmt, stdout); } for (i = 0, j = buffer.cons; i < mx; i += out->width) { out->func(&buffer, j); j += out->width; j &= buffer.mask; } (void) fputs("\n", stdout); } buffer.cons += mx; buffer.cons &= buffer.mask; offset += mx; buffer.navail -= mx; } /*LINTED E_SEC_PRINTF_VAR_FMT*/ (void) printf(afmt, offset); (void) fputs("\n", stdout); return (0); }
static int snappy_java_uncompress(FILE *infp, FILE *outfp, int skip_magic) { snappy_java_header_t header; work_buffer_t wb; int err = 1; int outfd; wb.c = NULL; wb.uc = NULL; if (skip_magic) { /* read header except magic */ if (fread_unlocked(&header.version, sizeof(header) - sizeof(header.magic), 1, infp) != 1) { print_error("Failed to read a file: %s\n", strerror(errno)); goto cleanup; } } else { /* read header */ if (fread_unlocked(&header, sizeof(header), 1, infp) != 1) { print_error("Failed to read a file: %s\n", strerror(errno)); goto cleanup; } /* check magic */ if (memcmp(header.magic, SNAPPY_JAVA_MAGIC, SNAPPY_JAVA_MAGIC_LEN) != 0) { print_error("This is not a snappy-java file.\n"); goto cleanup; } } /* check rest header */ header.version = ntohl(header.version); if (header.version != SNAPPY_JAVA_FILE_VERSION) { print_error("Unknown snappy-java version %d\n", header.version); goto cleanup; } header.compatible_version = ntohl(header.compatible_version); if (header.compatible_version != SNAPPY_JAVA_FILE_VERSION) { print_error("Unknown snappy-java compatible version %d\n", header.compatible_version); goto cleanup; } /* Use a file descriptor 'outfd' instead of the stdio file pointer 'outfp' * to reduce the number of write system calls. */ fflush(outfp); outfd = fileno(outfp); /* read body */ work_buffer_init(&wb, DEFAULT_BLOCK_SIZE); for (;;) { /* read the compressed length in a block */ size_t compressed_length = 0; size_t uncompressed_length = wb.uclen; int idx; for (idx = 3; idx >= 0; idx--) { int chr = getc_unlocked(infp); if (chr == -1) { if (idx == 3) { /* read all blocks */ err = 0; goto cleanup; } print_error("Unexpected end of file.\n"); goto cleanup; } compressed_length |= (chr << (idx * 8)); } trace("read 4 bytes (compressed_length = %ld)\n", (long)compressed_length); if (compressed_length == 0) { print_error("Invalid compressed length %ld\n", (long)compressed_length); goto cleanup; } if (compressed_length > wb.clen) { work_buffer_resize(&wb, compressed_length, 0); } /* read the compressed data */ if (fread_unlocked(wb.c, compressed_length, 1, infp) != 1) { if (feof_unlocked(infp)) { print_error("Unexpected end of file\n"); } else { print_error("Failed to read a file: %s\n", strerror(errno)); } goto cleanup; } trace("read %ld bytes.\n", (long)(compressed_length)); /* check the uncompressed length */ err = snappy_uncompressed_length(wb.c, compressed_length, &uncompressed_length); if (err != 0) { print_error("Invalid data: GetUncompressedLength failed %d\n", err); goto cleanup; } err = 1; if (uncompressed_length > wb.uclen) { work_buffer_resize(&wb, 0, uncompressed_length); } /* uncompress and write */ if (snappy_uncompress(wb.c, compressed_length, wb.uc, &uncompressed_length)) { print_error("Invalid data: RawUncompress failed\n"); goto cleanup; } if (write_full(outfd, wb.uc, uncompressed_length) != uncompressed_length) { print_error("Failed to write a file: %s\n", strerror(errno)); goto cleanup; } trace("write %ld bytes\n", (long)uncompressed_length); } cleanup: work_buffer_free(&wb); return err; }
static int my_getc_unlocked(FILE *fp) { return getc_unlocked(_get_actual_fp(fp)); }
int main(int argc, char *argv[]) { char buffer[BUFSIZ]; wchar_t wbuffer[BUFSIZ]; char *buf; wchar_t *wbuf; FILE *f; off_t off; fpos_t pos; size_t size; int fd, r; char c; wchar_t wc; if ((fd = dup(1)) == -1) err(2, "dup"); if ((dup_stdout = fdopen(fd, "w")) == NULL) err(2, "fdopen"); if ((fd = mkstemp(filename)) == -1) err(2, "mkstemp"); if (write(fd, "0123456789\n\n", 12) != 12 || close(fd)) err(2, "write + close"); /* status */ TEST_UNCHANGED(fwide(f, 0)); TEST_NARROW(fwide(f, -1)); TEST_WIDE(fwide(f, 1)); TEST_UNCHANGED(feof(f)); TEST_UNCHANGED(ferror(f)); TEST_UNCHANGED(fileno(f)); TEST_UNCHANGED(clearerr(f)); /* flush and purge */ TEST_UNCHANGED(fflush(f)); TEST_UNCHANGED(fpurge(f)); /* positioning */ TEST_UNCHANGED(fgetpos(f, &pos)); TEST_UNCHANGED(fgetpos(f, &pos); fsetpos(f, &pos)); TEST_UNCHANGED(ftell(f)); TEST_UNCHANGED(ftello(f)); TEST_UNCHANGED(fseek(f, 1, SEEK_CUR)); TEST_UNCHANGED(fseek(f, 1, SEEK_SET)); TEST_UNCHANGED(fseek(f, 1, SEEK_END)); TEST_UNCHANGED(fseeko(f, 1, SEEK_CUR)); TEST_UNCHANGED(fseeko(f, 1, SEEK_SET)); TEST_UNCHANGED(fseeko(f, 1, SEEK_END)); TEST_UNCHANGED(rewind(f)); /* buffering */ TEST_UNCHANGED(setbuf(f, NULL)); TEST_UNCHANGED(setbuf(f, buffer)); TEST_UNCHANGED(setvbuf(f, buffer, _IONBF, BUFSIZ)); TEST_UNCHANGED(setvbuf(f, buffer, _IOLBF, BUFSIZ)); TEST_UNCHANGED(setvbuf(f, buffer, _IOFBF, BUFSIZ)); TEST_UNCHANGED(setvbuf(f, NULL, _IONBF, 0)); TEST_UNCHANGED(setvbuf(f, NULL, _IOLBF, 0)); TEST_UNCHANGED(setvbuf(f, NULL, _IOFBF, 0)); TEST_UNCHANGED(setbuffer(f, NULL, 0)); TEST_UNCHANGED(setbuffer(f, buffer, BUFSIZ)); TEST_UNCHANGED(setlinebuf(f)); /* locking */ TEST_UNCHANGED(flockfile(f);funlockfile(f)); TEST_UNCHANGED(ftrylockfile(f);funlockfile(f)); /* input */ TEST_NARROW(getc(f)); TEST_NARROW(getc_unlocked(f)); TEST_NARROW(fgetc(f)); TEST_NARROW(c = fgetc(f); ungetc(c, f)); TEST_NARROW(fgets(buffer, BUFSIZ, f)); TEST_NARROW(fscanf(f, "%s\n", buffer)); TEST_NARROW(fgetln(f, &size)); /* output */ TEST_NARROW(putc('c', f)); TEST_NARROW(putc_unlocked('c', f)); TEST_NARROW(fputc('c', f)); TEST_NARROW(fputs("foo", f)); TEST_NARROW(fprintf(f, "%s\n", "foo")); /* input from stdin */ TEST_NARROW_STD(stdin, getchar()); TEST_NARROW_STD(stdin, getchar_unlocked()); TEST_NARROW_STD(stdin, fgets(buffer, BUFSIZ, stdin)); TEST_NARROW_STD(stdin, scanf("%s\n", buffer)); /* output to stdout */ TEST_NARROW_STD(stdout, putchar('c')); TEST_NARROW_STD(stdout, putchar_unlocked('c')); TEST_NARROW_STD(stdout, puts("foo")); TEST_NARROW_STD(stdout, printf("foo")); /* word-size ops */ /* * fread and fwrite are specified as being implemented in * terms of fgetc() and fputc() and therefore must set the * stream orientation to narrow. */ TEST_NARROW(fread(buffer, 4, BUFSIZ / 4, f)); TEST_NARROW(fwrite(buffer, 4, BUFSIZ / 4, f)); /* * getw() and putw() aren't specified anywhere but logically * should behave the same as fread/fwrite. Not all OSes agree: * Solaris 10 has them not changing the orientation. */ TEST_NARROW(getw(f)); TEST_NARROW(putw(1234, f)); /* WIDE CHAR TIME! */ /* input */ TEST_WIDE(getwc(f)); TEST_WIDE(fgetwc(f)); TEST_WIDE(wc = fgetwc(f); ungetwc(wc, f)); TEST_WIDE(fgetws(wbuffer, BUFSIZ, f)); TEST_WIDE(fwscanf(f, L"%s\n", wbuffer)); /* output */ TEST_WIDE(putwc(L'c', f)); TEST_WIDE(fputwc(L'c', f)); TEST_WIDE(fputws(L"foo", f)); TEST_WIDE(fwprintf(f, L"%s\n", L"foo")); /* input from stdin */ TEST_WIDE_STD(stdin, getwchar()); TEST_WIDE_STD(stdin, wscanf(L"%s\n", wbuffer)); /* output to stdout */ TEST_WIDE_STD(stdout, putwchar(L'c')); TEST_WIDE_STD(stdout, wprintf(L"foo")); /* memory streams */ f = open_memstream(&buf, &size); if (!((r = fwide(f, 0)) < 0)) fail(__LINE__, r, "<", "open_memstream()"); fclose(f); f = open_wmemstream(&wbuf, &size); if (!((r = fwide(f, 0)) > 0)) fail(__LINE__, r, ">", "open_wmemstream()"); fclose(f); /* random stuff? */ TEST_UNCHANGED_STD(stderr, perror("foo")); remove(filename); if (failures) exit(1); exit(0); }
int getchar_unlocked(void) { return getc_unlocked(stdin); }
int VMPI_getc_unlocked(FILE *stream) { return getc_unlocked( stream ); }
/* * Returns 0 if positive match, -1 if _not_ ok. */ static int __ivaliduser2(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser, const char *rhost) { register const char *user; register char *p; int hcheck, ucheck; char *buf = NULL; size_t bufsize = 0; int retval = -1; while (getline (&buf, &bufsize, hostf) > 0) { buf[bufsize - 1] = '\0'; /* Make sure it's terminated. */ p = buf; /* Skip empty or comment lines */ if (__isempty (p)) { continue; } /* Skip lines that are too long. */ if (strchr (p, '\n') == NULL) { int ch = getc_unlocked (hostf); while (ch != '\n' && ch != EOF) ch = getc_unlocked (hostf); continue; } for (; *p && !isspace(*p); ++p) { *p = tolower (*p); } /* Next we want to find the permitted name for the remote user. */ if (*p == ' ' || *p == '\t') { /* <nul> terminate hostname and skip spaces */ for (*p++='\0'; *p && isspace (*p); ++p); user = p; /* this is the user's name */ while (*p && !isspace (*p)) ++p; /* find end of user's name */ } else user = p; *p = '\0'; /* <nul> terminate username (+host?) */ /* buf -> host(?) ; user -> username(?) */ /* First check host part */ hcheck = __icheckhost (raddr, buf, rhost); if (hcheck < 0) break; if (hcheck) { /* Then check user part */ if (! (*user)) user = luser; ucheck = __icheckuser (user, ruser); /* Positive 'host user' match? */ if (ucheck > 0) { retval = 0; break; } /* Negative 'host -user' match? */ if (ucheck < 0) break; /* Neither, go on looking for match */ } } free (buf); return retval; }
// We used unlocked IO while processing a query, with lock and unlock surrounding the query read. static inline int fgetchar(FILE * in) { return getc_unlocked(in); }
/** * pangolite_read_line: * @stream: a stdio stream * @str: #GString buffer into which to write the result * * Read an entire line from a file into a buffer. Lines may * be delimited with '\n', '\r', '\n\r', or '\r\n'. The delimiter * is not written into the buffer. Text after a '#' character is treated as * a comment and skipped. '\' can be used to escape a # character. * '\' proceding a line delimiter combines adjacent lines. A '\' proceding * any other character is ignored and written into the output buffer * unmodified. * * Return value: 0 if the stream was already at an EOF character, otherwise * the number of lines read (this is useful for maintaining * a line number counter which doesn't combine lines with \) **/ gint pangolite_read_line (FILE *stream, GString *str) { gboolean quoted = FALSE; gboolean comment = FALSE; int n_read = 0; int lines = 1; flockfile (stream); g_string_truncate (str, 0); while (1) { int c; c = getc_unlocked (stream); if (c == EOF) { if (quoted) g_string_append_c (str, '\\'); goto done; } else n_read++; if (quoted) { quoted = FALSE; switch (c) { case '#': g_string_append_c (str, '#'); break; case '\r': case '\n': { int next_c = getc_unlocked (stream); if (!(next_c == EOF || (c == '\r' && next_c == '\n') || (c == '\n' && next_c == '\r'))) ungetc (next_c, stream); lines++; break; } default: g_string_append_c (str, '\\'); g_string_append_c (str, c); } } else { switch (c) { case '#': comment = TRUE; break; case '\\': if (!comment) quoted = TRUE; break; case '\n': { int next_c = getc_unlocked (stream); if (!(c == EOF || (c == '\r' && next_c == '\n') || (c == '\n' && next_c == '\r'))) ungetc (next_c, stream); goto done; } default: if (!comment) g_string_append_c (str, c); } } } done: funlockfile (stream); return (n_read > 0) ? lines : 0; }
int(getc_unlocked)(FILE* f) { return getc_unlocked(f); }
static void cut_fields(FILE *stream ) { int c ; size_t field_idx ; _Bool found_any_selected_field ; _Bool buffer_first_field ; _Bool tmp ; int tmp___0 ; ssize_t len ; size_t n_bytes ; int tmp___1 ; int tmp___2 ; unsigned char tmp___3 ; _Bool tmp___4 ; _Bool tmp___5 ; { field_idx = 1UL; found_any_selected_field = (_Bool)0; c = getc_unlocked(stream); if (c == -1) { return; } else { } ungetc(c, stream); tmp = print_kth(1UL, (_Bool *)((void *)0)); if (tmp) { tmp___0 = 0; } else { tmp___0 = 1; } buffer_first_field = (_Bool )((int )suppress_non_delimited ^ tmp___0); while (1) { if (field_idx == 1UL) { if (buffer_first_field) { len = getndelim2(& field_1_buffer, & field_1_bufsize, 0UL, 4294967295UL, (int )delim, '\n', stream); if (len < 0L) { free((void *)field_1_buffer); field_1_buffer = (char *)((void *)0); tmp___1 = ferror_unlocked(stream); if (tmp___1) { break; } else { tmp___2 = feof_unlocked(stream); if (tmp___2) { break; } else { } } xalloc_die(); } else { } n_bytes = (unsigned long )len; if (! (n_bytes != 0UL)) { __assert_fail("n_bytes != 0", "cut.c", 626U, "cut_fields"); } else { } tmp___3 = to_uchar(*(field_1_buffer + (n_bytes - 1UL))); if ((int )tmp___3 != (int )delim) { if (! suppress_non_delimited) { fwrite_unlocked((void const */* __restrict */)field_1_buffer, sizeof(char ), n_bytes, (FILE */* __restrict */)stdout); if ((int )*(field_1_buffer + (n_bytes - 1UL)) != 10) { putchar_unlocked('\n'); } else { } } else { } continue; } else { } tmp___4 = print_kth(1UL, (_Bool *)((void *)0)); if (tmp___4) { fwrite_unlocked((void const */* __restrict */)field_1_buffer, sizeof(char ), n_bytes - 1UL, (FILE */* __restrict */)stdout); found_any_selected_field = (_Bool)1; } else { } field_idx ++; } else { } } else { } if (c != -1) { tmp___5 = print_kth(field_idx, (_Bool *)((void *)0)); if (tmp___5) { if (found_any_selected_field) { fwrite_unlocked((void const */* __restrict */)output_delimiter_string, sizeof(char ), output_delimiter_length, (FILE */* __restrict */)stdout); } else { } found_any_selected_field = (_Bool)1; while (1) { c = getc_unlocked(stream); if (c != (int )delim) { if (c != 10) { if (! (c != -1)) { break; } else { } } else { break; } } else { break; } putchar_unlocked(c); } } else { while (1) { c = getc_unlocked(stream); if (c != (int )delim) { if (c != 10) { if (! (c != -1)) { break; } else { } } else { break; } } else { break; } } } } else { } if (c == 10) { c = getc_unlocked(stream); if (c != -1) { ungetc(c, stream); c = '\n'; } else { } } else { } if (c == (int )delim) { field_idx ++; } else { if (c == 10) { goto _L; } else { if (c == -1) { _L: if (found_any_selected_field) { putchar_unlocked('\n'); } else { if (suppress_non_delimited) { if (! (field_idx == 1UL)) { putchar_unlocked('\n'); } else { } } else { putchar_unlocked('\n'); } } if (c == -1) { break; } else { } field_idx = 1UL; found_any_selected_field = (_Bool)0; } else { } } } } return; } }
int do_scrub_next_char( FILE *fp) { /* State 0: beginning of normal line 1: After first whitespace on normal line (flush more white) 2: After first non-white on normal line (keep 1white) 3: after second white on normal line (flush white) 4: after putting out a .line, put out digits 5: parsing a string, then go to old-state 6: putting out \ escape in a "d string. 7: After putting out a .file, put out string. 8: After putting out a .file string, flush until newline. -1: output string in out_string and go to the state in old_state -2: flush text until a '*' '/' is seen, then go to state old_state */ #ifndef NeXT_MOD /* .include feature */ static state; static old_state; static char *out_string; static char out_buf[20]; static add_newlines = 0; #endif /* NeXT_MOD .include feature */ int ch; if(state==-1) { ch= *out_string++; if(*out_string==0) { state=old_state; old_state=3; } return ch; } if(state==-2) { for(;;) { do ch=getc_unlocked(fp); while(ch!=EOF && ch!='\n' && ch!='*'); if(ch=='\n' || ch==EOF) return ch; ch=getc_unlocked(fp); if(ch==EOF || ch=='/') break; ungetc(ch, fp); } state=old_state; return ' '; } if(state==4) { ch=getc_unlocked(fp); if(ch==EOF || (ch>='0' && ch<='9')) return ch; else { while(ch!=EOF && IS_WHITESPACE(ch)) ch=getc_unlocked(fp); if(ch=='"') { ungetc(ch, fp); #if defined(M88K) || defined(PPC) || defined(HPPA) out_string="@ .file "; #else out_string="; .file "; #endif old_state=7; state= -1; return *out_string++; } else { while(ch!=EOF && ch!='\n') ch=getc_unlocked(fp); #ifdef NeXT_MOD /* bug fix for bug #8918, which was when * a full line comment line this: * # 40 MP1 = M + 1 * got confused with a cpp output like: * # 1 "hello.c" 1 */ state = 0; #endif /* NeXT_MOD */ return ch; } } } if(state==5) { ch=getc_unlocked(fp); #ifdef PPC if(flagseen[(int)'p'] == TRUE && ch=='\'') { state=old_state; return '\''; } else #endif /* PPC */ if(ch=='"') { state=old_state; return '"'; } else if(ch=='\\') { state=6; return ch; } else if(ch==EOF) { state=old_state; ungetc('\n', fp); #ifdef PPC if(flagseen[(int)'p'] == TRUE){ as_warn("End of file in string: inserted '\''"); return '\''; } #endif /* PPC */ as_warn("End of file in string: inserted '\"'"); return '"'; } else { return ch; } } if(state==6) { state=5; ch=getc_unlocked(fp); switch(ch) { /* This is neet. Turn "string more string" into "string\n more string" */ case '\n': ungetc('n', fp); add_newlines++; return '\\'; case '\'': case '"': case '\\': case 'b': case 'f': case 'n': case 'r': case 't': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': break; default: as_warn("Unknown escape '\\%c' in string: Ignored",ch); break; case EOF: as_warn("End of file in string: '\"' inserted"); return '"'; } return ch; } if(state==7) { ch=getc_unlocked(fp); state=5; old_state=8; return ch; } if(state==8) { do ch= getc_unlocked(fp); while(ch!='\n'); state=0; return ch; } flushchar: ch=getc_unlocked(fp); switch(ch) { case ' ': case '\t': do ch=getc_unlocked(fp); while(ch!=EOF && IS_WHITESPACE(ch)); if(ch==EOF) return ch; if(IS_COMMENT(ch) || (state==0 && IS_LINE_COMMENT(ch)) || ch=='/' || IS_LINE_SEPERATOR(ch)) { ungetc(ch, fp); goto flushchar; } ungetc(ch, fp); if(state==0 || state==2) { state++; return ' '; } #ifdef ARM /* because it COULDN'T POSSIBLY BE THE CASE that spaces could be * significant between keywords?!?! */ if (state == 3) return ' '; #endif #ifdef PPC if(flagseen[(int)'p'] == TRUE && state == 3){ return ' '; } #endif else goto flushchar; case '/': ch=getc_unlocked(fp); if(ch=='*') { for(;;) { do { ch=getc_unlocked(fp); if(ch=='\n') add_newlines++; } while(ch!=EOF && ch!='*'); ch=getc_unlocked(fp); if(ch==EOF || ch=='/') break; ungetc(ch, fp); } if(ch==EOF) as_warn("End of file in '/' '*' string: */ inserted"); ungetc(' ', fp); goto flushchar; } else { #if defined(I860) || defined(M88K) || defined(PPC) || defined(I386) || \ defined(HPPA) || defined (SPARC) if (ch == '/') { do { ch=getc_unlocked(fp); } while (ch != EOF && (ch != '\n')); if (ch == EOF) as_warn("End of file before newline in // comment"); if ( ch == '\n' ) /* Push NL back so we can complete state */ ungetc(ch, fp); goto flushchar; } #endif if(IS_COMMENT('/') || (state==0 && IS_LINE_COMMENT('/'))) { ungetc(ch, fp); ch='/'; goto deal_misc; } if(ch!=EOF) ungetc(ch, fp); return '/'; } break; case '"': old_state=state; state=5; return '"'; break; case '\'': #ifdef PPC if(flagseen[(int)'p'] == TRUE){ old_state=state; state=5; return '\''; break; } #endif ch=getc_unlocked(fp); if(ch==EOF) { as_warn("End-of-file after a ': \\000 inserted"); ch=0; } sprintf(out_buf,"(%d)",ch&0xff); old_state=state; state= -1; out_string=out_buf; return *out_string++; case ':': if(state!=3) state=0; return ch; case '\n': if(add_newlines) { --add_newlines; ungetc(ch, fp); } /* Fall through. */ #if defined(M88K) || defined(PPC) || defined(HPPA) case '@': #else case ';': #endif state=0; return ch; default: deal_misc: if(state==0 && IS_LINE_COMMENT(ch)) { do ch=getc_unlocked(fp); while(ch!=EOF && IS_WHITESPACE(ch)); if(ch==EOF) { as_warn("EOF in comment: Newline inserted"); return '\n'; } if(ch<'0' || ch>'9') { if(ch!='\n'){ do ch=getc_unlocked(fp); while(ch!=EOF && ch!='\n'); } if(ch==EOF) as_warn("EOF in Comment: Newline inserted"); state=0; return '\n'; } ungetc(ch, fp); old_state=4; state= -1; out_string=".line "; return *out_string++; } else if(IS_COMMENT(ch)) { do ch=getc_unlocked(fp); while(ch!=EOF && ch!='\n'); if(ch==EOF) as_warn("EOF in comment: Newline inserted"); state=0; return '\n'; } else if(state==0) { state=2; return ch; } else if(state==1) { state=2; return ch; } else { return ch; } case EOF: if(state==0) return ch; as_warn("End-of-File not at end of a line"); } return -1; }
static int grscan(int search, gid_t gid, const char *name, struct group *p_gr, struct group_storage *gs, int *foundyp) { char *cp, **m; char *bp, *endp; u_long ul; #ifdef YP char *key, *data; int keylen, datalen; int r; #endif char **members; char *line; int saved_errno; if (gs == NULL) return 0; members = gs->members; line = gs->line; saved_errno = errno; for (;;) { #ifdef YP if (__ypmode) { if (__ypcurrent) { r = yp_next(__ypdomain, "group.byname", __ypcurrent, __ypcurrentlen, &key, &keylen, &data, &datalen); free(__ypcurrent); __ypcurrent = key; __ypcurrentlen = keylen; } else { r = yp_first(__ypdomain, "group.byname", &__ypcurrent, &__ypcurrentlen, &data, &datalen); } if (r) { __ypmode = 0; __ypcurrent = NULL; if (r == YPERR_NOMORE) continue; else return 0; } bcopy(data, line, datalen); free(data); line[datalen] = '\0'; bp = line; goto parse; } #endif if (!fgets(line, sizeof(gs->line), _gr_fp)) { if (feof(_gr_fp) && !ferror(_gr_fp)) errno = saved_errno; return 0; } bp = line; /* skip lines that are too big */ if (!strchr(line, '\n')) { int ch; while ((ch = getc_unlocked(_gr_fp)) != '\n' && ch != EOF) ; continue; } #ifdef YP if (line[0] == '+' || line[0] == '-') { if (__ypdomain == NULL && yp_get_default_domain(&__ypdomain)) goto parse; switch (yp_bind(__ypdomain)) { case 0: break; case YPERR_BADARGS: case YPERR_YPBIND: goto parse; default: return 0; } } if (line[0] == '+') { switch (line[1]) { case ':': case '\0': case '\n': if (foundyp) { *foundyp = 1; errno = saved_errno; return 0; } if (!search) { __ypmode = 1; continue; } if (name) { r = yp_match(__ypdomain, "group.byname", name, strlen(name), &data, &datalen); } else { char buf[20]; snprintf(buf, sizeof buf, "%u", gid); r = yp_match(__ypdomain, "group.bygid", buf, strlen(buf), &data, &datalen); } switch (r) { case 0: break; case YPERR_KEY: continue; default: return 0; } bcopy(data, line, datalen); free(data); line[datalen] = '\0'; bp = line; p_gr->gr_name = strsep(&bp, ":\n"); if (__ypexclude_is(&__ypexhead, p_gr->gr_name)) continue; p_gr->gr_passwd = strsep(&bp, ":\n"); if (!(cp = strsep(&bp, ":\n"))) continue; if (name) { ul = strtoul(cp, &endp, 10); if (*endp != '\0' || endp == cp || ul >= GID_MAX) continue; p_gr->gr_gid = ul; } else p_gr->gr_gid = gid; goto found_it; default: bp = strsep(&bp, ":\n") + 1; if ((search && name && strcmp(bp, name)) || __ypexclude_is(&__ypexhead, bp)) continue; r = yp_match(__ypdomain, "group.byname", bp, strlen(bp), &data, &datalen); switch (r) { case 0: break; case YPERR_KEY: continue; default: return 0; } bcopy(data, line, datalen); free(data); line[datalen] = '\0'; bp = line; } } else if (line[0] == '-') { if (__ypexclude_add(&__ypexhead, strsep(&line, ":\n") + 1)) return 0; if (foundyp) { *foundyp = -1; errno = saved_errno; return 0; } continue; } parse: #endif p_gr->gr_name = strsep(&bp, ":\n"); if (search && name && strcmp(p_gr->gr_name, name)) continue; #ifdef YP if (__ypmode && __ypexclude_is(&__ypexhead, p_gr->gr_name)) continue; #endif p_gr->gr_passwd = strsep(&bp, ":\n"); if (!(cp = strsep(&bp, ":\n"))) continue; ul = strtoul(cp, &endp, 10); if (endp == cp || *endp != '\0' || ul >= GID_MAX) continue; p_gr->gr_gid = ul; if (search && name == NULL && p_gr->gr_gid != gid) continue; #ifdef YP found_it: #endif cp = NULL; if (bp == NULL) continue; for (m = p_gr->gr_mem = members;; bp++) { if (m == &members[MAXGRP - 1]) break; if (*bp == ',') { if (cp) { *bp = '\0'; *m++ = cp; cp = NULL; } } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') { if (cp) { *bp = '\0'; *m++ = cp; } break; } else if (cp == NULL) cp = bp; } *m = NULL; errno = saved_errno; return 1; } /* NOTREACHED */ }
static int snappy_in_java_uncompress(FILE *infp, FILE *outfp, int skip_magic) { snappy_in_java_header_t header; work_buffer_t wb; int err = 1; int outfd; wb.c = NULL; wb.uc = NULL; if (!skip_magic) { /* read header */ if (fread_unlocked(&header, sizeof(header), 1, infp) != 1) { print_error("Failed to read a file: %s\n", strerror(errno)); goto cleanup; } /* check header */ if (memcmp(header.magic, SNAPPY_IN_JAVA_MAGIC, SNAPPY_IN_JAVA_MAGIC_LEN) != 0) { print_error("This is not a snappy-java file.\n"); goto cleanup; } } /* Use a file descriptor 'outfd' instead of the stdio file pointer 'outfp' * to reduce the number of write system calls. */ fflush(outfp); outfd = fileno(outfp); /* read body */ work_buffer_init(&wb, MAX_BLOCK_SIZE); for (;;) { int compressed_flag; size_t length = 0; unsigned int crc32c = 0; /* read compressed flag */ compressed_flag = getc_unlocked(infp); switch (compressed_flag) { case EOF: /* read all blocks */ err = 0; goto cleanup; case COMPRESSED_FLAG: case UNCOMPRESSED_FLAG: /* pass */ break; default: print_error("Unknown compressed flag 0x%02x\n", compressed_flag); goto cleanup; } /* read data length. */ length |= (getc_unlocked(infp) << 8); length |= (getc_unlocked(infp) << 0); /* read crc32c. */ crc32c |= (getc_unlocked(infp) << 24); crc32c |= (getc_unlocked(infp) << 16); crc32c |= (getc_unlocked(infp) << 8); crc32c |= (getc_unlocked(infp) << 0); /* check read error */ if (feof_unlocked(infp)) { print_error("Unexpected end of file.\n"); goto cleanup; } else if (ferror_unlocked(infp)) { print_error("Failed to read a file: %s\n", strerror(errno)); goto cleanup; } /* read data */ if (fread_unlocked(wb.c, length, 1, infp) != 1) { if (feof_unlocked(infp)) { print_error("Unexpected end of file\n"); } else { print_error("Failed to read a file: %s\n", strerror(errno)); } goto cleanup; } trace("read %ld bytes.\n", (long)(length)); if (compressed_flag == COMPRESSED_FLAG) { /* check the uncompressed length */ size_t uncompressed_length; err = snappy_uncompressed_length(wb.c, length, &uncompressed_length); if (err != 0) { print_error("Invalid data: GetUncompressedLength failed %d\n", err); goto cleanup; } err = 1; if (uncompressed_length > wb.uclen) { print_error("Invalid data: too long uncompressed length\n"); goto cleanup; } /* uncompress and write */ if (snappy_uncompress(wb.c, length, wb.uc, &uncompressed_length)) { print_error("Invalid data: RawUncompress failed\n"); goto cleanup; } if (check_and_write_block(outfd, wb.uc, uncompressed_length, 1, crc32c)) { goto cleanup; } } else { if (check_and_write_block(outfd, wb.c, length, 1, crc32c)) { goto cleanup; } } } cleanup: work_buffer_free(&wb); return err; }
static int do_test (void) { const char blah[] = "BLAH"; char buf[strlen (blah) + 1]; FILE *fp, *f; const char *cp; char *wp; if ((fp = fdopen (fd, "w+")) == NULL) exit (1); flockfile (fp); f = fp; cp = blah; if (ftello (fp) != 0 || fwrite_unlocked (blah, blah - blah, strlen (blah), f++) != 0 || f != fp + 1 || fwrite_unlocked ("", 5.0, 0, --f) != 0 || f != fp || fwrite_unlocked (cp++, 16, 0.25, fp) != 0 || cp != blah + 1 || fwrite_unlocked (--cp, 0.25, 16, fp) != 0 || cp != blah || fwrite_unlocked (blah, 0, -0.0, fp) != 0 || ftello (fp) != 0) { puts ("One of fwrite_unlocked tests failed"); exit (1); } if (fwrite_unlocked (blah, 1, strlen (blah) - 2, fp) != strlen (blah) - 2) { puts ("Could not write string into file"); exit (1); } if (putc_unlocked ('A' + 0x1000000, fp) != 'A') { puts ("putc_unlocked failed"); exit (1); } f = fp; cp = blah + strlen (blah) - 1; if (putc_unlocked (*cp++, f++) != 'H' || f != fp + 1 || cp != strchr (blah, '\0')) { puts ("fputc_unlocked failed"); exit (1); } if (ftello (fp) != (off_t) strlen (blah)) { printf ("Failed to write %zd bytes to temporary file", strlen (blah)); exit (1); } rewind (fp); f = fp; wp = buf; memset (buf, ' ', sizeof (buf)); if (ftello (fp) != 0 || fread_unlocked (buf, buf - buf, strlen (blah), f++) != 0 || f != fp + 1 || fread_unlocked (buf, 5.0, 0, --f) != 0 || f != fp || fread_unlocked (wp++, 16, 0.25, fp) != 0 || wp != buf + 1 || fread_unlocked (--wp, 0.25, 16, fp) != 0 || wp != buf || fread_unlocked (buf, 0, -0.0, fp) != 0 || ftello (fp) != 0 || memcmp (buf, " ", sizeof (buf)) != 0) { puts ("One of fread_unlocked tests failed"); exit (1); } if (fread_unlocked (buf, 1, strlen (blah) - 2, fp) != strlen (blah) - 2) { puts ("Could not read string from file"); exit (1); } if (getc_unlocked (fp) != 'A') { puts ("getc_unlocked failed"); exit (1); } f = fp; if (fgetc_unlocked (f++) != 'H' || f != fp + 1 || fgetc_unlocked (--f) != EOF || f != fp) { puts ("fgetc_unlocked failed"); exit (1); } if (ftello (fp) != (off_t) strlen (blah)) { printf ("Failed to read %zd bytes from temporary file", strlen (blah)); exit (1); } funlockfile (fp); fclose (fp); return 0; }
int __darwin___srget(__darwin_FILE* f) { return getc_unlocked(f->linux_fp); }
ssize_t getline(char **linep, size_t *np, FILE *stream) { char *p = NULL; size_t i = 0; int ch = 0; if (!linep || !np) { errno = EINVAL; return -1; } if (!(*linep) || !(*np)) { *np = 120; *linep = (char *) malloc(*np); if (!(*linep)) { return -1; } } flockfile(stream); p = *linep; for (ch = 0; (ch = getc_unlocked(stream)) != EOF;) { if (i > *np) { /* Grow *linep. */ size_t m = *np * 2; char *s = (char *) realloc(*linep, m); if (!s) { int error = errno; funlockfile(stream); errno = error; return -1; } *linep = s; *np = m; } p[i] = ch; if ('\n' == ch) break; i += 1; } funlockfile(stream); /* Null-terminate the string. */ if (i > *np) { /* Grow *linep. */ size_t m = *np * 2; char *s = (char *) realloc(*linep, m); if (!s) { return -1; } *linep = s; *np = m; } p[i + 1] = '\0'; return ((i > 0) ? i : -1); }
static int parse_grp(struct request *request) { register char *l; char *p; int i; struct group *grp; int max; char **mem; if ((l = strchr(request->buffer, '\n')) == NULL) { int tc; /* eat the entire line */ while ((tc = getc_unlocked(request->dbase->fp)) != '\n' && tc != EOF) { /* nothing to do */ } return ERANGE; } *l = '\0'; i = l - request->buffer; if(!(p = strchr(l = request->buffer, ':'))) { return EINVAL; } i += sizeof *mem - (i % (int)(sizeof *mem)); max = (request->bufsize - i) / sizeof *mem - 1; if(max <= 0) { return ERANGE; } grp = &request->record->grp; grp->gr_mem = (char **)&request->buffer[i]; *p++ = '\0'; grp->gr_name = l; l = p; if(!(p = strchr(l, ':'))) { return EINVAL; } *p++ = '\0'; grp->gr_passwd = l; l = p; grp->gr_gid = strtol(l, &p, 10); if(*p++ != ':') { *grp->gr_mem = 0; return EOK; } for(i = 0, l = p, mem = grp->gr_mem; ++i < max;) { if(*l) { *mem++ = l; } if(!(p = strchr(l, ','))) { break; } *p++ = '\0'; if(*(l = p) == '\0') { break; } } *mem = 0; return EOK; }