/* Search appdir defined directories for access rights lib is the directory within the appdirs to check for access rights. fullpath is the returned pathname of the first directory with access rights. If fullpath is NULL, the pathname is not returned The directory is checked for read, write, execute permission. */ int appdirAccess(const char *lib, char *fullpath) { int findIndex = 0; char delimiters[] = ";"; char *ptr; char searchPath[MAXPATH]; char currentPath[MAXPATH]; if (appdirPaths == NULL) getappdirPaths(); ptr = strwrd(appdirPaths, currentPath, MAXPATH, delimiters); while ( currentPath[0] != '\0' ) { findIndex++; sprintf(searchPath,"%s/%s",currentPath,lib); if (!access( searchPath, R_OK | X_OK | W_OK)) { if (fullpath != NULL) strcpy(fullpath,searchPath); return(findIndex); } ptr = strwrd(ptr, currentPath, MAXPATH, delimiters); } return(0); }
/* rename - rename a file or directory * Errors - path resolution, ENOENT, EINVAL, EEXIST * * ENOENT - source does not exist * EEXIST - destination already exists * EINVAL - source and destination are not in the same directory * * Note that this is a simplified version of the UNIX rename * functionality - see 'man 2 rename' for full semantics. In * particular, the full version can move across directories, replace a * destination file, and replace an empty directory with a full one. */ static int hw4_rename(const char *src_path, const char *dst_path) { char *sub_src_path = (char*)calloc(16*44,sizeof(char)); char *sub_dst_path = (char*)calloc(16*44,sizeof(char)); char path1[20][44]; char path2[20][44]; int k = 0; strcpy(sub_src_path, src_path); strcpy(sub_dst_path, dst_path); // save src path to path1 // save dst path to path2 while(1) { sub_src_path = strwrd(sub_src_path, path1[k], 44, " /"); sub_dst_path = strwrd(sub_dst_path, path2[k], 44, " /"); if (sub_src_path == NULL && sub_dst_path == NULL) break; if (sub_src_path == NULL || sub_dst_path == NULL) return -EINVAL; if (strcmp(path1[k], path2[k]) != 0) return -EINVAL; k++; } char *filename = (char*)malloc(sizeof(char)*44); strcpy(filename, path2[k]); int blk_num = super->root_dirent.start; int current_blk_num; // find the block and entry number of the src path int entry = parsePath((void*)src_path, blk_num); current_blk_num = new_current_blk_num; // find the entry number of the dst path int dst_entry = lookupEntry(filename, current_blk_num); // check if the dst file is existed if (dst_entry != -1) return -EEXIST; strcpy(directory[entry].name, filename); directory[entry].mtime = time(NULL); // write back disk->ops->write(disk, current_blk_num*2, 2, (void*)directory); free(filename); free(sub_src_path); free(sub_dst_path); return 0; }
void DoSimpleHttp( SOCKET sock ) { uint16_t maxlen = DATA_BUF_SIZE; uint16_t len; uint8_t workingBuffer[100]; uint8_t* msgBuffer = gHttpRxBuffer; uint8_t* outbuffer = gHttpTxBuffer; HTMLPageDef* htmlpagePtr; // Nachsehen, wieviele Daten angekommen sind len = getSn_RX_RSR(sock); if( len < 1 ) return; // nicht mehr abholen, als Daten da sind if (len > maxlen) len = maxlen; // daten aus dem wiznet buffer holen len = recv(sock, msgBuffer, len); //Aufbau einer unterstützten Anfrage: // GET /url HTTP/1.1 HOST: ip .... msgBuffer = strwrd( msgBuffer, workingBuffer, 100, " "); if( msgBuffer ) { printf( "[%s]\n", workingBuffer); if( !strcmp(workingBuffer, "GET") ) { // Get Anfrage gefunden msgBuffer = strwrd( msgBuffer, workingBuffer, 100, " "); if( msgBuffer) { printf( "[%s]\n", workingBuffer); if( workingBuffer[0] == '/' ) { // eine URL gefudnen htmlpagePtr = HTMLPagesGet_index(); len = sprintf( outbuffer, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: keep-alive\r\nContent-Length: %i \r\n\r\n %s", strlen(htmlpagePtr->PageContent), htmlpagePtr->PageContent ); send( sock, outbuffer, len ); } } } } }
// Calculate secondary structure prediction with PSIPRED void CalculateSS(char *ss_pred, char *ss_conf, char *tmpfile) { // Initialize std::string command; char line[LINELEN]=""; char filename[NAMELEN]; strcpy(ss_pred,"-"); strcpy(ss_conf,"-"); // Run PSIPRED // Check for PSIPRED ver >= 3.0 (weights.dat4 doesn't exists anymore) strcpy(filename,par.psipred_data); strcat(filename,"/weights.dat4"); FILE* check_exists = fopen(filename,"r"); if (check_exists) { // Psipred version < 3.0 command = (std::string)par.psipred + "/psipred " + (std::string)tmpfile + ".mtx " + (std::string)par.psipred_data + "/weights.dat " + (std::string)par.psipred_data + "/weights.dat2 " + (std::string)par.psipred_data + "/weights.dat3 " + (std::string)par.psipred_data + "/weights.dat4 > " + (std::string)tmpfile + ".ss"; } else { command = (std::string)par.psipred + "/psipred " + (std::string)tmpfile + ".mtx " + (std::string)par.psipred_data + "/weights.dat " + (std::string)par.psipred_data + "/weights.dat2 " + (std::string)par.psipred_data + "/weights.dat3 > " + (std::string)tmpfile + ".ss"; } runSystem(command,v); command = (std::string)par.psipred + "/psipass2 " + (std::string)par.psipred_data + "/weights_p2.dat 1 0.98 1.09 " + (std::string)tmpfile + ".ss2 " + (std::string)tmpfile + ".ss > " + (std::string)tmpfile + ".horiz"; runSystem(command,v); // Read results strcpy(filename,tmpfile); strcat(filename,".horiz"); FILE* horizf = fopen(filename,"r"); if (!horizf) return; while (fgets(line,LINELEN,horizf)) { char tmp_seq[NAMELEN]=""; char* ptr=line; if (!strncmp(line,"Conf:",5)) { ptr+=5; strwrd(tmp_seq,ptr); strcat(ss_conf,tmp_seq); } if (!strncmp(line,"Pred:",5)) { ptr+=5; strwrd(tmp_seq,ptr); strcat(ss_pred,tmp_seq); } } fclose(horizf); if (v>3) { printf("SS-pred: %s\n",ss_pred); printf("SS-conf: %s\n",ss_conf); } }
/* parsePath * parse the path and return the entry number */ int parsePath(char *path, int blk_num) { new_blk_num = blk_num; int entry = 0; // root directory if(strlen(path) == 1) { disk->ops->read(disk, new_blk_num * 2, 2, (void*)directory); return entry; } while(path != NULL) { memset(name,0,sizeof(name)); path = strwrd((char*)path, name, sizeof(name),"/"); disk->ops->read(disk, new_blk_num * 2, 2, (void*)directory); entry = lookupEntry(name, new_blk_num); if (entry == -1) return -ENOENT; if (entry <0) return entry; new_current_blk_num = new_blk_num; new_blk_num = directory[entry].start; } return entry; }
static int hw4_getattr(const char *path, struct stat *sb) { char *sub_path = (char*)calloc(16*44,sizeof(char)); strcpy(sub_path, path); int start = super->root_dirent.start; int entry; // find the block and entry of the path while(sub_path != NULL) { memset(name,0,sizeof(name)); sub_path = strwrd(sub_path, name, sizeof(name), "/"); disk->ops->read(disk, start * 2, 2, (void*)directory); entry = lookupEntry(name, start); if(entry == -1) return -ENOENT; start = directory[entry].start; } convertAttr(entry, sb); free(sub_path); return 0; }
/* * Note - see c-programming.pdf for sample code to split a line into * separate tokens. */ void q2(void) { vector[0] = &print; vector[1] = &readline; vector[2] = &getarg; char* s = NULL; while (1) { printf("> "); /* get a line */ readline(line, sizeof(line)); /* split it into words */ //get command s = strwrd(line, command, sizeof(argv[0]), " \t\n"); /* if zero words, continue */ if (s == NULL) { continue; } /* if first word is "quit", break */ if (!strcmp(command, "quit")) { break; } //get arguments int argc; for (argc = 0; argc < NUM_ARGS; argc++) { s = strwrd(s, argv[argc], sizeof(argv[argc]), " \t\n"); /* make sure 'getarg' can find the remaining words */ if (s == NULL) break; } /* load and run the command */ if(readfile(command,proc1)) { void (*f)(void) = proc1; f(); } else printf("bad command: %s\n", command); } /* * Note that you should allow the user to load an arbitrary command, * and print an error if you can't find and load the command binary. */ }
void checklist_add_items(char *name) { // strncpy(line, name, MAX_NAME_LENGTH - 1); char line[MAX_NAME_LENGTH], token[MAX_NAME_LENGTH]; while(name != NULL) { // set "token" to the next word name = strwrd(name, token, sizeof(token), ".,"); // add the word add_item(token); } }
/* Search appdir defined directories for filename filename is the name of the file to look for lib is the directory within the appdirs to search for filename fullpath is the returned pathname, if the filename is found. If fullpath is NULL, the pathname is not returned suffix is a secondary search for filename. The search order is: 1. filename is searched in the first appdir 2. filename.suffix is searched in the first appdir 3. filename is searched in the second appdir 4. filename.suffix is searched in the second appdir. 5. etc. If suffix is supplied as NULL or a null string, the suffix searches are skipped. perm is the permissions checked for accessing the file. */ int appdirFind(const char *filename, const char *lib, char *fullpath, const char *suffix, int perm) { int findIndex = 0; char delimiters[] = ";"; char *ptr; char searchPath[MAXPATH]; char currentPath[MAXPATH]; if (appdirPaths == NULL) getappdirPaths(); ptr = strwrd(appdirPaths, currentPath, MAXPATH, delimiters); while ( currentPath[0] != '\0' ) { findIndex++; sprintf(searchPath,"%s/%s/%s",currentPath,lib,filename); if (!access( searchPath, perm)) { if (fullpath != NULL) strcpy(fullpath,searchPath); return(findIndex); } if ( (suffix != NULL) && (suffix[0] != '\0') ) { strcat(searchPath,suffix); if (!access( searchPath, perm)) { if (fullpath != NULL) strcpy(fullpath,searchPath); return(findIndex); } } ptr = strwrd(ptr, currentPath, MAXPATH, delimiters); } return(0); }
/* * Note - see c-programming.pdf for sample code to split a line into * separate tokens. */ void q2(void) { FILE *fp=NULL; int (*ptr)(void); int argc; char* buf=(char*) malloc(280*sizeof(char)); char* ptr_buf=NULL; vector[0]=print; vector[1]=readline; vector[2]=getarg; while (1) { ptr_buf=buf; readline(ptr_buf,280); /* get a line */ for(argc=0;argc<10;argc++){ ptr_buf=strwrd(ptr_buf,argv[argc],sizeof(argv[argc])," \t"); if(ptr_buf==NULL) break; }; if(!strlen(argv[0])) /* if zero words, continue */ { continue;} else if(strcmp("quit",argv[0])==0) { exit(0); /* if first word is "quit", break */ } else { fp=fopen(argv[0],"r"); if(fp==NULL){ perror("Can't open file"); exit(-1); } fread(proc2,4096,1,fp); ptr=proc2; (*ptr)(); } /* make sure 'getarg' can find the remaining words */ /* load and run the command */ } ptr_buf=NULL; free(buf); exit(0); /* * Note that you should allow the user to load an arbitrary command, * not just 'ugrep' and 'upcase', and print an error if you can't * find and load the command binary. */ }
String RobotCmd::processMessage(String incomingMessage, long currTime) { char* msg = const_cast<char*>( incomingMessage.c_str()); if(detectNewDanger(msg,currTime)){ return String(DANGER); } if(accel.collision){ motor.setState(STATE_COLLISION); accel.collision = false; return ""; } if( processDanger(currTime)) { return ""; } if(motor.motorState == STATE_COLLISION) { //when encountering a collision get us out of a jam before resuming all activities return ""; } if(currState != RBT_DANCE && msg[0]==MARCO) //received marco { //Serial.println("process MARCO"); currState = RBT_WAIT_DANCE; return String(POLO); //send back polo; } if(currState == RBT_WAIT_POLO && msg[0]==POLO) //recived polo { //Serial.println("process POLO"); currState = RBT_DANCE; String newSong = singer.createSong(safeFeeling,safeFeeling+10); String newDance = "";//dancer.createDance(); //disbaled dance cause we can't hear RF signals with it return String(SONG+newSong+DANCE+newDance); } if(currState == RBT_WAIT_DANCE && msg[0]==SONG) { String sSep(SONG); String dSep(DANCE); //split message into song and dance char * song = new char[255* sizeof(char)]; char * dance = new char[255* sizeof(char)]; dance = strwrd ( msg,song,255* sizeof(char),dSep.c_str()); //strip first char off of theDance and song and everything is correct; or handle it within the routine singer.playSong(const_cast<char*>(song+1)); //dancer.performDance(const_cast<char*>(dance+1)); currState = RBT_DANCE; return ""; } //we just finished dancing if(currState == RBT_DANCE) { if( !dancer.dancing && !singer.singing) { currState = RBT_IDLE; freeTime = rand() *1000;//set the time before we connect to other robot freeTime = freeTime<0?-1*freeTime:freeTime; } return ""; } if(currState == RBT_IDLE) { if((currTime - freeTime )> commandTimeout) { //see if robot is responding currState = RBT_WAIT_POLO; lastCommandSent = currTime; //determines the song we sing safeFeeling = safeFeeling< (singer.maxSafeFeeling-10)?safeFeeling+10:safeFeeling; return String(MARCO); } } if((currTime - lastCommandSent) >commandTimeout) { currState = RBT_IDLE; lastCommandSent = currTime; freeTime = abs(rand() *500+currTime); freeTime = freeTime<0?-1*freeTime:freeTime; } return ""; }
/* * Cereate the entry depending on the isDir */ int createEntry(const char *path, mode_t mode, int isDir) { int entry=0; char *new_name = (char*)calloc(44,sizeof(char)); int i, j; // save the path in paths // save the name in new_name char paths[20][44]; int k = 0; while(1) { path = strwrd((char*)path, paths[k], sizeof(paths[k]), "/"); strcpy(new_name, paths[k]); if (path == NULL) break; k++; } int blk_num = super->root_dirent.start; // find the location of the path for(i = 0; i < k; i++) { disk->ops->read(disk, blk_num * 2, 2, (void*)directory); entry = lookupEntry(paths[i], blk_num); if(entry == -1) return -ENOENT; blk_num = directory[entry].start; } disk->ops->read(disk, blk_num * 2, 2, (void*)directory); // check if the entry is exsisted for(i = 0; i < 16; i++) if(strcmp(directory[i].name, paths[k]) == 0) return -EEXIST; // find an entry that is free for(i = 0; i < 16; i++) if(directory[i].valid == 0) break; // find a free block for(j = 0; j < 1024; j++) { if(fat[j].inUse == 0) { fat[j].inUse = 1; fat[j].eof = 1; break; } } if(j==1024) return -ENOSPC; // initialize the file directory[i].valid = 1; directory[i].isDir = isDir; directory[i].length = 0; directory[i].start = j; directory[i].mode = mode; directory[i].mtime = time(NULL); directory[i].gid = getgid(); directory[i].uid = getuid(); directory[i].pad = 0; strcpy(directory[i].name, new_name); // write back disk->ops->write(disk,blk_num*2,2,(void*)directory); disk->ops->write(disk,2,8,(void*)fat); free(new_name); return 0; }