forked from ryphil/OS-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project2.cpp~
executable file
·126 lines (112 loc) · 3.76 KB
/
project2.cpp~
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
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <sys/wait.h> /* Wait for Process Termination */
#include <stdlib.h> /* General Utilities */
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
if ( argc != 3 ) {
cout << "Incorrect input. (argc = " << argc << ") wm will now exit." << endl;
exit(1);
}
/*Create child pid_t so we can wait fork off children and wait on them to finish*/
pid_t childpid2;
pid_t childpid3;
pid_t childpid4;
int status; /* parent process: child's exit status */
//use this to set the type of image, (1=jpg, 2=png, 3=tif) for saving
//the image as well as when viewing the final image on the web.
int imageType;
string imageURL = argv[1];
switch (imageURL[imageURL.size() - 2]) {
case'i': {
imageType = 3;
break;
}
case'p': {
imageType = 1;
break;
}
case'n': {
imageType = 2;
break;
}
}
childpid2 = fork(); /* Create new child to apply watermark*/
if ( childpid2 >= 0 )
{
if ( childpid2 == 0 )
{
//call the exec in child2 to apply the watermark on the image.
cout << "CHILD2: Begin Apply Watermark." << endl;
if ( imageType == 1) {
int err = execl ("/usr/bin/composite", "composite", "-compose","bumpmap", "-tile",argv[2],argv[1], "./jpg_image.jpg", (char*)0);
cout << err << endl;
} else if (imageType == 2 ) {
int err = execl ("/usr/bin/composite", "composite", "-compose","bumpmap", "-tile",argv[2],argv[1], "./png_image.png", (char*)0);
cout << err << endl;
} else {
int err = execl ("/usr/bin/composite", "composite", "-compose","bumpmap", "-tile",argv[2],argv[1], "./tif_image.tif", (char*)0);
cout << err << endl;
}
cout << "CHILD2: Watermark Failed." << endl;
}
else {
//wait for child2 to finish applying the watermark.
wait (&status);
//Create the 3rd child to upload the image.
childpid3 = fork();
if ( childpid3 >= 0 )
{
if ( childpid3 == 0 )
{
//In the child Process, we must exec a new process to
//upload the image to the server.
cout << "CHILD3: Begin upload." << endl;
if ( imageType == 1) {
execl ("/usr/bin/sftp", "sftp","-o", "batchmode no", "-b", "jpg.bat", "phil7017@gpel1.cs.ou.edu", (char*)0);
} else if (imageType == 2 ) {
execl ("/usr/bin/sftp", "sftp","-o", "batchmode no", "-b", "png.bat", "phil7017@gpel1.cs.ou.edu", (char*)0);
} else {
execl ("/usr/bin/sftp", "sftp","-o", "batchmode no", "-b", "tif.bat", "phil7017@gpel1.cs.ou.edu", (char*)0);
}
//execl ("/usr/bin/sftp", "sftp","-o", "batchmode no", "-b", "jpg.bat", "phil7017@gpel1.cs.ou.edu", (char*)0);
cout << "CHILD3: Exec failed." << endl;
}
else {
//wait for the child to finish uploading
wait (&status);
childpid4 = fork();
if ( childpid4 >= 0 )
{
if ( childpid4 == 0 ) {
//Launch browser here
cout << "CHILD4: child 4 launches web browser." << endl;
char* browser = getenv("BROWSER");
cout << browser << endl;
if(browser == NULL){
cout<< "BROWSER not set, will launch Firefox." << endl;
exit(3);
} else{
if ( imageType == 1 ){
execl("/usr/bin/firefox",browser,"accounts.cs.ou.edu/~phil7017/jpg_image.jpg",(char*)0);
} else if ( imageType == 2 ) {
execl("/usr/bin/firefox",browser,"accounts.cs.ou.edu/~phil7017/png_image.png",(char*)0);
} else {
execl("/usr/bin/firefox",browser,"accounts.cs.ou.edu/~phil7017/tif_image.tif",(char*)0);
}
}
}
else {
wait (&status);
}
}
}
}
}
}
exit(0); /* parent exits with code 0 since everything completed successfully */
return 0;
}