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