00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <stdio.h>
00038 #include <errno.h>
00039 #include <stdlib.h>
00040 #include <unistd.h>
00041 #include <sys/stat.h>
00042
00046 void PressAKey()
00047 {
00048 fflush(stdout);
00049 getchar();
00050 }
00055 int DisplayErrorMessage(char msg[])
00056 {
00057 printf("%s [Error NUMBER = %d]\n",msg,errno);
00058 PressAKey();
00059 return EXIT_FAILURE;
00060 }
00065 void CreateASecureDirectory()
00066 {
00067 int result;
00068
00074 result = mkdir("mydirectory",S_IWUSR);
00075 if(result == 0)
00076 {
00077 printf(" A secure directory for the program is created successfully\n");
00078 PressAKey();
00079 }
00080 else
00081 {
00082 DisplayErrorMessage("\nError in creating directory\n");
00083 PressAKey();
00084 }
00085 }
00090 void WriteDataToAFile(char FileName[])
00091 {
00092 FILE* fp= NULL;
00093
00099 fp = fopen(FileName, "w");
00100 if (fp== NULL)
00101 {
00102 DisplayErrorMessage("\nError in writing data in to the file=%d");
00103 PressAKey();
00104 }
00105 else
00106 {
00107 fprintf(fp,"%s","hello");
00108 fflush(fp);
00109 fclose(fp);
00110 printf(" Writing data in to the file from the secure directory is successful");
00111 }
00112 }
00113
00118 void ReadDataFromAFile(char FileName[])
00119 {
00120 FILE* fp= NULL;
00121
00127 fp = fopen(FileName, "r");
00128 if (fp == NULL)
00129 {
00130 DisplayErrorMessage("\nError in reading data from the file = %d");
00131 printf(" Press any key to exit from the example");
00132 PressAKey();
00133 }
00134 else
00135 {
00136 char x[30];
00137
00138
00139 fseek(fp, SEEK_SET, 0);
00140
00141
00142 fscanf(fp,"%s",x);
00143 printf("\n Data present in the file is : %s",x);
00144 fclose(fp);
00145 printf("\n Reading data of the file from the directory is successful\n");
00146 }
00147 }
00152 void RemoveADirectory(char FileName[])
00153 {
00154 int result;
00155
00161 result = remove(FileName);
00162 if(result == 0)
00163 {
00164 printf(" Removing the file from the directory is successful\n");
00165 }
00166 else
00167 {
00168 DisplayErrorMessage(" Error in removing the file from the directory=%d");
00169 PressAKey();
00170 }
00176 result = rmdir("mydirectory");
00177
00178 if(result == 0)
00179 {
00180 printf(" Removing the the directory is successful\n");
00181 PressAKey();
00182 }
00183 else
00184 {
00185 DisplayErrorMessage(" Error in removing the directory=%d");
00186 PressAKey();
00187 }
00188 }
00193 void WriteDataToFileOfAnotherProgram(char FileName[])
00194 {
00195 FILE* fp= NULL;
00196
00202 fp = fopen(FileName, "w");
00203
00204 if (fp == NULL)
00205 {
00206 printf(" Writing data to files in a private directory belonging to another program is not possible");
00207 }
00208 else
00209 {
00210 printf("\n Writing data to files in a private directory belonging to another program is possible");
00211 PressAKey();
00212 }
00213 }
00218 void ReadDataFromFileOfAnotherProgram(char FileName[])
00219 {
00220 FILE* fp= NULL;
00221
00227 fp = fopen(FileName, "r");
00228
00229 if (fp == NULL)
00230 {
00231 printf("\n Reading data from files in a private directory belonging to another program is not possible");
00232 PressAKey();
00233 }
00234 else
00235 {
00236 printf("\n Reading data from files in a private directory belonging to another program is possible");
00237 PressAKey();
00238 }
00239 }
00244 void WriteDataToFileOfSysBin(char FileName[])
00245 {
00246 FILE* fp= NULL;
00247
00253 fp = fopen(FileName, "w");
00254
00255 if (fp == NULL)
00256 {
00257 printf("\n Writing data to files in the sys/bin directory is not possible");
00258 }
00259 else
00260 {
00261 printf("\n Writing data to files in the sys/bin directory is possible");
00262 PressAKey();
00263 }
00264 }
00269 void ReadDataToFileOfSysBin(char FileName[])
00270 {
00271 FILE* fp = NULL;
00272
00278 fp = fopen(FileName, "r");
00279
00280 if (fp == NULL)
00281 {
00282 printf("\n Reading data from files in the sys/bin directory is not possible");
00283 PressAKey();
00284 }
00285 else
00286 {
00287 printf("\n Reading data from files in the sys/bin directory is possible");
00288 printf(" Press any key to exit from the example");
00289 PressAKey();
00290 }
00291 }
00292
00293 int main()
00294 {
00295 char ownFileName[50] = "\\private\\e80000c9\\mydirectory\\myownfile.txt";
00296 char otherFileName[40] = "\\private\\e80000c8\\otherfile.txt";
00297 char sysBinFileName[30] = "\\sys\\bin\\euser.dll";
00298
00299 printf("\n Welcome to the file access example using POSIX APIs\n");
00300
00301
00302 printf("\n Press enter key to step through the example\n");
00303 PressAKey();
00304
00305
00306 CreateASecureDirectory();
00307
00308
00309 WriteDataToAFile(ownFileName);
00310
00311
00312 ReadDataFromAFile(ownFileName);
00313
00314
00315 RemoveADirectory(ownFileName);
00316
00317
00318 WriteDataToFileOfAnotherProgram(otherFileName);
00319
00320
00321 ReadDataFromFileOfAnotherProgram(otherFileName);
00322
00323
00324 WriteDataToFileOfSysBin(sysBinFileName);
00325
00326
00327 ReadDataToFileOfSysBin(sysBinFileName);
00328
00329
00330 printf("\n Press enter key to exit from the example application");
00331 PressAKey();
00332
00333
00334 return EXIT_SUCCESS;
00335 }
00336
00337