/ src / main.c
main.c
 1  /*
 2   * main.c
 3   * Copyright 2023 Dirk Gottschalk <dirk.gottschalk1980@googlemail.com>
 4   * 
 5   * SPDX-License-Identifier: GPL-2.0
 6   */
 7  
 8  #ifndef _GNU_SOURCE
 9  #define _GNU_SOURCE
10  #endif
11  
12  #include "bottles.h"
13  #include <stdio.h>
14  #include <stdlib.h>
15  #include <time.h>
16  #include <unistd.h>
17  
18  void help(void)
19  {
20  	printf("%s version %s\n\n", PACKAGE_NAME, PACKAGE_VERSION);
21  	printf("Available Options:\n\n");
22  	printf("	-n <number>	Number of loops.\n");
23  	printf("	-d <delay>	Delay between the verses in seconds.\n");
24  	printf("	-h		Show this help.\n\n");
25  }
26  
27  int main(int argc, char **argv)
28  {
29  	int b;
30  	int loops;
31  	int sleeptime;
32  	int opt;
33  
34  	loops = 1;
35  	sleeptime = 0;
36  
37  	while ((opt = getopt(argc, argv, "n:d:hv")) != -1) {
38  		switch (opt) {
39  		case 'n':
40  			loops = (int)strtol(optarg, NULL, 10);
41  			break;
42  
43  		case 'd':
44  			sleeptime = (int)strtol(optarg, NULL, 10);
45  			break;
46  
47  		case 'v':
48  			/* Show program version and exit */
49  			printf("%s version %s\n\n", PACKAGE_NAME,
50  			       PACKAGE_VERSION);
51  			exit(EXIT_SUCCESS);
52  			break;
53  
54  		case 'h':
55  			help();
56  			exit(EXIT_SUCCESS);
57  			break;
58  
59  		default:
60  			break;
61  		}
62  	}
63  
64  	printf("\n");
65  
66  	while (loops > 0) {
67  		for (b = 99; b >= 0; b--) {
68  			switch (b) {
69  			case 0:
70  				printf("No more bottles of beer on the wall, ");
71  				printf("no more bottles of beer.\n");
72  				printf("Go to the store and buy some more, ");
73  				printf("99 bottles of beer on the wall.\n\n");
74  				break;
75  
76  			case 1:
77  				printf("1 bottle of beer on the wall, ");
78  				printf("1 bottle of beer.\n");
79  				printf("If this bottle should happen to fall, no more");
80  				printf("bottles of beer on the wall\n\n");
81  				break;
82  
83  			default:
84  				printf("%d bottles of beer on the wall, ", b);
85  				printf("%d bottles of beer.\n", b);
86  				printf("If one of those bottles should happen to fall, ");
87  				printf("%d %s of beer on the wall.\n\n", b - 1,
88  				       ((b - 1) > 1) ? "bottles" : "bottle");
89  				break;
90  			}
91  
92  			if (sleeptime > 0)
93  				sleep(sleeptime);
94  		}
95  		loops--;
96  	}
97  	return EXIT_SUCCESS;
98  }