int minisize(struct stat mystat, int ct, int ct2) { if (S_ISBLK(mystat.st_mode) || S_ISCHR(mystat.st_mode)) { ct2 = intsize(major(mystat.st_rdev)) + intsize(minor(mystat.st_rdev)) + 2; if (ct <= ct2) ct = ct2; } else if (ct <= intsize(mystat.st_size)) ct = intsize(mystat.st_size); return (ct); }
int sizemaj(char **tab, char *dir) { int ct; int i; char *buf; struct stat mystat; i = 0; ct = 0; while (tab[i]) { buf = ft_strjoin(dir, tab[i]); if (lstat(buf, &mystat) == 0) { if (ct <= intsize(major(mystat.st_rdev)) && (S_ISBLK(mystat.st_mode) || S_ISCHR(mystat.st_mode))) ct = intsize(major(mystat.st_rdev)); } i++; } free(buf); return (ct); }
//processes data and updates the log void processandlog(int sock, char * ipadd, int portnum) { struct stat checkfile; int getstat = 0; int fd = 0; int sendfail = 0; int filesize = 0; int sendreturn = 0; char requestedfile [1024] = ""; int fail = 0; int getreq = getrequest(sock, requestedfile, 1024); if (!getreq) { if (requestedfile[0] == '/') { getstat = stat(&(requestedfile[1]), &checkfile); } else { getstat = stat(requestedfile, &checkfile); } if (!getstat) { filesize = checkfile.st_size; int datasize = 63 + intsize(filesize) + filesize; char data [datasize]; sprintf(data, HTTP_200, (int)checkfile.st_size); if (requestedfile[0] == '/') { fd = open(&(requestedfile[1]), O_RDONLY); } else { fd = open(requestedfile, O_RDONLY); } read(fd, &(data[(63 + intsize(filesize))]), checkfile.st_size); data[datasize] = '\0'; sendreturn = senddata(sock, data, datasize); if (sendreturn == -1) { printf("%s", "Data sending failed! So sad"); sendfail = 1; } shutdown(sock, SHUT_RDWR); close(fd); } else { fail = 1; } } else { fail = 1; //getrequest fails means that we won't ever find the file, return file not found } if(fail){ sendreturn = senddata(sock, HTTP_404, strlen(HTTP_404)); if (sendreturn == -1) { printf("%s", "Data sending failed! So sad"); sendfail = 1; } shutdown(sock, SHUT_RDWR); } if(!sendfail) { pthread_mutex_lock(&log_mutex); FILE * log = fopen("weblog.txt", "a"); fwrite(ipadd, strlen(ipadd), 1, log); char portstr[intsize(portnum)]; sprintf(portstr, ":%d ", portnum); fwrite(portstr, strlen(portstr), 1, log); time_t now = time(NULL); char * time = ctime(&now); fwrite(time, strlen(time)-1, 1, log); fwrite(" \"GET " , 6, 1, log); fwrite(requestedfile, strlen(requestedfile), 1, log); if(fail){ fwrite("\" 404 ", 6, 1, log); } else { fwrite("\" 200 ", 6, 1, log); } char buffer[intsize(sendreturn)]; sprintf(buffer, "%d\n", sendreturn); fwrite(buffer, strlen(buffer), 1, log); fclose(log); pthread_mutex_unlock(&log_mutex); } else { //since we want to write to log for every request, it makes sense to keep track of senddata failures pthread_mutex_lock(&log_mutex); FILE * log = fopen("weblog.txt", "a"); fwrite("Senddata failed\n", 16, 1, log); fclose(log); pthread_mutex_unlock(&log_mutex); } }
newfunc () { char n[NAMESIZE], *ptr; fexitlab = getlabel(); if (!symname (n) ) { error ("illegal function or declaration"); kill (); return; } if (ptr = findglb (n)) { if (ptr[IDENT] != FUNCTION) multidef (n); else if (ptr[OFFSET] == FUNCTION) multidef (n); else ptr[OFFSET] = FUNCTION; } else addglb (n, FUNCTION, CINT, FUNCTION, PUBLIC); prologue (); if (!match ("(")) error ("missing open paren"); prefix (); outstr (n); col (); nl (); locptr = STARTLOC; argstk = 0; while (!match (")")) { if (symname (n)) { if (findloc (n)) multidef (n); else { addloc (n, 0, 0, argstk, AUTO); argstk = argstk + intsize(); } } else { error ("illegal argument name"); junk (); } blanks (); if (!streq (line + lptr, ")")) { if (!match (",")) error ("expected comma"); } if (endst ()) break; } stkp = 0; argtop = argstk; while (argstk) { if (amatch ("register", 8)) { if (amatch("char", 4)) getarg(CCHAR); else if (amatch ("int", 3)) getarg(CINT); else getarg(CINT); ns(); } else if (amatch ("char", 4)) { getarg (CCHAR); ns (); } else if (amatch ("int", 3)) { getarg (CINT); ns (); } else { error ("wrong number args"); break; } } statement(YES); printlabel(fexitlab); col(); nl(); modstk (0); gret (); stkp = 0; locptr = STARTLOC; }