Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Annotation of /trunk/src/files.c
Parent Directory
|
Revision Log
Revision 97 -
(hide annotations)
(download)
(as text)
Sun Feb 1 17:11:08 2009 UTC (16 years, 1 month ago) by amb
File MIME type: text/x-csrc
File size: 2459 byte(s)
Sun Feb 1 17:11:08 2009 UTC (16 years, 1 month ago) by amb
File MIME type: text/x-csrc
File size: 2459 byte(s)
Rename some variable types.
1 | amb | 2 | /*************************************** |
2 | amb | 97 | $Header: /home/amb/CVS/routino/src/files.c,v 1.3 2009-02-01 17:11:07 amb Exp $ |
3 | amb | 2 | |
4 | Functions to map a file into memory. | ||
5 | ******************/ /****************** | ||
6 | Written by Andrew M. Bishop | ||
7 | |||
8 | amb | 87 | This file Copyright 2008,2009 Andrew M. Bishop |
9 | amb | 2 | It may be distributed under the GNU Public License, version 2, or |
10 | any higher version. See section COPYING of the GNU Public license | ||
11 | for conditions under which this file may be redistributed. | ||
12 | ***************************************/ | ||
13 | |||
14 | |||
15 | #include <unistd.h> | ||
16 | amb | 87 | #include <assert.h> |
17 | amb | 2 | #include <stdlib.h> |
18 | #include <fcntl.h> | ||
19 | #include <sys/types.h> | ||
20 | #include <sys/stat.h> | ||
21 | #include <sys/mman.h> | ||
22 | |||
23 | #include "functions.h" | ||
24 | |||
25 | |||
26 | /*++++++++++++++++++++++++++++++++++++++ | ||
27 | Open a file and map it into memory. | ||
28 | |||
29 | void *MapFile Returns the address of the file. | ||
30 | |||
31 | const char *filename The name of the file to open. | ||
32 | ++++++++++++++++++++++++++++++++++++++*/ | ||
33 | |||
34 | void *MapFile(const char *filename) | ||
35 | { | ||
36 | int fd; | ||
37 | struct stat buf; | ||
38 | void *address; | ||
39 | |||
40 | /* Open the file */ | ||
41 | |||
42 | fd=open(filename,O_RDONLY); | ||
43 | |||
44 | if(fd==-1) | ||
45 | return(NULL); | ||
46 | |||
47 | /* Get the length of the file */ | ||
48 | |||
49 | if(fstat(fd,&buf)) | ||
50 | { | ||
51 | close(fd); | ||
52 | return(NULL); | ||
53 | } | ||
54 | |||
55 | /* Map the file */ | ||
56 | |||
57 | address=mmap(NULL,buf.st_size,PROT_READ,MAP_PRIVATE,fd,0); | ||
58 | |||
59 | if(!address) | ||
60 | { | ||
61 | close(fd); | ||
62 | return(NULL); | ||
63 | } | ||
64 | |||
65 | return(address); | ||
66 | } | ||
67 | |||
68 | |||
69 | /*++++++++++++++++++++++++++++++++++++++ | ||
70 | amb | 97 | Open a new file on disk. |
71 | amb | 2 | |
72 | amb | 97 | int OpenFile Returns the file descriptor if OK or something negative else in case of an error. |
73 | |||
74 | const char *filename The name of the file to create. | ||
75 | amb | 2 | ++++++++++++++++++++++++++++++++++++++*/ |
76 | |||
77 | amb | 97 | int OpenFile(const char *filename) |
78 | amb | 2 | { |
79 | amb | 97 | int fd; |
80 | amb | 2 | |
81 | amb | 97 | /* Open the file */ |
82 | amb | 2 | |
83 | amb | 97 | fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IRGRP|S_IROTH); |
84 | amb | 2 | |
85 | amb | 97 | if(fd<0) |
86 | assert(0); | ||
87 | amb | 2 | |
88 | amb | 97 | return(fd); |
89 | amb | 2 | } |
90 | |||
91 | |||
92 | /*++++++++++++++++++++++++++++++++++++++ | ||
93 | amb | 97 | Write data to a file on disk. |
94 | amb | 2 | |
95 | int WriteFile Returns 0 if OK or something else in case of an error. | ||
96 | |||
97 | amb | 97 | int fd The file descriptor to write to. |
98 | amb | 2 | |
99 | void *address The address of the data to be written. | ||
100 | |||
101 | size_t length The length of data to write. | ||
102 | ++++++++++++++++++++++++++++++++++++++*/ | ||
103 | |||
104 | amb | 97 | int WriteFile(int fd,void *address,size_t length) |
105 | amb | 2 | { |
106 | /* Write the data */ | ||
107 | |||
108 | if(write(fd,address,length)!=length) | ||
109 | return(-1); | ||
110 | |||
111 | return(0); | ||
112 | } | ||
113 | amb | 87 | |
114 | |||
115 | /*++++++++++++++++++++++++++++++++++++++ | ||
116 | amb | 97 | Close a file on disk. |
117 | amb | 87 | |
118 | amb | 97 | int fd The file descriptor to close. |
119 | amb | 87 | ++++++++++++++++++++++++++++++++++++++*/ |
120 | |||
121 | amb | 97 | void CloseFile(int fd) |
122 | amb | 87 | { |
123 | amb | 97 | close(fd); |
124 | amb | 87 | } |
Properties
Name | Value |
---|---|
cvs:description | File handling functions. |