int main(int argc, char **argv) { long long i = 0; int verbose = 0; int skip = 0; if(argc == 1) { printf("%zu\n", count_entries(".")); } else if(argc == 2 && strcmp(argv[1], "-v") == 0) { printf("./\t%zu\n", count_entries(".")); } else { /* check for -v flag */ for (i = 1 ; i < argc; i++ ) { if (strcmp(argv[i], "-v") == 0) { verbose = 1; skip = i; } } for (i = 1 ; i < argc; i++ ) { if( i != skip) { ssize_t count = count_entries(argv[i]); if(count == -1) continue; if(verbose) printf("%s\t%zu\n", argv[i], count); else printf("%zu\n", count); } } } }
int xmdinit2(const char *project) { struct p2_options *p2opt = NULL; ce_xmd_pool = npool_init(); p2opt = p2_load(project, state, ce_xmd_pool); if (!xmd_fields) xmd_fields = (p2opt->catalog_fields ? p2opt->catalog_fields : default_fields); if (!xmd_widths) xmd_widths = (p2opt->catalog_widths ? p2opt->catalog_widths : default_widths); if (!xmd_labels) xmd_labels = (p2opt->catalog_labels ? p2opt->catalog_labels : default_labels); nfields = count_entries(xmd_fields, "catalog-fields"); nwidths = count_entries(xmd_widths, "catalog-widths"); nlabels = count_entries(xmd_labels, "catalog-widths"); if (nfields != nwidths) { fprintf(stderr, "ce_xmd2: %s/00lib/config.xml: `%s-catalog-fields' and `%s-catalog-widths' should have same number of entries\n", project, state, state); return 1; } if (xmd_labels && nfields != nlabels) { fprintf(stderr, "ce_xmd2: %s/00lib/config.xml: `%s-catalog-fields' and `%s-catalog-labels' should have same number of entries\n", project, state, state); return 1; } field_specs = malloc((nfields+1)*sizeof(const char *)); width_specs = malloc((nwidths+1)*sizeof(const char *)); label_specs = malloc((nlabels+1)*sizeof(const char *)); set_entries(field_specs, xmd_fields); set_entries(width_specs, xmd_widths); if (xmd_labels) set_entries(label_specs, xmd_labels); set_field_lists(field_specs); return 0; }
static void parse_ranges_property (struct hw *current, const char *property_name, const char *property_value) { int nr_ranges; int range_nr; range_property_spec *ranges; const char *chp; /* determine the number of ranges specified */ nr_ranges = count_entries (current, property_name, property_value, 3); /* create a property of that size */ ranges = zalloc (nr_ranges * sizeof(*ranges)); /* fill it in */ chp = property_value; for (range_nr = 0; range_nr < nr_ranges; range_nr++) { chp = parse_address (current, current, chp, &ranges[range_nr].child_address); chp = parse_address (current, hw_parent(current), chp, &ranges[range_nr].parent_address); chp = parse_size (current, current, chp, &ranges[range_nr].size); } /* create it */ hw_add_range_array_property (current, property_name, ranges, nr_ranges); free (ranges); }
/*=gfunc count * * what: definition count * * exparg: ag-name, name of AutoGen value * * doc: Count the number of entries for a definition. * The input argument must be a string containing the name * of the AutoGen values to be counted. If there is no * value associated with the name, the result is an SCM * immediate integer value of zero. =*/ SCM ag_scm_count(SCM obj) { int ent_len = count_entries(ag_scm2zchars(obj, "ag object")); return AG_SCM_INT2SCM(ent_len); }
parse_reg_property(device *current, const char *property_name, const char *property_value) { int nr_regs; int reg_nr; reg_property_spec *regs; const char *chp; device *bus = device_parent(current); /* determine the number of reg entries by counting tokens */ nr_regs = count_entries(current, property_name, property_value, 1 + (device_nr_size_cells(bus) > 0)); /* create working space */ regs = zalloc(nr_regs * sizeof(*regs)); /* fill it in */ chp = property_value; for (reg_nr = 0; reg_nr < nr_regs; reg_nr++) { chp = parse_address(current, bus, chp, ®s[reg_nr].address); if (device_nr_size_cells(bus) > 0) chp = parse_size(current, bus, chp, ®s[reg_nr].size); else memset(®s[reg_nr].size, 0, sizeof (®s[reg_nr].size)); } /* create it */ device_add_reg_array_property(current, property_name, regs, nr_regs); zfree(regs); }
static void parse_reg_property (struct hw *current, const char *property_name, const char *property_value) { int nr_regs; int reg_nr; reg_property_spec *regs; const char *chp; /* determine the number of reg entries by counting tokens */ nr_regs = count_entries (current, property_name, property_value, 2); /* create working space */ regs = zalloc (nr_regs * sizeof (*regs)); /* fill it in */ chp = property_value; for (reg_nr = 0; reg_nr < nr_regs; reg_nr++) { chp = parse_address (current, hw_parent(current), chp, ®s[reg_nr].address); chp = parse_size (current, hw_parent(current), chp, ®s[reg_nr].size); } /* create it */ hw_add_reg_array_property (current, property_name, regs, nr_regs); free (regs); }
static int set_servers (struct global *g) { int ret = 0; // count servers size_t num_entries = count_entries(g); // allocate sort array struct dns_sort_entry *sort_entries = BAllocArray(num_entries, sizeof(sort_entries[0])); if (!sort_entries) { goto fail0; } // fill sort array num_entries = 0; for (LinkedList1Node *n = LinkedList1_GetFirst(&g->instances); n; n = LinkedList1Node_Next(n)) { struct instance *o = UPPER_OBJECT(n, struct instance, instances_node); for (LinkedList1Node *en = LinkedList1_GetFirst(&o->entries); en; en = LinkedList1Node_Next(en)) { struct dns_entry *e = UPPER_OBJECT(en, struct dns_entry, list_node); sort_entries[num_entries].line = e->line; sort_entries[num_entries].priority= e->priority; num_entries++; } } // sort by priority // use a custom insertion sort instead of qsort() because we want a stable sort struct dns_sort_entry temp; BInsertionSort(sort_entries, num_entries, sizeof(sort_entries[0]), dns_sort_comparator, &temp); ExpString estr; if (!ExpString_Init(&estr)) { goto fail1; } for (size_t i = 0; i < num_entries; i++) { if (!ExpString_Append(&estr, sort_entries[i].line)) { goto fail2; } } // set servers if (!NCDIfConfig_set_resolv_conf(ExpString_Get(&estr), ExpString_Length(&estr))) { goto fail2; } ret = 1; fail2: ExpString_Free(&estr); fail1: BFree(sort_entries); fail0: return ret; }
// cjfeng 12/08/2018 // Using fin int check_dipole(char *Dipnames, char *Hamname, int nosc, int nframes) { int vals; vals = count_entries(Dipnames); if(vals!=nosc) { // Checking number of oscillators printf("Error! Different number of oscillators (%d vs. %d) in Hamiltonian file %s and dipole file %s\n", vals, nosc, Hamname, Dipnames); return 3; } vals = count_lines(Dipnames); if(vals!=nframes) { // Checking number of frames printf("Error! Different number of lines (%d vs. %d) in Hamiltonian file %s and dipole file %s\n", vals, nframes, Hamname, Dipnames); return 3; } return 0; }
/* * The dbfile has lines of the format: * user type bridge nicname */ bool get_nic_if_avail(int fd, char *me, char *pid, char *intype, char *br, int allowed, char **nicname) { off_t len, slen; struct stat sb; char *buf = NULL, *newline; int ret, count = 0; cull_entries(fd, me, intype, br); fstat(fd, &sb); len = sb.st_size; if (len != 0) { buf = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (buf == MAP_FAILED) { fprintf(stderr, "Failed to create mapping\n"); return false; } count = count_entries(buf, len, me, intype, br); if (count >= allowed) return false; } get_new_nicname(nicname, br, pid); /* me ' ' intype ' ' br ' ' *nicname + '\n' + '\0' */ slen = strlen(me) + strlen(intype) + strlen(br) + strlen(*nicname) + 5; newline = alloca(slen); ret = snprintf(newline, slen, "%s %s %s %s\n", me, intype, br, *nicname); if (ret < 0 || ret >= slen) { if (lxc_netdev_delete_by_name(*nicname) != 0) fprintf(stderr, "Error unlinking %s!\n", *nicname); return false; } if (len) munmap(buf, len); if (ftruncate(fd, len + slen)) fprintf(stderr, "Failed to set new file size\n"); buf = mmap(NULL, len + slen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (buf == MAP_FAILED) { fprintf(stderr, "Failed to create mapping after extending: error %d\n", errno); if (lxc_netdev_delete_by_name(*nicname) != 0) fprintf(stderr, "Error unlinking %s!\n", *nicname); return false; } strcpy(buf+len, newline); munmap(buf, len+slen); return true; }
int check_sites(char* Sitesname, char* Hamname, int nosc, int nframes) { int vals; if( (strlen(Sitesname)>0) ) { vals = count_entries(Sitesname); if(nosc!=vals) { // Checking number of oscillators printf("Error! Different number of oscillators (%d vs. %d) located in Hamiltonian file %s and sites file %s\n", nosc, vals, Hamname, Sitesname); return 3; } vals = count_lines(Sitesname); if((nframes!=vals)) { // Checking number of frames printf("Error! Different number of lines (%d vs. %d) in Hamiltonian file %s and sites file %s\n", nframes, vals, Hamname, Sitesname); return 3; } } return 0; }
static void compute_dist(HashTablePtr table) { int i; HashBucketPtr bucket; printf("Entries = %ld, hits = %ld, partials = %ld, misses = %ld\n", table->entries, table->hits, table->partials, table->misses); clear_dist(); for (i = 0; i < HASH_SIZE; i++) { bucket = table->buckets[i]; update_dist(count_entries(bucket)); } for (i = 0; i < DIST_LIMIT; i++) { if (i != DIST_LIMIT-1) printf("%5d %10d\n", i, dist[i]); else printf("other %10d\n", dist[i]); } }
int check_dip2Q(char *Dip2Qnames, char *Ham2Qname, int n2Q, int nframes) { int vals; if (strlen(Dip2Qnames) == 0 ) { printf("Error! File names of 2Q dipole moments are not provided completely.\n"); return 3; } vals = count_entries(Dip2Qnames); if(vals!=n2Q) { // Checking number of oscillators printf("Error! Different number of 2Q oscillators (%d vs. %d) in Ham2Q file %s and dip2Q file %s\n", vals, n2Q, Ham2Qname, Dip2Qnames); return 3; } vals = count_lines(Dip2Qnames); if(vals!=nframes) { // Checking number of frames printf("Error! Different number of lines (%d vs. %d) in Ham2Q file %s and dip2Q file %s\n", vals, nframes, Ham2Qname, Dip2Qnames); return 3; } return 0; }
// cjfeng 01/04/2019 // Added traj_param int check_ham(char* Hamname, int *nframes, traj_param *traj_param, int info, int window, int nthreads) { int vals; int nbuffer = traj_param->nbuffer; int nread = traj_param->nread; *nframes = count_lines(Hamname); printf("Located %d lines in input file %s\n", *nframes, Hamname); vals = count_entries(Hamname); traj_param->nosc = floor(sqrt(vals)+0.5); traj_param->n2Q = (traj_param->nosc)*( (traj_param->nosc) + 1 )/2; printf("Located %d oscillators in input file %s\n", traj_param->nosc, Hamname); if(*nframes<nbuffer-nread+1) { printf("Error: Not enough (%d) frames for accurate averaging with requested window size (%d).\n", *nframes, window); printf("At least %d frames have to be provided to compute spectra for %d thread(s).\n", nbuffer, nthreads); return 3; } if( (traj_param->nosc!=info) && (info!=0) ) { printf("Error! Info file specifies %d oscillators, but found %d in Hamiltonian file. \n", info, traj_param->nosc); return 3; } return 0; }
// cjfeng 01/04/2019 // Added traj_param int check_ham2Q(char* Ham2Qname, int *nframes, traj_param *traj_param, int info, int win2d, int nthreads) { if( (strlen(Ham2Qname)>0) ) { int vals, n2Q_tmp; int nbuffer = traj_param->nbuffer; int nread = traj_param->nread; *nframes = count_lines(Ham2Qname); printf("Located %d lines in input file %s\n", *nframes, Ham2Qname); vals = count_entries(Ham2Qname); traj_param->n2Q = floor(sqrt(vals)+0.5); n2Q_tmp = (traj_param->nosc)*( (traj_param->nosc) + 1 )/2; printf("Located %d 2Q oscillators in input file %s\n", traj_param->n2Q, Ham2Qname); if ( traj_param->n2Q != n2Q_tmp ) { printf("n2Q (%d) from the input file %s is not equal to n2Q (%d) computed from the 1Q Ham.\n", traj_param->n2Q, Ham2Qname, n2Q_tmp); return 3; } if(*nframes<nbuffer-nread+1) { printf("Error: Not enough (%d) frames for accurate averaging with requested window size (%d).\n", *nframes, win2d); printf("At least %d frames have to be provided to compute spectra for %d thread(s).\n", nbuffer, nthreads); return 3; } } return 0; }
void fastq_sample(unsigned long rng_seed, const char* prefix, const char* cprefix, FILE* file1, FILE* file2, unsigned long k, double p) { /* * The basic idea is this: * * 1. Count the number of lines in the file, n. * * 2a. If sampling with replacement, generate k random integers in [0, n-1]. * * 2b. If sampling without replacement, generate a list of integers 0..(n-1), * shuffle with fisher-yates, then consider the first k. * * 3. Sort the integer list. * * 3. Read through the file again, when the number at the front of the integer * list matches the index of the fastq etry, print the entry, and pop the * number. */ unsigned long n, n2; fastq_t* f1 = fastq_create(file1); fastq_t* f2 = file2 == NULL ? NULL : fastq_create(file2); n = count_entries(f1); if (f2 != NULL) { n2 = count_entries(f2); if (n != n2) { fprintf(stderr, "Input files have differing numbers of entries (%lu != %lu).\n", n, n2); exit(1); } } fastq_rewind(f1); if (f2 != NULL) fastq_rewind(f2); if (p > 0.0) { k = (unsigned long) round(p * (double) n); if (!replacement_flag && k > n) k = n; } rng_t* rng = fastq_rng_alloc(); fastq_rng_seed(rng, rng_seed); unsigned long* xs; if (replacement_flag) xs = index_with_replacement(rng, n, k); else xs = index_without_replacement(rng, n); qsort(xs, k, sizeof(unsigned long), cmpul); /* open output */ FILE* fout1; FILE* fout2; char* output_name; size_t output_len; if (file2 == NULL) { output_len = strlen(prefix) + 7; output_name = malloc_or_die((output_len + 1) * sizeof(char)); snprintf(output_name, output_len, "%s.fastq", prefix); fout1 = open_without_clobber(output_name); if (fout1 == NULL) { fprintf(stderr, "Cannot open file %s for writing.\n", output_name); exit(1); } fout2 = NULL; free(output_name); } else { output_len = strlen(prefix) + 9; output_name = malloc_or_die((output_len + 1) * sizeof(char)); snprintf(output_name, output_len, "%s.1.fastq", prefix); fout1 = open_without_clobber(output_name); if (fout1 == NULL) { fprintf(stderr, "Cannot open file %s for writing.\n", output_name); exit(1); } snprintf(output_name, output_len, "%s.2.fastq", prefix); fout1 = open_without_clobber(output_name); if (fout1 == NULL) { fprintf(stderr, "Cannot open file %s for writing.\n", output_name); exit(1); } free(output_name); } /* open complement output */ FILE* cfout1 = NULL; FILE* cfout2 = NULL; if (cprefix != NULL && file2 == NULL) { output_len = strlen(cprefix) + 7; output_name = malloc_or_die((output_len + 1) * sizeof(char)); snprintf(output_name, output_len, "%s.fastq", cprefix); cfout1 = fopen(output_name, "wb"); if (cfout1 == NULL) { fprintf(stderr, "Cannot open file %s for writing.\n", output_name); exit(1); } cfout2 = NULL; free(output_name); } else if (cprefix != NULL) { output_len = strlen(cprefix) + 9; output_name = malloc_or_die((output_len + 1) * sizeof(char)); snprintf(output_name, output_len, "%s.1.fastq", cprefix); cfout1 = fopen(output_name, "wb"); if (cfout1 == NULL) { fprintf(stderr, "Cannot open file %s for writing.\n", output_name); exit(1); } snprintf(output_name, output_len, "%s.2.fastq", cprefix); cfout2 = fopen(output_name, "wb"); if (cfout1 == NULL) { fprintf(stderr, "Cannot open file %s for writing.\n", output_name); exit(1); } free(output_name); } unsigned long i = 0; // read number unsigned long j = 0; // index into xs int ret; seq_t* seq1 = seq_create(); seq_t* seq2 = seq_create(); while (j < k && fastq_read(f1, seq1)) { if (f2 != NULL) { ret = fastq_read(f2, seq2); if (ret == 0) { fputs("Input files have differing numbers of entries.\n", stderr); exit(1); } } if (xs[j] == i) { while (j < k && xs[j] == i) { fastq_print(fout1, seq1); if (f2 != NULL) fastq_print(fout2, seq2); ++j; } } else if (cfout1 != NULL) { fastq_print(cfout1, seq1); if (f2 != NULL) fastq_print(cfout2, seq2); } ++i; } seq_free(seq1); seq_free(seq2); fastq_free(f1); if (f2 != NULL) fastq_free(f2); fclose(fout1); if (fout2 != NULL) fclose(fout2); if (cfout1 != NULL) fclose(cfout1); if (cfout2 != NULL) fclose(cfout2); fastq_rng_free(rng); free(xs); }
/* allocate, populate and sort the jitentry arrays */ void create_arrays(void) { max_entry_count = entry_count = count_entries(); entries_symbols_ascending = create_sorted_array(cmp_symbolname); entries_address_ascending = create_sorted_array(cmp_address); }
/* * Import authlist from the userspace */ static int write_authlist(struct authlist *authlist, void __user *buffer, size_t *lenp, loff_t *ppos) { struct authlist_entry *entries = NULL, *old_entries; char *memblk = NULL; int error = 0; int nentries; int ne; int terminal = -1; const char *p; /* ensure atomic transfer */ if (*ppos != 0) return -EINVAL; if (*lenp > AUTHLIST_LENGTH_LIMIT) return -EINVAL; memblk = kmalloc(*lenp + 1, GFP_KERNEL); if (memblk == NULL) return -ENOMEM; if (copy_from_user(memblk, buffer, *lenp)) error_out(-EFAULT); memblk[*lenp] = '\0'; nentries = count_entries(memblk); if (nentries == 0) error_out(-EINVAL); entries = kmalloc(sizeof(struct authlist_entry) * nentries, GFP_KERNEL); if (entries == NULL) error_out(-ENOMEM); for (p = memblk, ne = 0;; ne++) { /* skip leading whitespace */ while (is_ws(*p)) p++; /* reached the end of the string? */ if (*p == '\0') break; error = parse_entry(&p, entries + ne); if (error) goto out; switch (entries[ne].kind) { case AUTHLIST_KIND_EVERYBODY: case AUTHLIST_KIND_NOBODY: if (terminal != -1) error_out(-EINVAL); terminal = ne; break; default: break; } } /* * Last entry must be everybody/nobody. * Intermediate entry cannot be everybody/nobody. */ if (terminal != nentries - 1) error_out(-EINVAL); down_write(&authlist->rws); old_entries = authlist->entries; authlist->nentries = nentries; authlist->entries = entries; up_write(&authlist->rws); kfree(old_entries); entries = NULL; *ppos += *lenp; out: kfree(memblk); kfree(entries); return error; }