home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Modules / fcntlmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-28  |  5.5 KB  |  265 lines  |  [TEXT/CWIE]

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* fcntl module */
  33.  
  34. #include "allobjects.h"
  35. #include "modsupport.h"
  36.  
  37. #include <fcntl.h>
  38.  
  39.  
  40. /* fcntl(fd, opt, [arg]) */
  41.  
  42. static object *
  43. fcntl_fcntl(self, args)
  44.     object *self; /* Not used */
  45.     object *args;
  46. {
  47.     int fd;
  48.     int code;
  49.     int arg;
  50.     int ret;
  51.     char *str;
  52.     int len;
  53.     char buf[1024];
  54.  
  55.     if (getargs(args, "(iis#)", &fd, &code, &str, &len)) {
  56.         if (len > sizeof buf) {
  57.             err_setstr(ValueError, "fcntl string arg too long");
  58.             return NULL;
  59.         }
  60.         memcpy(buf, str, len);
  61.         BGN_SAVE
  62.         ret = fcntl(fd, code, buf);
  63.         END_SAVE
  64.         if (ret < 0) {
  65.             err_errno(IOError);
  66.             return NULL;
  67.         }
  68.         return newsizedstringobject(buf, len);
  69.     }
  70.  
  71.     err_clear();
  72.     if (getargs(args, "(ii)", &fd, &code))
  73.         arg = 0;
  74.     else {
  75.         err_clear();
  76.         if (!getargs(args, "(iii)", &fd, &code, &arg))
  77.             return NULL;
  78.     }
  79.     BGN_SAVE
  80.     ret = fcntl(fd, code, arg);
  81.     END_SAVE
  82.     if (ret < 0) {
  83.         err_errno(IOError);
  84.         return NULL;
  85.     }
  86.     return newintobject((long)ret);
  87. }
  88.  
  89.  
  90. /* ioctl(fd, opt, [arg]) */
  91.  
  92. static object *
  93. fcntl_ioctl(self, args)
  94.     object *self; /* Not used */
  95.     object *args;
  96. {
  97.     int fd;
  98.     int code;
  99.     int arg;
  100.     int ret;
  101.     char *str;
  102.     int len;
  103.     char buf[1024];
  104.  
  105.     if (getargs(args, "(iis#)", &fd, &code, &str, &len)) {
  106.         if (len > sizeof buf) {
  107.             err_setstr(ValueError, "ioctl string arg too long");
  108.             return NULL;
  109.         }
  110.         memcpy(buf, str, len);
  111.         BGN_SAVE
  112.         ret = ioctl(fd, code, buf);
  113.         END_SAVE
  114.         if (ret < 0) {
  115.             err_errno(IOError);
  116.             return NULL;
  117.         }
  118.         return newsizedstringobject(buf, len);
  119.     }
  120.  
  121.     err_clear();
  122.     if (getargs(args, "(ii)", &fd, &code))
  123.         arg = 0;
  124.     else {
  125.         err_clear();
  126.         if (!getargs(args, "(iii)", &fd, &code, &arg))
  127.             return NULL;
  128.     }
  129.     BGN_SAVE
  130.     ret = ioctl(fd, code, arg);
  131.     END_SAVE
  132.     if (ret < 0) {
  133.         err_errno(IOError);
  134.         return NULL;
  135.     }
  136.     return newintobject((long)ret);
  137. }
  138.  
  139.  
  140. /* flock(fd, operation) */
  141.  
  142. static object *
  143. fcntl_flock(self, args)
  144.     object *self; /* Not used */
  145.     object *args;
  146. {
  147.     int fd;
  148.     int code;
  149.     int ret;
  150.     FILE *f;
  151.  
  152.     if (!getargs(args, "(ii)", &fd, &code))
  153.         return NULL;
  154.  
  155.     BGN_SAVE
  156. #ifdef HAVE_FLOCK
  157.     ret = flock(fd, code);
  158. #else
  159.  
  160. #ifndef LOCK_SH
  161. #define LOCK_SH        1    /* shared lock */
  162. #define LOCK_EX        2    /* exclusive lock */
  163. #define LOCK_NB        4    /* don't block when locking */
  164. #define LOCK_UN        8    /* unlock */
  165. #endif
  166.     {
  167.         struct flock l;
  168.         if (code == LOCK_UN)
  169.             l.l_type = F_UNLCK;
  170.         else if (code & LOCK_SH)
  171.             l.l_type = F_RDLCK;
  172.         else if (code & LOCK_EX)
  173.             l.l_type = F_WRLCK;
  174.         else {
  175.             err_setstr(ValueError, "unrecognized flock argument");
  176.             return NULL;
  177.         }
  178.         l.l_whence = l.l_start = l.l_len = 0;
  179.         ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
  180.     }
  181. #endif /* HAVE_FLOCK */
  182.     END_SAVE
  183.     if (ret < 0) {
  184.         err_errno(IOError);
  185.         return NULL;
  186.     }
  187.     INCREF(None);
  188.     return None;
  189. }
  190.  
  191. /* lockf(fd, operation) */
  192. static object *
  193. fcntl_lockf(self, args)
  194.     object *self; /* Not used */
  195.     object *args;
  196. {
  197.     int fd, code, len = 0, start = 0, whence = 0, ret;
  198.     FILE *f;
  199.  
  200.     if (!PyArg_ParseTuple(args, "ii|iii", &fd, &code, &len, 
  201.                    &start, &whence))
  202.         return NULL;
  203.  
  204.     BGN_SAVE
  205. #ifndef LOCK_SH
  206. #define LOCK_SH        1    /* shared lock */
  207. #define LOCK_EX        2    /* exclusive lock */
  208. #define LOCK_NB        4    /* don't block when locking */
  209. #define LOCK_UN        8    /* unlock */
  210. #endif
  211.     {
  212.         struct flock l;
  213.         if (code == LOCK_UN)
  214.             l.l_type = F_UNLCK;
  215.         else if (code & LOCK_SH)
  216.             l.l_type = F_RDLCK;
  217.         else if (code & LOCK_EX)
  218.             l.l_type = F_WRLCK;
  219.         else {
  220.             err_setstr(ValueError, "unrecognized flock argument");
  221.             return NULL;
  222.         }
  223.         l.l_len = len;
  224.         l.l_start = start;
  225.         l.l_whence = whence;
  226.         ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
  227.     }
  228.     END_SAVE
  229.     if (ret < 0) {
  230.         err_errno(IOError);
  231.         return NULL;
  232.     }
  233.     INCREF(None);
  234.     return None;
  235. }
  236.  
  237. /* List of functions */
  238.  
  239. static struct methodlist fcntl_methods[] = {
  240.     {"fcntl",    fcntl_fcntl},
  241.     {"ioctl",    fcntl_ioctl},
  242.     {"flock",    fcntl_flock},
  243.     {"lockf",       fcntl_lockf, 1},
  244.     {NULL,        NULL}        /* sentinel */
  245. };
  246.  
  247.  
  248. /* Module initialisation */
  249.  
  250. void
  251. initfcntl()
  252. {
  253.     object *m, *d;
  254.  
  255.     /* Create the module and add the functions */
  256.     m = initmodule("fcntl", fcntl_methods);
  257.  
  258.     /* Add some symbolic constants to the module */
  259.     d = getmoduledict(m);
  260.  
  261.     /* Check for errors */
  262.     if (err_occurred())
  263.         fatal("can't initialize module fcntl");
  264. }
  265.