Annotation of capa/capa51/pProj/capaSubjective.c, revision 1.2

1.1       albertel    1: #include "capaCommon.h"
                      2: #include <sys/stat.h>
                      3: #include <sys/types.h>
                      4: #include <fcntl.h>
                      5: #include <unistd.h>
                      6: #include <ctype.h>
                      7: 
                      8: char* strtoupper(char* source)
                      9: {
                     10:   char* result;
                     11:   int i,len=strlen(source);
1.2     ! albertel   12:   result=capa_malloc(len+2,1);
1.1       albertel   13:   for(i=0;i<len;i++) result[i]=toupper(source[i]);
1.2     ! albertel   14:   result[i]='\0';
1.1       albertel   15:   return result;
                     16: }
                     17: 
                     18: int capa_set_subjective (int set,int problem,char *student_num,char* response)
                     19: {
                     20:   char buf[FILE_NAME_LENGTH],*upperstunum;
                     21:   FILE *responsefile;
                     22: 
                     23:   sprintf(buf,"records/set%d",set);
                     24:   if( capa_access(buf, F_OK) == -1 ) { 
                     25:     if ( mkdir(buf, S_IREAD | S_IWRITE | S_IEXEC ) == -1 ) { return -1; }
                     26:   }
                     27: 
                     28:   sprintf(buf,"records/set%d/problem%d",set,problem);
                     29:   if( capa_access(buf, F_OK) == -1 ) { 
                     30:     if ( mkdir(buf, S_IREAD | S_IWRITE | S_IEXEC ) == -1 ) { return -2; }
                     31:   }
                     32: 
                     33:   upperstunum=strtoupper(student_num);
                     34:   sprintf(buf,"records/set%d/problem%d/%s",set,problem,upperstunum);
                     35:   if ((responsefile=fopen(buf,"w"))==NULL)  { return -3; }
                     36:   
                     37:   fwrite(response,strlen(response),1,responsefile);
                     38:   fclose(responsefile);
                     39:   return 0;
                     40: }
                     41: 
                     42: char* capa_get_subjective (int set,int problem,char *student_num)
                     43: {
                     44:   char buf[FILE_NAME_LENGTH],*upperstunum,*response;
                     45:   FILE *responsefile;
                     46:   long length;
                     47: 
                     48:   upperstunum=strtoupper(student_num);
                     49:   sprintf(buf,"records/set%d/problem%d/%s",set,problem,upperstunum);
                     50:   if ((responsefile=fopen(buf,"r"))==NULL)  { return NULL; }
                     51:   fseek(responsefile,0,SEEK_END);
                     52:   length=ftell(responsefile);
                     53:   rewind(responsefile);
                     54:   response=capa_malloc(length+1,1);
                     55:   fread(response,length,1,responsefile);
                     56:   response[length]='\0';
                     57:   return response;
                     58: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>