forked from michaelrsweet/epm
-
Notifications
You must be signed in to change notification settings - Fork 8
/
dist.c
2113 lines (1686 loc) · 62.8 KB
/
dist.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Distribution functions for the ESP Package Manager (EPM).
*
* Copyright 2020 by Jim Jagielski
* Copyright 1999-2020 by Michael R Sweet
* Copyright 1999-2010 by Easy Software Products.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Include necessary headers...
*/
#include "epm.h"
#include <pwd.h>
/*
* Some versions of Solaris don't define gethostname()...
*/
#ifdef __sun
extern int gethostname(char *, size_t);
#endif /* __sun */
/*
* Local functions...
*/
static int compare_files(const file_t *f0, const file_t *f1);
static void expand_name(char *buffer, char *name, size_t bufsize, int warn);
static char *get_file(const char *filename, char *buffer, size_t size);
static char *get_inline(const char *term, FILE *fp, char *buffer, size_t size);
static char *get_line(char *buffer, int size, FILE *fp, struct utsname *platform,
const char *format, int *skip);
static char *get_string(char **src, char *dst, size_t dstsize);
static int patmatch(const char *, const char *);
static int sort_subpackages(char **a, char **b);
static void update_architecture(char *buffer, size_t bufsize);
/*
* Conditional "skip" bits...
*/
#define SKIP_SYSTEM 1 /* Not the right system */
#define SKIP_FORMAT 2 /* Not the right format */
#define SKIP_ARCH 4 /* Not the right architecture */
#define SKIP_IF 8 /* Set if the current #if was not satisfied */
#define SKIP_IFACTIVE 16 /* Set if we're in an #if */
#define SKIP_IFSAT 32 /* Set if an #if statement has been satisfied */
#define SKIP_MASK 15 /* Bits to look at */
/*
* 'add_command()' - Add a command to the distribution...
*/
void add_command(dist_t *dist, /* I - Distribution */
FILE *fp, /* I - Distribution file */
int type, /* I - Command type */
const char *command, /* I - Command string */
const char *subpkg, /* I - Subpackage */
const char *section) /* I - Literal text section */
{
command_t *temp; /* New command */
char buf[16384]; /* File import buffer */
if (!strncmp(command, "<<", 2)) {
for (command += 2; isspace(*command & 255); command++)
;
command = get_inline(command, fp, buf, sizeof(buf));
} else if (command[0] == '<' && command[1]) {
for (command++; isspace(*command & 255); command++)
;
command = get_file(command, buf, sizeof(buf));
}
if (!command)
return;
if (dist->num_commands == 0)
temp = malloc(sizeof(command_t));
else
temp = realloc(dist->commands, (dist->num_commands + 1) * sizeof(command_t));
if (!temp) {
perror("epm: Out of memory allocating a command");
return;
}
dist->commands = temp;
temp += dist->num_commands;
temp->type = type;
temp->command = strdup(command);
if (!temp->command) {
perror("epm: Out of memory duplicating a command string");
return;
}
temp->subpackage = subpkg;
if (section && *section) {
temp->section = strdup(section);
if (!temp->section) {
perror("epm: Out of memory duplicating a literal section");
free(temp->command);
return;
}
} else
temp->section = NULL;
dist->num_commands++;
}
/*
* 'add_depend()' - Add a dependency to the distribution...
*/
void add_depend(dist_t *dist, /* I - Distribution */
int type, /* I - Type of dependency */
const char *line, /* I - Line from file */
const char *subpkg) /* I - Subpackage */
{
int i; /* Looping var */
depend_t *temp; /* New dependency */
char *ptr; /* Pointer into string */
const char *lineptr; /* Temporary pointer into line */
/*
* Allocate memory for the dependency...
*/
if (dist->num_depends == 0)
temp = malloc(sizeof(depend_t));
else
temp = realloc(dist->depends, (dist->num_depends + 1) * sizeof(depend_t));
if (temp == NULL) {
perror("epm: Out of memory allocating a dependency");
return;
}
dist->depends = temp;
temp += dist->num_depends;
dist->num_depends++;
/*
* Initialize the dependency record...
*/
memset(temp, 0, sizeof(depend_t));
temp->type = type;
temp->subpackage = subpkg;
/*
* Get the product name string...
*/
for (ptr = temp->product; *line && !isspace(*line & 255); line++)
if (ptr < (temp->product + sizeof(temp->product) - 1))
*ptr++ = *line;
while (isspace(*line & 255))
line++;
/*
* Get the version strings, if any...
*/
for (i = 0; *line && i < 2; i++) {
/*
* Handle <version, >version, etc.
*/
if (!isalnum(*line & 255)) {
if (*line == '<' && i == 0) {
strlcpy(temp->version[0], "0.0", sizeof(temp->version[0]));
i++;
}
while (!isdigit(*line & 255) && *line)
line++;
}
if (!*line)
break;
/*
* Grab the version string...
*/
for (ptr = temp->version[i]; *line && !isspace(*line & 255); line++)
if (ptr < (temp->version[i] + sizeof(temp->version[i]) - 1))
*ptr++ = *line;
while (isspace(*line & 255))
line++;
/*
* Get the version number, if any...
*/
for (lineptr = line; isdigit(*lineptr & 255); lineptr++)
;
if (!*line || (!isspace(*lineptr & 255) && *lineptr)) {
/*
* No version number specified, or the next number is a version
* string...
*/
temp->vernumber[i] = get_vernumber(temp->version[i]);
} else {
/*
* Grab the version number directly from the line...
*/
temp->vernumber[i] = atoi(line);
for (line = lineptr; isspace(*line & 255); line++)
;
}
}
/*
* Handle assigning default values based on the number of version numbers...
*/
switch (i) {
case 0:
strlcpy(temp->version[0], "0.0", sizeof(temp->version[0]));
/* no break */
case 1:
strlcpy(temp->version[1], "999.99.99p99", sizeof(temp->version[1]));
temp->vernumber[1] = INT_MAX;
break;
}
}
/*
* 'add_description()' - Add a description to the distribution.
*/
void add_description(dist_t *dist, /* I - Distribution */
FILE *fp, /* I - Source file */
const char *description, /* I - Description string */
const char *subpkg) /* I - Subpackage name */
{
description_t *temp; /* Temporary description array */
char buf[16384]; /* File import buffer */
if (!strncmp(description, "<<", 2)) {
for (description += 2; isspace(*description & 255); description++)
;
description = get_inline(description, fp, buf, sizeof(buf));
} else if (description[0] == '<' && description[1]) {
for (description++; isspace(*description & 255); description++)
;
description = get_file(description, buf, sizeof(buf));
}
if (description == NULL)
return;
if (dist->num_descriptions == 0)
temp = malloc(sizeof(description_t));
else
temp = realloc(dist->descriptions,
(dist->num_descriptions + 1) * sizeof(description_t));
if (temp == NULL) {
perror("epm: Out of memory adding description");
return;
}
dist->descriptions = temp;
temp += dist->num_descriptions;
dist->num_descriptions++;
temp->subpackage = subpkg;
if ((temp->description = strdup(description)) == NULL)
perror("epm: Out of memory duplicating description");
}
/*
* 'add_file()' - Add a file to the distribution.
*/
file_t * /* O - New file */
add_file(dist_t *dist, /* I - Distribution */
const char *subpkg) /* I - Subpackage name */
{
file_t *file; /* New file */
if (dist->num_files == 0)
dist->files = (file_t *)malloc(sizeof(file_t));
else
dist->files =
(file_t *)realloc(dist->files, sizeof(file_t) * (dist->num_files + 1));
file = dist->files + dist->num_files;
dist->num_files++;
file->subpackage = subpkg;
return (file);
}
/*
* 'add_subpackage()' - Add a subpackage to the distribution.
*/
char * /* O - Subpackage pointer */
add_subpackage(dist_t *dist, /* I - Distribution */
const char *subpkg) /* I - Subpackage name */
{
char **temp, /* Temporary array pointer */
*s; /* Subpackage pointer */
if (dist->num_subpackages == 0)
temp = malloc(sizeof(char *));
else
temp = realloc(dist->subpackages, (dist->num_subpackages + 1) * sizeof(char *));
if (temp == NULL)
return (NULL);
dist->subpackages = temp;
temp += dist->num_subpackages;
dist->num_subpackages++;
*temp = s = strdup(subpkg);
if (dist->num_subpackages > 1)
qsort(dist->subpackages, (size_t)dist->num_subpackages, sizeof(char *),
(int (*)(const void *, const void *))sort_subpackages);
/*
* Return the new string...
*/
return (s);
}
/*
* 'find_subpackage()' - Find a subpackage in the distribution.
*/
char * /* O - Subpackage pointer */
find_subpackage(dist_t *dist, /* I - Distribution */
const char *subpkg) /* I - Subpackage name */
{
char **match; /* Matching subpackage */
if (!subpkg || !*subpkg)
return (NULL);
if (dist->num_subpackages > 0)
match = bsearch(&subpkg, dist->subpackages, (size_t)dist->num_subpackages,
sizeof(char *),
(int (*)(const void *, const void *))sort_subpackages);
else
match = NULL;
if (match != NULL)
return (*match);
else
return (add_subpackage(dist, subpkg));
}
/*
* 'free_dist()' - Free memory used by a distribution.
*/
void free_dist(dist_t *dist) /* I - Distribution to free */
{
int i; /* Looping var */
if (dist->num_files > 0)
free(dist->files);
for (i = 0; i < dist->num_descriptions; i++)
free(dist->descriptions[i].description);
if (dist->num_descriptions)
free(dist->descriptions);
for (i = 0; i < dist->num_subpackages; i++)
free(dist->subpackages[i]);
if (dist->num_subpackages)
free(dist->subpackages);
for (i = 0; i < dist->num_commands; i++) {
free(dist->commands[i].command);
if (dist->commands[i].section)
free(dist->commands[i].section);
}
if (dist->num_commands)
free(dist->commands);
if (dist->num_depends)
free(dist->depends);
free(dist);
}
/*
* 'getoption()' - Get an option from a file.
*/
const char * /* O - Value */
get_option(file_t *file, /* I - File */
const char *name, /* I - Name of option */
const char *defval) /* I - Default value of option */
{
char *ptr; /* Pointer to option */
static char option[256]; /* Copy of file option */
/*
* See if the option exists...
*/
snprintf(option, sizeof(option), "%s(", name);
if ((ptr = strstr(file->options, option)) == NULL)
return (defval);
/*
* Yes, copy the value and truncate at the first ")"...
*/
ptr += strlen(option);
memmove(option, ptr, strlen(ptr) + 1);
if ((ptr = strchr(option, ')')) != NULL) {
*ptr = '\0';
return (option);
} else
return (defval);
}
/*
* 'get_platform()' - Get the operating system information...
*/
void get_platform(struct utsname *platform) /* O - Platform info */
{
char *temp; /* Temporary pointer */
#ifdef __APPLE__
FILE *fp; /* SystemVersion.plist file */
char line[1024], /* Line from file */
*ptr; /* Pointer into line */
int major, minor; /* Major and minor release numbers */
#endif /* __APPLE__ */
/*
* Get the system identification information...
*/
uname(platform);
#ifdef __APPLE__
/*
* Try to get the OS version number from the SystemVersion.plist file.
* If not present, use the uname() results for Darwin...
*/
if ((fp = fopen("/System/Library/CoreServices/SystemVersion.plist", "r")) != NULL) {
ptr = NULL;
while (fgets(line, sizeof(line), fp) != NULL)
if ((ptr = strstr(line, "ProductUserVisibleVersion")) != NULL)
break;
else if ((ptr = strstr(line, "ProductVersion")) != NULL)
break;
if (ptr && fgets(line, sizeof(line), fp) != NULL) {
major = 10;
minor = 2;
if ((ptr = strstr(line, "<string>")) != NULL)
sscanf(ptr + 8, "%d.%d", &major, &minor);
snprintf(platform->release, sizeof(platform->release), "%d.%d", major, minor);
} else {
/*
* Couldn't find the version number, so assume it is 10.1...
*/
strlcpy(platform->release, "10.1", sizeof(platform->release));
}
fclose(fp);
strlcpy(platform->sysname, "macos", sizeof(platform->sysname));
}
#endif /* __APPLE__ */
/*
* Adjust the CPU type accordingly...
*/
#ifdef __sgi
strlcpy(platform->machine, "mips", sizeof(platform->machine));
#elif defined(__hpux)
strlcpy(platform->machine, "hppa", sizeof(platform->machine));
#elif defined(_AIX)
strlcpy(platform->machine, "ppc", sizeof(platform->machine));
#else
update_architecture(platform->machine, sizeof(platform->machine));
#endif /* __sgi */
#ifdef _AIX
/*
* AIX stores the major and minor version numbers separately;
* combine them...
*/
snprintf(platform->release, sizeof(platform->release), "%d.%d",
atoi(platform->version), atoi(platform->release));
#else
/*
* Remove any extra junk from the release number - we just want the
* major and minor numbers...
*/
while (!isdigit((int)platform->release[0]) && platform->release[0])
memmove(platform->release, platform->release + 1, strlen(platform->release));
if (platform->release[0] == '.')
memmove(platform->release, platform->release + 1, strlen(platform->release));
for (temp = platform->release; *temp && isdigit(*temp & 255); temp++)
;
if (*temp == '.')
for (temp++; *temp && isdigit(*temp & 255); temp++)
;
*temp = '\0';
#endif /* _AIX */
/*
* Convert the operating system name to lowercase, and strip out
* hyphens and underscores...
*/
for (temp = platform->sysname; *temp != '\0'; temp++)
if (*temp == '-' || *temp == '_') {
memmove(temp, temp + 1, strlen(temp));
temp--;
} else
*temp = tolower(*temp);
/*
* SunOS 5.x is really Solaris 2.x or Solaris X, and OSF1 is really
* Digital UNIX a.k.a. Compaq Tru64 UNIX...
*/
if (!strcmp(platform->sysname, "sunos") && platform->release[0] >= '5') {
strlcpy(platform->sysname, "solaris", sizeof(platform->sysname));
if (atoi(platform->release + 2) < 7)
platform->release[0] -= 3;
else {
/*
* Strip 5. from the front of the version number...
*/
for (temp = platform->release; temp[2]; temp++)
*temp = temp[2];
*temp = '\0';
}
} else if (!strcmp(platform->sysname, "osf1"))
strlcpy(platform->sysname, "tru64",
sizeof(platform->sysname)); /* AKA Digital UNIX */
else if (!strcmp(platform->sysname, "irix64"))
strlcpy(platform->sysname, "irix", sizeof(platform->sysname)); /* IRIX */
#ifdef DEBUG
printf("sysname = %s\n", platform->sysname);
printf("release = %s\n", platform->release);
printf("machine = %s\n", platform->machine);
#endif /* DEBUG */
}
/*
* 'get_runlevels()' - Get the run levels for the specified init script.
*/
const char * /* O - Run levels */
get_runlevels(file_t *file, /* I - File */
const char *deflevels) /* I - Default run levels */
{
const char *runlevels; /* Pointer to runlevels option */
if ((runlevels = strstr(file->options, "runlevel(")) != NULL)
runlevels += 9;
else if ((runlevels = strstr(file->options, "runlevels(")) != NULL)
runlevels += 10; /* Compatible mis-spelling... */
else
runlevels = deflevels;
return (runlevels);
}
/*
* 'get_start()' - Get the start number for an init script.
*/
int /* O - Start number */
get_start(file_t *file, /* I - File */
int defstart) /* I - Default start number */
{
const char *start; /* Pointer to start option */
if ((start = strstr(file->options, "start(")) != NULL)
return (atoi(start + 6));
else
return (defstart);
}
/*
* 'get_stop()' - Get the stop number for an init script.
*/
int /* O - Start number */
get_stop(file_t *file, /* I - File */
int defstop) /* I - Default stop number */
{
const char *stop; /* Pointer to stop option */
if ((stop = strstr(file->options, "stop(")) != NULL)
return (atoi(stop + 5));
else
return (defstop);
}
/*
* 'new_dist()' - Create a new, empty software distribution.
*/
dist_t * /* O - New distribution */
new_dist(void) {
/*
* Create a new, blank distribution...
*/
return ((dist_t *)calloc(sizeof(dist_t), 1));
}
/*
* 'read_dist()' - Read a software distribution.
*/
dist_t * /* O - New distribution */
read_dist(const char *filename, /* I - Main distribution list file */
struct utsname *platform, /* I - Platform information */
const char *format) /* I - Format of distribution */
{
FILE *listfiles[10]; /* File lists */
int listlevel; /* Level in file list */
char line[2048], /* Expanded line from list file */
buf[1024]; /* Original line from list file */
int type; /* File type */
char dst[256], /* Destination path */
src[256], /* Source path */
pattern[256], /* Pattern for source files */
user[32], /* User */
group[32], /* Group */
*temp, /* Temporary pointer */
options[256]; /* File options */
mode_t mode; /* File permissions */
int skip; /* 1 = skip files, 0 = archive files */
dist_t *dist; /* Distribution data */
file_t *file; /* Distribution file */
struct stat fileinfo; /* File information */
DIR *dir; /* Directory */
DIRENT *dent; /* Directory entry */
struct passwd *pwd; /* Password entry */
const char *subpkg; /* Subpackage */
/*
* Create a new, blank distribution...
*/
dist = new_dist();
/*
* Open the main list file...
*/
if ((listfiles[0] = fopen(filename, "r")) == NULL) {
fprintf(stderr, "epm: Unable to open list file \"%s\" -\n %s\n", filename,
strerror(errno));
return (NULL);
}
/*
* Find any product descriptions, etc. in the list file...
*/
if (Verbosity)
puts("Searching for product information...");
skip = 0;
listlevel = 0;
subpkg = NULL;
do {
while (get_line(buf, sizeof(buf), listfiles[listlevel], platform, format,
&skip) != NULL) {
/*
* Do variable substitution...
*/
line[0] = buf[0]; /* Don't expand initial $ */
expand_name(line + 1, buf + 1, sizeof(line) - 1,
strncmp(buf, "%if", 3) || strncmp(buf, "%elseif", 7));
/*
* Check line for config stuff...
*/
if (line[0] == '%') {
/*
* Find whitespace...
*/
for (temp = line; !isspace(*temp & 255) && *temp; temp++)
;
for (; isspace(*temp & 255); *temp++ = '\0')
;
/*
* Process directive...
*/
if (!strcmp(line, "%include")) {
listlevel++;
if ((listfiles[listlevel] = fopen(temp, "r")) == NULL) {
fprintf(stderr, "epm: Unable to include \"%s\" -\n %s\n",
temp, strerror(errno));
listlevel--;
}
} else if (!strcmp(line, "%description"))
add_description(dist, listfiles[listlevel], temp, subpkg);
else if (!strcmp(line, "%preinstall"))
add_command(dist, listfiles[listlevel], COMMAND_PRE_INSTALL, temp,
subpkg, NULL);
else if (!strcmp(line, "%install") || !strcmp(line, "%postinstall"))
add_command(dist, listfiles[listlevel], COMMAND_POST_INSTALL, temp,
subpkg, NULL);
else if (!strcmp(line, "%remove") || !strcmp(line, "%preremove"))
add_command(dist, listfiles[listlevel], COMMAND_PRE_REMOVE, temp,
subpkg, NULL);
else if (!strcmp(line, "%postremove"))
add_command(dist, listfiles[listlevel], COMMAND_POST_REMOVE, temp,
subpkg, NULL);
else if (!strcmp(line, "%prepatch"))
add_command(dist, listfiles[listlevel], COMMAND_PRE_PATCH, temp,
subpkg, NULL);
else if (!strcmp(line, "%patch") || !strcmp(line, "%postpatch"))
add_command(dist, listfiles[listlevel], COMMAND_POST_PATCH, temp,
subpkg, NULL);
else if (!strncmp(line, "%literal(", 9)) {
char *ptr, /* Pointer to parenthesis */
*section; /* Section for literal text */
section = line + 9;
if ((ptr = strchr(section, ')')) != NULL) {
*ptr = '\0';
add_command(dist, listfiles[listlevel], COMMAND_LITERAL, temp,
subpkg, section);
} else
fputs("epm: Ignoring bad %literal(section) line in list file.\n",
stderr);
} else if (!strcmp(line, "%product")) {
if (!dist->product[0])
strlcpy(dist->product, temp, sizeof(dist->product));
else
fputs("epm: Ignoring %product line in list file.\n", stderr);
} else if (!strcmp(line, "%copyright")) {
if (!dist->copyright[0])
strlcpy(dist->copyright, temp, sizeof(dist->copyright));
else
fputs("epm: Ignoring %copyright line in list file.\n", stderr);
} else if (!strcmp(line, "%vendor")) {
if (!dist->vendor[0])
strlcpy(dist->vendor, temp, sizeof(dist->vendor));
else
fputs("epm: Ignoring %vendor line in list file.\n", stderr);
} else if (!strcmp(line, "%packager")) {
if (!dist->packager[0])
strlcpy(dist->packager, temp, sizeof(dist->packager));
else
fputs("epm: Ignoring %packager line in list file.\n", stderr);
} else if (!strcmp(line, "%license")) {
if (!dist->license[0])
strlcpy(dist->license, temp, sizeof(dist->license));
else
fputs("epm: Ignoring %license line in list file.\n", stderr);
} else if (!strcmp(line, "%readme")) {
if (!dist->readme[0])
strlcpy(dist->readme, temp, sizeof(dist->readme));
else
fputs("epm: Ignoring %readme line in list file.\n", stderr);
} else if (!strcmp(line, "%subpackage")) {
subpkg = find_subpackage(dist, temp);
} else if (!strcmp(line, "%version")) {
if (!dist->version[0]) {
if (strchr(temp, ':')) {
/*
* Grab the epoch...
*/
dist->epoch = (int)strtol(temp, &temp, 10);
if (*temp == ':')
temp++;
}
strlcpy(dist->version, temp, sizeof(dist->version));
if ((temp = strchr(dist->version, ' ')) != NULL) {
*temp++ = '\0';
dist->vernumber = atoi(temp);
} else
dist->vernumber = get_vernumber(dist->version);
if ((temp = strrchr(dist->version, '-')) != NULL) {
*temp++ = '\0';
strlcpy(dist->release, temp, sizeof(dist->release));
}
}
} else if (!strcmp(line, "%release")) {
strlcpy(dist->release, temp, sizeof(dist->release));
dist->vernumber += atoi(temp);
} else if (!strcmp(line, "%incompat"))
add_depend(dist, DEPEND_INCOMPAT, temp, subpkg);
else if (!strcmp(line, "%provides"))
add_depend(dist, DEPEND_PROVIDES, temp, subpkg);
else if (!strcmp(line, "%replaces"))
add_depend(dist, DEPEND_REPLACES, temp, subpkg);
else if (!strcmp(line, "%requires"))
add_depend(dist, DEPEND_REQUIRES, temp, subpkg);
else {
fprintf(stderr, "epm: Unknown directive \"%s\" ignored.\n", line);
fprintf(stderr, " %s %s\n", line, temp);
}
} else if (line[0] == '$') {
/*
* Define a variable...
*/
if (line[1] == '{' && (temp = strchr(line + 2, '}')) != NULL) {
/*
* Remove {} from name...
*/
memmove(temp, temp + 1, strlen(temp));
memmove(line + 1, line + 2, strlen(line + 1));
} else if (line[1] == '(' && (temp = strchr(line + 2, ')')) != NULL) {
/*
* Remove () from name...
*/
memmove(temp, temp + 1, strlen(temp));
memmove(line + 1, line + 2, strlen(line + 1));
}
if ((temp = strchr(line + 1, '=')) != NULL) {
/*
* Only define the variable if it is not in the environment
* or on the command-line.
*/
*temp = '\0';
if (getenv(line + 1) == NULL) {
*temp = '=';
if ((temp = strdup(line + 1)) != NULL)
putenv(temp);
}
}
} else {
type = line[0];
if (!isspace(line[1] & 255)) {
fprintf(stderr, "epm: Expected whitespace after file type: %s\n",
line);
continue;
}
temp = line + 2;
mode = strtol(temp, &temp, 8);
if (temp == (line + 2)) {
fprintf(stderr,
"epm: Expected file permissions after file type: %s\n", line);
continue;
}
if (get_string(&temp, user, sizeof(user)) == NULL) {
fprintf(stderr, "epm: Expected user after file permissions: %s\n",
line);
continue;
}
if (get_string(&temp, group, sizeof(group)) == NULL) {
fprintf(stderr, "epm: Expected group after user: %s\n", line);
continue;
}
if (get_string(&temp, dst, sizeof(dst)) == NULL) {
fprintf(stderr, "epm: Expected destination after group: %s\n", line);
continue;
}
get_string(&temp, src, sizeof(src));
get_string(&temp, options, sizeof(options));
if (tolower(type) == 'd' || type == 'R') {
strlcpy(options, src, sizeof(options));
src[0] = '\0';
}
#ifdef __osf__ /* Remap group "sys" to "system" */
if (!strcmp(group, "sys"))
strlcpy(group, "system", sizeof(group));
#elif defined(__linux) /* Remap group "sys" to "root" */
if (!strcmp(group, "sys"))
strlcpy(group, "root", sizeof(group));
#endif /* __osf__ */
if ((temp = strrchr(src, '/')) == NULL)
temp = src;
else
temp++;
for (; *temp; temp++)
if (strchr("*?[", *temp))
break;
if (*temp) {
/*
* Add using wildcards...
*/
if ((temp = strrchr(src, '/')) == NULL)
temp = src;
else
*temp++ = '\0';
strlcpy(pattern, temp, sizeof(pattern));
if (dst[strlen(dst) - 1] != '/')
strlcat(dst, "/", sizeof(dst));
if (temp == src)
dir = opendir(".");
else
dir = opendir(src);
if (dir == NULL)
fprintf(stderr, "epm: Unable to open directory \"%s\": %s\n", src,
strerror(errno));
else {
/*
* Make sure we have a directory separator...
*/
if (temp > src)
temp[-1] = '/';
while ((dent = readdir(dir)) != NULL) {
strlcpy(temp, dent->d_name,
sizeof(src) - (size_t)(temp - src));
if (stat(src, &fileinfo))
continue; /* Skip files we can't read */
if (S_ISDIR(fileinfo.st_mode))
continue; /* Skip directories */