Routino SVN Repository Browser

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

ViewVC logotype

Contents of /trunk/src/files.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 177 - (show annotations) (download) (as text)
Fri May 15 17:32:14 2009 UTC (15 years, 10 months ago) by amb
File MIME type: text/x-csrc
File size: 3852 byte(s)
Error checking on opening files (to read/write data and to write output).

1 /***************************************
2 $Header: /home/amb/CVS/routino/src/files.c,v 1.6 2009-05-15 17:32:14 amb Exp $
3
4 Functions to map a file into memory.
5
6 Part of the Routino routing software.
7 ******************/ /******************
8 This file Copyright 2008,2009 Andrew M. Bishop
9
10 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 ***************************************/
23
24
25 #include <unistd.h>
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <fcntl.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/mman.h>
33
34 #include "functions.h"
35
36
37 /*++++++++++++++++++++++++++++++++++++++
38 Return a filename composed of the dirname, prefix and filename.
39
40 char *FileName Returns an allocated filename.
41
42 const char *dirname The directory name.
43
44 const char *prefix The file prefix.
45
46 const char *name The filename.
47 ++++++++++++++++++++++++++++++++++++++*/
48
49 char *FileName(const char *dirname,const char *prefix, const char *name)
50 {
51 char *filename=(char*)malloc((dirname?strlen(dirname):0)+1+(prefix?strlen(prefix):0)+1+strlen(name));
52
53 sprintf(filename,"%s%s%s%s%s",dirname?dirname:"",dirname?"/":"",prefix?prefix:"",prefix?"-":"",name);
54
55 return(filename);
56 }
57
58
59 /*++++++++++++++++++++++++++++++++++++++
60 Open a file and map it into memory.
61
62 void *MapFile Returns the address of the file.
63
64 const char *filename The name of the file to open.
65 ++++++++++++++++++++++++++++++++++++++*/
66
67 void *MapFile(const char *filename)
68 {
69 int fd;
70 struct stat buf;
71 void *address;
72
73 /* Open the file */
74
75 fd=open(filename,O_RDONLY);
76
77 if(fd<0)
78 {
79 fprintf(stderr,"Cannot open file '%s' to read.\n",filename);
80 exit(EXIT_FAILURE);
81 }
82
83 /* Get the length of the file */
84
85 if(fstat(fd,&buf))
86 {
87 close(fd);
88
89 fprintf(stderr,"Cannot stat file '%s'.\n",filename);
90 exit(EXIT_FAILURE);
91 }
92
93 /* Map the file */
94
95 address=mmap(NULL,buf.st_size,PROT_READ,MAP_PRIVATE,fd,0);
96
97 if(!address)
98 {
99 close(fd);
100
101 fprintf(stderr,"Cannot mmap file '%s'.\n",filename);
102 exit(EXIT_FAILURE);
103 }
104
105 return(address);
106 }
107
108
109 /*++++++++++++++++++++++++++++++++++++++
110 Open a new file on disk.
111
112 int OpenFile Returns the file descriptor if OK or something negative else in case of an error.
113
114 const char *filename The name of the file to create.
115 ++++++++++++++++++++++++++++++++++++++*/
116
117 int OpenFile(const char *filename)
118 {
119 int fd;
120
121 /* Open the file */
122
123 fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IRGRP|S_IROTH);
124
125 if(fd<0)
126 {
127 fprintf(stderr,"Cannot open file '%s' to write.\n",filename);
128 exit(EXIT_FAILURE);
129 }
130
131 return(fd);
132 }
133
134
135 /*++++++++++++++++++++++++++++++++++++++
136 Write data to a file on disk.
137
138 int WriteFile Returns 0 if OK or something else in case of an error.
139
140 int fd The file descriptor to write to.
141
142 void *address The address of the data to be written.
143
144 size_t length The length of data to write.
145 ++++++++++++++++++++++++++++++++++++++*/
146
147 int WriteFile(int fd,void *address,size_t length)
148 {
149 /* Write the data */
150
151 if(write(fd,address,length)!=length)
152 return(-1);
153
154 return(0);
155 }
156
157
158 /*++++++++++++++++++++++++++++++++++++++
159 Close a file on disk.
160
161 int fd The file descriptor to close.
162 ++++++++++++++++++++++++++++++++++++++*/
163
164 void CloseFile(int fd)
165 {
166 close(fd);
167 }

Properties

Name Value
cvs:description File handling functions.