Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/fileaccessexample_8c_source.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>TB9.2 Example Applications: examples/PIPS/FileAccessExample/fileaccessexample.c Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.2 -->
<h1>examples/PIPS/FileAccessExample/fileaccessexample.c</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// fileaccessexample.c</span>
<a name="l00002"></a>00002 <span class="comment">//</span>
<a name="l00003"></a>00003 <span class="comment">// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).</span>
<a name="l00004"></a>00004 <span class="comment">// All rights reserved.</span>
<a name="l00005"></a>00005 <span class="comment">// This component and the accompanying materials are made available</span>
<a name="l00006"></a>00006 <span class="comment">// under the terms of "Eclipse Public License v1.0"</span>
<a name="l00007"></a>00007 <span class="comment">// which accompanies this distribution, and is available</span>
<a name="l00008"></a>00008 <span class="comment">// at the URL "http://www.eclipse.org/legal/epl-v10.html".</span>
<a name="l00009"></a>00009 <span class="comment">//</span>
<a name="l00010"></a>00010 <span class="comment">// Initial Contributors:</span>
<a name="l00011"></a>00011 <span class="comment">// Nokia Corporation - initial contribution.</span>
<a name="l00012"></a>00012 <span class="comment">//</span>
<a name="l00013"></a>00013 <span class="comment">// Contributors:</span>
<a name="l00014"></a>00014 <span class="comment">//</span>
<a name="l00015"></a>00015 <span class="comment">// Description:</span>
<a name="l00016"></a>00016 <span class="comment">// Component description file</span>
<a name="l00017"></a>00017 <span class="comment">//</span>
<a name="l00018"></a>00018
<a name="l00019"></a>00019 <span class="comment">// This example demonstrates the Symbian platform Platform Security model.</span>
<a name="l00020"></a>00020 <span class="comment">// This shows the restrictions on the directories that can be accessed by a program, </span>
<a name="l00021"></a>00021 <span class="comment">// depending on the capabilities that the program has.</span>
<a name="l00022"></a>00022
<a name="l00023"></a>00023 <span class="comment">// Include files</span>
<a name="l00024"></a>00024 <span class="preprocessor">#include <stdio.h></span>
<a name="l00025"></a>00025 <span class="preprocessor">#include <errno.h></span>
<a name="l00026"></a>00026 <span class="preprocessor">#include <stdlib.h></span>
<a name="l00027"></a>00027 <span class="preprocessor">#include <unistd.h></span>
<a name="l00028"></a>00028 <span class="preprocessor">#include <sys/stat.h></span>
<a name="l00029"></a>00029
<a name="l00033"></a>00033 <span class="keywordtype">void</span> PressAKey()
<a name="l00034"></a>00034 {
<a name="l00035"></a>00035 fflush(stdout);
<a name="l00036"></a>00036 getchar();
<a name="l00037"></a>00037 }
<a name="l00042"></a>00042 <span class="keywordtype">int</span> DisplayErrorMessage(<span class="keywordtype">char</span> msg[])
<a name="l00043"></a>00043 {
<a name="l00044"></a>00044 printf(<span class="stringliteral">"%s [Error NUMBER = %d]\n"</span>,msg,errno);
<a name="l00045"></a>00045 PressAKey();
<a name="l00046"></a>00046 <span class="keywordflow">return</span> EXIT_FAILURE;
<a name="l00047"></a>00047 }
<a name="l00052"></a>00052 <span class="keywordtype">void</span> CreateASecureDirectory()
<a name="l00053"></a>00053 {
<a name="l00054"></a>00054 <span class="keywordtype">int</span> result;
<a name="l00055"></a>00055
<a name="l00061"></a>00061 result = mkdir(<span class="stringliteral">"mydirectory"</span>,S_IWUSR);
<a name="l00062"></a>00062 <span class="keywordflow">if</span>(result == 0)
<a name="l00063"></a>00063 {
<a name="l00064"></a>00064 printf(<span class="stringliteral">" A secure directory for the program is created successfully\n"</span>);
<a name="l00065"></a>00065 PressAKey();
<a name="l00066"></a>00066 }
<a name="l00067"></a>00067 <span class="keywordflow">else</span>
<a name="l00068"></a>00068 {
<a name="l00069"></a>00069 DisplayErrorMessage(<span class="stringliteral">"\nError in creating directory\n"</span>);
<a name="l00070"></a>00070 PressAKey();
<a name="l00071"></a>00071 }
<a name="l00072"></a>00072 }
<a name="l00077"></a>00077 <span class="keywordtype">void</span> WriteDataToAFile(<span class="keywordtype">char</span> FileName[])
<a name="l00078"></a>00078 {
<a name="l00079"></a>00079 FILE* fp= NULL;
<a name="l00080"></a>00080
<a name="l00086"></a>00086 fp = fopen(FileName, <span class="stringliteral">"w"</span>);
<a name="l00087"></a>00087 <span class="keywordflow">if</span> (fp== NULL)
<a name="l00088"></a>00088 {
<a name="l00089"></a>00089 DisplayErrorMessage(<span class="stringliteral">"\nError in writing data in to the file=%d"</span>);
<a name="l00090"></a>00090 PressAKey();
<a name="l00091"></a>00091 }
<a name="l00092"></a>00092 <span class="keywordflow">else</span>
<a name="l00093"></a>00093 {
<a name="l00094"></a>00094 fprintf(fp,<span class="stringliteral">"%s"</span>,<span class="stringliteral">"hello"</span>);
<a name="l00095"></a>00095 fflush(fp);
<a name="l00096"></a>00096 fclose(fp);
<a name="l00097"></a>00097 printf(<span class="stringliteral">" Writing data in to the file from the secure directory is successful"</span>);
<a name="l00098"></a>00098 }
<a name="l00099"></a>00099 }
<a name="l00100"></a>00100
<a name="l00105"></a>00105 <span class="keywordtype">void</span> ReadDataFromAFile(<span class="keywordtype">char</span> FileName[])
<a name="l00106"></a>00106 {
<a name="l00107"></a>00107 FILE* fp= NULL;
<a name="l00108"></a>00108
<a name="l00114"></a>00114 fp = fopen(FileName, <span class="stringliteral">"r"</span>);
<a name="l00115"></a>00115 <span class="keywordflow">if</span> (fp == NULL)
<a name="l00116"></a>00116 {
<a name="l00117"></a>00117 DisplayErrorMessage(<span class="stringliteral">"\nError in reading data from the file = %d"</span>);
<a name="l00118"></a>00118 printf(<span class="stringliteral">" Press any key to exit from the example"</span>);
<a name="l00119"></a>00119 PressAKey();
<a name="l00120"></a>00120 }
<a name="l00121"></a>00121 <span class="keywordflow">else</span>
<a name="l00122"></a>00122 {
<a name="l00123"></a>00123 <span class="keywordtype">char</span> x[30];
<a name="l00124"></a>00124
<a name="l00125"></a>00125 <span class="comment">// Seek to the beginning of the file</span>
<a name="l00126"></a>00126 fseek(fp, SEEK_SET, 0);
<a name="l00127"></a>00127
<a name="l00128"></a>00128 <span class="comment">// Get the data present in file from the beginning </span>
<a name="l00129"></a>00129 fscanf(fp,<span class="stringliteral">"%s"</span>,x);
<a name="l00130"></a>00130 printf(<span class="stringliteral">"\n Data present in the file is : %s"</span>,x);
<a name="l00131"></a>00131 fclose(fp);
<a name="l00132"></a>00132 printf(<span class="stringliteral">"\n Reading data of the file from the directory is successful\n"</span>);
<a name="l00133"></a>00133 }
<a name="l00134"></a>00134 }
<a name="l00139"></a>00139 <span class="keywordtype">void</span> RemoveADirectory(<span class="keywordtype">char</span> FileName[])
<a name="l00140"></a>00140 {
<a name="l00141"></a>00141 <span class="keywordtype">int</span> result;
<a name="l00142"></a>00142
<a name="l00148"></a>00148 result = <span class="keyword">remove</span>(FileName);
<a name="l00149"></a>00149 <span class="keywordflow">if</span>(result == 0)
<a name="l00150"></a>00150 {
<a name="l00151"></a>00151 printf(<span class="stringliteral">" Removing the file from the directory is successful\n"</span>);
<a name="l00152"></a>00152 }
<a name="l00153"></a>00153 <span class="keywordflow">else</span>
<a name="l00154"></a>00154 {
<a name="l00155"></a>00155 DisplayErrorMessage(<span class="stringliteral">" Error in removing the file from the directory=%d"</span>);
<a name="l00156"></a>00156 PressAKey();
<a name="l00157"></a>00157 }
<a name="l00163"></a>00163 result = rmdir(<span class="stringliteral">"mydirectory"</span>);
<a name="l00164"></a>00164
<a name="l00165"></a>00165 <span class="keywordflow">if</span>(result == 0)
<a name="l00166"></a>00166 {
<a name="l00167"></a>00167 printf(<span class="stringliteral">" Removing the the directory is successful\n"</span>);
<a name="l00168"></a>00168 PressAKey();
<a name="l00169"></a>00169 }
<a name="l00170"></a>00170 <span class="keywordflow">else</span>
<a name="l00171"></a>00171 {
<a name="l00172"></a>00172 DisplayErrorMessage(<span class="stringliteral">" Error in removing the directory=%d"</span>);
<a name="l00173"></a>00173 PressAKey();
<a name="l00174"></a>00174 }
<a name="l00175"></a>00175 }
<a name="l00180"></a>00180 <span class="keywordtype">void</span> WriteDataToFileOfAnotherProgram(<span class="keywordtype">char</span> FileName[])
<a name="l00181"></a>00181 {
<a name="l00182"></a>00182 FILE* fp= NULL;
<a name="l00183"></a>00183
<a name="l00189"></a>00189 fp = fopen(FileName, <span class="stringliteral">"w"</span>);
<a name="l00190"></a>00190
<a name="l00191"></a>00191 <span class="keywordflow">if</span> (fp == NULL)
<a name="l00192"></a>00192 {
<a name="l00193"></a>00193 printf(<span class="stringliteral">" Writing data to files in a private directory belonging to another program is not possible"</span>);
<a name="l00194"></a>00194 }
<a name="l00195"></a>00195 <span class="keywordflow">else</span>
<a name="l00196"></a>00196 {
<a name="l00197"></a>00197 printf(<span class="stringliteral">"\n Writing data to files in a private directory belonging to another program is possible"</span>);
<a name="l00198"></a>00198 PressAKey();
<a name="l00199"></a>00199 }
<a name="l00200"></a>00200 }
<a name="l00205"></a>00205 <span class="keywordtype">void</span> ReadDataFromFileOfAnotherProgram(<span class="keywordtype">char</span> FileName[])
<a name="l00206"></a>00206 {
<a name="l00207"></a>00207 FILE* fp= NULL;
<a name="l00208"></a>00208
<a name="l00214"></a>00214 fp = fopen(FileName, <span class="stringliteral">"r"</span>);
<a name="l00215"></a>00215
<a name="l00216"></a>00216 <span class="keywordflow">if</span> (fp == NULL)
<a name="l00217"></a>00217 {
<a name="l00218"></a>00218 printf(<span class="stringliteral">"\n Reading data from files in a private directory belonging to another program is not possible"</span>);
<a name="l00219"></a>00219 PressAKey();
<a name="l00220"></a>00220 }
<a name="l00221"></a>00221 <span class="keywordflow">else</span>
<a name="l00222"></a>00222 {
<a name="l00223"></a>00223 printf(<span class="stringliteral">"\n Reading data from files in a private directory belonging to another program is possible"</span>);
<a name="l00224"></a>00224 PressAKey();
<a name="l00225"></a>00225 }
<a name="l00226"></a>00226 }
<a name="l00231"></a>00231 <span class="keywordtype">void</span> WriteDataToFileOfSysBin(<span class="keywordtype">char</span> FileName[])
<a name="l00232"></a>00232 {
<a name="l00233"></a>00233 FILE* fp= NULL;
<a name="l00234"></a>00234
<a name="l00240"></a>00240 fp = fopen(FileName, <span class="stringliteral">"w"</span>);
<a name="l00241"></a>00241
<a name="l00242"></a>00242 <span class="keywordflow">if</span> (fp == NULL)
<a name="l00243"></a>00243 {
<a name="l00244"></a>00244 printf(<span class="stringliteral">"\n Writing data to files in the sys/bin directory is not possible"</span>);
<a name="l00245"></a>00245 }
<a name="l00246"></a>00246 <span class="keywordflow">else</span>
<a name="l00247"></a>00247 {
<a name="l00248"></a>00248 printf(<span class="stringliteral">"\n Writing data to files in the sys/bin directory is possible"</span>);
<a name="l00249"></a>00249 PressAKey();
<a name="l00250"></a>00250 }
<a name="l00251"></a>00251 }
<a name="l00256"></a>00256 <span class="keywordtype">void</span> ReadDataToFileOfSysBin(<span class="keywordtype">char</span> FileName[])
<a name="l00257"></a>00257 {
<a name="l00258"></a>00258 FILE* fp = NULL;
<a name="l00259"></a>00259
<a name="l00265"></a>00265 fp = fopen(FileName, <span class="stringliteral">"r"</span>);
<a name="l00266"></a>00266
<a name="l00267"></a>00267 <span class="keywordflow">if</span> (fp == NULL)
<a name="l00268"></a>00268 {
<a name="l00269"></a>00269 printf(<span class="stringliteral">"\n Reading data from files in the sys/bin directory is not possible"</span>);
<a name="l00270"></a>00270 PressAKey();
<a name="l00271"></a>00271 }
<a name="l00272"></a>00272 <span class="keywordflow">else</span>
<a name="l00273"></a>00273 {
<a name="l00274"></a>00274 printf(<span class="stringliteral">"\n Reading data from files in the sys/bin directory is possible"</span>);
<a name="l00275"></a>00275 printf(<span class="stringliteral">" Press any key to exit from the example"</span>);
<a name="l00276"></a>00276 PressAKey();
<a name="l00277"></a>00277 }
<a name="l00278"></a>00278 }
<a name="l00279"></a>00279
<a name="l00280"></a>00280 <span class="keywordtype">int</span> <a class="code" href="redirectprintf_8c.html#a840291bc02cba5474a4cb46a9b9566fe">main</a>()
<a name="l00281"></a>00281 {
<a name="l00282"></a>00282 <span class="keywordtype">char</span> ownFileName[50] = <span class="stringliteral">"\\private\\e80000c9\\mydirectory\\myownfile.txt"</span>;
<a name="l00283"></a>00283 <span class="keywordtype">char</span> otherFileName[40] = <span class="stringliteral">"\\private\\e80000c8\\otherfile.txt"</span>;
<a name="l00284"></a>00284 <span class="keywordtype">char</span> sysBinFileName[30] = <span class="stringliteral">"\\sys\\bin\\euser.dll"</span>;
<a name="l00285"></a>00285
<a name="l00286"></a>00286 printf(<span class="stringliteral">"\n Welcome to the file access example using POSIX APIs\n"</span>);
<a name="l00287"></a>00287
<a name="l00288"></a>00288 <span class="comment">// Print the message to start the example application.</span>
<a name="l00289"></a>00289 printf(<span class="stringliteral">"\n Press enter key to step through the example\n"</span>);
<a name="l00290"></a>00290 PressAKey();
<a name="l00291"></a>00291
<a name="l00292"></a>00292 <span class="comment">// Call CreateASecureDirectory() function to create a secure directory for the program.</span>
<a name="l00293"></a>00293 CreateASecureDirectory();
<a name="l00294"></a>00294
<a name="l00295"></a>00295 <span class="comment">// Call WriteDataToAFile() function to write data in to a file from the created directory.</span>
<a name="l00296"></a>00296 WriteDataToAFile(ownFileName);
<a name="l00297"></a>00297
<a name="l00298"></a>00298 <span class="comment">// Call WriteDataToAFile() function to write data in to a file from the created directory.</span>
<a name="l00299"></a>00299 ReadDataFromAFile(ownFileName);
<a name="l00300"></a>00300
<a name="l00301"></a>00301 <span class="comment">// Call RemoveADirectory() function to remove the directory.</span>
<a name="l00302"></a>00302 RemoveADirectory(ownFileName);
<a name="l00303"></a>00303
<a name="l00304"></a>00304 <span class="comment">// Call WriteDataToFileOfAnotherProgram() function to write data in to a file of another program.</span>
<a name="l00305"></a>00305 WriteDataToFileOfAnotherProgram(otherFileName);
<a name="l00306"></a>00306
<a name="l00307"></a>00307 <span class="comment">// Call ReadDataFromFileOfAnotherProgram() function to read data from a file of another program.</span>
<a name="l00308"></a>00308 ReadDataFromFileOfAnotherProgram(otherFileName);
<a name="l00309"></a>00309
<a name="l00310"></a>00310 <span class="comment">// Call WriteDataToFileOfSysBin() function to write data in to a file of sys/bin directory.</span>
<a name="l00311"></a>00311 WriteDataToFileOfSysBin(sysBinFileName);
<a name="l00312"></a>00312
<a name="l00313"></a>00313 <span class="comment">// Call ReadDataFromFileOfSysBin() function to read data from a file in the sys/bin directory.</span>
<a name="l00314"></a>00314 ReadDataToFileOfSysBin(sysBinFileName);
<a name="l00315"></a>00315
<a name="l00316"></a>00316 <span class="comment">// Print the message to exit from the example application.</span>
<a name="l00317"></a>00317 printf(<span class="stringliteral">"\n Press enter key to exit from the example application"</span>);
<a name="l00318"></a>00318 PressAKey();
<a name="l00319"></a>00319
<a name="l00320"></a>00320 <span class="comment">// returns the success code.</span>
<a name="l00321"></a>00321 <span class="keywordflow">return</span> EXIT_SUCCESS;
<a name="l00322"></a>00322 }
<a name="l00323"></a>00323
<a name="l00324"></a>00324
</pre></div></div>
<hr size="1"/><address style="text-align: right;"><small>Generated by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2 </small></address>
</body>
</html>