Functions - 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1: /* File functions.c 2: // Group: 13 3: // Name: Jose Zarate - Matrikelnummer: 2729486 4: // Name: Phu Hung Ngo - Matrikelnummer: 2765479 5: // Name: Fangli Pi - Matrikelnummer: 2710479 6: // Name: Yanfei Quan - Matrikelnummer: 2710505 7: */ 8: 9: #include <stdio.h> 10: #include <stdlib.h> 11: #include <unistd.h> 12: #include <signal.h> 13: #include <string.h> 14: #include <sys/wait.h> 15: #include <sys/types.h> 16: #include <ctype.h> 17: #include <errno.h> 18: #include <sys/stat.h> 19: #include <fcntl.h> 20: 21: typedef char * string ; 22: #define SIZE_OF_CMD 2048 // length of a command 23: #define USER_DEFAULT "Group13"; // default username 24: string HOST_DEFAULT = "OS" ; // default hostname 25: 26: /* ErrorMessage 27: // List of error messages 28: */ 29: void ErrorMessage ( int function_number ) { 30: printf ( "Error encountered:\n" ) ; 31: switch ( function_number ) { 32: case 1 : // 1a. username 33: fprintf ( stderr , "Could not get username. Set default value for it.\n" ) ; 34: break ; 35: case 2 : // 1a. hostname 36: fprintf ( stderr , "Could not get hostname. Set default value for it.\n" ) ; 37: break ; 38: case 3 : // 1a. cwd 39: fprintf ( stderr , "Could not get current working directory.\n" ) ; 40: break ; 41: case 4 : // 2a. cd 42: fprintf ( stderr , "Could not change to new directory.\n" ) ; 43: break ; 44: case 5 : // 2b. setenv <envar> <value>, setenv <envar> 45: fprintf ( stderr , "Could not set this environment variable.\n" ) ; 46: break ; 47: case 6 : // 2b. unsetenv <envar> 48: fprintf ( stderr , "Could not undefine this environment variable.\n" ) ; 49: break ; 50: case 7 : // 2c. execve 51: fprintf ( stderr , "Command not found.\n" ) ; 52: break ; 53: case 8 : // 2c. 2d. 2e. fork 54: fprintf ( stderr , "The fork failed.\n" ) ;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
55: break ; 56: case 9 : // 2f. pipe 57: fprintf ( stderr , "The piping: syntax error.\n" ) ; 58: break ; 59: case 10 : // 2f. pipe 60: fprintf ( stderr , "The piping error.\n" ) ; 61: break ; 62: case 11 : // 2f. malloc 63: fprintf ( stderr , "Could not malloc enough space.\n" ) ; 64: break ; 65: case 12 : // 2f. file 66: fprintf ( stderr , "Could not find the file.\n" ) ; 67: break ; 68: case 13 : // 2f. redirect output 69: fprintf ( stderr , "Redirect output error.\n" ) ; 70: break ; 71: case 14 : // 2f. redirect input 72: fprintf ( stderr , "Redirect input error.\n" ) ; 73: break ; 74: default : 75: fprintf ( stderr , "Error undefined.\n" ) ; 76: break ; 77: } 78: } 79: /* end ErrorMessage 80: */ 81: 82: /* CreatePrompt 83: // for question 1a. 84: */ 85: string CreatePrompt ( string cmd_user , char cmd_host [ ] , char cmd_cwd [ ] ) { 86: string cmd_prompt = malloc ( 1024 ) ; 87: strcat ( cmd_prompt , cmd_user ) ; 88: strcat ( cmd_prompt , "@" ) ; 89: strcat ( cmd_prompt , cmd_host ) ; 90: strcat ( cmd_prompt , " " ) ; 91: strcat ( cmd_prompt , cmd_cwd ) ; 92: strcat ( cmd_prompt , " > " ) ; 93: return cmd_prompt ; 94: } 95: /* end CreatePrompt 96: */ 97: 98: 99: /* ListEnviron 100: // for question 2b. 101: */ 102: void ListEnviron ( char * * envp ) { 103: int i = 0 ; 104: char * parameter ; 105: printf ( "The environment strings currently defined:\n" ) ; 106: while ( ( parameter = * envp ++ ) ) { 107: i ++ ; 108: printf ( "%d. %s\n" , i , parameter ) ; 109: } 110: printf ( "End listing.\n" ) ; } 111: /* end ListEnviron
Background image of page 2
112: */ 113: 114: /* SetEnvironVal 115: // for question 2b. 116: */ 117: void SetEnvironVal ( string v_name , string v_val ) { 118: int ok = - 1 ; 119: string check_var = malloc ( 128 ) ; 120: string ans = malloc ( 512 ) ; 121: 122: check_var = getenv ( v_name ) ; 123: if ( check_var == NULL ) { 124: ok = setenv ( v_name , v_val , 1 ) ; 125: if ( ok != 0 ) { 126: ErrorMessage ( 5 ) ; 127: } 128: } 129: else { 130: printf ( "This variable is already defined.\n" ) ; 131: printf ( "Are you sure to change its value (y/n)? " ) ; 132: fflush ( stdout ) ; 133: gets ( ans ) ; 134: if ( ( strlen ( ans ) == 1 ) && ( strcmp ( ans , "y" ) == 0 ) ) { 135: ok = setenv ( v_name , v_val , 1 ) ; 136: if ( ok != 0 ) { 137: ErrorMessage ( 5 ) ; 138: } 139: else { 140: printf ( "Update successful!\n" ) ; 141: } 142: } 143: } 144: } 145: /* end SetEnvironVal 146: */ 147: 148: /* UnsetEnviron 149: // for question 2b.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

Functions - 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online