24.01.2014 Views

Codice

Codice

Codice

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

386 volume VI os16<br />

Sorgenti della libreria generale 387<br />

«<br />

3890007 |//----------------------------------------------------------------------<br />

3890008 |int<br />

3890009 |execve (const char *path, char *const argv[], char *const envp[])<br />

3890010 |{<br />

3890011 | sysmsg_exec_t msg;<br />

3890012 | size_t size;<br />

3890013 | size_t arg_size;<br />

3890014 | int argc;<br />

3890015 | size_t env_size;<br />

3890016 | int envc;<br />

3890017 | char *arg_data = msg.arg_data;<br />

3890018 | char *env_data = msg.env_data;<br />

3890019 | //<br />

3890020 | msg.ret = 0;<br />

3890021 | msg.errno = 0;<br />

3890022 | //<br />

3890023 | strncpy (msg.path, path, PATH_MAX);<br />

3890024 | //<br />

3890025 | // Copy ‘argv[]’ inside a the message buffer ‘msg.arg_data’,<br />

3890026 | // separating each string with a null character and counting the<br />

3890027 | // number of strings inside ‘argc’.<br />

3890028 | //<br />

3890029 | for (argc = 0, arg_size = 0, size = 0;<br />

3890030 | argv != NULL &&<br />

3890031 | argc < (ARG_MAX/16) &&<br />

3890032 | arg_size < ARG_MAX/2 &&<br />

3890033 | argv[argc] != NULL;<br />

3890034 | argc++, arg_size += size)<br />

3890035 | {<br />

3890036 | size = strlen (argv[argc]);<br />

3890037 | size++; // Count also the final null character.<br />

3890038 | if (size > (ARG_MAX/2 - arg_size))<br />

3890039 | {<br />

3890040 | errset (E2BIG); // Argument list too long.<br />

3890041 | return (-1);<br />

3890042 | }<br />

3890043 | strncpy (arg_data, argv[argc], size);<br />

3890044 | arg_data += size;<br />

3890045 | }<br />

3890046 | msg.argc = argc;<br />

3890047 | //<br />

3890048 | // Copy ‘envp[]’ inside a the message buffer ‘msg.env_data’,<br />

3890049 | // separating each string with a null character and counting the<br />

3890050 | // number of strings inside ‘envc’.<br />

3890051 | //<br />

3890052 | for (envc = 0, env_size = 0, size = 0;<br />

3890053 | envp != NULL &&<br />

3890054 | envc < (ARG_MAX/16) &&<br />

3890055 | env_size < ARG_MAX/2 &&<br />

3890056 | envp[envc] != NULL;<br />

3890057 | envc++, env_size += size)<br />

3890058 | {<br />

3890059 | size = strlen (envp[envc]);<br />

3890060 | size++; // Count also the final null character.<br />

3890061 | if (size > (ARG_MAX/2 - env_size))<br />

3890062 | {<br />

3890063 | errset (E2BIG); // Argument list too long.<br />

3890064 | return (-1);<br />

3890065 | }<br />

3890066 | strncpy (env_data, envp[envc], size);<br />

3890067 | env_data += size;<br />

3890068 | }<br />

3890069 | msg.envc = envc;<br />

3890070 | //<br />

3890071 | // System call.<br />

3890072 | //<br />

3890073 | sys (SYS_EXEC, &msg, (sizeof msg));<br />

3890074 | //<br />

3890075 | // Should not return, but if it does, then there is an error.<br />

3890076 | //<br />

3890077 | errno = msg.errno;<br />

3890078 | errln = msg.errln;<br />

3890079 | strncpy (errfn, msg.errfn, PATH_MAX);<br />

3890080 | return (msg.ret);<br />

3890081 |}<br />

105.17.14 lib/unistd/execvp.c<br />

Si veda la sezione 98.20.<br />

3900001 |#include <br />

3900002 |#include <br />

3900003 |#include <br />

3900004 |#include <br />

3900005 |#include <br />

3900006 |//----------------------------------------------------------------------<br />

3900007 |int<br />

3900008 |execvp (const char *path, char *const argv[])<br />

3900009 |{<br />

3900010 | char command[PATH_MAX];<br />

3900011 | int status;<br />

3900012 | //<br />

3900013 | // Get a full command path if necessary.<br />

3900014 | //<br />

3900015 | status = namep (path, command, (size_t) PATH_MAX);<br />

3900016 | if (status != 0)<br />

3900017 | {<br />

3900018 | //<br />

3900019 | // Variable ‘errno’ is already set by ‘namep()’.<br />

3900020 | //<br />

3900021 | return (-1);<br />

3900022 | }<br />

3900023 | //<br />

3900024 | // Return calling ‘execve()’<br />

3900025 | //<br />

3900026 | return (execve (command, argv, environ)); // [1]<br />

3900027 |}<br />

