Attachment 'filecase.c'
Download 1 #include "Python.h"
2
3 #include <fcntl.h>
4 //#include <errno.h>
5 #include <unistd.h>
6 #include <sys/param.h>
7
8 static PyObject* get_canonical_case(PyObject *self, PyObject *args)
9 {
10 char *path;
11 char canonical_path[sizeof(char)*MAXPATHLEN+1];
12
13 int fd;
14
15 // Python seems to return the correct error here.
16 // If you explicitly free path, it crashes.
17 if (!PyArg_ParseTuple(args, "s", &path))
18 return NULL;
19
20 fd = open(path, O_RDONLY);
21
22 // both open and fcntl will set errno if fine grained error reporting is desired.
23 // currently there is no specific error report.
24 if(fd == -1 || fcntl(fd, F_GETPATH, canonical_path) == -1){
25 PyErr_SetString(PyExc_IOError, "Unable to open file");
26 return NULL;
27 }
28
29 // close the file
30 if(close(fd) == -1){
31 PyErr_SetString(PyExc_IOError, "Failed to close file");
32 return NULL;
33 }
34 return Py_BuildValue("s", canonical_path);
35 }
36
37 static PyMethodDef FilecaseMethods[] = {
38 {"get_canonical_case", get_canonical_case, METH_VARARGS,
39 "Get the canonical case of the file."},
40 {NULL, NULL, 0, NULL} /* Sentinel */
41 };
42
43 PyMODINIT_FUNC initfilecase(void)
44 {
45 (void) Py_InitModule("filecase", FilecaseMethods);
46 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.