+/* Return true if the inferior is using the same TTY for input as GDB
+ is. If this is true, then we save/restore terminal flags/state.
+
+ This is necessary because if inf->attach_flag is set, we don't
+ offhand know whether we are sharing a terminal with the inferior or
+ not. Attaching a process without a terminal is one case where we
+ do not; attaching a process which we ran from the same shell as GDB
+ via `&' is one case where we do.
+
+ If we can't determine, we assume the TTY is being shared. This
+ works OK if you're only debugging one inferior. However, if you're
+ debugging more than one inferior, and e.g., one is spawned by GDB
+ with "run" (sharing terminal with GDB), and another is attached to
+ (and running on a different terminal, as is most common), then it
+ matters, because we can only restore the terminal settings of one
+ of the inferiors, and in that scenario, we want to restore the
+ settings of the "run"'ed inferior.
+
+ Note, this is not the same as determining whether GDB and the
+ inferior are in the same session / connected to the same
+ controlling tty. An inferior (fork child) may call setsid,
+ disconnecting itself from the ctty, while still leaving
+ stdin/stdout/stderr associated with the original terminal. If
+ we're debugging that process, we should also save/restore terminal
+ settings. */
+
+static bool
+sharing_input_terminal (inferior *inf)
+{
+ terminal_info *tinfo = get_inflow_inferior_data (inf);
+
+ tribool res = sharing_input_terminal_1 (inf);
+
+ if (res == TRIBOOL_UNKNOWN)
+ {
+ /* As fallback, if we can't determine by stat'ing the inferior's
+ tty directly (because it's not supported on this host) and
+ the child was spawned, check whether run_terminal is our tty.
+ This isn't ideal, since this is checking the child's
+ controlling terminal, not the input terminal (which may have
+ been redirected), but is still better than nothing. A false
+ positive ("set inferior-tty" points to our terminal, but I/O
+ was redirected) is much more likely than a false negative
+ ("set inferior-tty" points to some other terminal, and then
+ output was redirected to our terminal), and with a false
+ positive we just end up trying to save/restore terminal
+ settings when we didn't need to or we actually can't. */
+ if (tinfo->run_terminal != NULL)
+ res = is_gdb_terminal (tinfo->run_terminal);
+
+ /* If we still can't determine, assume yes. */
+ if (res == TRIBOOL_UNKNOWN)
+ return true;
+ }
+
+ return res == TRIBOOL_TRUE;
+}
+
+/* Put the inferior's terminal settings into effect. This is
+ preparation for starting or resuming the inferior. */