'Getting Bus error: 10 when I run my C code
I'm writing a function in C to make my web server display the directory listing as an HTML document. When I run make, everything seems to compile fine, but when I run the file, I keep getting bus error:10. Thank you for all of your help!
This is the HTML template that I want to write into the socket id to display when I run my file on localhost:3000 (or localhost:8000):
<ul>
<li><a href='./'>.</a></li>
<li><a href='../'>..</a></li>
<li><a href='.DS_Store'>.DS_Store</a></li>
<li><a href='resume/'>resume</a></li>
<li><a href='lolcode.html'>lolcode.html</a></li>
<li><a href='ybruf.pid'>ybruf.pid</a></li>
</ul>
This is my code for this function:
static bool process_dir(int sock_id, char *doc)
{
// Open the directory for reading
DIR *dir = opendir(doc);
/* if the dir failed to open */
if (dir == NULL)
{
syslog(LOG_ERR, "opendir(): %s", strerror(errno));
write_http_header(sock_id, PROTO "404 File Not Found",
"File Not Found");
return false;
}
// Write the header - we seem to be ok
// Technically, this is not correct. We must first fully read the direct
/* successfull open the dir */
write_http_header(sock_id, PROTO "200 OK", NULL);
// Define the template
struct dirent *dirp;
dirp = readdir(dir);
char dirp_name[1024]; // if it is a diretory then dirp_name will have /
char *ul_open = "<ul>\n";
char *ul_close = "</ul>";
write(sock_id, ul_open, strlen(ul_open));
char *li_open = "<li><a href='";
char *li_middle = "'>";
char *li_close = "</a></li>\n";
while (dirp != NULL)
{
// if it's a file, then just need to get the name
// for example: resume.html
strcpy(dirp_name, dirp->d_name);
if (dirp->d_type == DT_DIR)
{
// if it's a directory then need to have / at the end
// for example: resume/
strcat(dirp_name, "/");
}
// for example: <li><a href='resume/'>resume</a></ li>
write(sock_id, li_open, strlen(li_open));
write(sock_id, dirp_name, strlen(dirp_name));
write(sock_id, li_middle, strlen(li_middle));
write(sock_id, dirp->d_name, dirp->d_namlen);
write(sock_id, li_close, strlen(li_close));
dirp = readdir(dir);
}
write(sock_id, ul_close, strlen(ul_close));
errno = 0; // Assume no errors
closedir(dir);
if (errno)
{
// Too late to complain!
syslog(LOG_ERR, "%s", strerror(errno));
}
return errno == 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
