File: MCrontab.m

package info (click to toggle)
mysql-gui-tools 5.0r14%2BopenSUSE-2.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 116,956 kB
  • ctags: 48,715
  • sloc: sql: 341,918; pascal: 276,698; ansic: 91,020; cpp: 90,451; objc: 33,236; sh: 29,481; yacc: 10,756; xml: 10,589; java: 10,079; php: 2,806; python: 2,092; makefile: 1,783; perl: 4
file content (400 lines) | stat: -rw-r--r-- 7,164 bytes parent folder | download | duplicates (4)
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
//
//  MCrontab.m
//  MySQLGUICommon
//
//  Created by Vlad on 06.05.05.
//  Copyright 2005 __MyCompanyName__. All rights reserved.
//

#import "MCrontab.h"

#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

int readfd(int fd, char **pbuf)
{
	int allocsize = 1024;
	int readcount;

	*pbuf = malloc(allocsize);
	int usedspace = 0;
	int freespace = allocsize;

	while((readcount = read(fd, *pbuf + usedspace, freespace)) > 0)
	{
		freespace -= readcount;
		usedspace += readcount;
		if(freespace == 0)
		{
			char *tmp = malloc(allocsize * 2);
			memcpy(tmp, *pbuf, allocsize);
			*pbuf = tmp;
			freespace = allocsize;
			allocsize *= 2;
		}
	}
	if(freespace == 0)
	{
			char *tmp = malloc(allocsize+1);
			memcpy(tmp, *pbuf, allocsize);
			*pbuf = tmp;
			freespace = 1;
			allocsize += 1;
	}
	(*pbuf)[usedspace] = '\0';
	usedspace++;
	freespace--;

	return usedspace;
}

int launch_sync(char *argv[], char *envp[], char **stdout_text, char **stderr_text)
{
	int status = 0;

	int stdout_pipe[2];
	int stderr_pipe[2];

	pipe(stdout_pipe); 
	pipe(stderr_pipe); 

	pid_t pid = fork();
	if(pid == 0)
	{
		close(STDOUT_FILENO); dup(stdout_pipe[1]); close(stdout_pipe[1]);
		close(STDERR_FILENO); dup(stderr_pipe[1]); close(stderr_pipe[1]);
		execve(argv[0], argv, envp);
	}
	else
	{
		close(stdout_pipe[1]);
		close(stderr_pipe[1]);
		waitpid(pid, &status, 0);
		if(stdout_text != NULL)
		{
			readfd(stdout_pipe[0], stdout_text);
		}
		if(stderr_text != NULL)
		{
			readfd(stderr_pipe[0], stderr_text);
		}
	}
	return status;
}

@implementation MCrontabEntry : NSObject

- (id)init
{
	if(self = [super init]) 
	{
		_minute = -1;
		_hour = -1;
		_month = -1;
		_weekday = -1;
		_day = @"";
		_command = @"";
		
		return self;
	}
	return nil;
}

@end

@implementation MCrontab : NSObject

- (id) init
{
	if(self = [super init])
	{
		_lines = [NSMutableArray arrayWithCapacity: 16];
		return self;
	}
	return nil;
}

- (NSString*) formatEntry: (MCrontabEntry *) entry
{
  NSString *line;
  
  if (entry->_minute < 0)
    line = @"*";
  else
    line = [[NSString alloc] initWithFormat: @"%d", entry->_minute];
  
  if (entry->_hour < 0)
    line = [line stringByAppendingString: @" *"];
  else 
  {
    line = [line stringByAppendingFormat: @" %d", entry->_hour]; 
  }
  
  if ([entry->_day length] == 0)
    line = [line stringByAppendingString: @" *"];
  else
  {
    line = [line stringByAppendingFormat: @" %@", entry->_day]; 
  }
  
  if (entry->_month < 0)
    line = [line stringByAppendingString: @" *"];
  else 
  {
    line = [line stringByAppendingFormat: @" %d", entry->_month]; 
  }
  
  if (entry->_weekday == -1)
    line = [line stringByAppendingString: @" *"];
  else
  {
    NSString *wdays = [[NSString alloc] init];
	int i;
    for (i = 0; i < 7; i++)
    {
      if (entry->_weekday & (1<<i))
      {
        if ([wdays length] == 0)
          wdays = [NSString stringWithFormat: @"%d", i];
        else
		{
		  wdays = [wdays stringByAppendingFormat: @",%d", i];
		}
      }
    }
	line = [line stringByAppendingFormat: @" %@", wdays];
  }

  line = [line stringByAppendingFormat: @" %@", entry->_command];
  return line;
}

