0

I've created a new file in order to test my lookup function for pset6.

Here's the content of my test file:

#include <stdio.h>

#include "server.c"

int main(int argc, char* argv[]) {
    printf("html was: %s\n", lookup("html"));
    printf("css  was: %s\n", lookup("css"));
    printf("test was: %s\n", lookup("failing test"));
}

However, using make on this file (test-s.c) creates numerous errors, such as:

clang -ggdb3 -O0 -std=c11 -Wall -Werror -o test-s test-s.c server server.c -lms
In file included from test-s.c:3:
./server.c:87:19: error: implicit declaration of function 'getopt' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    while ((opt = getopt(argc, argv, "hp:")) != -1)
                  ^
./server.c:98:29: error: use of undeclared identifier 'optarg'
                port = atoi(optarg);
                            ^
./server.c:104:45: error: use of undeclared identifier 'optind'
    if (port < 0 || port > SHRT_MAX || argv[optind] == NULL || strlen(argv[optind]) == 0)
                                            ^
./server.c:104:76: error: use of undeclared identifier 'optind'
    if (port < 0 || port > SHRT_MAX || argv[optind] == NULL || strlen(argv[optind]) == 0)
                                                                           ^
./server.c:114:22: error: use of undeclared identifier 'optind'
    start(port, argv[optind]);
                     ^
./server.c:117:22: error: variable has incomplete type 'struct sigaction'
    struct sigaction act;
                     ^
./server.c:117:12: note: forward declaration of 'struct sigaction'
    struct sigaction act;
           ^
./server.c:120:5: error: implicit declaration of function 'sigemptyset' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    sigemptyset(&act.sa_mask);
    ^
./server.c:121:5: error: implicit declaration of function 'sigaction' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    sigaction(SIGINT, &act, NULL);
    ^
./server.c:471:18: error: implicit declaration of function 'popen' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    FILE* file = popen(command, "r");
                 ^
./server.c:471:18: note: did you mean 'fopen'?
/usr/include/stdio.h:272:14: note: 'fopen' declared here
extern FILE *fopen (const char *__restrict __filename,
             ^
In file included from test-s.c:3:
./server.c:471:11: error: incompatible integer to pointer conversion initializing 'FILE *' (aka 'struct _IO_FILE *') with an expression of type 'int'
      [-Werror,-Wint-conversion]
    FILE* file = popen(command, "r");
          ^      ~~~~~~~~~~~~~~~~~~~
./server.c:488:5: error: implicit declaration of function 'pclose' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    pclose(file);
    ^
./server.c:537:13: error: implicit declaration of function 'scandir' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    int n = scandir(path, &namelist, NULL, alphasort);
            ^
./server.c:537:44: error: use of undeclared identifier 'alphasort'
    int n = scandir(path, &namelist, NULL, alphasort);
                                           ^
./server.c:829:9: error: implicit declaration of function 'dprintf' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    if (dprintf(cfd, "HTTP/1.1 %i %s\r\n", code, phrase) < 0)
        ^
./server.c:873:12: error: implicit declaration of function 'realpath' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    root = realpath(path, NULL);
           ^
./server.c:873:10: error: incompatible integer to pointer conversion assigning to 'char *' from 'int' [-Werror,-Wint-conversion]
    root = realpath(path, NULL);
         ^ ~~~~~~~~~~~~~~~~~~~~
test-s.c:5:5: error: redefinition of 'main'
int main(int argc, char* argv[]) {
    ^
./server.c:72:5: note: previous definition is here
int main(int argc, char* argv[])
    ^
17 errors generated.
make: *** [test-s] Error 1

This is what I have for my Make file:

server: server.c Makefile
    clang -ggdb3 -O0 -std=c11 -Wall -Werror -o server server.c -lm

test-s: test-s.c Makefile
    clang -ggdb3 -O0 -std=c11 -Wall -Werror -o test-s test-s.c server server.c -lms

clean:
    rm -f *.o core server test

Finally, this is my lookup function:

const char* lookup(const char* path)
{

    char* returnValue = strrchr(path, '.');

    if (strcasecmp(returnValue, ".css") == 0) {
        return "text/css";
    } else if (strcasecmp(returnValue, ".html") == 0) {
        return "text/html";
    } else if (strcasecmp(returnValue, ".gif") == 0) {
        return "image/gif";
    } else if (strcasecmp(returnValue, ".ico") == 0) {
        return "image/x-icon";
    } else if (strcasecmp(returnValue, ".jpg") == 0) {
        return "image/jpg";
    } else if (strcasecmp(returnValue, ".js") == 0) {
        return "text/javascript";
    } else if (strcasecmp(returnValue, ".php") == 0) {
        return "text/x-php";
    } else if (strcasecmp(returnValue, ".png") == 0) {
        return "image/png";
    } else {
        return NULL;
    }
}

I find these errors to be odd, since running make server yields no error, yet running make test-s yields numerous errors. I have also used test files for my other psets in the past without any errors, so this issue is odd for me.

  • I think the problem could be with the #include "server.c" and the makefile, as discussed here. I also ran into some test trouble when doing this pset, so at first I just copied the relevant function and variable declarations in my test file. In the end, however, I used gdb debugging to test the functions line by line and it was really helpful. Let me know if you need help doing that (I had to look for a while before I found out how to do it). – Sraffa Sep 12 '16 at 4:39
  • Thanks for your reply. When using gdb, how can I access gdb's functions (such as next) when the server is running? – Lyxpudox Sep 13 '16 at 2:54
  • What I did was add a break statement before running it. So something like: 1) gdb server, 2) break lookup (or a line number) and then 3) run public to start the server. Then I sent requests from another terminal window (for example curl -i http://localhost:8080/hello.html), and in the terminal where the server is running you should see that you are now able to debug the code line by line. – Sraffa Sep 14 '16 at 13:58
  • Thanks! Hopefully everything will work well. – Lyxpudox Sep 17 '16 at 3:10

You must log in to answer this question.

Browse other questions tagged .