/** Read input from fp_in. * * If there is a LF, CR, CR/LF, or LF/CR it returns exactly on LF. Also counts the number of * characters. */ static int getsymbol( PPOS* ppos ) { int c; assert(ppos != NULL); if (ppos->nextsym == 0) c = my_getc(ppos); else { c = ppos->nextsym; ppos->nextsym = 0; } assert(ppos->nextsym == 0); if (((c == '\n') && (ppos->lastsym == '\r')) || ((c == '\r') && (ppos->lastsym == '\n'))) c = my_getc(ppos); ppos->lastsym = c; if (c == '\r') c = '\n'; if (c == '\n') ppos->lineno++; return c; }
/* ** dump_to_eol ** ** go to the character after the next \n, \r, or EOF */ void dump_to_eol(FILE * fp) { int c=my_getc(fp); while (c != '\r' && c != '\n') c=my_getc(fp); if (my_whitespace(c) != IOU_EOF) ungetc(c, fp); }
_ssize_t _read_r( struct _reent *r, int file, void *ptr, size_t len) { char c; int i; unsigned char *p; p = (unsigned char*)ptr; for (i = 0; i < len; i++) { // c = uart0Getch(); // c = uart0GetchW(); while ( !my_kbhit() ) ; c = (char) my_getc(); if (c == 0x0D) { *p='\0'; break; } *p++ = c; ////// uart0_putc(c); } return len - i; }
/* ** read_char ** ** read the next char from fp, skipping whitespace */ int read_char(FILE * fp) { int c; while (1) { c = my_getc(fp); switch(c) { case -1: return -1; case '\n': case '\f': case '\r': case '\t': case ' ': /* um... white space... skip it */ break; case '#': /* comment... read till end of line */ dump_to_eol(fp); break; default: return (int) c; } } return(0); }
/** Read input from fp_in (variant). * * Here we convert all LF or CR into SPACE and return maximally one SPACE after the other. * * @note This function counts lines differently. On systems that have only one '\\r' as line feed * (MAC) it does not count correctly. */ static int getsymbol( PPOS* ppos ) { int c; assert(ppos != NULL); do { if (ppos->nextsym == 0) c = my_getc(ppos); else { c = ppos->nextsym; ppos->nextsym = 0; } assert(ppos->nextsym == 0); if (c == '\n') ppos->lineno++; if ((c == '\n') || (c == '\r')) c = ' '; } while((c == ' ') && (ppos->lastsym == c)); ppos->lastsym = c; debugMessage("[%c]\n", c); return c; }
/* ** read_int ** ** read the next number (negative or non) from fp, skipping whitespace */ int read_int(FILE * fp) { int c = read_char(fp); int number = 0; while (1) { if (c == '-') return -1 * read_int(fp); /* Recursion! */ else if (c >= '0' && c <= '9') { number = (number * 10) + (c - '0'); c = my_getc(fp); } else return number; } }
/* ** read_string ** ** read the next string in from fp, returning it through *string */ char * read_string(FILE * fp) { int c = read_char(fp); int len = 0; char *newstr; char temp[100]; do { temp[len++] = c ; } while ((c=my_getc(fp)) != EOF && !isspace(c)); temp[len] = '\0'; newstr = (char *) malloc (len+1); strcpy(newstr, temp); return (newstr); }
int main (int argc, char **argv) { const char *pgm; int last_argc = -1; int print_config = 0; int i, c; int state = 0; char strbuf[4096]; int stridx = 0; if (argc) { pgm = strrchr (*argv, '/'); if (pgm) pgm++; else pgm = *argv; argc--; argv++; } else pgm = "?"; while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--")) { argc--; argv++; break; } else if (!strcmp (*argv, "--version") || !strcmp (*argv, "--help")) { printf ("%s " MPICALC_VERSION "\n" "libgcrypt %s\n" "Copyright (C) 1997, 2013 Werner Koch\n" "License LGPLv2.1+: GNU LGPL version 2.1 or later " "<http://gnu.org/licenses/old-licenses/lgpl-2.1.html>\n" "This is free software: you are free to change and " "redistribute it.\n" "There is NO WARRANTY, to the extent permitted by law.\n" "\n" "Syntax: mpicalc [options]\n" "Simple interactive big integer RPN calculator\n" "\n" "Options:\n" " --version print version information\n" " --print-config print the Libgcrypt config\n" " --disable-hwf NAME disable feature NAME\n", pgm, gcry_check_version (NULL)); exit (0); } else if (!strcmp (*argv, "--print-config")) { argc--; argv++; print_config = 1; } else if (!strcmp (*argv, "--disable-hwf")) { argc--; argv++; if (argc) { if (gcry_control (GCRYCTL_DISABLE_HWF, *argv, NULL)) fprintf (stderr, "%s: unknown hardware feature `%s'" " - option ignored\n", pgm, *argv); argc--; argv++; } } } if (argc) { fprintf (stderr, "usage: %s [options] (--help for help)\n", pgm); exit (1); } if (!gcry_check_version (NEED_LIBGCRYPT_VERSION)) { fprintf (stderr, "%s: Libgcrypt is too old (need %s, have %s)\n", pgm, NEED_LIBGCRYPT_VERSION, gcry_check_version (NULL) ); exit (1); } gcry_control (GCRYCTL_DISABLE_SECMEM, 0); gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); if (print_config) { gcry_control (GCRYCTL_PRINT_CONFIG, stdout); exit (0); } for (i = 0; i < STACKSIZE; i++) stack[i] = NULL; stackidx = 0; while ((c = my_getc ()) != EOF) { if (!state) /* waiting */ { if (isdigit (c)) { state = 1; ungetc (c, stdin); strbuf[0] = '0'; strbuf[1] = 'x'; stridx = 2; } else if (isspace (c)) ; else { switch (c) { case '#': state = 2; break; case '+': if ((c = my_getc ()) == '+') do_inc (); else { ungetc (c, stdin); do_add (); } break; case '-': if ((c = my_getc ()) == '-') do_dec (); else if (isdigit (c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) { state = 1; ungetc (c, stdin); strbuf[0] = '-'; strbuf[1] = '0'; strbuf[2] = 'x'; stridx = 3; } else { ungetc (c, stdin); do_sub (); } break; case '*': do_mul (); break; case 'm': do_mulm (); break; case '/': do_div (); break; case '%': do_rem (); break; case '^': do_powm (); break; case '<': do_lshift (); break; case '>': do_rshift (); break; case 'I': do_inv (); break; case 'G': do_gcd (); break; case 'i': /* dummy */ if (!stackidx) fputs ("stack underflow\n", stderr); else { mpi_release (stack[stackidx - 1]); stackidx--; } break; case 'd': /* duplicate the tos */ if (!stackidx) fputs ("stack underflow\n", stderr); else if (stackidx < STACKSIZE) { mpi_release (stack[stackidx]); stack[stackidx] = mpi_copy (stack[stackidx - 1]); stackidx++; } else fputs ("stack overflow\n", stderr); break; case 'r': /* swap top elements */ if (stackidx < 2) fputs ("stack underflow\n", stderr); else if (stackidx < STACKSIZE) { gcry_mpi_t tmp = stack[stackidx-1]; stack[stackidx-1] = stack[stackidx - 2]; stack[stackidx-2] = tmp; } break; case 'b': do_nbits (); break; case 'P': do_primecheck (); break; case 'c': for (i = 0; i < stackidx; i++) { mpi_release (stack[i]); stack[i] = NULL; } stackidx = 0; break; case 'p': /* print the tos */ if (!stackidx) puts ("stack is empty"); else { print_mpi (stack[stackidx - 1]); putchar ('\n'); } break; case 'f': /* print the stack */ for (i = stackidx - 1; i >= 0; i--) { printf ("[%2d]: ", i); print_mpi (stack[i]); putchar ('\n'); } break; case '?': print_help (); break; default: fputs ("invalid operator\n", stderr); } } } else if (state == 1) /* In a number. */ { if (!isxdigit (c)) { /* Store the number */ state = 0; ungetc (c, stdin); if (stridx < sizeof strbuf) strbuf[stridx] = 0; if (stackidx < STACKSIZE) { if (!stack[stackidx]) stack[stackidx] = mpi_new (0); if (scan_mpi (stack[stackidx], strbuf)) fputs ("invalid number\n", stderr); else stackidx++; } else fputs ("stack overflow\n", stderr); } else { /* Store a digit. */ if (stridx < sizeof strbuf - 1) strbuf[stridx++] = c; else if (stridx == sizeof strbuf - 1) { strbuf[stridx] = 0; fputs ("input too large - truncated\n", stderr); stridx++; } } } else if (state == 2) /* In a comment. */ { if (c == '\n') state = 0; } } for (i = 0; i < stackidx; i++) mpi_release (stack[i]); return 0; }
/*tga2gif(ac, av) int ac; char **av;*/ tga2gif(char *filename) { int i, w, h, c, r, g, b; long total; char file[80], /* root of file name */ infile[80], /* input file name */ outfile[80], /* gif file name */ palfile[80]; /* palette file name */ int (*grab_a_pix)(), get_pix(), get_pix_fs(); // strcpy(name, strrchr(filename[0], PATH_SEPARATOR)+1); /* get rid of the path */ // *strchr(name, '.') = 0; /* get rid of the .exe */ // strlwr(name); /* gimmee lower case */ palfile[0] = 0; /* NULL string */ initdata(); grab_a_pix = get_pix; /* default, non dithered function for gif */ /*if(ac < 2) usage(name);*/ infile[0] = '\0'; //for(i=1; i<ac; i++) { /* loop through command line args */ strlwr(filename); strcpy(file, filename); strcpy(infile, filename); strcpy(outfile, filename); strcat(infile, ".tga"); strcat(outfile, ".gif"); //} /* end of i loop through args */ /* Open image file for reading */ infp = fopen(infile, READ_MODE); if(!infp) { fprintf(stderr, "Error opening file %s for input.\n", infile); exit(1); } /* Read targa header. 3rd byte should be 2 signifying a type 2 targa file. Get resolution. */ my_getc(infp); my_getc(infp); if(my_getc(infp) != 2) { fprintf(stderr, "Sorry, but this program only works for type 2 Targa files.\n"); exit(1); } for(i=3; i<12; i++) { my_getc(infp); } /* get res */ xres = my_getc(infp); xres += my_getc(infp)<<8; yres = my_getc(infp); yres += my_getc(infp)<<8; /* get last two bytes of the header */ my_getc(infp); my_getc(infp); printf("image size : %d x %d\n", xres, yres); /* Put width of image in w */ w = xres; width = w; /* Put height of image in h */ h = yres; height = h; /* read in image and build tree if we need to */ if(method==POP || method==MEDIAN_BOX) { printf("Building tree.\n total colors = "); total_pixels = total_colors; total = (long)w * (long)h; while(total > 0) { /* Read color run length */ c = 1; if(c==0) c = 1024; /* read an rgb triple */ b = my_getc(infp); g = my_getc(infp); r = my_getc(infp); add_color(r>>2, g>>2, b>>2, c); /* add to tree */ total -= c; if(total_pixels != total_colors) { if((total_colors % (PRINT_STEP<<2)) == 0) printf("%8ld\b\b\b\b\b\b\b\b", total_colors); total_pixels = total_colors; } } printf("%8ld\b\b\b\b\b\b\b\b", total_colors); printf("\n"); }
int __fgetpwent_r (FILE* stream, struct passwd* result_buf, char* buffer, size_t buflen, struct passwd** result) { char* write_crs = buffer; size_t bytes_used = 0; char** fields[10]; int numerical_error = 0; enum parse_state { init = 0, name = 1, password = 2, age = 3, uid = 4, gid = 5, gecos = 6, home = 7, shell = 8, finished = 9 } state; if (stream == NULL || result == NULL || result_buf == NULL) { __set_errno (EFAULT); return -1; } else if (buffer == NULL) buflen = 0; fields[0] = NULL; fields[1] = &result_buf->pw_name; fields[2] = &result_buf->pw_passwd; fields[3] = &result_buf->pw_age; fields[4] = NULL; fields[5] = NULL; fields[6] = &result_buf->pw_gecos; fields[7] = &result_buf->pw_dir; fields[8] = &result_buf->pw_shell; fields[9] = NULL; *result = NULL; /* Now parse the passwd file. The technique looks awkwared but I don't see anything better. We read characters one by one from the passwd file keeping track of the current parse state. If we are in a state that reads a string then we copy the currently read byte into the user-supplied buffer. For numerical fields we re-calculate the current value. Thus we optimize the usage of the buffer and we stay re-entrant. */ state = init; memset (result_buf, 0, sizeof *result_buf); result_buf->pw_name = result_buf->pw_passwd = result_buf->pw_age = result_buf->pw_gecos = result_buf->pw_comment = result_buf->pw_dir = result_buf->pw_shell = space; while (state != finished) { int the_char = my_getc (stream); if (state == init) { if (the_char == ' ' || the_char == '\t' || the_char == '\r' || the_char == '\n') continue; else if (the_char == '#') { /* A comment, eat up the rest of the line. */ while (1) { the_char = my_getc (stream); if (the_char == '\n') break; else if (the_char == EOF) { *result = NULL; return -1; } } continue; } else if (the_char == EOF) { *result = NULL; return -1; } else { state++; ungetc (the_char, stream); *fields[state] = write_crs; continue; } } if (the_char == ':') { if (fields[state] != NULL && *fields[state] != NULL) { *write_crs++ = '\0'; bytes_used++; } ++state; if (state == age) /* No password aging found. */ ++state; if (bytes_used < buflen && fields[state] != NULL) *fields[state] = write_crs; numerical_error = 0; continue; } else if (the_char == '\n' || the_char == EOF) { /* Hm, what about crippled entries? Let the program handle this. */ if (state > init) { if (bytes_used < buflen && fields[state] != NULL && *fields[state] != NULL) { *write_crs++ = '\0'; bytes_used++; } else if (fields[state] != NULL) *fields[state] = NULL; /* Invalidates last read entry. */ state = finished; } else if (the_char == EOF) { *result = NULL; return -1; } continue; } if (bytes_used < buflen && fields[state] != NULL && *fields[state] != NULL) { *write_crs++ = the_char; bytes_used++; } switch (state) { case uid: if (the_char < '0' || the_char > '9') numerical_error = 1; /* Should we return an error instead? */ else if (!numerical_error) { result_buf->pw_uid *= 10; result_buf->pw_uid += the_char - '0'; } break; case gid: if (the_char < '0' || the_char > '9') numerical_error = 1; /* Should we return an error instead? */ else if (!numerical_error) { result_buf->pw_gid *= 10; result_buf->pw_gid += the_char - '0'; } break; case password: if (the_char == ',') { /* Age count found. */ if (bytes_used <= buflen && fields[state] != NULL && *fields[state] != NULL) { /* The comma has already been written. */ write_crs[-1] = '\0'; } ++state; if (bytes_used < buflen && fields[state] != NULL) *fields[state] = write_crs; } break; default: break; } } result_buf->pw_comment = result_buf->pw_gecos; *result = result_buf; return 0; }