summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLAN-TW <lantw44@gmail.com>2013-10-13 11:23:46 +0800
committerLAN-TW <lantw44@gmail.com>2013-10-13 11:23:46 +0800
commit96f5002d13aa53e25a55a837a6d2d7f536f44a68 (patch)
tree6af701f2c052a03d6546d825016f4ada4829e4c6
parent30c51b48403b2a21ac0783b6a6bf830d3999f6f1 (diff)
downloadsp2013-96f5002d13aa53e25a55a837a6d2d7f536f44a68.tar.gz
sp2013-96f5002d13aa53e25a55a837a6d2d7f536f44a68.tar.zst
sp2013-96f5002d13aa53e25a55a837a6d2d7f536f44a68.zip
HW1: 加入可攜版本的 getdtablesize
-rw-r--r--hw1/configure.ac3
-rw-r--r--hw1/main.c25
2 files changed, 25 insertions, 3 deletions
diff --git a/hw1/configure.ac b/hw1/configure.ac
index b4cf785..0193228 100644
--- a/hw1/configure.ac
+++ b/hw1/configure.ac
@@ -10,7 +10,8 @@ AM_SILENT_RULES([yes])
# Checks for programs.
AC_PROG_CC
-AC_PROG_CC_STDC
+AC_PROG_CC_C99
+AC_C_VOLATILE
AC_PROG_RANLIB
# Checks for typedefs, structures, and compiler characteristics.
diff --git a/hw1/main.c b/hw1/main.c
index a35984d..cec60c8 100644
--- a/hw1/main.c
+++ b/hw1/main.c
@@ -38,8 +38,9 @@ static void message_cb (server* svr, request* req, int maxfd) {
#endif
" Server hostname: %s\n"
" Server port: %hu\n"
+ " Server maxfd: %d\n"
" Server is listening on fd %d\n",
- svr->hostname, svr->port, svr->listen_fd);
+ svr->hostname, svr->port, maxfd, svr->listen_fd);
puts ("Dump request table ...");
for (int i = 0; i < maxfd; i++) {
@@ -79,7 +80,21 @@ static void message_cb (server* svr, request* req, int maxfd) {
proc_message_request = 0;
}
-int main(int argc, char** argv) {
+#ifndef HAVE_GETDTABLESIZE
+static int my_getdtablesize (void) {
+ long rval = sysconf (_SC_OPEN_MAX);
+ if (rval < 0) {
+# ifdef _POSIX_OPEN_MAX
+ rval = _POSIX_OPEN_MAX
+# else
+ rval = 20;
+# endif
+ }
+ return rval;
+}
+#endif
+
+int main (int argc, char** argv) {
if (argc != 2) {
fprintf(stderr, "usage: %s [port]\n", argv[0]);
exit(1);
@@ -98,7 +113,13 @@ int main(int argc, char** argv) {
sigaction (SIGQUIT, &signal_action, NULL);
// Get file descripter table size
+#ifdef HAVE_GETDTABLESIZE
int maxfd = getdtablesize();
+#else
+ int maxfd = my_getdtablesize();
+#endif
+ if (maxfd < 0)
+ e_err_exit ("getdtablesize");
// Initialize server
server svr; // server