Annotation of capa/capa51/pProj/capaNewCgi.c, revision 1.1.1.1
1.1 albertel 1: char *makeword(char *line, char stop)
2: {
3: int x = 0,y;
4: char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
5:
6: for(x=0;((line[x]) && (line[x] != stop));x++)
7: word[x] = line[x];
8:
9: word[x] = '\0';
10: if(line[x]) ++x;
11: y=0;
12:
13: while((line[y++] = line[x++]));
14: return word;
15: }
16:
17: char *fmakeword(FILE *f,char stop,int * cl)
18: {
19: int wsize;
20: char *word;
21: int ll;
22:
23: wsize = 102400;
24: ll=0;
25: word = (char *) malloc(sizeof(char) * (wsize + 1));
26:
27: while(1) {
28: word[ll] = (char)fgetc(f);
29: if(ll==wsize) {
30: word[ll+1] = '\0';
31: wsize+=102400;
32: word = (char *)realloc(word,sizeof(char)*(wsize+1));
33: }
34: --(*cl);
35: if((word[ll] == stop) || (feof(f)) || (!(*cl))) {
36: if(word[ll] != stop) ll++;
37: word[ll] = '\0';
38: return word;
39: }
40: ++ll;
41: }
42: }
43:
44: char x2c(char *what)
45: {
46: register char digit;
47:
48: digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
49: digit *= 16;
50: digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
51: return(digit);
52: }
53:
54: void unescape_url(char *url)
55: {
56: register int x,y;
57:
58: for(x=0,y=0;url[y];++x,++y) {
59: if((url[x] = url[y]) == '%') {
60: url[x] = x2c(&url[y+1]);
61: y+=2;
62: }
63: }
64: url[x] = '\0';
65: }
66:
67: void plustospace(char *str)
68: {
69: register int x;
70:
71: for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
72: }
73:
74: void web_parse_input(char * submissions_str)
75: {
76: int x;
77:
78: for(x=0; x <= m; x++) {
79: if( !strcmp(g_entries[x].name,"CLASS") ) {
80: strncpy(g_class_name,g_entries[x].val,MAX_CLASS_CHAR);
81: }
82: if( !strcmp(g_entries[x].name,"M") ) {
83: sscanf(g_entries[x].val,"%d",&g_run_mode);
84: }
85: if( !strcmp(g_entries[x].name,"SNUM") ) {
86: strncpy(g_student_number,g_entries[x].val,MAX_STUDENT_NUMBER+4);
87: }
88: if( !strcmp(g_entries[x].name,"CAPAID") ) {
89: sscanf(g_entries[x].val,"%d",&g_entered_pin);
90: }
91: if( !strcmp(g_entries[x].name,"SET") ) {
92: sscanf(g_entries[x].val,"%d",&g_set);
93: }
94: if( !strcmp(g_entries[x].name,"VSET") ) {
95: if (g_entries[x].val[0] == '\0') {
96: g_vset=0;
97: } else {
98: sscanf(g_entries[x].val,"%d",&g_vset);
99: }
100: }
101: if( !strcmp(g_entries[x].name,"KND") ) {
102: sscanf(g_entries[x].val,"%d",&g_skind);
103: }
104: if( !strncmp(g_entries[x].name,"INPUT",5) ) {
105: sscanf(g_entries[x].name,"INPUT%d",&q_idx);
106: if( q_idx > 0 && q_idx < MAX_PROBLEM_CNT ) {
107: strncpy(g_student_answer[q_idx],g_entries[x].val,MAX_ANSWER_CHAR);
108: }
109: if ( g_student_answer[q_idx][0] != '\0' ) {
110: sprintf(buf,"%d\t%s\t",q_idx,g_student_answer[q_idx]);
111: strcat(submissions_str,buf);
112: }
113: }
114: if( !strncmp(g_entries[x].name,"LAST",4) ) {
115: sscanf(g_entries[x].name,"LAST%d",&q_idx);
116: if( q_idx > 0 && q_idx < MAX_PROBLEM_CNT ) {
117: strncpy(g_last_answer[q_idx],g_entries[x].val,MAX_ANSWER_CHAR);
118: }
119: }
120: free(g_entries[x].val);
121: free(g_entries[x].name);
122: }
123: }
124:
125: int web_login()
126: {
127: if( g_entered_pin != 0 ) {
128: g_login_set = capa_PIN(g_student_number,999,g_entered_pin);
129: } else {
130: return WEB_ERR_ENTERED_PIN;
131: }
132:
133: if (!g_login_set) {
134: return WEB_ERR_BADLOGIN;
135: } else {
136: if ( g_login_set > 99 ) { return WEB_ERR_LOGINTOHIGH; }
137: if(g_login_set < g_vset ) {
138: return WEB_ERR_NOTVIEWABLE;
139: }
140: chdir(g_class_fullpath); /* again, to make sure */
141:
142: if ( capa_get_student(g_student_number,&g_student_data) == 0 ) {
143: return WEB_ERR_STUDENT_NOT_EXIST;
144: } else {
145: time(&curtime);
146: if (capa_get_header(&header, g_login_set, wgt, pcr)) {
147: return WEB_ERR_SET_NOT_READY;
148: }
149: if(capa_check_date(CHECK_OPEN_DATE,g_student_data.s_sec,
150: g_login_set) < 0 ) {
151:
152: return WEB_ERR_SET_NOT_OPEN;
153: }
154: }
155: }
156: return (error);
157: }
158:
159: int web_get_input()
160: {
161:
162: envPtr=getenv("REQUEST_METHOD");
163: if (!envPtr ) { return WEB_ERR_REQ_METHOD; }
164: if (strcmp(envPtr,"POST")) { return WEB_ERR_ENV_POST; }
165: envPtr=getenv("CONTENT_TYPE");
166: if (!envPtr ) { return WEB_ERR_CONTENT_TYPE; }
167: if (strcmp(envPtr,"application/x-www-form-urlencoded")) {
168: return WEB_ERR_ENV_CONTENT;
169: }
170: envPtr=getenv("CONTENT_LENGTH");
171: if (!envPtr ) { return WEB_ERR_CONTENT_LENGTH; }
172: content_length=atoi(envPtr);
173:
174: /* read the form into the g_entries array*/
175: for(x=0;content_length && (!feof(stdin));x++) {
176: m=x;
177: g_entries[x].val = fmakeword(stdin,'&',&content_length);
178: plustospace(g_entries[x].val);
179: unescape_url(g_entries[x].val);
180: g_entries[x].name = makeword(g_entries[x].val,'=');
181: }
182:
183: web_parse_input(submissions_str);
184:
185: if ( g_run_mode == WEB_CHECKIN ) {
186: time(&curtime); time_str = ctime(&curtime);
187: time_str[ strlen(time_str)-1 ] = '\0';
188: envPtr=getenv("REMOTE_HOST");
189: envPtr2=getenv("HTTP_USER_AGENT");
190: sprintf(log_str,"%s\t%s\t%s\t%s\t%s\n",g_class_name,g_student_number,
191: time_str,envPtr,envPtr2);
192: if (web_log(log_str) == -1 ) { return WEB_ERR_WEB_LOG; }
193: }
194:
195: getwd(g_cwd);
196:
197: web_getclassdir(&g_cpath, &g_cowner, g_class_name);
198: sprintf(g_class_fullpath,"%s/%s",g_cpath,g_class_name);
199: if( !capa_access(g_class_fullpath, F_OK) == 0 ) { return WEB_ERR_ACCESS; }
200:
201: chdir(g_class_fullpath);
202: if ( g_run_mode == M_CHECKANS) {
203: if (w_log_submissions(g_student_number,g_set,submissions_str) == -1 ) {
204: return WEB_ERR_SUBMMISIONS_LOG;
205: }
206: }
207:
208: result=read_capa_config("capaweb_cgibin_path",buf);
209: if (result != 0 && result != -1) {
210: g_cgibin_path=capa_malloc(strlen(buf)+1,1);
211: strcpy(g_cgibin_path,buf);
212: } else {
213: g_cgibin_path=capa_malloc(strlen("capa-bin")+1,1);
214: strcpy(g_cgibin_path,"capa-bin");
215: }
216: return web_login();
217: }
218:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>