{
char *fname, *commands;
struct stat st;
- int fd;
+ int fd = -1;
if (argc != 3) {
fprintf(stderr, usage, argv[0]);
for (commands = argv[2]; *commands; commands++) {
switch (*commands) {
- case 'o':
- fd = open(fname, O_RDONLY);
- if (fd == -1) {
- perror("open(O_RDONLY)");
- exit(1);
- }
+ case '_':
+ pause();
break;
- case 'O':
- fd = open(fname, O_CREAT|O_RDWR, 0644);
- if (fd == -1) {
- perror("open(O_RDWR|O_CREAT");
+ case 'c':
+ if (close(fd) == -1) {
+ perror("close");
exit(1);
}
+ fd = -1;
break;
case 'm':
if (mknod(fname, S_IFREG | 0644, 0) == -1) {
exit(1);
}
break;
- case 'u':
- if (unlink(fname) == -1) {
- perror("unlink");
+ case 'O':
+ fd = open(fname, O_CREAT|O_RDWR, 0644);
+ if (fd == -1) {
+ perror("open(O_RDWR|O_CREAT");
exit(1);
}
break;
- case 'c':
- if (close(fd) == -1) {
- perror("close");
+ case 'o':
+ fd = open(fname, O_RDONLY);
+ if (fd == -1) {
+ perror("open(O_RDONLY)");
exit(1);
}
break;
- case '_':
- pause();
+ case 'r': {
+ char buf;
+ if (read(fd, &buf, 1) == -1) {
+ perror("read");
+ exit(1);
+ }
+ }
+ case 'S':
+ if (fstat(fd, &st) == -1) {
+ perror("fstat");
+ exit(1);
+ }
break;
case 's':
if (stat(fname, &st) == -1) {
exit(1);
}
break;
- case 'S':
- if (fstat(fd, &st) == -1) {
- perror("fstat");
+ case 'u':
+ if (unlink(fname) == -1) {
+ perror("unlink");
exit(1);
}
break;
+ case 'w':
+ if (write(fd, "w", 1) == -1) {
+ perror("write");
+ exit(1);
+ }
+ break;
+ case 'z':
+ if (lseek(fd, 0, SEEK_SET) == -1) {
+ perror("lseek");
+ exit(1);
+ }
+ break;
default:
fprintf(stderr, "unknown command \"%c\"\n", *commands);
fprintf(stderr, usage, argv[0]);