File: xcal_cal.c

package info (click to toggle)
xcal 4.1-19
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, lenny, squeeze, stretch, wheezy
  • size: 780 kB
  • ctags: 3,851
  • sloc: ansic: 5,576; makefile: 4,265; sh: 573
file content (227 lines) | stat: -rw-r--r-- 5,229 bytes parent folder | download | duplicates (6)
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
#ifndef lint
static char *sccsid = "@(#)xcal_cal.c	1.1+ (Hillside Systems) 7/14/90";
#endif  /* lint */
/***

* program name:
	xcal_cal.c
* function:
	read files generated by xcal and produce a file compatible
	with the standard "calendar" utility
* switches:
	-d dir	use "dir" instead of "Calendar"
	-f file use "file" instead of .xcal
	-m	copy all of multi-line entries
* history:
	Written July, 1990
	Ed Gould
	mt Xinu, Inc.
* (C) Copyright: 1990 mt Xinu, Inc.
	
	Permission to use, copy, modify, and distribute this software
	and its documentation for any purpose is hereby granted to
	anyone, provided that the above copyright notice appear in
	all copies and that both that copyright notice and this
	permission notice appear in supporting documentation, and
	that the names of Ed Gould and mt Xinu, Inc. not be used
	in advertising or publicity pertaining to distribution of
	the software without specific, written prior permission.
	mt Xinu, Inc. makes no representations about the suitability
	of this software for any purpose.  It is provided "as is"
	without express or implied warranty.

	Ed Gould and mt Xinu, Inc. DISCLAIM ALL WARRANTIES WITH
	REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
	OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL Ed Gould
	or mt Xinu, Inc. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
	CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
	FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
	CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
	OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
	SOFTWARE.

***/
#include <stdio.h>
#include <ctype.h>
#include <sys/param.h>
#include <unistd.h>
#if defined(NeXT)
# include <sys/fcntl.h>
# include <sys/dir.h>
# include <sys/dirent.h>
#else
# include <dirent.h>
#endif
#include <string.h>
#include <time.h>


char	*directory	= "Calendar";
char	*file		= ".xcal";
char	*home;

#ifndef MAXPATHLEN
#  define MAXPATHLEN 256
#endif

char	usedir[MAXPATHLEN];
char	usefile[MAXPATHLEN];
char	line[BUFSIZ];

char	*months[]	= {"jan", "feb", "mar", "apr", "may", "jun",
			   "jul", "aug", "sep", "oct", "nov", "dec"};

int	debug;
int	mflag;

FILE	*out;

void
main(argc, argv)
	char **argv;
{
	register int c;
	time_t now;
	register struct tm *lt;
	char *getenv();

	while((c = getopt(argc, argv, "d:f:m#")) != EOF) {
		extern char *optarg;

		switch (c) {

		case 'd':
			directory = optarg;
			break;

		case 'f':
			file = optarg;
			break;

		case 'm':
			mflag++;
			break;

		case '#':
			debug++;
			break;

		case '?':
		default:
			fprintf(stderr, "usage: %s [-d dir] [-f file]\n",
								argv[0]);
			exit(1);
		}
	}
	home = getenv("HOME");
	/*
	 * For both directory and output file, 
	 *
	 *   /...	is absolute path
	 *   ./...	is relative to current directory
	 *
	 * otherwise, relative to $HOME, if available
	 */
	if(file[0] == '/' || (file[0] == '.' && file[1] == '/') ||
	   home == NULL)
		strcpy(usefile, file);
	else
		sprintf(usefile, "%s/%s", home, file);
	if(debug)
		fprintf(stderr, "output to %s\n", usefile);
	if((out = fopen(usefile, "w")) == NULL) {
		perror(usefile);
		exit(1);
	}
	now = time((time_t *)NULL);
	lt = localtime(&now);
	calfiles(directory, lt->tm_year + 1900, lt->tm_mon, lt->tm_mday);
	if(lt->tm_mday >= 25 ) {
		/*
		 * close to end of month: include next month, too
		 */
		if(lt->tm_mon == 11)
			calfiles(directory, lt->tm_year + 1900 + 1, 0, 1);
		else
			calfiles(directory, lt->tm_year + 1900,
							lt->tm_mon + 1, 1);
	}
}

calfiles(dir, year, thismonth, today)
	register char *dir;
{
	register DIR *dp;
	register char *to;
	register char *from;
	register struct dirent *d;
	char day[3];
	char month[4];

	if(dir[0] == '/' || (dir[0] == '.' && dir[1] == '/') ||
	   home == NULL)
		sprintf(usedir, "%s/xy%4d", dir, year);
	else
		sprintf(usedir, "%s/%s/xy%4d", home, dir, year);
	if(debug)
		fprintf(stderr, "looking in directory %s\n", usedir);
	if((dp = opendir(usedir)) == NULL) {
		perror(usedir);
		exit(1);
	}
	while((d = readdir(dp)) != NULL) {
		register FILE *in;

		if(d->d_name[0] != 'x' || d->d_name[1] != 'c' ||
		   !isascii(d->d_name[2]) || !isdigit(d->d_name[2]))
			continue;
		sprintf(usefile, "%s/%s", usedir, d->d_name);
		if(debug)
			fprintf(stderr, "looking in file %s\n", usefile);
		if((in = fopen(usefile, "r")) == NULL) {
			if(debug)
				perror(usefile);
			continue;
		}
		from = &d->d_name[2];
		to = day;
		while(isascii(*from) && isdigit(*from))
			*to++ = *from++;
		*to = '\0';
		to = month;
		while(isascii(*from) && !isdigit(*from)) {
			if(isupper(*from))
				*from = tolower(*from);
			*to++ = *from++;
		}
		*to = '\0';
		if(strcmp(month, months[thismonth]) != 0 ||
		   atoi(day) < today) {
			if(debug)
				fprintf(stderr, "\tskipped - date\n");
			fclose(in);
			continue;
		}
		while(fgets(line, sizeof(line), in) != NULL) {
			char *cp;

			if((to = strchr(line, '\n')) != NULL)
				*to = '\0';
			cp = line;
			while(isspace(*cp))
				cp++;
			if(*cp == '\0') {
				if(debug)
					fprintf(stderr, "\tskipped - empty\n");
				continue;
			}
			if(debug)
				fprintf(stderr, "==>\t%s %s\t%s\n", month,
								day, cp);
			fprintf(out, "%s %s\t%s\n", month, day, cp);
			if(mflag == 0)
				break;
		}
		fclose(in);
	}
}