Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Annotation of /trunk/src/files.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 245 - (hide annotations) (download) (as text)
Tue Aug 25 17:59:51 2009 UTC (15 years, 7 months ago) by amb
File MIME type: text/x-csrc
File size: 3820 byte(s)
Bug fix for mmap().

1 amb 2 /***************************************
2 amb 245 $Header: /home/amb/CVS/routino/src/files.c,v 1.8 2009-08-25 17:59:51 amb Exp $
3 amb 2
4     Functions to map a file into memory.
5 amb 151
6     Part of the Routino routing software.
7 amb 2 ******************/ /******************
8 amb 151 This file Copyright 2008,2009 Andrew M. Bishop
9 amb 2
10 amb 151 This program is free software: you can redistribute it and/or modify
11     it under the terms of the GNU Affero General Public License as published by
12     the Free Software Foundation, either version 3 of the License, or
13     (at your option) any later version.
14    
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18     GNU Affero General Public License for more details.
19    
20     You should have received a copy of the GNU Affero General Public License
21     along with this program. If not, see <http://www.gnu.org/licenses/>.
22 amb 2 ***************************************/
23    
24    
25     #include <unistd.h>
26     #include <stdlib.h>
27 amb 162 #include <string.h>
28 amb 2 #include <fcntl.h>
29     #include <sys/stat.h>
30     #include <sys/mman.h>
31    
32     #include "functions.h"
33    
34    
35     /*++++++++++++++++++++++++++++++++++++++
36 amb 162 Return a filename composed of the dirname, prefix and filename.
37    
38     char *FileName Returns an allocated filename.
39    
40     const char *dirname The directory name.
41    
42     const char *prefix The file prefix.
43    
44     const char *name The filename.
45     ++++++++++++++++++++++++++++++++++++++*/
46    
47     char *FileName(const char *dirname,const char *prefix, const char *name)
48     {
49     char *filename=(char*)malloc((dirname?strlen(dirname):0)+1+(prefix?strlen(prefix):0)+1+strlen(name));
50    
51     sprintf(filename,"%s%s%s%s%s",dirname?dirname:"",dirname?"/":"",prefix?prefix:"",prefix?"-":"",name);
52    
53     return(filename);
54     }
55    
56    
57     /*++++++++++++++++++++++++++++++++++++++
58 amb 2 Open a file and map it into memory.
59    
60     void *MapFile Returns the address of the file.
61    
62     const char *filename The name of the file to open.
63     ++++++++++++++++++++++++++++++++++++++*/
64    
65     void *MapFile(const char *filename)
66     {
67     int fd;
68     struct stat buf;
69     void *address;
70    
71     /* Open the file */
72    
73     fd=open(filename,O_RDONLY);
74    
75 amb 177 if(fd<0)
76     {
77     fprintf(stderr,"Cannot open file '%s' to read.\n",filename);
78     exit(EXIT_FAILURE);
79     }
80 amb 2
81     /* Get the length of the file */
82    
83     if(fstat(fd,&buf))
84     {
85     close(fd);
86 amb 177
87     fprintf(stderr,"Cannot stat file '%s'.\n",filename);
88     exit(EXIT_FAILURE);
89 amb 2 }
90    
91     /* Map the file */
92    
93     address=mmap(NULL,buf.st_size,PROT_READ,MAP_PRIVATE,fd,0);
94    
95 amb 245 if(address==MAP_FAILED)
96 amb 2 {
97     close(fd);
98 amb 177
99     fprintf(stderr,"Cannot mmap file '%s'.\n",filename);
100     exit(EXIT_FAILURE);
101 amb 2 }
102    
103     return(address);
104     }
105    
106    
107     /*++++++++++++++++++++++++++++++++++++++
108 amb 97 Open a new file on disk.
109 amb 2
110 amb 97 int OpenFile Returns the file descriptor if OK or something negative else in case of an error.
111    
112     const char *filename The name of the file to create.
113 amb 2 ++++++++++++++++++++++++++++++++++++++*/
114    
115 amb 97 int OpenFile(const char *filename)
116 amb 2 {
117 amb 97 int fd;
118 amb 2
119 amb 97 /* Open the file */
120 amb 2
121 amb 97 fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IRGRP|S_IROTH);
122 amb 2
123 amb 97 if(fd<0)
124 amb 177 {
125     fprintf(stderr,"Cannot open file '%s' to write.\n",filename);
126     exit(EXIT_FAILURE);
127     }
128 amb 2
129 amb 97 return(fd);
130 amb 2 }
131    
132    
133     /*++++++++++++++++++++++++++++++++++++++
134 amb 97 Write data to a file on disk.
135 amb 2
136     int WriteFile Returns 0 if OK or something else in case of an error.
137    
138 amb 97 int fd The file descriptor to write to.
139 amb 2
140     void *address The address of the data to be written.
141    
142     size_t length The length of data to write.
143     ++++++++++++++++++++++++++++++++++++++*/
144    
145 amb 97 int WriteFile(int fd,void *address,size_t length)
146 amb 2 {
147     /* Write the data */
148    
149     if(write(fd,address,length)!=length)
150     return(-1);
151    
152     return(0);
153     }
154 amb 87
155    
156     /*++++++++++++++++++++++++++++++++++++++
157 amb 97 Close a file on disk.
158 amb 87
159 amb 97 int fd The file descriptor to close.
160 amb 87 ++++++++++++++++++++++++++++++++++++++*/
161    
162 amb 97 void CloseFile(int fd)
163 amb 87 {
164 amb 97 close(fd);
165 amb 87 }

Properties

Name Value
cvs:description File handling functions.