Example #1
0
int main(int argc, char* argv[]) {
        int r;

        log_parse_environment();
        log_open();

        r = parse_argv(argc, argv);
        if (r <= 0)
                goto finish;

        if (argc > optind) {
                int i, q;

                for (i = optind; i < argc; i++) {
                        q = print_home(argv[i]);
                        if (q < 0)
                                r = q;
                }
        } else
                r = list_homes();


finish:
        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
Example #2
0
int main(){
/* this function is the actual main function in my program*/
	struct name * arruser[16];
	int num_mem;/*this is the total number of members in the group*/
	char * mem[16];/* this the array which stores addresses of usernames*/
	char * pass[16];/* this array stores addresses of respective passwords*/
	char grpname[10];/* char array containing the group name*/
	char grppass[10]; /* this array contains the password of the group*/
	int user;/* this is the current user that has logged in( users index in the array)*/
	int i, j, k;/* these are some random variables to be used as counters.*/
	int state = 1;/* this acs like flag between two different switch cases which go on in an infinite loop wher only few condition can break out of it depending on the value of this flag.*/
	int choice; /*this variable selects the case in switch*/
	struct data * curr1;
	struct list * curr2;
	char buffer[10];
	char * buffer1;
	/*now to start calling functions one by one:*/
	printf("Loading...\n");
	print_line();
	j = load_file(grpname, grppass, mem, &num_mem, pass, arruser);
	if(j == 0){
	 printf(" the standard file could not be opened\n you can still go ahead with working but previous data is lost now\n");
	 }
	 if(j == 2){
	 printf(" Looks like you are a new user!\n welcome to splitwise!\n");
	 }
	 if(j == 1){
	 printf("All previous user data is now loaded!\n welcome back to splitwise!\n");
	 }
	 if(j == 4){
	 	printf(" welcome back to splitwise!\n you already have a group created\n but there are no members in this group\n please add yourself to the group first\n");
	 	printf(" please enter your username\n");
	 	scanf(" %s", buffer);
	 	mem[0] = (char * ) malloc(10);
	 	if(mem[0] == NULL){
	 		printf(" not enough memory\n");
	 		exit(1);
	 	}
	 	strcpy(mem[0], buffer);
	 	buffer1 = getpass(" hello, please enter you password\n");
	 	pass[0] = (char*)malloc(10);
	 	if(pass[0] == NULL){
	 		printf(" not enough memory\n");
	 		exit(1);
	 	}
	 	strcpy(pass[0], buffer1);
	 	printf(" you are now added to the group %s \n welcome!\n", grpname);
	 	arruser[0] = (struct name*)malloc(sizeof(struct name));
	 	arruser[0]->q = NULL;
	 	arruser[0]->p = NULL;
	 	arruser[0]->num_data = 0;
	 	arruser[0]->num_stmt = 0;
	 }
	 print_line();
	 print_home();
	 scanf("%d", &choice);
	 while(1){
	 	
	 	if(state == 1){
	 		switch(choice){
	 			case 1: 
	 				if(num_mem == 0){
	 					printf(" there aren't any group members\n");
	 					create_grp(grpname, grppass,mem, pass, &num_mem,arruser); 
	 					print_home();
	 					scanf("%d", &choice);
	 					print_line();
	 					state = 1;
	 					/* check the return values*/
	 					break;
	 				}
	 				j = login(mem, pass, &user);
	 				print_line();
	 				if(j == 0){
	 					state = 3;
	 					break;
	 				}	
	 				if(j == 1){
	 					print_menu();
	 					scanf("%d", &choice);
	 					print_line();
	 					state = 2;
	 				}
	 				break;
	 			case 2: 
					j = check_grp(grpname);
					if(j == 0){
						/* no group exists*/
						k = create_grp(grpname, grppass, mem, pass, &num_mem, arruser);
						if(k == 0){
							printf(" creating group failed\n");
							state = 3;
						}
						if(k == 1){
							print_home();
	 						scanf("%d", &choice);
							print_line();
							state = 1;
						}
					}
					else{
						printf("A group alredy exists. you cannot create multiple groups. if youi want to create a new group the current group will have to be deleted.\n press 1 to delete existing group\n press 2 to exit\n");
						scanf("%d", &k);
						print_line();
						if(k == 1){
							delete_grp(grpname, grppass, mem, pass, &num_mem, arruser);
							i = create_grp(grpname, grppass, mem, pass, &num_mem,arruser);
							if(i == 0){
							printf(" creating group failed\n");
							state = 3;
							}
							if(i == 1){
								print_home();
	 							scanf("%d", &choice);
								print_line();
								state = 1;
							}
						}
						if(k == 2){
						print_home();
						scanf("%d", &choice);
							print_line();
							state  = 1;
						}
					}
					break;
				
				case 3:
					j = check_grp(grpname);
					if(j == 0){
					;
					}
					else{
						delete_grp(grpname, grppass, mem, pass, &num_mem, arruser);
					}
					print_home();
					scanf("%d", &choice);
					print_line();
					state = 1;
					break;
				
				case 4: delete_member(grpname, grppass, mem, pass, &num_mem, arruser);
					print_home();
					scanf("%d", &choice);
					print_line();
					state = 1;
					break;
				case 5:
					state = 3;
					break;
					
				default:
						printf(" you have entered an invalid choice\n please enter again\n");
						print_home();
						scanf("%d", &choice);
						print_line();
						state = 1;
					break;
			}/* switch closed*/
		}/* if closed*/
		if( state == 2){
			switch(choice){
				case 1:
					j = add_expense(user, arruser);
					if(j == 0){
						printf(" sorry could not add any more expenses\n");
						print_menu();
	 					scanf("%d", &choice);
						print_line();
						state  = 2;
					}
					if(j == 1){
						print_menu();
	 					scanf("%d", &choice);
						print_line();
						state = 2;
					}
				break;
				 case 2:
				 	j = get_statement(user, arruser);
				 	if(j == 0){
				 		printf(" sorry, the file could no be opened\n");
				 	}
				 	print_menu();
	 				scanf("%d", &choice);
				 	print_line();
				 	state = 2;
				 	break;
				 	
				 case 3:
				 	j = split_expense(mem, arruser);
				 	print_menu();
	 				scanf("%d", &choice);
				 	print_line();
				 	state = 2;
				 	break;
				 
				 case 4:
				 	view_my_contri(user, arruser);
				 	print_menu();
	 				scanf("%d", &choice);
				 	print_line();
				 	state = 2;
				 	break;
				 	
				 case 5:
				 	j = split_expense(mem, arruser);
				 	if( j == 0){
						print_menu();
	 					scanf("%d", &choice);
						print_line();
				 		/* unsucessful*/
				 		state = 2;
				 		break;
				 	}
				 	j = add_member(grpname, grppass, mem, pass, &num_mem, arruser);
				 	if (j == 0){
				 	/* unsuccessful*/
				 		printf(" adding member was unsuccessful\n please try again\n");
				 		print_menu();
	 					scanf("%d", &choice);
				 		print_line();
				 		state = 2;
				 		break;
				 	}
				 	print_menu();
	 				scanf("%d", &choice);
				 	print_line();
				 	state = 2;
				 	break;
				 
				 case 6:
					 reset_1stmt(arruser, user);
					 printf("successful in clearing you statements!\n");
					 print_menu();
			       	 	 scanf("%d",&choice);	
			       	 	print_line();
			       	 	 state = 2;
				 	break;
				 	
				 case 7:
				 	user = -1;
				 	print_home();
				 	scanf("%d", &choice);
				 	print_line();
				 	state = 1;
				 	break;
				 
				 case 8 :
				 	state = 3;
				 	break;
				 	
				 default : printf(" you have entered a wrong choice\n please enter again\n");
				 print_menu();
	 			scanf("%d", &choice);
				print_line();
				 state = 2;
				 break;
			 }/* switch closed*/
		}/* if closed*/
		if(state == 3){
			FILE * fp;
			char data[] = "stddata";
			fp = fopen(data, "w");
			if(fp == NULL){
				printf(" file cannot be opened to store all the data you create in this session.\n sorry\n");
				return 0;
			}
			fprintf(fp, "%s\n", grpname);
			fprintf(fp, "%s\n",grppass);
			fprintf(fp, "%d\n", num_mem);
			for(i = 0; i< num_mem; i++){
				if(mem[i] != NULL){
					fprintf(fp, "%s\n", mem[i]);
					fprintf(fp, "%s\n", pass[i]);
					
				}/* if closed*/
			}
			fclose(fp);
			for(i = 0; i< num_mem; i++){
				fp = fopen(mem[i], "w+");
				fprintf(fp, "%d\n", arruser[i]->num_data);
				curr1 = arruser[i]->q;
				while(curr1){
					fprintf(fp, "%d ", curr1->amt);
					fprintf(fp, "%s\n", curr1->des);
					curr1 = curr1->n;
				}
				fprintf(fp, "%d ", arruser[i]->num_stmt);
				curr2 = arruser[i]->p;
				while(curr2){
					fprintf(fp, "%s\n", curr2->stmt);
					curr2 = curr2->next;
				}
				fclose(fp);
			}
			reset_data(arruser);
			reset_stmt(arruser);	
			for(i = 0; i < num_mem; i++){
				free(mem[i]);
				free(pass[i]);
				
			}
			break;
		}
	}/* while closed*/
	return 0;
}/* main closed*/