3900028 |//<br />

3900029 |// The variable ‘environ’ is declared as ‘char **environ’ and is<br />

3900030 |// included from .<br />

3900031 |//<br />

105.17.15 lib/unistd/fchdir.c<br />

Si veda la sezione 101.2.<br />

3910001 |#include <br />

3910002 |#include <br />

3910003 |//----------------------------------------------------------------------<br />

3910004 |int<br />

3910005 |fchdir (int fdn)<br />

3910006 |{<br />

3910007 | //<br />

3910008 | // os16 requires to keep track of the path for the current working<br />

3910009 | // directory. The standard function ‘fchdir()’ is not applicable.<br />

3910010 | //<br />

3910011 | errset (E_NOT_IMPLEMENTED);<br />

3910012 | return (-1);<br />

3910013 |}<br />

105.17.16 lib/unistd/fchown.c<br />

Si veda la sezione 97.5.<br />

3920001 |#include <br />

3920002 |#include <br />

3920003 |#include <br />

3920004 |#include <br />

3920005 |#include <br />

3920006 |#include <br />

3920007 |//----------------------------------------------------------------------<br />

3920008 |int<br />

3920009 |fchown (int fdn, uid_t uid, gid_t gid)<br />

3920010 |{<br />

3920011 | sysmsg_fchown_t msg;<br />

3920012 | //<br />

3920013 | msg.fdn = fdn;<br />

3920014 | msg.uid = uid;<br />

3920015 | msg.gid = gid;<br />

3920016 | //<br />

3920017 | sys (SYS_FCHOWN, &msg, (sizeof msg));<br />

3920018 | //<br />

3920019 | errno = msg.errno;<br />

3920020 | errln = msg.errln;<br />

3920021 | strncpy (errfn, msg.errfn, PATH_MAX);<br />

3920022 | return (msg.ret);<br />

3920023 |}<br />

105.17.17 lib/unistd/fork.c<br />

Si veda la sezione 97.14.<br />

3930001 |#include <br />

3930002 |#include <br />

3930003 |#include <br />

3930004 |#include <br />

3930005 |#include <br />

3930006 |//----------------------------------------------------------------------<br />

3930007 |pid_t<br />

3930008 |fork (void)<br />

3930009 |{<br />

3930010 | sysmsg_fork_t msg;<br />

3930011 | //<br />

3930012 | // Set the return value for the child process.<br />

3930013 | //<br />

3930014 | msg.ret = 0;<br />

3930015 | //<br />

3930016 | // Do the system call.<br />

3930017 | //<br />

3930018 | sys (SYS_FORK, &msg, (sizeof msg));<br />

3930019 | //<br />

3930020 | // If the system call has successfully generated a copy of<br />

3930021 | // the original process, the following code is executed from<br />

3930022 | // the parent and the child. But the child has the ‘msg’<br />

3930023 | // structure untouched, while the parent has, at least, the<br />

3930024 | // pid number inside ‘msg.ret’.<br />

3930025 | // If the system call fails, there is no child, and the<br />

3930026 | // parent finds the return value equal to -1, with an<br />

3930027 | // error number.<br />

3930028 | //<br />

3930029 | errno = msg.errno;<br />

3930030 | errln = msg.errln;<br />

3930031 | strncpy (errfn, msg.errfn, PATH_MAX);<br />

3930032 | return (msg.ret);<br />

3930033 |}<br />

105.17.18 lib/unistd/getcwd.c<br />

Si veda la sezione 97.16.<br />

3940001 |#include <br />

3940002 |#include <br />

3940003 |#include <br />

3940004 |#include <br />

3940005 |#include <br />

3940006 |#include <br />

3940007 |//----------------------------------------------------------------------<br />

3940008 |char *<br />

3940009 |getcwd (char *buffer, size_t size)<br />

3940010 |{<br />

3940011 | sysmsg_uarea_t msg;<br />

3940012 | //<br />

3940013 | // Check arguments: the buffer must be given.<br />

3940014 | //<br />

3940015 | if (buffer == NULL)<br />

3940016 | {<br />

3940017 | errset (EINVAL);<br />

3940018 | return (NULL);<br />

3940019 | }<br />

3940020 | //<br />

3940021 | // Make shure that the last character, inside the working directory<br />

3940022 | // path is a null character.<br />

3940023 | //<br />

3940024 | msg.path_cwd[PATH_MAX-1] = 0;<br />

3940025 | //<br />

3940026 | // Just get the user area data.<br />

«<br />

«<br />

«<br />

«

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!