static errno_t get_active_uid_linux(hash_table_t *table, uid_t search_uid) { DIR *proc_dir = NULL; struct dirent *dirent; int ret, err; pid_t pid = -1; uid_t uid; hash_key_t key; hash_value_t value; proc_dir = opendir("/proc"); if (proc_dir == NULL) { ret = errno; DEBUG(SSSDBG_CRIT_FAILURE, "Cannot open proc dir.\n"); goto done; }; errno = 0; while ((dirent = readdir(proc_dir)) != NULL) { if (only_numbers(dirent->d_name) != 0) continue; ret = name_to_pid(dirent->d_name, &pid); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "name_to_pid failed.\n"); goto done; } ret = get_uid_from_pid(pid, &uid); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "get_uid_from_pid failed.\n"); goto done; } if (table != NULL) { key.type = HASH_KEY_ULONG; key.ul = (unsigned long) uid; value.type = HASH_VALUE_ULONG; value.ul = (unsigned long) uid; ret = hash_enter(table, &key, &value); if (ret != HASH_SUCCESS) { DEBUG(SSSDBG_CRIT_FAILURE, "cannot add to table [%s]\n", hash_error_string(ret)); ret = ENOMEM; goto done; } } else { if (uid == search_uid) { ret = EOK; goto done; } } errno = 0; } if (errno != 0 && dirent == NULL) { ret = errno; DEBUG(SSSDBG_CRIT_FAILURE, "readdir failed.\n"); goto done; } ret = closedir(proc_dir); proc_dir = NULL; if (ret == -1) { DEBUG(SSSDBG_CRIT_FAILURE, "closedir failed, watch out.\n"); } if (table != NULL) { ret = EOK; } else { ret = ENOENT; } done: if (proc_dir != NULL) { err = closedir(proc_dir); if (err) { DEBUG(SSSDBG_CRIT_FAILURE, "closedir failed, bad dirp?\n"); } } return ret; }
int main(void) { FILE *primefile; char first_range_string[128], second_range_string[128]; char *endptr; unsigned char filecount; bool line_number_switch; u_long_64 file_linecount, *semiprimes = NULL, semiprimes_count = 0; u_long_64 first_range,second_range; unsigned int x; int perc = 0; welcome(); for(;;) { // first, lets clean up memset(first_range_string,'\0',128); memset(second_range_string,'\0',128); first_range=second_range=perc=0; line_number_switch=0; filecount=1; file_linecount=2; //! First Range printf("\n\nPlease enter the lowest range number: "); scanf("%s",&first_range_string); if(!strcasecmp("exit",first_range_string)) return 0; if(first_range_string[0] == '-') { negative_input(); /*if the input is negative, show the error message*/ continue; // you can see continue just like goto } first_range = strtoull(first_range_string,&endptr,0); if(*endptr == '.') /*if the user types a float number, show this message*/ decimal_input(first_range, first_range_string); if((*endptr) && *endptr != '.') { only_numbers(); continue; } //! Second Range printf("Please enter the highest range number: "); scanf("%s",&second_range_string); if(!strcasecmp("exit",second_range_string)) return(0); if(second_range_string[0] == '-') { negative_input(); /*if the input is negative, show the error message*/ continue; } second_range = strtoull(second_range_string,&endptr,0); if(*endptr == '.') decimal_input(second_range, second_range_string); /*if the user types a float number, show this message*/ if((*endptr) && *endptr != '.') { only_numbers(); continue; } //! Check Ranges if(first_range>=second_range) /*if the user types a bad range, show error message*/ { bad_ranges(); continue; } //! End error check printf("\n"); primefile = file_init(primefile); semiprimes = semiprimegen(first_range, second_range, &semiprimes_count); #ifdef PROGRESS printf("\nWriting to file...\n"); #ifdef WIN32 fflush(stdout); #else fpurge(stoud); #endif #endif // prints semiprimes found for(x=0; x<semiprimes_count; x++) { // printf(VAL_FORMAT "\t", *(semiprimes + x)); if(line_number_switch==0) // first line { fprintf(primefile,"1."); line_number_switch=true; } fprintf(primefile,"\t" VAL_FORMAT , *(semiprimes + x)); // print the number to the text file if(filecount==10) // it will insert a new line in the text file, change value if desired { fprintf(primefile,"\n" VAL_FORMAT ".",file_linecount); filecount=0; // reset file_linecount++; // number of lines written } filecount++; // number of printed primes #ifdef PROGRESS if( (perc+1) <= (int)(100.0 * x / semiprimes_count)) { perc = (int)(100.0 * x / semiprimes_count); progress(false, perc); } #endif } #ifdef PROGRESS progress(false, 100); printf("\n"); #ifdef WIN32 fflush(stdout); #else fpurge(stoud); #endif #endif results(primefile, first_range, second_range, semiprimes_count); //! After Results Given // unload free(semiprimes); printf("\nCheck the \"semiprimes.txt\" file,\nGenerated in the same Directory this Program is In.\n"); } //! End for return 0; }
int main(void) { char first_range_string[128], second_range_string[128]; char *endptr; welcome(); for(;;){ // first, lets clean up just in case memset(first_range_string,'\0',128); memset(second_range_string,'\0',128); first_range=second_range=prime_numbers=0; //! First Range printf("\n\nPlease enter the lowest range number: "); scanf("%s",&first_range_string); if(!strcasecmp("exit",first_range_string)) return 0; else if(first_range_string[0] == '-' && first_range_string[1] == '-') { optparse(&first_range_string[2]); continue; } if(first_range_string[0] == '-') { negative_input(); /*if the input is negative, show the error message*/ continue; // you can see continue just like goto } first_range = strtoull(first_range_string,&endptr,0); if(*endptr == '.') /*if the user types a float number, show this message*/ decimal_input(first_range, first_range_string); if((*endptr) && *endptr != '.') { only_numbers(); continue; } //! Second Range printf("Please enter the highest range number: "); scanf("%s",&second_range_string); if(!strcasecmp("exit",second_range_string)) return(0); else if(first_range_string[0] == '-' && first_range_string[1] == '-') { optparse(&first_range_string[2]); continue; } if(second_range_string[0] == '-') { negative_input(); /*if the input is negative, show the error message*/ continue; } second_range = strtoull(second_range_string,&endptr,0); if(*endptr == '.') decimal_input(second_range, second_range_string); /*if the user types a float number, show this message*/ if((*endptr) && *endptr != '.') { only_numbers(); continue; } //! Check Ranges if(first_range>=second_range) /*if the user types a bad range, show error message*/ { bad_ranges(); continue; } //! End error check printf("\n"); file_init(); if(!options.progress) primegen(); else primegen_prog(); results(); //! After Results Given printf("\nCheck the \"primes.txt\" file, Generated in the same Directory this Program is In.\n"); } //! End for return 0; }