annotate Python/dup2.c @ 28646:193b8aec00ce legacy-trunk

Changes from Jonathan Riehl to allow his pgen extension (PEP 269) to work. This includes some more code that used to be part of pgen in the main parser; I'm okay with that. I'll see if the Windows build needs work next.
author Guido van Rossum <guido@python.org>
date Thu, 17 Apr 2003 14:55:42 +0000
parents f79434ee41fa
children cd73917f2478
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1449
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /*
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 * Public domain dup2() lookalike
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 * by Curtis Jackson @ AT&T Technologies, Burlington, NC
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 * electronic address: burl!rcj
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 *
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 * dup2 performs the following functions:
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 *
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 * Check to make sure that fd1 is a valid open file descriptor.
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 * Check to see if fd2 is already open; if so, close it.
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 * Duplicate fd1 onto fd2; checking to make sure fd2 is a valid fd.
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 * Return fd2 if all went well; return BADEXIT otherwise.
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 */
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 #include <fcntl.h>
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 #define BADEXIT -1
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 1825
diff changeset
19 dup2(int fd1, int fd2)
1449
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 {
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 if (fd1 != fd2) {
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 if (fcntl(fd1, F_GETFL) < 0)
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 return BADEXIT;
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 if (fcntl(fd2, F_GETFL) >= 0)
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 close(fd2);
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 if (fcntl(fd1, F_DUPFD, fd2) < 0)
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 return BADEXIT;
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 }
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 return fd2;
ae93ec4dd556 New files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 }