int main(void) { printf("Enter array size, followed by the elements, followed by element to find\n"); printf("> "); size_t arr_sz; while (scanf("%zu", &arr_sz) == 1) { size_t i; for (i = 0; i < arr_sz; i++) { scanf("%d", &array_buf[i]); } int val; scanf("%d", &val); ssize_t res = find_elem(arr_sz, array_buf, val); if (res == -1) { printf("Not found.\n"); } else { printf("Found at array[%zd]\n", res); } printf("> "); } return 0; }
elem_t *lookup(hash_table_t table, app_pc addr) { list_t *list = table[hash_func(addr)]; if (list != NULL) return find_elem(list, addr); return NULL; }
/* If the default token, which is "token", is already among frequent words, generate random string to replace "token". */ static void set_token(struct Parameters *pParam) { while (find_elem(pParam->token, pParam->ppWordTable, pParam->wordTableSize, pParam->wordTableSeed)) { gen_random_string(pParam->token, TOKENLEN - 1); } }
/* * Restrict a model defined by var and value to subvar * - the result is stored in subvalue * - n = size of arrays subvar and subvalue * - preconditions: * var is a sorted index vector * every element of subvar occurs in var * value has the same size as var */ static void project_model(int32_t *var, term_t *value, term_t *subvar, term_t *subvalue, uint32_t n) { uint32_t i; int32_t k; for (i=0; i<n; i++) { k = find_elem(var, subvar[i]); assert(k >= 0 && subvar[i] == var[k]); subvalue[i] = value[k]; } }
/* * heap_rem_elem - Removes an arbitrary element in the heap by finding * its index with linear search and then swapping * and deleting with the bottom-most, right-most element. */ void heap_rem_elem(heap H, elem x) { REQUIRES(is_heap(H) && !heap_empty(H)); int idx = find_elem(H, x); H->next--; if (H->next > 1) { H->data[idx] = H->data[H->next]; sift_down(H, idx); } ENSURES(is_heap(H)); }
void * hash_search ( void *el, hashtab *htab) { hashnode **hnode_ptr; hnode_ptr = find_elem (el, htab); if (*hnode_ptr == NULL) return NULL; else return (*hnode_ptr)->el; }
void interface_1(List *list) { annotation(); while (1) { int interact = 0; int key = 0; printf("Num of actinon:\n"); scanf("%d", &interact); if (interact == 0) break; if (interact == 1) { interface_2(list); continue; } if (interact == 2) { if (list_is_empty(list)) { printf("Nothing to delete, enter element, just press 1\n"); continue; } printf("Key of delete element?\n"); scanf("%d", &key); list_delete_elem(list, find_elem(list, key)); continue; } if (interact == 3) { list = merge_sort(list); continue; } if (interact == 4) { list_print(list); continue; } if (interact == 9) { annotation(); continue; } printf("Wrong input try again\n"); } }
void * hash_remove ( void *el, hashtab *htab) { hashnode **hnode_ptr; hashnode *remove; void *ret_el; hnode_ptr = find_elem (el, htab); if (*hnode_ptr == NULL) return NULL; ret_el = (*hnode_ptr)->el; remove = *hnode_ptr; *hnode_ptr = (*hnode_ptr)->next; delete (remove); --htab->cardinal; return ret_el; }
int main(void) { printf("Enter number of elements in array, followed by array elements, followed by value to find.\n"); printf("> "); size_t arr_sz; while (scanf("%zu", &arr_sz) == 1) { size_t i; for (i = 0; i < arr_sz; i++) { scanf("%d", &array_buf[i]); } int val; scanf("%d", &val); ssize_t res = find_elem(array_buf, arr_sz, val); if (res == -1) { printf("Not found.\n"); } else { printf("Found at index %zd\n", res); } printf("> "); } return 0; }
int cec_add_elem(u64 pfn) { struct ce_array *ca = &ce_arr; unsigned int to; int count, ret = 0; /* * We can be called very early on the identify_cpu() path where we are * not initialized yet. We ignore the error for simplicity. */ if (!ce_arr.array || ce_arr.disabled) return -ENODEV; mutex_lock(&ce_mutex); ca->ces_entered++; if (ca->n == MAX_ELEMS) WARN_ON(!del_lru_elem_unlocked(ca)); ret = find_elem(ca, pfn, &to); if (ret < 0) { /* * Shift range [to-end] to make room for one more element. */ memmove((void *)&ca->array[to + 1], (void *)&ca->array[to], (ca->n - to) * sizeof(u64)); ca->array[to] = (pfn << PAGE_SHIFT) | (DECAY_MASK << COUNT_BITS) | 1; ca->n++; ret = 0; goto decay; } count = COUNT(ca->array[to]); if (count < count_threshold) { ca->array[to] |= (DECAY_MASK << COUNT_BITS); ca->array[to]++; ret = 0; } else { u64 pfn = ca->array[to] >> PAGE_SHIFT; if (!pfn_valid(pfn)) { pr_warn("CEC: Invalid pfn: 0x%llx\n", pfn); } else { /* We have reached max count for this page, soft-offline it. */ pr_err("Soft-offlining pfn: 0x%llx\n", pfn); memory_failure_queue(pfn, MF_SOFT_OFFLINE); ca->pfns_poisoned++; } del_elem(ca, to); /* * Return a >0 value to denote that we've reached the offlining * threshold. */ ret = 1; goto unlock; } decay: ca->decay_count++; if (ca->decay_count >= CLEAN_ELEMS) do_spring_cleaning(ca); unlock: mutex_unlock(&ce_mutex); return ret; }
void recieveUpdate(){ struct sockaddr_in addr; int fd, nbytes,addrlen; struct ip_mreq mreq; char buf[BUF_SIZE]; u_int yes=1; /* create what looks like an ordinary UDP socket */ if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) { perror("socket"); exit(1); } /**** MODIFICATION TO ORIGINAL */ /* allow multiple sockets to use the same PORT number */ if (setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) { perror("Reusing ADDR failed"); exit(1); } /*** END OF MODIFICATION TO ORIGINAL */ /* set up destination address */ memset(&addr,0,sizeof(addr)); addr.sin_family=AF_INET; addr.sin_addr.s_addr=htonl(INADDR_ANY); /* N.B.: differs from sender */ addr.sin_port=htons(PORT); /* bind to receive address */ if (bind(fd,(struct sockaddr *) &addr,sizeof(addr)) < 0) { perror("bind"); exit(1); } printf("\nlisten on socket UDP %i\n", PORT); /* use setsockopt() to request that the kernel join a multicast group */ mreq.imr_multiaddr.s_addr=inet_addr(DST_IP); mreq.imr_interface.s_addr=htonl(INADDR_ANY); if (setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)) < 0) { perror("setsockopt"); exit(1); } /* now just enter a read-print loop */ while (1) { addrlen=sizeof(addr); if ((nbytes=recvfrom(fd,buf,BUF_SIZE,0,(struct sockaddr *) &addr,&addrlen)) < 0) { perror("recvfrom"); exit(1); } printf("recived RIP update from %s\n", intToIp(addr.sin_addr.s_addr)); int entrySize = sizeof(entry)-sizeof(UT_hash_handle); int i; int numOfEntries = (nbytes - sizeof(header))/entrySize; for(i = 0; i < numOfEntries; i++){ entry *e = malloc(entrySize); memcpy(e, &buf[4+i*entrySize], entrySize); uint32_t network = e->network_addr; struct entry *entr = find_elem(network); if(entr == NULL && e->metric <= 15){ sem_wait(&mutex); add_elem(entr); printf("%s/%s Metric %u",intToIp(entr->network_addr), intToIp(entr->subnet_mask), entr->metric); sem_post(&mutex); }else{ if(entr->network_addr == 2130706432) continue; if(e->metric < entr->metric && e->metric <= 15){ sem_wait(&mutex); printf("%s/%s Metric %u",intToIp(entr->network_addr), intToIp(entr->subnet_mask), entr->metric); printf("found better route to %s %s Metric %u through %s\n", intToIp(entr->network_addr), intToIp(entr->subnet_mask), entr->metric, intToIp(e->next_hop)); delete_elem(entr); add_elem(e); sem_post(&mutex); } } } } }
int main(int argc, char **argv) { char buffer[65536]; elem elt; elem *ep; int hsize; ulong_t mode, filesize, namesize; size_t nsize; int uid, gid; char extra; struct stat sb; int chr; boolean_t trailer_copy; int errflg = 0; int verbose = 0; size_t inoffset; boolean_t no_unknowns; if ((myname = *argv) == NULL) myname = "cpiotranslate"; while (errflg == 0 && (chr = getopt(argc, argv, "ve:")) != EOF) { switch (chr) { case 'v': verbose++; break; case 'e': if (except_count >= MAX_EXCEPT_LIST) errflg++; else except_file[except_count++] = optarg; break; default: errflg++; break; } } if (errflg != 0) usage(); /* Allows selection of previous BFU behavior */ no_unknowns = getenv("CPIOTRANSLATE_ALL") != NULL; init_list(&exception_list, HASH_SIZE); while (--except_count >= 0) { (void) read_in_exceptions(except_file[except_count], verbose); } /* Read in all the packaging information */ init_list(&list, HASH_SIZE); while (optind < argc) { if (stat(argv[optind], &sb) == -1) { perror(argv[optind]); } else if (S_ISDIR(sb.st_mode)) { (void) read_in_protodir(argv[optind], &list, verbose); } else if (S_ISREG(sb.st_mode)) { (void) read_in_protolist(argv[optind], &list, verbose); } else { (void) fprintf(stderr, "%s: %s: bad type of object\n", myname, argv[optind]); return (1); } optind++; } /* Process the cpio stream, one file at a time. */ inoffset = 0; for (;;) { /* Read the next cpio header */ hsize = fread(buffer, 1, ASCSZ, stdin); if (hsize == 0 || (hsize == -1 && feof(stdin))) { return (0); } if (hsize == -1) { perror("cpio input"); break; } inoffset += hsize; if (hsize != ASCSZ) { (void) fprintf(stderr, "%s: bad cpio header; only %d bytes\n", myname, hsize); break; } /* Get the data we care about: mode and name sizes */ if (sscanf(buffer+14, "%8lx%*32s%8lx%*32s%8lx", &mode, &filesize, &namesize) != 3) { (void) fprintf(stderr, "%s: bad cpio header; cannot read file size\n", myname); if (verbose != 0) (void) fprintf(stderr, "Header: '%.*s'\n", hsize, buffer); break; } /* Read in file name; account for padding */ nsize = ASCSZ + namesize; if (namesize <= 1 || nsize >= sizeof (buffer)) { (void) fprintf(stderr, "%s: bad cpio header; file name size %lu\n", myname, namesize); break; } if ((nsize & 3) != 0) nsize += 4 - (nsize & 3); hsize = fread(buffer + ASCSZ, 1, nsize - ASCSZ, stdin); if (hsize == -1) { if (feof(stdin)) { (void) fprintf(stderr, "%s: missing file name\n", myname); } else { perror("cpio input"); } break; } inoffset += hsize; if (hsize != nsize - ASCSZ) { (void) fprintf(stderr, "%s: truncated file name\n", myname); break; } buffer[nsize] = '\0'; #ifdef DEBUG if (verbose) { (void) fprintf(stderr, "'%s' at offset %d: nlen %lu flen %lu\n", buffer + ASCSZ, inoffset - nsize, namesize, filesize); } #endif /* Locate file name in packaging information database */ (void) strlcpy(elt.name, buffer + ASCSZ, sizeof (elt.name)); if (nsize == ASCSZ + 14 && filesize == 0 && strcmp(elt.name, "TRAILER!!!") == 0) { trailer_copy = B_TRUE; goto skip_update; } trailer_copy = B_FALSE; elt.arch = P_ISA; ep = find_elem(&list, &elt, FOLLOW_LINK); if (ep == NULL) { ep = find_elem_mach(&list, &elt, FOLLOW_LINK); } if (ep == NULL) { /* * If it's on the exception list, remove it * from the archive. It's not part of the * system. */ ep = find_elem(&exception_list, &elt, FOLLOW_LINK); if (ep != NULL) { if (verbose) { (void) fprintf(stderr, "%s: %s: removed; exception list\n", myname, elt.name); } /* * Cannot use fseek here because input * is usually a pipeline. */ if (filesize & 3) filesize += 4 - (filesize & 3); while (filesize > 0) { nsize = filesize; if (nsize > sizeof (buffer)) nsize = sizeof (buffer); hsize = fread(buffer, 1, nsize, stdin); if (hsize == -1 && ferror(stdin)) { perror("cpio read"); goto failure; } if (hsize != -1) inoffset += hsize; if (hsize != nsize) { (void) fprintf(stderr, "%s: cpio file truncated\n", myname); goto failure; } filesize -= hsize; } continue; } } /* * No mode, user, group on symlinks in the packaging * information. Leave mode alone and set user and * group to 'root' (0). This is what a netinstall * would do. */ if (ep == NULL) { uid = 0; gid = 3; if (!no_unknowns) { (void) fprintf(stderr, "%s: %s: no packaging info\n", myname, elt.name); goto skip_update; } } else if (ep->file_type == SYM_LINK_T) { uid = gid = 0; } else { mode = (mode & S_IFMT) | (ep->perm & ~S_IFMT); if ((uid = stdfind(ep->owner, usernames)) == -1) { (void) fprintf(stderr, "%s: %s: user '%s' unknown\n", myname, elt.name, ep->owner); uid = 0; } if ((gid = stdfind(ep->group, groupnames)) == -1) { (void) fprintf(stderr, "%s: %s: group '%s' unknown\n", myname, elt.name, ep->group); gid = 3; } } /* save character overwritten by sprintf's NUL terminator. */ extra = buffer[38]; /* snprintf not needed; cannot possibly overflow */ (void) sprintf(buffer + 14, "%08lx%08x%08x", mode, uid, gid); /* recover char overwritten with NUL by sprintf above. */ buffer[38] = extra; /* Write out the updated header information */ skip_update: hsize = fwrite(buffer, 1, nsize, stdout); if (hsize == -1) { perror("cpio output"); break; } if (hsize != nsize) { (void) fprintf(stderr, "%s: cpio output disk full\n", myname); break; } if (trailer_copy) { while ((chr = getchar()) != EOF && chr != '0') (void) putchar(chr); if (chr == '0') (void) ungetc(chr, stdin); continue; } /* Copy the file data */ while (filesize > 0) { if ((nsize = filesize) > sizeof (buffer)) nsize = sizeof (buffer); if (nsize & 3) nsize += 4 - (nsize & 3); hsize = fread(buffer, 1, nsize, stdin); if (hsize == -1 && ferror(stdin)) { perror("cpio read"); goto failure; } if (hsize != -1) inoffset += hsize; if (hsize != nsize) { (void) fprintf(stderr, "%s: cpio file truncated\n", myname); goto failure; } hsize = fwrite(buffer, 1, nsize, stdout); if (hsize == -1) { perror("cpio output"); goto failure; } if (hsize != nsize) { (void) fprintf(stderr, "%s: cpio output disk full\n", myname); goto failure; } if (hsize > filesize) break; filesize -= hsize; } } failure: if (verbose != 0) { (void) fprintf(stderr, "%s: stopped at offset %u\n", myname, inoffset); } return (1); }
wordnumber_t step_4_find_outliers(struct Parameters *pParam) { FILE *pOutliers; FILE *pFile; struct InputFile *pFilePtr; char logStr[MAXLOGMSGLEN]; char line[MAXLINELEN]; char key[MAXKEYLEN]; char words[MAXWORDS][MAXWORDLEN]; int len, wordcount, i; struct Elem *pWord, *pElem; wordnumber_t outlierNum; outlierNum = 0; if (!(pOutliers = fopen(pParam->pOutlier, "w"))) { sprintf(logStr, "Can't open outliers file %s", pParam->pOutlier); log_msg(logStr, LOG_ERR, pParam); exit(1); } for (pFilePtr = pParam->pInputFiles; pFilePtr; pFilePtr = pFilePtr->pNext) { if (!(pFile = fopen(pFilePtr->pName, "r"))) { sprintf(logStr, "Can't open input file %s", pFilePtr->pName); log_msg(logStr, LOG_ERR, pParam); continue; } while (fgets(line, MAXLINELEN, pFile)) { len = (int) strlen(line); if (line[len - 1] == '\n') { line[len - 1] = 0; } wordcount = find_words(line, words, pParam); *key = 0; for (i = 0; i < wordcount; i++) { pWord = find_elem(words[i], pParam->ppWordTable, pParam->wordTableSize, pParam->wordTableSeed); if (words[i][0] != 0 && pWord) { strcat(key, words[i]); len = (int) strlen(key); key[len] = CLUSTERSEP; key[len + 1] = 0; } } if (*key == 0 && wordcount) { fprintf(pOutliers, "%s\n", line); outlierNum++; continue; } pElem = find_elem(key, pParam->ppClusterTable, pParam->clusterTableSize, pParam->clusterTableSeed); if (!pElem || (pElem->count < pParam->support)) { fprintf(pOutliers, "%s\n", line); outlierNum++; } } } return outlierNum; }
void interface_2(List *list) { annotation_2(); while (1) { int key_1 = 0; int value_1 = 0; int n = 0; printf("Num of actinon(2):\n"); scanf("%d", &n); if (n == 0) break; if (n == 1) { printf("Key after each add\n"); int find_key = 0; scanf("%d", &find_key); if (!find_elem(list, find_key)) { printf("Try again, press 1\n"); continue; } printf("Key - Value\n"); scanf("%d%d", &key_1, &value_1); sData *ins_data = list_node_data_create(key_1, value_1); if (ins_data == NULL) { printf("ERRROR\n"); continue; } list_insert_after_elem(find_elem(list, find_key), ins_data); continue; } if (n == 2) { printf("Key before each add\n"); int find_key = 0; scanf("%d", &find_key); if (!find_elem(list, find_key)) { printf("Try again, press 1\n"); continue; } printf("Key - Value\n"); scanf("%d%d", &key_1, &value_1); sData *ins_data = list_node_data_create(key_1, value_1); if (ins_data == NULL) { printf("ERRROR\n"); continue; } list_insert_before_elem(find_elem(list, find_key), ins_data); continue; } if (n == 3) { printf("Key - Value\n"); scanf("%d%d", &key_1, &value_1); sData *ins_data = list_node_data_create(key_1, value_1); if (ins_data == NULL) { printf("ERRROR\n"); continue; } list_insert_front(list, ins_data); continue; } if (n == 4) { printf("Key - Value\n"); scanf("%d%d", &key_1, &value_1); sData *ins_data = list_node_data_create(key_1, value_1); if (ins_data == NULL) { printf("ERRROR\n"); continue; } list_insert_last(list, ins_data); continue; } if (n == 5) { list_print(list); continue; } if (n == 9) { annotation_2(); continue; } printf("Wrong input try again\n"); } }