File azan.c changed (mode: 100644) (index bed1c2a..ba30940) |
... |
... |
create_cache_file(time_t t, char* full_path) |
376 |
376 |
(long)prayer_array[5] |
(long)prayer_array[5] |
377 |
377 |
); |
); |
378 |
378 |
|
|
|
379 |
|
free(cache_dir); |
|
380 |
|
cache_dir = NULL; |
379 |
381 |
(void)fclose(fpointer); |
(void)fclose(fpointer); |
380 |
382 |
} |
} |
381 |
383 |
|
|
|
... |
... |
read_azan_cache_file(char* azan_cache_file) |
398 |
400 |
(void)fclose(fp); |
(void)fclose(fp); |
399 |
401 |
|
|
400 |
402 |
time_t* pray_times_array; /* the returned array */ |
time_t* pray_times_array; /* the returned array */ |
401 |
|
pray_times_array = calloc(6, sizeof(time_t)); |
|
|
403 |
|
pray_times_array = calloc(12, sizeof(time_t)); |
402 |
404 |
|
|
403 |
405 |
if (pray_times_array == NULL) { |
if (pray_times_array == NULL) { |
404 |
406 |
printf("Error, allocating memory [ Line %d ]\n", __LINE__); |
printf("Error, allocating memory [ Line %d ]\n", __LINE__); |
405 |
407 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
406 |
408 |
} |
} |
407 |
|
|
|
408 |
409 |
char* tmp_pray[6]; /* string array hold temp values for checking */ |
char* tmp_pray[6]; /* string array hold temp values for checking */ |
409 |
410 |
|
|
410 |
411 |
/* |
/* |
411 |
|
* split each key value sepeated by <,> |
|
412 |
|
* the first strtok should take the string |
|
|
412 |
|
* split each key value sepeated by <\n> |
|
413 |
|
* the first strtok take the string |
413 |
414 |
* if the value is not NULL: |
* if the value is not NULL: |
414 |
|
* 1-clean_json string, 2-convert to time_t 3-assign to returned array |
|
|
415 |
|
* 1-convert to time_t 2-assign to returned array |
415 |
416 |
*/ |
*/ |
416 |
417 |
char delim[] = "\n"; |
char delim[] = "\n"; |
417 |
418 |
|
|
418 |
|
for(int i=0; i<6; i++){ |
|
419 |
|
if (i == 0) { |
|
420 |
|
tmp_pray[0] = strtok(buffer, delim); |
|
421 |
|
} else { |
|
422 |
|
tmp_pray[i] = strtok(NULL, delim); |
|
423 |
|
} |
|
|
419 |
|
tmp_pray[0] = strtok(buffer, delim); |
|
420 |
|
if (tmp_pray[0] != NULL) { |
|
421 |
|
pray_times_array[0] = (time_t)atol(tmp_pray[0]); |
|
422 |
|
} |
|
423 |
|
|
|
424 |
|
for(int i=1; i<5; i++){ |
|
425 |
|
tmp_pray[i] = strtok(NULL, delim); |
424 |
426 |
if (tmp_pray[i] != NULL) { |
if (tmp_pray[i] != NULL) { |
425 |
427 |
pray_times_array[i] = (time_t)atol(tmp_pray[i]); |
pray_times_array[i] = (time_t)atol(tmp_pray[i]); |
426 |
428 |
} |
} |
427 |
429 |
} |
} |
428 |
430 |
|
|
429 |
|
return pray_times_array; |
|
|
431 |
|
if (pray_times_array != NULL) { |
|
432 |
|
return pray_times_array; |
|
433 |
|
} else { |
|
434 |
|
exit(EXIT_FAILURE); |
|
435 |
|
} |
430 |
436 |
} |
} |
431 |
437 |
|
|
432 |
438 |
static int |
static int |
|
... |
... |
print_next_prayer(time_t current_time, char* azan_cache_file) |
452 |
458 |
} |
} |
453 |
459 |
|
|
454 |
460 |
free(azan_array); |
free(azan_array); |
|
461 |
|
azan_array = NULL; |
455 |
462 |
return 0; |
return 0; |
456 |
463 |
} |
} |
457 |
464 |
|
|
|
... |
... |
get_cache_dir(void) |
515 |
522 |
char* cache_dir; |
char* cache_dir; |
516 |
523 |
cache_dir = calloc(128, sizeof(char)); |
cache_dir = calloc(128, sizeof(char)); |
517 |
524 |
|
|
518 |
|
if (user_home == NULL) { |
|
|
525 |
|
if (user_home == NULL || cache_dir == NULL) { |
519 |
526 |
printf("Error, allocating memory [ Line %d ]\n", __LINE__); |
printf("Error, allocating memory [ Line %d ]\n", __LINE__); |
520 |
527 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
521 |
528 |
} |
} |
|
... |
... |
get_cache_file_name(time_t t) |
540 |
547 |
(void)snprintf(azan_cache_file, max_file_name, "azan_%.2i-%.2i-%i.txt", |
(void)snprintf(azan_cache_file, max_file_name, "azan_%.2i-%.2i-%i.txt", |
541 |
548 |
day, month, year); |
day, month, year); |
542 |
549 |
|
|
543 |
|
static char full_path[256]; |
|
|
550 |
|
char* full_path = calloc(128, sizeof(char)); |
|
551 |
|
if (full_path == NULL) { |
|
552 |
|
printf("Error! reading array [ Line %d ]\n", __LINE__); |
|
553 |
|
exit(EXIT_FAILURE); |
|
554 |
|
} |
544 |
555 |
strcpy(full_path, cache_dir); |
strcpy(full_path, cache_dir); |
545 |
556 |
strcat(full_path, "/"); |
strcat(full_path, "/"); |
546 |
557 |
strcat(full_path, azan_cache_file); |
strcat(full_path, azan_cache_file); |
547 |
558 |
|
|
|
559 |
|
free(cache_dir); |
548 |
560 |
return full_path; |
return full_path; |
549 |
561 |
} |
} |
550 |
562 |
|
|
551 |
|
static void |
|
|
563 |
|
static int |
552 |
564 |
send_notification(char* azan_cache_file) |
send_notification(char* azan_cache_file) |
553 |
565 |
{ |
{ |
554 |
566 |
time_t* azan_array; |
time_t* azan_array; |
|
... |
... |
send_notification(char* azan_cache_file) |
564 |
576 |
strncpy(command, body, sizeof(char)*sizeof(body)); |
strncpy(command, body, sizeof(char)*sizeof(body)); |
565 |
577 |
|
|
566 |
578 |
for (int i=0; i<5; i++) { |
for (int i=0; i<5; i++) { |
567 |
|
// convert_timet_to_str; |
|
568 |
579 |
char prayer_first_letter = conv_prayer_int_to_char(i); |
char prayer_first_letter = conv_prayer_int_to_char(i); |
569 |
580 |
char pfl[3]; |
char pfl[3]; |
570 |
581 |
pfl[0] = prayer_first_letter; |
pfl[0] = prayer_first_letter; |
|
... |
... |
send_notification(char* azan_cache_file) |
574 |
585 |
strncat(command, pfl, sizeof(char)*3); |
strncat(command, pfl, sizeof(char)*3); |
575 |
586 |
strncat(command, prayer_time, sizeof(char)*128); |
strncat(command, prayer_time, sizeof(char)*128); |
576 |
587 |
strncat(command, "\\n", sizeof(char)*2); |
strncat(command, "\\n", sizeof(char)*2); |
577 |
|
// printf("%c %s\n", prayer_first_letter, prayer_time); |
|
578 |
588 |
free(prayer_time); |
free(prayer_time); |
|
589 |
|
prayer_time = NULL; |
579 |
590 |
} |
} |
580 |
591 |
strncat(command, "'", sizeof(char)*1); |
strncat(command, "'", sizeof(char)*1); |
|
592 |
|
|
581 |
593 |
free(azan_array); |
free(azan_array); |
582 |
|
(void)system(command); |
|
|
594 |
|
azan_array = NULL; |
|
595 |
|
if (system(command) == 0){ |
|
596 |
|
return 0; |
|
597 |
|
} else { |
|
598 |
|
return -1; |
583 |
599 |
} |
} |
|
600 |
|
} |
584 |
601 |
|
|
585 |
602 |
int |
int |
586 |
603 |
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
|
... |
... |
main(int argc, char *argv[]) |
596 |
613 |
(void)puts("azan-"VERSION); |
(void)puts("azan-"VERSION); |
597 |
614 |
break; |
break; |
598 |
615 |
case 'n': |
case 'n': |
599 |
|
send_notification(full_path); |
|
|
616 |
|
if (send_notification(full_path) < 0){ |
|
617 |
|
printf("Error Creating Cache File\n"); |
|
618 |
|
return -1; |
|
619 |
|
} |
|
620 |
|
free(full_path); |
|
621 |
|
full_path = NULL; |
600 |
622 |
break; |
break; |
601 |
623 |
case 'a': |
case 'a': |
602 |
624 |
print_all_prayers(full_path); |
print_all_prayers(full_path); |
|
625 |
|
free(full_path); |
|
626 |
|
full_path = NULL; |
603 |
627 |
break; |
break; |
604 |
628 |
case 'h': |
case 'h': |
605 |
629 |
usage(); |
usage(); |
|
... |
... |
main(int argc, char *argv[]) |
614 |
638 |
/* Create cache file if not exists */ |
/* Create cache file if not exists */ |
615 |
639 |
if (create_cache_file(t, full_path) < 0) { |
if (create_cache_file(t, full_path) < 0) { |
616 |
640 |
printf("Error Creating Cache File\n"); |
printf("Error Creating Cache File\n"); |
617 |
|
free(full_path); |
|
618 |
|
full_path = NULL; |
|
619 |
641 |
return -1; |
return -1; |
620 |
642 |
} |
} |
621 |
643 |
|
|
622 |
644 |
/* Read cache file */ |
/* Read cache file */ |
623 |
645 |
if (print_next_prayer(t, full_path) < 0){ |
if (print_next_prayer(t, full_path) < 0){ |
624 |
646 |
printf("Error Reading Cache File\n"); |
printf("Error Reading Cache File\n"); |
625 |
|
free(full_path); |
|
626 |
|
full_path = NULL; |
|
627 |
647 |
return -1; |
return -1; |
628 |
648 |
} |
} |
|
649 |
|
free(full_path); |
|
650 |
|
full_path = NULL; |
629 |
651 |
} |
} |
630 |
652 |
|
|
631 |
653 |
return 0; |
return 0; |