- (MCrontabEntry *) findEntryByComment: (NSString *) comment
{
  BOOL next_is_the_one = NO;

  int i;
  for(i = 0; i < [_lines count]; i++)
  {
	NSString *next = [_lines objectAtIndex: i];
	if([next length] == 0)
	{
		continue;
	}
	if([next characterAtIndex: 0] == '#')
	{
		NSRange range = [next rangeOfString: comment];
		if(range.length != 0) 
		{
			next_is_the_one = YES;
		}
	}
	else if(next_is_the_one)
	{
		MCrontabEntry *entry = [[MCrontabEntry alloc] init];
		if([self parseLine: next toEntry: entry])
		{
			return entry;
		}
		else
		{
			return nil;
		}
	}
  }
  return nil; 
}

- (BOOL) removeCommand: (NSString *) command withComment: (NSString *) comment
{
  BOOL next_is_the_one = NO;
  int i;
  
  for(i = 0; i < [_lines count]; i++)
  {
	NSString *next = [_lines objectAtIndex: i];
	if([next length] == 0)
	{
		continue;
	}
	if([next characterAtIndex: 0] == '#')
	{
		NSRange range = [next rangeOfString: comment];
		if(range.length != 0) 
		{
			next_is_the_one = YES;
		}
	}
	else if(next_is_the_one)
	{
		MCrontabEntry *entry = [[MCrontabEntry alloc] init];
		if([self parseLine: next toEntry: entry])
		{
			if(([command length] == 0) || ([command hasPrefix: command]))
			{
				[_lines removeObjectAtIndex: i-1];
				[_lines removeObjectAtIndex: i-1];
				return YES;
			}
		}
	}
  }
  return NO;
}

- (void) addEntry: (MCrontabEntry *) entry withComment: (NSString *) comment
{
	[_lines addObject: [NSString stringWithFormat: @"# %@", comment]];
	[_lines addObject: [self formatEntry: entry]];
}

- (BOOL) load
{
	char *cmdline[3] = {"/usr/bin/crontab", "-l", NULL};
	char *stdout_text = NULL;
	char *stderr_text = NULL;

	launch_sync(cmdline, NULL, &stdout_text, &stderr_text);
	char *tok = strtok(stdout_text, "\n");
	while (tok)
    {
		[_lines addObject: [NSString stringWithCString: tok]];
		tok = strtok(NULL, "\n");
	}

	if (stdout_text)
		free(stdout_text);
	if (stderr_text)
		free(stderr_text);

	return YES;
}

- (BOOL) installTable
{
	char fname[32];
	int fd;
	BOOL ok = YES;

	strcpy(fname, "/tmp/cron.XXXXXX");
  
	// carefull here, we dont want to allow someone else to write in our
	// tmp file and schedule stuff on our behalf
	fd = mkstemp(fname);

	if (fd < 0)
	{
		NSLog(@"could not create tmp file %s", fname);
		return NO;
	}

	int i;
	for(i = 0; i < [_lines count]; i++)
	{
		NSString *next = [_lines objectAtIndex: i];
		if((write(fd, [next cString], [next cStringLength]) < 0) || (write(fd, "\n", 1) < 0))
		{
			NSLog(@"error writing to tmp file %s: %s", fname, strerror(errno));
			ok = NO;
			break;
		}
	}

	close(fd);
  
	if (ok)
	{
		char *cmdline[3] = {"/usr/bin/crontab", fname, NULL};

		char *so;
		char *se;
		// now install the cron file
		launch_sync(cmdline, NULL, &so, &se);
	}
  
	// delete tmp file
	unlink(fname);

	return ok;
}

- (BOOL) parseLine: (NSString *)line toEntry: (MCrontabEntry *) entry
{
	char *tmp = strdup([line cString]);
	char *tok;
  
	tok = strtok(tmp, " ");
	if (!tok)
		goto error;
	if (*tok == '*')
		entry->_minute = -1;
	else
		entry->_minute = atoi(tok);

	tok = strtok(NULL, " ");
	
	if (!tok)
		goto error;
	if (*tok == '*')
		entry->_hour = -1;
	else
		entry->_hour = atoi(tok);

	tok = strtok(NULL, " ");
	if (!tok)
		goto error;
	if (*tok == '*')
		entry->_day = @"";
	else
		entry->_day = [NSString stringWithCString: tok];

	tok = strtok(NULL, " ");
	if (!tok)
		goto error;
	if (*tok == '*')
		entry->_month = -1;
	else
		entry->_month = atoi(tok);

	tok = strtok(NULL, " ");
	if (!tok)
		goto error;
	if (*tok == '*')
		entry->_weekday= -1;
	else
	{
		char *tt = strdup(tok);
		entry->_weekday = 0;
		char *n = tt;
		char *t = strsep(&n, ",");
		if(t)
		{
			entry->_weekday |= (1 << atoi(t));
		}
		while(t = strsep(&n, ","))
		{
			entry->_weekday |= (1 << atoi(t));
		}
		free(tt);
	}

	tok = strtok(NULL, "\n");
	if (!tok)
		goto error;
	entry->_command = [NSString stringWithCString: tok];

	free(tmp);
	return YES;

error:
	free(tmp);
	return YES;
}

- (NSArray *) lines
{
	return _lines;
}

@end