Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 267 //Error in Fork
- 268 case -1: { perror("Hull Breach!\n"); exit(1); break; }
- 269
- 270 //Child Process
- 271 case 0:
- 272 {
- 273 if (secondArray[0] != NULL)
- 274 {
- 275 if(strcmp(secondArray[0], ">") == 0)
- 276 {
- 277 int fileDesc = open(secondArray[1], O_WRONLY | O_APPEND | O_CREAT, 0644);
- 278 if (fileDesc == -1)
- 279 {
- 280 perror(NULL);
- 281 exit(1);
- 282 }
- 283 dup2(fileDesc, 1);
- 284 }
- 285 if(strcmp(secondArray[0], "<") == 0)
- 286 {
- 287 int fileDesc2 = open(secondArray[1], O_RDONLY);
- 288 if (fileDesc2 == -1)
- 289 {
- 290 perror(NULL);
- 291 exit(1);
- 292 }
- 293 dup2(fileDesc2, 0);
- 294
- 295 }
- 296 if (strcmp(secondArray[secondCounter - 2], "&") == 0)
- 297 {
- 298 printf("This is a background process\n");
- 299 background = 1;
- 300 }
- 301 }
- 302 execvp(userInputArray[0], userInputArray);
- 303 perror("CHILD: exec failure!\n");
- 304 exit(2);
- 305 break;
- 306 }
- 307 //Parent Process
- 308 default:
- 309 {
- 310 //If process is run in the foreground, no reason to add the the array of processes
- 311 if(background == 1)
- 312 {
- 313 PIDArray[counterX] = spawnPid;
- 314 pid_t myPID = waitpid(spawnPid, &childExitStatus, WNOHANG);
- 315 }
- 316 fflush(stdout);
- 317 pid_t actualPid = waitpid(spawnPid, &childExitStatus, 0);
- 318 fflush(stdout);
- 319 break;
- 320 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement