summaryrefslogtreecommitdiff
path: root/monadic.c
blob: 3c7fac25b08c7822b5a481819345918f5f75ac36 (plain)
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
#include "internal.h"
#include <stdio.h>
#include <string.h>
#include <grp.h>

extern int
monadic_main(
 struct FileInfo *	i
,int				argc
,char * *			argv)
{
	int	status = 0;

	while ( argc > 1 && argv[1][0] == '-' ) {
		switch ( argv[1][1] ) {
		case 'c':
			i->create = 1;
			break;
		case 'f':
			i->force = 1;
			break;
		case 'g':
			if ( argc > 2 ) {
				struct group *	g;
				if ( (g = getgrnam(argv[2])) == 0 ) {
					fprintf(stderr, "%s: no such group.\n", argv[1]);
					return 1;
				}
				i->groupID = g->gr_gid;
				i->changeGroupID = 1;
				i->complainInPostProcess = 1;
				argc--;
				argv++;
				break;
			}
			usage(i->applet->usage);
			return 1;
		case 'm':
			if ( argc > 2 ) {
				status = parse_mode(
				 argv[2]
				,&i->orWithMode
				,&i->andWithMode, 0);

				if ( status == 0 ) {
					i->changeMode = 1;
					i->complainInPostProcess = 1;
					argc--;
					argv++;
					break;
				}
			}
			usage(i->applet->usage);
			return 1;
		case 'o':
			if ( argc > 2 ) {
				status = parse_user_name(argv[2], i);
				if ( status != 0 )
					return status;

				i->changeUserID = 1;
				i->complainInPostProcess = 1;
				argc--;
				argv++;
				break;
			}
			usage(i->applet->usage);
			return 1;
		case 'p':
			i->makeParentDirectories = 1;
			break;
		case 'r':
		case 'R':
			i->recursive = 1;
			break;
		case 's':
			i->makeSymbolicLink = 1;
			break;
		default:
			usage(i->applet->usage);
			return 1;
		}
		argv++;
		argc--;
	}
	while ( argc > 1 ) {
		char *	slash;
		i->source = argv[1];
		if ( (slash = strrchr(i->source, '/')) != 0 ) {
			i->directoryLength = slash - i->source;
			if ( i->source[i->directoryLength] == '\0' )
				i->directoryLength = 0;
		}
		else
			i->directoryLength = 0;
		if ( !i->dyadic )
			i->destination = i->source;

		if ( lstat(i->source, &i->stat) == 0 ) {
			i->isSymbolicLink = (i->stat.st_mode & S_IFMT)==S_IFLNK;
			if ( i->isSymbolicLink )
				if ( stat(i->source, &i->stat) != 0 )
					memset(&i->stat, 0, sizeof(i->stat));
		}
		else
			memset(&i->stat, 0, sizeof(i->stat));

		if ( i->isSymbolicLink
		 || !i->recursive
		 || ((i->stat.st_mode & S_IFMT) != S_IFDIR) ) {

			if ( i->applet->function )
				status = i->applet->function(i);
			if ( status == 0 )
				status = post_process(i);
		}
		else
			status = descend(i, i->applet->function);

		if ( status != 0 && !i->force )
			return status;
		argv++;
		argc--;
	}
	return 0;
}