-
Notifications
You must be signed in to change notification settings - Fork 2
/
lab1b.c
168 lines (150 loc) · 3.75 KB
/
lab1b.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ftw.h>
#include <unistd.h>
#include <time.h>
#include <dirent.h>
#define MAX_ELEMS 1000
#define BUFF 20
#define SUM 100
#define MAX_LINE 20
#define ERR(source) (perror(source),\
fprintf(stderr,"%s:%d\n",__FILE__,__LINE__),\
exit(EXIT_FAILURE))
void usage(char* pname){
fprintf(stderr,"Usage: %s [OPTION]...\n"
"Sprawdza, czy ciag jest ciagiem arytmetycznym.\n"
" -l LICZBA\t\tkolejny wyraz ciagu\n"
" -k KATALOG\t\ttworzy KATALOG spelniajacy warunki zadania\n"
" -p \t\ttworzy plik, tylko z flaga -k\n",
pname);
exit(EXIT_FAILURE);
}
extern int optind;
extern char* optarg;
int my_cmp (const void * a, const void * b)
{
int _a = *(int*)a;
int _b = *(int*)b;
if(_a < _b) return 1;
else if(_a == _b) return 0;
else return -1;
}
int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
{
if(remove(fpath))ERR("unlink");
return 0;
}
void makedir(char* name,int* tab,int ind)
{
DIR *dirp;
if(NULL !=(dirp = opendir(name)))
{
if(nftw(name, unlink_cb, 64, FTW_DEPTH | FTW_PHYS)!=0) ERR("nftw");
if(closedir(dirp)) ERR("closedir");
}
if(mkdir(name,0700)) ERR("mkdir");
if(chdir(name))ERR("chdir");
FILE* s1;
for(int i=0;i<ind;i++)
{
int rnd = (rand()%5) + 1;
char tmp[MAX_LINE];
if(sprintf(tmp, "%d", tab[i])<0) ERR("sprintf");
if(rnd <= 2)
{
if((s1=fopen(tmp,"w+"))==NULL)ERR("fopen");
if(fclose(s1))ERR("fclose");
}
else
{
if(symlink(".",tmp)!=0) ERR("symlink");
}
}
if(chdir(".."))ERR("chdir");
}
void mfile(char *name,int* tab,int ind){
FILE* s1;
strcat(name,"f");
if((s1=fopen(name,"w+"))==NULL)ERR("fopen");
for(int i=0;i<ind;i++)
{
fprintf(s1,"%d ",tab[i]);
}
if(fclose(s1))ERR("fclose");
name[strlen(name)-1] = '\0';
}
int main(int argc, char** argv)
{
int ind=0;
int tab[MAX_ELEMS];
int c;
int x;
int num = SUM;
int optp = 0;
char name[BUFF] = "";
while ((c = getopt (argc, argv, "l:k:p")) != -1)
switch (c){
case 'l':
x=strtol(optarg, (char **)NULL, 8);
if (x==-1) usage(argv[0]);
tab[ind++]=x;
break;
case 'k':
strcpy(name,optarg);
break;
case 'p':
optp=1;
break;
case '?':
default: usage(argv[0]);
}
if(argc>optind) usage(argv[0]);
qsort(tab,ind,sizeof(int),my_cmp);
int r = tab[1]-tab[0];
for(int i=2;i<ind;i++)
{
if(tab[i]-tab[i-1]!=r)
return EXIT_FAILURE;
}
printf("Ciag jest arytmetyczny!\n");
if(strcmp(name,"")!=0)
{
srand(time(NULL));
makedir(name,tab,ind);
}
if(optp==1 && strcmp(name,"")!=0)
{
mfile(name,tab,ind);
}
else
{
usage(argv[0]);
}
char a;
if(putenv("SUM=NieZapisanoSumy")!=0) ERR("putenv");
while(scanf("%c",&a) == 1)
{
int sum=0;
char buff[MAX_LINE];
switch(a)
{
case 's':
if(ind<num) usage(argv[0]);
sum = (tab[0]+tab[num-1])*(num)/2;
snprintf(buff,MAX_LINE,"SUM=%d",sum);
if(putenv(buff)!=0) ERR("putenv");
break;
case 'x':
printf("%s\n",getenv("SUM"));
break;
case 'n':
scanf("%d",&num);
break;
}
}
return EXIT_SUCCESS;
}