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.

308 volume VI os16<br />

Sorgenti della libreria generale 309<br />

«<br />

«<br />

2280023 |// Flags to be set with:<br />

2280024 |// fcntl (fd, F_SETFD, ...);<br />

2280025 |//----------------------------------------------------------------------<br />

2280026 |#define FD_CLOEXEC 1 // Close the file descriptor upon<br />

2280027 | // execution of an exec() family<br />

2280028 | // function.<br />

2280029 |//----------------------------------------------------------------------<br />

2280030 |// Values for type ‘l_type’, used for record locking with ‘fcntl()’.<br />

2280031 |//----------------------------------------------------------------------<br />

2280032 |#define F_RDLCK 0 // Read lock.<br />

2280033 |#define F_WRLCK 1 // Write lock.<br />

2280034 |#define F_UNLCK 2 // Remove lock.<br />

2280035 |//----------------------------------------------------------------------<br />

2280036 |// Flags for file creation, in place of ‘oflag’ parameter for function<br />

2280037 |// ‘open()’.<br />

2280038 |//----------------------------------------------------------------------<br />

2280039 |#define O_CREAT 000010 // Create file if it does not exist.<br />

2280040 |#define O_EXCL 000020 // Exclusive use flag.<br />

2280041 |#define O_NOCTTY 000040 // Do not assign a controlling terminal.<br />

2280042 |#define O_TRUNC 000100 // Truncation flag.<br />

2280043 |//----------------------------------------------------------------------<br />

2280044 |// Flags for the file status, used with ‘open()’ and ‘fcntl()’.<br />

2280045 |//----------------------------------------------------------------------<br />

2280046 |#define O_APPEND 000200 // Write append.<br />

2280047 |#define O_DSYNC 000400 // Synchronized write operations.<br />

2280048 |#define O_NONBLOCK 001000 // Non-blocking mode.<br />

2280049 |#define O_RSYNC 002000 // Synchronized read operations.<br />

2280050 |#define O_SYNC 004000 // Synchronized read and write.<br />

2280051 |//----------------------------------------------------------------------<br />

2280052 |// File access mask selection.<br />

2280053 |//----------------------------------------------------------------------<br />

2280054 |#define O_ACCMODE 000003 // Mask to select the last three bits,<br />

2280055 | // used to specify the main access<br />

2280056 | // modes: read, write and both.<br />

2280057 |//----------------------------------------------------------------------<br />

2280058 |// Main access modes.<br />

2280059 |//----------------------------------------------------------------------<br />

2280060 |#define O_RDONLY 000001 // Read.<br />

2280061 |#define O_WRONLY 000002 // Write.<br />

2280062 |#define O_RDWR (O_RDONLY | O_WRONLY) // Both read and write.<br />

2280063 |//----------------------------------------------------------------------<br />

2280064 |// Structure ‘flock’, used to file lock for POSIX standard. It is not<br />

2280065 |// used inside os16.<br />

2280066 |//----------------------------------------------------------------------<br />

2280067 |struct flock {<br />

2280068 | short int l_type; // Type of lock: F_RDLCK, F_WRLCK, or F_UNLCK.<br />

2280069 | short int l_whence; // Start reference point.<br />

2280070 | off_t l_start; // Offset, from ‘l_whence’, for the area start.<br />

2280071 | off_t l_len; // Locked area size. Zero means up to the end of<br />

2280072 | // the file.<br />

2280073 | pid_t l_pid; // The process id blocking the area.<br />

2280074 |};<br />

2280075 |//----------------------------------------------------------------------<br />

2280076 |// Function prototypes.<br />

2280077 |//----------------------------------------------------------------------<br />

2280078 |int creat (const char *path, mode_t mode);<br />

2280079 |int fcntl (int fdn, int cmd, ...);<br />

2280080 |int open (const char *path, int oflags, ...);<br />

2280081 |//----------------------------------------------------------------------<br />

2280082 |<br />

2280083 |#endif<br />

105.4.1 lib/fcntl/creat.c<br />

Si veda la sezione 98.11.<br />

2290001 |#include <br />

2290002 |#include <br />

2290003 |#include <br />

2290004 |//----------------------------------------------------------------------<br />

2290005 |int<br />

2290006 |creat (const char *path, mode_t mode)<br />

2290007 |{<br />

2290008 | return (open (path, O_WRONLY|O_CREAT|O_TRUNC, mode));<br />

2290009 |}<br />

105.4.2 lib/fcntl/fcntl.c<br />

Si veda la sezione 97.13.<br />

2300001 |#include <br />

2300002 |#include <br />

2300003 |#include <br />

2300004 |#include <br />

2300005 |#include <br />

2300006 |#include <br />

