int main ()
{
1
/ᑍ Get and print the real user id with the getuid() function. ᑍ/
UserID = getuid();
printf("The real user id is %u. \n",UserID);
2
/ᑍ Get the current working directory and store it in InitialDirectory. ᑍ/
if ( NULL == getcwd(InitialDirectory,BUFFER_SIZE) )
{
perror("getcwd Error");
CleanUpOnError(1);
return ;
}
printf("The current working directory is %s. \n",InitialDirectory);
3
/ᑍ Create the file TEST_FILE for writing, if it does not exist.
Give the owner authority to read, write, and execute. ᑍ/
FilDes = open(TEST_FILE, O_WRONLY | O_CREAT | O_EXCL, S_IRWXU);
if ( -1 == FilDes )
{
perror("open Error");
CleanUpOnError(2);
return ;
}
printf("Created %s in directory %s.\n",TEST_FILE,InitialDirectory);
4
/ᑍ Write TEST_DATA to TEST_FILE via FilDes ᑍ/
BytesWritten = write(FilDes,TEST_DATA,strlen(TEST_DATA));
if ( -1 == BytesWritten )
{
perror("write Error");
CleanUpOnError(3);
return ;
}
printf("Wrote %s to file %s.\n",TEST_DATA,TEST_FILE);
5
/ᑍ Close TEST_FILE via FilDes ᑍ/
if ( -1 == close(FilDes) )
{
perror("close Error");
CleanUpOnError(4);
return ;
}
FilDes = -1;
printf("File %s closed.\n",TEST_FILE);
6
/ᑍ Open the TEST_FILE file for reading only. ᑍ/
if ( -1 == (FilDes = open(TEST_FILE,O_RDONLY)) )
{
Appendix B. Original Examples in Additional Languages B-177