1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
| #include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
char yyyy[10]; /* переменная yyyy (год) глобальная, т.к. используется и в main и в handler */
void handler (int signo)
{
if((atoi(yyyy) % 4) == 0) /* % 4 означает, что число берём по модулю 4. Если получится 0, значит 366 дней */
printf("\t366 days in a %d year\n", atoi(yyyy));
else
printf("\t365 days in a %d year\n", atoi(yyyy));
}
int main()
{
char str[200]; /* в этой переменной собираем строку вида: cal mm yyyy | grep " dd " | cut -a" " -f1 */
char dd[10];
char mm[10];
sigset_t old;
struct sigaction new;
/* настройка sigaction и переменных для её использования */
sigemptyset(&old);
sigprocmask(0, 0, &old);
sigemptyset(&new.sa_mask);
new.sa_handler=handler;
new.sa_mask=old;
new.sa_flags=0;
if(sigaction(SIGINT, &new, 0) == -1)
perror("sigaction");
/* выводим календарь текущего месяца */
printf("\nCalendar of this month: \n");
system("cal");
/* ввод даты, день недели которой хотим узнать */
printf("\n\nEnter day in a format dd: ");
scanf("%s", dd);
printf("Enter month in a format mm: ");
scanf("%s", mm);
printf("Enter year in a format yyyy: ");
scanf("%s", yyyy);
/* лихорадочно жмём Ctrl+C */
sleep(1);
sleep(1);
sleep(1);
sleep(1);
sleep(1);
/* Собираем строку для вывода дня недели введённой даты */
strcpy(str, "cal ");
strcat(str, mm);
strcat(str, " ");
strcat(str, yyyy);
strcat(str, " | grep ' ");
strcat(str, dd);
strcat(str, " ' | cut -d ' ' -f1");//вот здесь заключается ошибка, хотел сделать так чтобы он у календаря
//скопировал нужны символы дня недели, но не получилось
/* вывод дня недели введённой даты */
printf("\n\nDay of week %s %s %s:\n", dd, mm, yyyy);
system(str);
printf("\n");
return 0;
} |