/* RemoveFromList(...) id: >-1 search by id -1 search list by name -2 search list by uid return new database size or -1 if error */ int RemoveFromDB(int id, char const *name, char const *uid) { int z = 0; uint i = 0; if(id == -1) { z = searchdb(-1, name); if(z == -1) return -1; } else if(id == -2) { z = searchdb(-2, uid); if(z == -1) return -1; } else if(id > -1) { z = searchdb(); } if(z>-1) { for(i=z; i<db_size-1; i++) { db[i].id = db[i+1].id; strcpy(&db[i].name[0], &db[i+1].name[0]); strcpy(&db[i].uid[0], &db[i+1].uid[0]); strcpy(&db[i].priviledge, &db[i+1].priviledge); } return --db_size; } return -1; }
// Convert normal ip address into format usable by database // this func is called by ip2loc and will convert the given // ip address into the decimal form and then start the search. char* srchdb(char *ipaddr, FILE *dbp, int dbsize) { long long decaddr; int arr[5]; int i,j,k,ptr=0,fsize=0,res=1; char *ret,tmp[200],tmp2[200], *chk; char addr[30], seg[3]; FILE *f; // Convert normal IP address into database form: // a.b.c.d -> 256^3*a + 256^2*b + 256*c + d chk = ipaddr; //Make sure there are at least 3 '.'s in the addr for(i=0; i<3; i++) { chk = strchr(chk, '.'); if(chk == NULL) { //printf("Invalid IP addr format!: %s\n",ipaddr); return; } chk++; } // Copy ipaddr into our own string for(i=0; i<strlen(ipaddr)+1; i++) { addr[i] = *(ipaddr+i); } // Fill int arr[] with segments of addr for(i=0,j=0; i<4; i++) { k = 0; seg[0]=0;seg[1]=0;seg[2]=0; while(addr[j] != '.' && addr[j] != '\0' && k<3) { if(k>3) { printf("invalid ip addr due to k %s\n", ipaddr); return; } if(!strchr("0123456789",addr[j])) { printf("Invalid char \'%c\' in addr: %s\n", addr[j], ipaddr); return; } seg[k++] = addr[j]; j++; } arr[i] = atoi(seg); if(arr[i] > 255 || arr[i] < 0) { printf("invalid ip addr %s\n", ipaddr); return; } j++; } //for(i=0; i<4; i++) { printf("arr[%d] = %d\n", i, arr[i]); } decaddr = (long long)256*(long long)256*(long long)256*(long long)arr[0]+(long long)256*(long long)256*(long long)arr[1]+(long long)256*(long long)arr[2]+(long long)arr[3]; //printf("decaddr is %lld\n", decaddr); if(decaddr < 0) { printf("Error in decaddr\n"); return; } cursor = 0; searchupper = 0; searchlower = 0; count = 0; res = searchdb(dbp, decaddr, dbsize); if(res!=0) { //printf("Problem in dbsearch"); return; } printf("\"%s\",%s",ipaddr,pch); return pch; }
/** * Read in the database file given, returning the head of the linked list. * * @param filename - filename/path to open. * * @return head element to the database linked list. */ dbentry* read_db(char *filename) { FILE *file_fd = fopen( filename, "r" ); dbentry* head = NULL; char *buffer = NULL; if (file_fd == NULL) { fputs(DB_FILE_ERROR,stderr); return NULL; } else { int count_n = 0; bool first = true; dbentry* curr = NULL; buffer = allocate(DB_INPUT_LIMIT*sizeof(char)); if ( !checkalloc(buffer) ) { fclose(file_fd); return NULL; } while ( !feof(file_fd) ) { if ( count_n == DB_SIZE_LIMIT ) { fputs( DB_TRUNK_ERROR, stderr ); break; } char *ret; /** Read all fields for this entry fom file */ ret = fgets(buffer, DB_INPUT_LIMIT, file_fd); if ( ret == NULL ) break; char *artist = trim_limit(buffer,artists); if (!checkalloc( artist )) { goto allocfail; } ret = fgets(buffer, DB_INPUT_LIMIT, file_fd); if ( ret == NULL ) break; char *title = trim_limit(buffer, titles); if ( !checkalloc( title ) ) { unallocate( artist ); goto allocfail; } ret = fgets(buffer, DB_INPUT_LIMIT, file_fd); if ( ret == NULL ) break; char ntracks = (char) atoi(buffer); ret = fgets(buffer, DB_INPUT_LIMIT , file_fd); if ( ret == NULL ) break; int time_m = 0; int time_s = 0; sscanf( buffer, "%d:%d", &time_m, &time_s ); /* make sure this isn't a duplicate */ if ( searchdb( head, artist, title ) ) { fprintf(stderr,"Duplicate entry: '%s' '%s'\n",artist, title); unallocate(artist); unallocate(title); continue; } /** create ll node to hold this entry */ curr = allocate(sizeof(dbentry)); if ( !checkalloc( curr ) ) { unallocate( artist ); unallocate( title ); goto allocfail; } curr->artist = artist; curr->title = title; curr->tracks = ntracks; curr->time_m = time_m; curr->time_s = time_s; curr->title_next = NULL; if ( first ) { /* Set the next pointer NULL so we know where the ll stops */ curr->artist_next = NULL; head = curr; first = false; } else { /* prepend to the head of the list */ curr->artist_next = head; head = curr; } count_n++; } unallocate(buffer); } fclose( file_fd ); return head; allocfail: unallocate(buffer); deallocate_db( head ); head = NULL; fclose(file_fd); return NULL; }
// Function to search through our database int searchdb(FILE *f, long long decaddr, int dbsize) { if(count++ > 500) { //printf("Could not find within 500 attempts... Giving up."); return 1; } // Case1 - first search if(cursor == 0) { searchlower = 0; searchupper = dbsize; cursor = dbsize/2; //printf("Current cursor is %d\n", cursor); fseek(f,cursor,SEEK_SET); fgets(tmp,200,f); //printf("Original seek: %s", tmp); fgets(tmp,strlen(tmp)+cursor+1,f); //printf("Final seek: %s\n", tmp); getUpperLower(); searchdb(f, decaddr, dbsize); } // Case2 - Base Case if(decaddr >= lb && decaddr <= ub) { if(strncmp(pch,"\"-\",\"-\",\"-\",\"-\",\"0\",\"0\",\"-\",\"-\"\n",20) == 0) { while(tmp[0] != '\n') { //printf("tmp is %s\n",tmp); fseek(f,--cursor,SEEK_SET); fgets(tmp,200,f); } } //printf("Count = %d ...", count); return 0; } // Case3 - less than -- Go down if(decaddr < lb) { searchupper = cursor; cursor = (cursor + searchlower)/2; //printf("Current cursor is %d\n", cursor); fseek(f,cursor,SEEK_SET); fgets(tmp,200,f); //printf("Original seek: %s", tmp); fgets(tmp,strlen(tmp)+cursor+1,f); //printf("Final seek: %s\n", tmp); getUpperLower(); searchdb(f, decaddr, dbsize); } // Case4 - Greater than -- Go up if(decaddr > ub) { searchlower = cursor; cursor = (searchupper + cursor)/2; //printf("Current cursor is %d\n", cursor); fseek(f,cursor,SEEK_SET); fgets(tmp,200,f); //printf("Original seek: %s", tmp); fgets(tmp,strlen(tmp)+cursor+1,f); //printf("Final seek: %s\n", tmp); getUpperLower(); searchdb(f, decaddr, dbsize); } return 1; }