examples/PIPS/FileAccessExample/fileaccessexample.c

00001 // fileaccessexample.c
00002 //
00003 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
00004 // All rights reserved.
00005 // This component and the accompanying materials are made available
00006 // under the terms of "Eclipse Public License v1.0"
00007 // which accompanies this distribution, and is available
00008 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00009 //
00010 // Initial Contributors:
00011 // Nokia Corporation - initial contribution.
00012 //
00013 // Contributors:
00014 //
00015 // Description:
00016 // Component description file
00017 //
00018 
00019 // This example demonstrates the Symbian platform Platform Security model.
00020 // This shows the restrictions on the directories that can be accessed by a program, 
00021 // depending on the capabilities that the program has.
00022 
00023 // Include files
00024 #include <stdio.h>
00025 #include <errno.h>
00026 #include <stdlib.h>
00027 #include <unistd.h>
00028 #include <sys/stat.h>
00029 
00033 void PressAKey()
00034         {
00035         fflush(stdout);
00036         getchar();
00037         }
00042 int DisplayErrorMessage(char msg[])
00043         {
00044         printf("%s [Error NUMBER = %d]\n",msg,errno);
00045         PressAKey();
00046         return EXIT_FAILURE;
00047         }
00052 void CreateASecureDirectory()
00053         {
00054         int result;
00055         
00061         result = mkdir("mydirectory",S_IWUSR);     
00062         if(result == 0)
00063                 {
00064                 printf(" A secure directory for the program is created successfully\n");
00065                 PressAKey();
00066                 }
00067         else
00068                 {
00069                 DisplayErrorMessage("\nError in creating directory\n");
00070                 PressAKey();
00071                 }
00072         }
00077 void WriteDataToAFile(char FileName[])
00078         {
00079         FILE* fp= NULL;
00080         
00086         fp = fopen(FileName, "w");
00087         if (fp== NULL)
00088                 {
00089                 DisplayErrorMessage("\nError in writing data in to the file=%d");
00090                 PressAKey();
00091                 }
00092         else
00093                 {
00094                 fprintf(fp,"%s","hello");
00095                 fflush(fp);
00096                 fclose(fp);
00097                 printf(" Writing data in to the file from the secure directory is successful");
00098                 }
00099         }
00100 
00105 void ReadDataFromAFile(char FileName[])
00106         {
00107         FILE* fp= NULL;
00108         
00114         fp = fopen(FileName, "r");
00115         if (fp == NULL)
00116                 {
00117         DisplayErrorMessage("\nError in reading data from the file = %d"); 
00118         printf(" Press any key to exit from the example");
00119         PressAKey();
00120                 }
00121         else
00122                 {
00123                 char x[30];
00124                                 
00125                 // Seek to the beginning of the file
00126         fseek(fp, SEEK_SET, 0); 
00127         
00128         // Get the data present in file from the beginning 
00129                 fscanf(fp,"%s",x);
00130                 printf("\n Data present in the file is : %s",x);
00131                 fclose(fp);
00132                 printf("\n Reading data of the file from the directory is successful\n");
00133                 }
00134         }
00139 void RemoveADirectory(char FileName[])
00140         {
00141         int result;
00142         
00148         result = remove(FileName);
00149         if(result == 0)
00150                         {
00151                         printf(" Removing the file from the directory is successful\n");
00152                         }
00153                 else
00154                         {
00155                         DisplayErrorMessage(" Error in removing the file from the directory=%d");
00156                         PressAKey();
00157                         }
00163         result = rmdir("mydirectory");
00164    
00165         if(result == 0)
00166                 {
00167                 printf(" Removing the the directory is successful\n");
00168                 PressAKey();
00169                 }
00170         else
00171                 {
00172                 DisplayErrorMessage(" Error in removing the directory=%d");
00173                 PressAKey();
00174                 }
00175         }
00180 void WriteDataToFileOfAnotherProgram(char FileName[])
00181         {
00182         FILE* fp= NULL;
00183         
00189         fp = fopen(FileName, "w");
00190         
00191         if (fp == NULL)
00192                 {
00193                 printf(" Writing data to files in a private directory belonging to another program is not possible"); 
00194         }
00195         else
00196                 {
00197                 printf("\n Writing data to files in a private directory belonging to another program is possible"); 
00198                 PressAKey();
00199                 }
00200         }
00205 void ReadDataFromFileOfAnotherProgram(char FileName[])
00206         {
00207         FILE* fp= NULL;
00208         
00214         fp = fopen(FileName, "r");
00215         
00216         if (fp == NULL)
00217                 {
00218                 printf("\n Reading data from files in a private directory belonging to another program is not possible"); 
00219                 PressAKey();
00220                 }
00221         else
00222                 {
00223                 printf("\n Reading data from files in a private directory belonging to another program is possible"); 
00224                 PressAKey(); 
00225                 }
00226         }
00231 void WriteDataToFileOfSysBin(char FileName[])
00232         {
00233         FILE* fp= NULL;
00234         
00240         fp = fopen(FileName, "w");
00241         
00242         if (fp == NULL)
00243                 {
00244                 printf("\n Writing data to files in the sys/bin directory is not possible"); 
00245                 }
00246         else
00247                 {
00248                 printf("\n Writing data to files in the sys/bin directory is possible"); 
00249                 PressAKey();
00250                 }
00251         }
00256 void ReadDataToFileOfSysBin(char FileName[])
00257         {
00258         FILE* fp = NULL;
00259         
00265         fp = fopen(FileName, "r");
00266         
00267         if (fp == NULL)
00268                 {
00269                 printf("\n Reading data from files in the sys/bin directory is not possible"); 
00270                 PressAKey();
00271                 }
00272         else
00273                 {
00274                 printf("\n Reading data from files in the sys/bin directory is possible"); 
00275                 printf(" Press any key to exit from the example");
00276                 PressAKey(); 
00277                 }       
00278         }
00279 
00280 int main()
00281 {
00282         char ownFileName[50] = "\\private\\e80000c9\\mydirectory\\myownfile.txt";
00283         char otherFileName[40] = "\\private\\e80000c8\\otherfile.txt";
00284         char sysBinFileName[30] = "\\sys\\bin\\euser.dll";
00285         
00286         printf("\n Welcome to the file access example using POSIX APIs\n");
00287         
00288         // Print the message to start the example application.
00289         printf("\n Press enter key to step through the example\n");
00290         PressAKey();
00291         
00292         // Call CreateASecureDirectory() function to create a secure directory for the program.
00293         CreateASecureDirectory();
00294         
00295         // Call WriteDataToAFile() function to write data in to a file from the created directory.
00296         WriteDataToAFile(ownFileName);
00297         
00298         // Call WriteDataToAFile() function to write data in to a file from the created directory.
00299         ReadDataFromAFile(ownFileName);
00300         
00301         // Call RemoveADirectory() function to remove the directory.
00302         RemoveADirectory(ownFileName);
00303         
00304         // Call WriteDataToFileOfAnotherProgram() function to write data in to a file of another program.
00305         WriteDataToFileOfAnotherProgram(otherFileName);
00306         
00307         // Call ReadDataFromFileOfAnotherProgram() function to read data from a file of another program.
00308         ReadDataFromFileOfAnotherProgram(otherFileName);
00309         
00310         // Call WriteDataToFileOfSysBin() function to write data in to a file of sys/bin directory.
00311         WriteDataToFileOfSysBin(sysBinFileName);
00312         
00313         // Call ReadDataFromFileOfSysBin() function to read data from a file in the sys/bin directory.
00314         ReadDataToFileOfSysBin(sysBinFileName);
00315         
00316         // Print the message to exit from the example application.
00317         printf("\n Press enter key to exit from the example application");
00318         PressAKey();
00319         
00320         // returns the success code.
00321         return EXIT_SUCCESS;
00322 }
00323 
00324 

Generated by  doxygen 1.6.2