[OT] Which IDE / Editor do you use?

H. S. Teoh hsteoh at quickfur.ath.cx
Sun Sep 15 18:39:01 PDT 2013


On Sun, Sep 15, 2013 at 12:53:24AM -0400, Nick Sabalausky wrote:
> On Sat, 14 Sep 2013 03:08:32 -0700
> "H. S. Teoh" <hsteoh at quickfur.ath.cx> wrote:
[...]
> > It's one of the many reasons I have an aversion to all things GUI.
> > In fact, in *my* book, a proper GUI program should automatically
> > detach itself from the terminal at startup -- there are well-known,
> > standard ways of doing this, but alas, most GUI developers don't
> > care enough to do it.)
> > 
> 
> I would buy that book ;) Actually though, do you have a link regarding
> that auto-detaching?

Well... it's actually very simple:

	int main(int argc, char *argv[]) {
		if (fork()==0) {
			/* debugMode gets all the spewage, since
			 * presumably the GUI developer actually cares
			 * about that stuff. */
			if (!debugMode) {
				/* Swallow all spewage */
				fclose(stdin);
				fclose(stdout);
				fclose(stderr);
			}

			/* GUI code doesn't need to run in terminal */
			startGui();
		}

		/* So return control back to shell immediately. */
		exit(0);
	}

:)

Though, granted, you probably want to reopen stdin/stdout/stderr
(possibly as /dev/null) in case something chokes on errors caused by
writing to a closed file descriptor. But in C, nobody cares about the
return code of printf anyway, so this is mostly a non-issue.

(Of course, Unix does have a few dark corners, and here is one of them
that catches most people unaware. Consider what happens after you call
fclose on stdin/stdout/stderr. Unix, by convention, assigns them to file
descriptors 0, 1, and 2. Since they are now closed, if you then open
other resource files, say, a database file (via whatever DB library
you're using), it gets assigned the lowest unused file descriptor, that
is, 0, then 1, etc.. Now consider what happens when there are random
printf's scattered all over the code, and you're unlucky enough to have
your DB file's fd assigned to 1.)


T

-- 
First Rule of History: History doesn't repeat itself -- historians merely repeat each other.


More information about the Digitalmars-d mailing list