int main(void) { char buf[MAXLINE]; if (tfgets(buf, MAXLINE, stdin) == NULL) printf("BOOM!\n"); else printf("%s", buf); exit(0); }
static int recompile_files (void) { file *f; putenv (xstrdup ("COMPILER_PATH=")); putenv (xstrdup ("LIBRARY_PATH=")); while ((f = file_pop ()) != NULL) { char *line, *command; FILE *stream = fopen (f->key, "r"); const char *const outname = frob_extension (f->key, ".rnw"); FILE *output = fopen (outname, "w"); while ((line = tfgets (stream)) != NULL) { switch (line[0]) { case 'C': case 'O': maybe_tweak (line, f); } fprintf (output, "%s\n", line); } fclose (stream); fclose (output); rename (outname, f->key); obstack_grow (&temporary_obstack, "cd ", 3); obstack_grow (&temporary_obstack, f->dir, strlen (f->dir)); obstack_grow (&temporary_obstack, "; ", 2); obstack_grow (&temporary_obstack, c_file_name, strlen (c_file_name)); obstack_1grow (&temporary_obstack, ' '); obstack_grow (&temporary_obstack, f->args, strlen (f->args)); obstack_1grow (&temporary_obstack, ' '); command = obstack_copy0 (&temporary_obstack, f->main, strlen (f->main)); if (tlink_verbose) fprintf (stderr, _("collect: recompiling %s\n"), f->main); if (tlink_verbose >= 3) fprintf (stderr, "%s\n", command); if (system (command) != 0) return 0; read_repo_file (f); obstack_free (&temporary_obstack, temporary_firstobj); } return 1; }
static void freadsym (FILE *stream, file *f, int chosen) { symbol *sym; { const char *name = tfgets (stream); sym = symbol_hash_lookup (name, true); } if (sym->file == NULL) { /* We didn't have this symbol already, so we choose this file. */ symbol_push (sym); sym->file = f; sym->chosen = chosen; } else if (chosen) { /* We want this file; cast aside any pretender. */ if (sym->chosen && sym->file != f) { if (sym->chosen == 1) file_push (sym->file); else { file_push (f); f = sym->file; chosen = sym->chosen; } } sym->file = f; sym->chosen = chosen; } }
static int scan_linker_output (const char *fname) { FILE *stream = fopen (fname, "r"); char *line; while ((line = tfgets (stream)) != NULL) { char *p = line, *q; symbol *sym; int end; while (*p && ISSPACE ((unsigned char) *p)) ++p; if (! *p) continue; for (q = p; *q && ! ISSPACE ((unsigned char) *q); ++q) ; /* Try the first word on the line. */ if (*p == '.') ++p; if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX))) p += strlen (USER_LABEL_PREFIX); end = ! *q; *q = 0; sym = symbol_hash_lookup (p, false); /* Some SVR4 linkers produce messages like ld: 0711-317 ERROR: Undefined symbol: .g__t3foo1Zi */ if (! sym && ! end && strstr (q + 1, "Undefined symbol: ")) { char *p = strrchr (q + 1, ' '); p++; if (*p == '.') p++; if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX))) p += strlen (USER_LABEL_PREFIX); sym = symbol_hash_lookup (p, false); } if (! sym && ! end) /* Try a mangled name in quotes. */ { const char *oldq = q + 1; demangled *dem = 0; q = 0; /* First try `GNU style'. */ p = strchr (oldq, '`'); if (p) p++, q = strchr (p, '\''); /* Then try "double quotes". */ else if (p = strchr (oldq, '"'), p) p++, q = strchr (p, '"'); else { /* Then try entire line. */ q = strchr (oldq, 0); if (q != oldq) p = (char *)oldq; } if (p) { /* Don't let the strstr's below see the demangled name; we might get spurious matches. */ p[-1] = '\0'; /* powerpc64-linux references .foo when calling function foo. */ if (*p == '.') p++; } /* We need to check for certain error keywords here, or we would mistakenly use GNU ld's "In function `foo':" message. */ if (q && (strstr (oldq, "ndefined") || strstr (oldq, "nresolved") || strstr (oldq, "nsatisfied") || strstr (oldq, "ultiple"))) { *q = 0; dem = demangled_hash_lookup (p, false); if (dem) sym = symbol_hash_lookup (dem->mangled, false); else { if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX))) p += strlen (USER_LABEL_PREFIX); sym = symbol_hash_lookup (p, false); } } } if (sym && sym->tweaked) { error ("`%s' was assigned to `%s', but was not defined " "during recompilation, or vice versa", sym->key, sym->file->key); fclose (stream); return 0; } if (sym && !sym->tweaking) { if (tlink_verbose >= 2) fprintf (stderr, _("collect: tweaking %s in %s\n"), sym->key, sym->file->key); sym->tweaking = 1; file_push (sym->file); } obstack_free (&temporary_obstack, temporary_firstobj); } fclose (stream); return (file_stack != NULL); }
static int recompile_files (void) { file *f; putenv (xstrdup ("COMPILER_PATH=")); putenv (xstrdup ("LIBRARY_PATH=")); while ((f = file_pop ()) != NULL) { char *line; const char *p, *q; char **argv; struct obstack arg_stack; FILE *stream = fopen (f->key, "r"); const char *const outname = frob_extension (f->key, ".rnw"); FILE *output = fopen (outname, "w"); while ((line = tfgets (stream)) != NULL) { switch (line[0]) { case 'C': case 'O': maybe_tweak (line, f); } fprintf (output, "%s\n", line); } fclose (stream); fclose (output); rename (outname, f->key); if (!f->args) { error ("repository file `%s' does not contain command-line " "arguments", f->key); return 0; } /* Build a null-terminated argv array suitable for tlink_execute(). Manipulate arguments on the arg_stack while building argv on the temporary_obstack. */ obstack_init (&arg_stack); obstack_ptr_grow (&temporary_obstack, c_file_name); for (p = f->args; *p != '\0'; p = q + 1) { /* Arguments are delimited by single-quotes. Find the opening quote. */ p = strchr (p, '\''); if (!p) goto done; /* Find the closing quote. */ q = strchr (p + 1, '\''); if (!q) goto done; obstack_grow (&arg_stack, p + 1, q - (p + 1)); /* Replace '\'' with '. This is how set_collect_gcc_options encodes a single-quote. */ while (q[1] == '\\' && q[2] == '\'' && q[3] == '\'') { const char *r; r = strchr (q + 4, '\''); if (!r) goto done; obstack_grow (&arg_stack, q + 3, r - (q + 3)); q = r; } obstack_1grow (&arg_stack, '\0'); obstack_ptr_grow (&temporary_obstack, obstack_finish (&arg_stack)); } done: obstack_ptr_grow (&temporary_obstack, f->main); obstack_ptr_grow (&temporary_obstack, NULL); argv = obstack_finish (&temporary_obstack); if (tlink_verbose) fprintf (stderr, _("collect: recompiling %s\n"), f->main); if (chdir (f->dir) != 0 || tlink_execute (c_file_name, argv, NULL) != 0 || chdir (initial_cwd) != 0) return 0; read_repo_file (f); obstack_free (&arg_stack, NULL); obstack_free (&temporary_obstack, temporary_firstobj); } return 1; }
static char * pfgets (FILE *stream) { return xstrdup (tfgets (stream)); }