gpt4 book ai didi

c - 在 C 中使用 tm 时出现段错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:57:22 25 4
gpt4 key购买 nike

所以我在下面发布了我的代码(抱歉,我知道它很长),但是当我尝试使用 tm 结构进行任何操作时,我遇到了段错误。我不知道为什么我会遇到这个段错误,我很确定我以前让它工作过,但现在我就是无法让它工作。如果有人知道如何摆脱段错误,那就太好了。段错误出现在代码块中下部的嵌套 if 语句的第一个,它在长长的 if-else block 之后。

非常感谢!

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <time.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <assert.h>


int main(){

int sockfd, clifd, status, monthModifiedi, newer;
struct sockaddr_in addr;
//char buf[1024] = "HEAD httpserver.c HTTP/1.1\nHost: www.reddit.com";
char buf[1024];
struct stat statBuf;
char *message, *filePath, *domain, *monthModified, *dayModified, *hourModified, *minuteModified, *secondModified, *yearModified;
FILE *pFile;
struct tm *tm;
time_t time;

//instantiate
strcpy(buf,"HEAD /httpserver.c HTTP/1.1\nHost: www.reddit.com\nIf-Modified-Since: Thu Mar 28 02:20:08 2013");

printf("%s\n\n", buf);

//parse buffer
message = strtok(buf, " ");
filePath = strtok(NULL, " ");
domain = strtok(NULL, "\n");
domain = strtok(NULL, " "); //domain will be overwritten with actual doman, this is filler
domain = strtok(NULL, " \n");
monthModified = strtok(NULL, " ");

printf("%s\n", filePath);
if(stat(filePath, &statBuf) == 0){
tm = localtime(&statBuf.st_mtime);
//time = &statBuf.st_mtime;
}

//newer will keep this value of 2 if there is no conditional GET request
newer = 2;

//If it is a conditional GET request
if(strcmp(monthModified, "If-Modified-Since:") == 0){

monthModified = strtok(NULL, " ");
monthModified = strtok(NULL, " ");
dayModified = strtok(NULL, " ");
hourModified = strtok(NULL, " :");
minuteModified = strtok(NULL,":");
secondModified = strtok(NULL,": ");
yearModified = strtok(NULL, " ");

//Turns string version of month to numeric value
if(strcmp(monthModified,"Jan") ==0)
monthModifiedi = 1;
else if(strcmp(monthModified,"Feb")==0)
monthModifiedi = 2;
else if(strcmp(monthModified,"Mar") ==0)
monthModifiedi = 3;
else if(strcmp(monthModified,"Apr") ==0)
monthModifiedi = 4;
else if(strcmp(monthModified,"May") ==0)
monthModifiedi = 5;
else if(strcmp(monthModified,"Jun") ==0)
monthModifiedi = 6;
else if(strcmp(monthModified,"Jul") ==0)
monthModifiedi = 7;
else if(strcmp(monthModified,"Aug") ==0)
monthModifiedi = 8;
else if(strcmp(monthModified, "Sep") ==0)
monthModifiedi = 9;
else if(strcmp(monthModified,"Oct") ==0)
monthModifiedi = 10;
else if(strcmp(monthModified, "Nov") ==0)
monthModifiedi = 11;
else if(strcmp(monthModified,"Dec") ==0)
monthModifiedi = 12;

//Determines if file has been modified since date requested from client
newer = 0;
if(atoi(yearModified) > (*tm).tm_year)
if(monthModifiedi > (*tm).tm_mon)
if(atoi(dayModified) > (*tm).tm_mday)
if(atoi(hourModified) > (*tm).tm_hour)
if(atoi(minuteModified) > (*tm).tm_min)
if(atoi(secondModified) > (*tm).tm_sec)
newer = 1;
}

printf("Value of newer: %i", newer);
printf("Message: %s Filename: %s Domain: %s monthModified: %i dayModified: %s hourModified: %s minuteModified: %s secondModified: %s yearModified: %s\n", message, filePath, domain, monthModifiedi, dayModified, hourModified, minuteModified, secondModified, yearModified);

if(message == "GET"){
if ((pFile = fopen(filePath, "w")) == NULL){
message = "HTTP/1.1 404 Not Found\n\r";
send(clifd, message, strlen(message), 0);
}
// If not conditinal request, or if file has been modified
else if(newer == 2 || newer == 1){
message = "HTTP/1.1 200 OK\n\r", pFile;
send(clifd, message, strlen(message), 0);
}
// If file has not been modified
else{
message = "HTTP/1.1 304 Not Modified\n\r";
send(clifd, message, strlen(message),0);
}
}
else if(message == "HEAD"){
if ((pFile = fopen(filePath, "w")) == NULL){
message = "HTTP/1.1 404 Not Found\n\r";
send(clifd, message, strlen(message), 0);
}
else{
message = "HTTP/1.1 200 OK\n\r";
send(clifd, message, strlen(message), 0);
}
}

return 0;
}

最佳答案

stat 的返回值是多少?

if(stat(filePath, &statBuf) == 0){
tm = localtime(&statBuf.st_mtime);
//time = &statBuf.st_mtime;
}

如果 stat 成功,您只初始化 tm,但如果不成功,您仍然可以访问它而无需检查。

关于c - 在 C 中使用 tm 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15938198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com