2300007 |#include <br />

2300008 |#include <br />

2300009 |//----------------------------------------------------------------------<br />

2300010 |int<br />

2300011 |fcntl (int fdn, int cmd, ...)<br />

2300012 |{<br />

2300013 | va_list ap;<br />

2300014 | sysmsg_fcntl_t msg;<br />

2300015 | va_start (ap, cmd);<br />

2300016 | //<br />

2300017 | // Well known arguments.<br />

2300018 | //<br />

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

2300020 | msg.cmd = cmd;<br />

2300021 | //<br />

2300022 | // Select other arguments.<br />

2300023 | //<br />

2300024 | switch (cmd)<br />

2300025 | {<br />

2300026 | case F_DUPFD:<br />

2300027 | case F_SETFD:<br />

2300028 | case F_SETFL:<br />

2300029 | msg.arg = va_arg (ap, int);<br />

2300030 | break;<br />

2300031 | case F_GETFD:<br />

2300032 | case F_GETFL:<br />

2300033 | break;<br />

2300034 | case F_GETOWN:<br />

2300035 | case F_SETOWN:<br />

2300036 | case F_GETLK:<br />

2300037 | case F_SETLK:<br />

2300038 | case F_SETLKW:<br />

2300039 | errset (E_NOT_IMPLEMENTED); // Not implemented.<br />

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

2300041 | default:<br />

2300042 | errset (EINVAL); // Not implemented.<br />

2300043 | return (NULL);<br />

2300044 | }<br />

2300045 | //<br />

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

2300047 | //<br />

2300048 | sys (SYS_FCNTL, &msg, (sizeof msg));<br />

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

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

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

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

2300053 |}<br />

105.4.3 lib/fcntl/open.c<br />

Si veda la sezione 97.28.<br />

2310001 |#include <br />

2310002 |#include <br />

2310003 |#include <br />

2310004 |#include <br />

2310005 |#include <br />

2310006 |#include <br />

2310007 |#include <br />

2310008 |#include <br />

2310009 |//----------------------------------------------------------------------<br />

2310010 |int<br />

2310011 |open (const char *path, int oflags, ...)<br />

2310012 |{<br />

2310013 | va_list ap;<br />

2310014 | sysmsg_open_t msg;<br />

2310015 | va_start (ap, oflags);<br />

2310016 | if (path == NULL || strlen (path) == 0)<br />

2310017 | {<br />

2310018 | errset (EINVAL); // Invalid argument.<br />

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

2310020 | }<br />

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

2310022 | msg.flags = oflags;<br />

2310023 | msg.mode = va_arg (ap, mode_t);<br />

2310024 | sys (SYS_OPEN, &msg, (sizeof msg));<br />

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

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

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

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

2310029 |}<br />

105.5 os16: «lib/grp.h»<br />

Si veda la sezione 101.2.<br />

2320001 |//----------------------------------------------------------------------<br />

2320002 |// os16 does not have a group management!<br />

2320003 |//----------------------------------------------------------------------<br />

2320004 |<br />

2320005 |#ifndef _GRP_H<br />

2320006 |#define _GRP_H 1<br />

2320007 |<br />

2320008 |#include <br />

2320009 |#include <br />

2320010 |#include // gid_t<br />

2320011 |<br />

2320012 |//----------------------------------------------------------------------<br />

2320013 |struct group {<br />

2320014 | char *gr_name;<br />

2320015 | gid_t gr_gid;<br />

2320016 | char **gr_mem;<br />

2320017 |};<br />

2320018 |//----------------------------------------------------------------------<br />

2320019 |struct group *getgrgid (gid_t gid);<br />

2320020 |struct group *getgrnam (const char *name);<br />

2320021 |//----------------------------------------------------------------------<br />

2320022 |<br />

2320023 |#endif<br />

105.5.1 lib/grp/getgrgid.c<br />

Si veda la sezione 101.2.<br />

2330001 |#include <br />

2330002 |#include <br />

2330003 |//----------------------------------------------------------------------<br />

2330004 |struct group *<br />

2330005 |getgrgid (gid_t gid)<br />

2330006 |{<br />

2330007 | static char *name = "none";<br />

2330008 | static struct group grp;<br />

2330009 | //<br />

2330010 | // os16 does not have a group management, so the answare is always<br />

2330011 | // the same.<br />

2330012 | //<br />

2330013 | grp.gr_name = name;<br />

2330014 | grp.gr_gid = (gid_t) -1;<br />

2330015 | grp.gr_mem = NULL;<br />

2330016 | //<br />

2330017 | return (&grp);<br />

2330018 |}<br />

«<br />

«<br />

«

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

Saved successfully!

Ooh no, something went wrong!