Open System Services Programmer's Guide

Example 48 Using the pax Utility From a Program
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
FILE *fpout;
#define MAXLINE 132
char paxbuf[MAXLINE];
char line[MAXLINE];
int main (int argc, char *argv[]) {
/* The program is called with name of a backup (archive) file.
Make sure the correct number of arguments are entered.
If not, print a usage message and exit.
*/
if(argc < 2) {
fprintf(stderr, "Usage: paxopen <backup file>\n");
exit(1);
}
/* Create a string for popen() with the necessary arguments
to the pax command to use it as a filter. */
sprintf(paxbuf, "pax -w -f %s", argv[1]);
/* Open the pax utility. */
if((fpout = popen(paxbuf, "w")) == NULL) {
fprintf(stderr, "popen error on %s\n", paxbuf);
exit(1);
}
/* Copy the names of the files to be backed up to the pax filter. */
for(;;) {
fprintf(stderr, "File to back up (^Y for end of list): ");
if(fgets(line, MAXLINE, stdin) == NULL)
break;
if(fputs(line, fpout) == EOF) {
fprintf(stderr, "fputs error to pipe\n");
exit(1);
}
}
/* Check for errors on input processing. */
if(ferror(stdin)) {
fprintf(stderr, "fgets error\n");
exit(1);
}
/* Check for errors in the execution of the pax command. */
if(pclose(fpout) == -1) {
fprintf(stderr, "pclose error\n");
exit(1);
}
printf("Backup file %s created\n", argv[1]);
exit(0);
}
Tape I/O With the Guardian API
To use a programmatic interface to tape or to use labeled tapes, you must use Guardian procedures.
Labeled tapes have the following advantages over unlabeled tapes:
A labeled tape contains information about the contents of the tape that prevent undesired
overwriting of the tape.
Automatic cartridge systems use only labeled tapes.
Tape I/O 209