⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gcc.c

📁 这是完整的gcc源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
}voidclear_failure_queue (){  failure_delete_queue = 0;}/* Compute a string to use as the base of all temporary file names.   It is substituted for %g.  */voidchoose_temp_base (){  extern char *getenv ();  char *base = getenv ("TMPDIR");  int len;  if (base == (char *)0)    {#ifdef P_tmpdir      if (access (P_tmpdir, R_OK | W_OK) == 0)	base = P_tmpdir;#endif      if (base == (char *)0)	{	  if (access ("/usr/tmp", R_OK | W_OK) == 0)	    base = "/usr/tmp/";	  else	    base = "/tmp/";	}    }  len = strlen (base);  temp_filename = (char *) xmalloc (len + sizeof("/ccXXXXXX"));  strcpy (temp_filename, base);  if (len > 0 && temp_filename[len-1] != '/')    temp_filename[len++] = '/';  strcpy (temp_filename + len, "ccXXXXXX");  mktemp (temp_filename);  temp_filename_length = strlen (temp_filename);}/* Search for an execute file through our search path.   Return 0 if not found, otherwise return its name, allocated with malloc.  */static char *find_exec_file (prog)     char *prog;{  int win = 0;  char *temp;  int size;  size = strlen (standard_exec_prefix);  if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)    size = strlen (user_exec_prefix);  if (env_exec_prefix != 0 && strlen (env_exec_prefix) > size)    size = strlen (env_exec_prefix);  if (strlen (standard_exec_prefix_1) > size)    size = strlen (standard_exec_prefix_1);  size += strlen (prog) + 1;  if (machine_suffix)    size += strlen (machine_suffix) + 1;  temp = (char *) xmalloc (size);  /* Determine the filename to execute.  */  if (user_exec_prefix)    {      if (machine_suffix)	{	  strcpy (temp, user_exec_prefix);	  strcat (temp, machine_suffix);	  strcat (temp, prog);	  win = (access (temp, X_OK) == 0);	}      if (!win)	{	  strcpy (temp, user_exec_prefix);	  strcat (temp, prog);	  win = (access (temp, X_OK) == 0);	}    }  if (!win && env_exec_prefix)    {      if (machine_suffix)	{	  strcpy (temp, env_exec_prefix);	  strcat (temp, machine_suffix);	  strcat (temp, prog);	  win = (access (temp, X_OK) == 0);	}      if (!win)	{	  strcpy (temp, env_exec_prefix);	  strcat (temp, prog);	  win = (access (temp, X_OK) == 0);	}    }  if (!win)    {      if (machine_suffix)	{	  strcpy (temp, standard_exec_prefix);	  strcat (temp, machine_suffix);	  strcat (temp, prog);	  win = (access (temp, X_OK) == 0);	}      if (!win)	{	  strcpy (temp, standard_exec_prefix);	  strcat (temp, prog);	  win = (access (temp, X_OK) == 0);	}    }  if (!win)    {      if (machine_suffix)	{	  strcpy (temp, standard_exec_prefix_1);	  strcat (temp, machine_suffix);	  strcat (temp, prog);	  win = (access (temp, X_OK) == 0);	}      if (!win)	{	  strcpy (temp, standard_exec_prefix_1);	  strcat (temp, prog);	  win = (access (temp, X_OK) == 0);	}    }  if (win)    return temp;  else    return 0;}/* stdin file number.  */#define STDIN_FILE_NO 0/* stdout file number.  */#define STDOUT_FILE_NO 1/* value of `pipe': port index for reading.  */#define READ_PORT 0/* value of `pipe': port index for writing.  */#define WRITE_PORT 1/* Pipe waiting from last process, to be used as input for the next one.   Value is STDIN_FILE_NO if no pipe is waiting   (i.e. the next command is the first of a group).  */int last_pipe_input;/* Fork one piped subcommand.  FUNC is the system call to use   (either execv or execvp).  ARGV is the arg vector to use.   NOT_LAST is nonzero if this is not the last subcommand   (i.e. its output should be piped to the next one.)  */static intpexecute (func, program, argv, not_last)     char *program;     int (*func)();     char *argv[];     int not_last;{  int pid;  int pdes[2];  int input_desc = last_pipe_input;  int output_desc = STDOUT_FILE_NO;  /* If this isn't the last process, make a pipe for its output,     and record it as waiting to be the input to the next process.  */  if (not_last)    {      if (pipe (pdes) < 0)	pfatal_with_name ("pipe");      output_desc = pdes[WRITE_PORT];      last_pipe_input = pdes[READ_PORT];    }  else    last_pipe_input = STDIN_FILE_NO;  pid = vfork ();  switch (pid)    {    case -1:      pfatal_with_name ("vfork");      break;    case 0: /* child */      /* Move the input and output pipes into place, if nec.  */      if (input_desc != STDIN_FILE_NO)	{	  close (STDIN_FILE_NO);	  dup (input_desc);	  close (input_desc);	}      if (output_desc != STDOUT_FILE_NO)	{	  close (STDOUT_FILE_NO);	  dup (output_desc);	  close (output_desc);	}      /* Close the parent's descs that aren't wanted here.  */      if (last_pipe_input != STDIN_FILE_NO)	close (last_pipe_input);      /* Exec the program.  */      (*func) (program, argv);      perror_exec (program);      exit (-1);      /* NOTREACHED */    default:      /* In the parent, after forking.	 Close the descriptors that we made for this child.  */      if (input_desc != STDIN_FILE_NO)	close (input_desc);      if (output_desc != STDOUT_FILE_NO)	close (output_desc);      /* Return child's process number.  */      return pid;    }}/* Execute the command specified by the arguments on the current line of spec.   When using pipes, this includes several piped-together commands   with `|' between them.   Return 0 if successful, -1 if failed.  */intexecute (){  int i, j;  int n_commands;		/* # of command.  */  char *string;  struct command    {      char *prog;		/* program name.  */      char **argv;		/* vector of args.  */      int pid;			/* pid of process for this command.  */    };  struct command *commands;	/* each command buffer with above info.  */  /* Count # of piped commands.  */  for (n_commands = 1, i = 0; i < argbuf_index; i++)    if (strcmp (argbuf[i], "|") == 0)      n_commands++;  /* Get storage for each command.  */  commands    = (struct command *) alloca (n_commands * sizeof (struct command));  /* Split argbuf into its separate piped processes,     and record info about each one.     Also search for the programs that are to be run.  */  commands[0].prog = argbuf[0]; /* first command.  */  commands[0].argv = &argbuf[0];  string = find_exec_file (commands[0].prog);  if (string)    commands[0].argv[0] = string;  for (n_commands = 1, i = 0; i < argbuf_index; i++)    if (strcmp (argbuf[i], "|") == 0)      {				/* each command.  */	argbuf[i] = 0;	/* termination of command args.  */	commands[n_commands].prog = argbuf[i + 1];	commands[n_commands].argv = &argbuf[i + 1];	string = find_exec_file (commands[n_commands].prog);	if (string)	  commands[n_commands].argv[0] = string;	n_commands++;      }  argbuf[argbuf_index] = 0;  /* If -v, print what we are about to do, and maybe query.  */  if (vflag)    {      /* Print each piped command as a separate line.  */      for (i = 0; i < n_commands ; i++)	{	  char **j;	  for (j = commands[i].argv; *j; j++)	    fprintf (stderr, " %s", *j);	  /* Print a pipe symbol after all but the last command.  */	  if (i + 1 != n_commands)	    fprintf (stderr, " |");	  fprintf (stderr, "\n");	}      fflush (stderr);#ifdef DEBUG      fprintf (stderr, "\nGo ahead? (y or n) ");      fflush (stderr);      j = getchar ();      if (j != '\n')	while (getchar () != '\n') ;      if (j != 'y' && j != 'Y')	return 0;#endif /* DEBUG */    }  /* Run each piped subprocess.  */  last_pipe_input = STDIN_FILE_NO;  for (i = 0; i < n_commands; i++)    {      extern int execv(), execvp();      char *string = commands[i].argv[0];      commands[i].pid = pexecute ((string != commands[i].prog ? execv : execvp),				  string, commands[i].argv,				  i + 1 < n_commands);      if (string != commands[i].prog)	free (string);    }  execution_count++;  /* Wait for all the subprocesses to finish.     We don't care what order they finish in;     we know that N_COMMANDS waits will get them all.  */  {    int ret_code = 0;    for (i = 0; i < n_commands; i++)      {	int status;	int pid;	char *prog;	pid = wait (&status);	if (pid < 0)	  abort ();	if (status != 0)	  {	    int j;	    for (j = 0; j < n_commands; j++)	      if (commands[j].pid == pid)		prog = commands[j].prog;	    if ((status & 0x7F) != 0)	      fatal ("Program %s got fatal signal %d.", prog, (status & 0x7F));	    if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)	      ret_code = -1;	  }      }    return ret_code;  }}/* Find all the switches given to us   and make a vector describing them.   The elements of the vector a strings, one per switch given.   If a switch uses the following argument, then the `part1' field   is the switch itself and the `part2' field is the following argument.   The `valid' field is nonzero if any spec has looked at this switch;   if it remains zero at the end of the run, it must be meaningless.  */struct switchstr{  char *part1;  char *part2;  int valid;};struct switchstr *switches;int n_switches;/* Also a vector of input files specified.  */char **infiles;int n_infiles;/* And a vector of corresponding output files is made up later.  */char **outfiles;/* Create the vector `switches' and its contents.   Store its length in `n_switches'.  */voidprocess_command (argc, argv)     int argc;     char **argv;{  extern char *getenv ();  register int i;  n_switches = 0;  n_infiles = 0;  env_exec_prefix = getenv ("GCC_EXEC_PREFIX");  /* Scan argv twice.  Here, the first time, just count how many switches     there will be in their vector, and how many input files in theirs.     Here we also parse the switches that cc itself uses (e.g. -v).  */  for (i = 1; i < argc; i++)    {      if (argv[i][0] == '-' && argv[i][1] != 'l')	{	  register char *p = &argv[i][1];	  register int c = *p;	  switch (c)	    {	    case 'b':	      machine_suffix = p + 1;	      break;	    case 'B':	      user_exec_prefix = p + 1;	      break;	    case 'v':	/* Print our subcommands and print versions.  */	      vflag++;	      n_switches++;	      break;	    default:	      n_switches++;	      if (SWITCH_TAKES_ARG (c) && p[1] == 0)		i++;	      else if (WORD_SWITCH_TAKES_ARG (p))		i++;	    }	}      else	n_infiles++;    }  /* Then create the space for the vectors and scan again.  */  switches = ((struct switchstr *)	      xmalloc ((n_switches + 1) * sizeof (struct switchstr)));  infiles = (char **) xmalloc ((n_infiles + 1) * sizeof (char *));  n_switches = 0;  n_infiles = 0;  /* This, time, copy the text of each switch and store a pointer     to the copy in the vector of switches.     Store all the infiles in their vector.  */  for (i = 1; i < argc; i++)    {      if (argv[i][0] == '-' && argv[i][1] != 'l')	{	  register char *p = &argv[i][1];	  register int c = *p;	  if (c == 'B' || c == 'b')	    continue;	  switches[n_switches].part1 = p;	  if ((SWITCH_TAKES_ARG (c) && p[1] == 0)	      || WORD_SWITCH_TAKES_ARG (p))	    switches[n_switches].part2 = argv[++i];	  else if (c == 'o') {	    /* On some systems, ld cannot handle -o without space.	       So split the -o and its argument.  */	    switches[n_switches].part2 = (char *) xmalloc (strlen (p));	    strcpy (switches[n_switches].part2, &p[1]);	    p[1] = 0;	  } else	    switches[n_switches].part2 = 0;	  switches[n_switches].valid = 0;	  n_switches++;	}      else	infiles[n_infiles++] = argv[i];    }  switches[n_switches].part1 = 0;  infiles[n_infiles] = 0;}/* Process a spec string, accumulating and running commands.  *//* These variables describe the input file name.   input_file_number is the index on outfiles of this file,   so that the output file name can be stored for later use by %o.   input_basename is the start of the part of the input file   sans all directory names, and basename_length is the number   of characters starting there excluding the suffix .c or whatever.  */char *input_filename;int input_file_number;int input_filename_length;int basename_length;char *input_basename;/* These are variables used within do_spec and do_spec_1.  *//* Nonzero if an arg has been started and not yet terminated   (with space, tab or newline).  */int arg_going;/* Nonzero means %d or %g has been seen; the next arg to be terminated   is a temporary file name.  */int delete_this_arg;/* Nonzero means %w has been seen; the next arg to be terminated   is the output file name of this compilation.  */int this_is_output_file;/* Nonzero means %s has been seen; the next arg to be terminated   is the name of a library file and we should try the standard   search dirs for it.  */int this_is_library_file;/* Process the spec SPEC and run the commands specified therein.   Returns 0 if the spec is successfully processed; -1 if failed.  */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -