These examples can help you program your optical file system using the integrated file system.
This topic demonstrates the use of the integrated file system UNIX-type APIs that pertain to the QOPT physical file system and are used with the ILE C for i5/OS™ programming language.
For more information about UNIX-Type APIs, see the UNIX-Type APIs topic in the iSeries™ Information Center.
/**********************************************************************/ /* */ /* This program demonstrates the use of various integrated file */ /* system functions applied to the QOPT physical file system */ /* including: */ /* chdir() - change current directory */ /* close() - close file */ /* closedir() - close directory */ /* creat() - create file */ /* lseek() - seek file (change file offset) */ /* open() - open file */ /* opendir() - open directory */ /* read() - read file */ /* readdir() - read directory entry */ /* rewinddir() - rewind directory entries */ /* stat() - directory statistics */ /* write() - write file */ /* */ /**********************************************************************/ #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> #include <fcntl.h> void main (void) { /*****************************************************************/ /* local variables, contents and defines */ /*****************************************************************/ char path[294]; /* optical path */ DIR *dirP; /* pointer to the directory */ int filedes; /* open file descriptor */ struct dirent *direntP; /* directory entry structure */ struct stat info; /* dir/file information */ int volume_number; /* what it says... */ int rc = 0; /* function return codes */ int kk = 0; /* local counter */ char data[] = "The quick red fox jumped over the fence"; /*****************************************************************/ /* Retrieve the list of volumes from the QOPT physical file */ /* system by opening the QOPT pfs root directory and reading the */ /* directory entries. */ /*****************************************************************/ memset(path, /* clear path name */ 0x00, sizeof(path)); strcpy(path, /* set physical file system */ "/QOPT"); rc = stat("/QOPT", &info);; /* determine number of files */ if (rc != 0) perror("stat() failed:"); dirP = opendir(path); /* open the directory */ if (dirP == NULL) perror("opendir() failed:"); for (kk = 1; kk <= info.st_nlink; kk++) { direntP = readdir(dirP); if (direntP == NULL) perror("readdir() failed:"); printf("%d) %s\n", kk, direntP->d_name); } /*****************************************************************/ /* Prompt user for the volume they want to work with and make it */ /* the current directory. */ /*****************************************************************/ printf("\nEnter the number the volume you want to work with:\n"); scanf("%d", &volume_number);; rewinddir(dirP); /* beginning of directory */ for (kk = 1; kk <= volume_number; kk++) direntP = readdir(dirP); /* get requested dir. entry */ strcat(path, "/"); strcat(path, direntP->d_name); rc = chdir(path); /* set current working dir. */ if (rc != 0) perror("chdir() failed:"); if (getcwd(path, sizeof(path)) == NULL) perror("getcwd() failed:"); printf("\nThe current working directory is: %s\n", path); rc = closedir(dirP); /* close the directory */ if (rc != 0) perror("closedir() failed:"); /*****************************************************************/ /* Create and open a file write only. If the file exists it */ /* will be truncated. The owner will have read, write, and */ /* execute authority to the file. */ /*****************************************************************/ strcat(path, "/"); printf("\nEnter a file name:\n"); scanf("%s", &path[strlen(path)]); filedes = creat(path, S_IRWXU); if (filedes == -1) { perror("creat() failed"); return; } rc = write(filedes, data, sizeof(data)); if (rc == -1) perror("write() failed:"); close(filedes); /*****************************************************************/ /* Read back the file and print it. */ /*****************************************************************/ memset(data, 0x00, sizeof(data)); filedes = open(path, O_RDWR); if (filedes == -1) { perror("open() failed"); return; } read(filedes, data, sizeof(data)); if (filedes == -1) { perror("read() failed"); return; } printf("\nThe data written to file is: %s\n", data); /*****************************************************************/ /* Change the offset into the file and change part of it. Read */ /* the entire file, print it out and close the file. */ /*****************************************************************/ lseek(filedes, 4, SEEK_SET); rc = write(filedes, "slow old ", 9); if (rc == -1) { perror("write() failed"); return; } lseek(filedes, 18, SEEK_SET); rc = write(filedes, "went under ", 11); if (rc == -1) { perror("write() failed"); return; } lseek(filedes, 0, SEEK_SET); read(filedes, data, sizeof(data)); if (filedes == -1) { perror("read() failed"); return; } printf("\nThe data now is: %s\n", data); close(filedes); printf("Done...\n"); return; }