Reformat code
This commit is contained in:
115
chp4/blocks.c
115
chp4/blocks.c
@@ -1,10 +1,10 @@
|
||||
#include <fcntl.h>
|
||||
#include <linux/fs.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/fs.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
/* get_block - for the file associated with the given fd, returns
|
||||
* the physical block mapping to logical_block
|
||||
@@ -22,66 +22,71 @@ int get_nr_blocks(int fd);
|
||||
*/
|
||||
void print_blocks(int fd);
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "usage: %s <file>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "usage: %s <file>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int fd = open(argv[1], O_RDONLY);
|
||||
if (fd < 0) {
|
||||
perror("open");
|
||||
return 1;
|
||||
}
|
||||
int fd = open(argv[1], O_RDONLY);
|
||||
if (fd < 0) {
|
||||
perror("open");
|
||||
return 1;
|
||||
}
|
||||
|
||||
print_blocks(fd);
|
||||
return 0;
|
||||
print_blocks(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int get_block(int fd, int logical_block) {
|
||||
int ret = ioctl(fd, FIBMAP, &logical_block);
|
||||
if (ret < 0) {
|
||||
perror("ioctl");
|
||||
return -1;
|
||||
}
|
||||
return logical_block;
|
||||
int get_block(int fd, int logical_block)
|
||||
{
|
||||
int ret = ioctl(fd, FIBMAP, &logical_block);
|
||||
if (ret < 0) {
|
||||
perror("ioctl");
|
||||
return -1;
|
||||
}
|
||||
return logical_block;
|
||||
}
|
||||
|
||||
int get_nr_blocks(int fd) {
|
||||
struct stat buf;
|
||||
int ret = fstat(fd, &buf);
|
||||
if (ret < 0) {
|
||||
perror("fstat");
|
||||
return -1;
|
||||
}
|
||||
return buf.st_blocks;
|
||||
int get_nr_blocks(int fd)
|
||||
{
|
||||
struct stat buf;
|
||||
int ret = fstat(fd, &buf);
|
||||
if (ret < 0) {
|
||||
perror("fstat");
|
||||
return -1;
|
||||
}
|
||||
return buf.st_blocks;
|
||||
}
|
||||
|
||||
void print_blocks(int fd) {
|
||||
int nr_blocks = get_nr_blocks(fd);
|
||||
if (nr_blocks < 0) {
|
||||
fprintf(stderr, "get_nr_blocks failed!\n");
|
||||
return;
|
||||
}
|
||||
void print_blocks(int fd)
|
||||
{
|
||||
int nr_blocks = get_nr_blocks(fd);
|
||||
if (nr_blocks < 0) {
|
||||
fprintf(stderr, "get_nr_blocks failed!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (nr_blocks == 0) {
|
||||
printf("no allocated blocks\n");
|
||||
return;
|
||||
} else if (nr_blocks == 1) {
|
||||
printf("1 block\n");
|
||||
} else {
|
||||
printf("%d blocks\n\n", nr_blocks);
|
||||
}
|
||||
if (nr_blocks == 0) {
|
||||
printf("no allocated blocks\n");
|
||||
return;
|
||||
} else if (nr_blocks == 1) {
|
||||
printf("1 block\n");
|
||||
} else {
|
||||
printf("%d blocks\n\n", nr_blocks);
|
||||
}
|
||||
|
||||
for (int i = 0; i < nr_blocks; i++) {
|
||||
int phys_block = get_block(fd, i);
|
||||
if (phys_block < 0) {
|
||||
fprintf(stderr, "get_block failed!\n");
|
||||
return;
|
||||
}
|
||||
if (!phys_block) continue;
|
||||
for (int i = 0; i < nr_blocks; i++) {
|
||||
int phys_block = get_block(fd, i);
|
||||
if (phys_block < 0) {
|
||||
fprintf(stderr, "get_block failed!\n");
|
||||
return;
|
||||
}
|
||||
if (!phys_block)
|
||||
continue;
|
||||
|
||||
printf("(%u, %u) ", i, phys_block);
|
||||
}
|
||||
putchar('\n');
|
||||
printf("(%u, %u) ", i, phys_block);
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
50
chp4/inode.c
50
chp4/inode.c
@@ -1,39 +1,41 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
/* get_inode - returns the inode of the file associated
|
||||
* with the given file descriptor, or -1 on failure.
|
||||
*/
|
||||
int get_inode(int fd);
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "usage: %s <file>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "usage: %s <file>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int fd = open(argv[1], O_RDONLY);
|
||||
if (fd < 0) {
|
||||
perror("open");
|
||||
return 1;
|
||||
}
|
||||
int fd = open(argv[1], O_RDONLY);
|
||||
if (fd < 0) {
|
||||
perror("open");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int inode = get_inode(fd);
|
||||
printf("%d\n", inode);
|
||||
|
||||
return 0;
|
||||
int inode = get_inode(fd);
|
||||
printf("%d\n", inode);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int get_inode(int fd) {
|
||||
struct stat buf;
|
||||
int get_inode(int fd)
|
||||
{
|
||||
struct stat buf;
|
||||
|
||||
int ret = fstat(fd, &buf);
|
||||
if (ret < 0) {
|
||||
perror("fstat");
|
||||
return -1;
|
||||
}
|
||||
return buf.st_ino;
|
||||
int ret = fstat(fd, &buf);
|
||||
if (ret < 0) {
|
||||
perror("fstat");
|
||||
return -1;
|
||||
}
|
||||
return buf.st_ino;
|
||||
}
|
||||
|
||||
13
chp4/mmap.c
13
chp4/mmap.c
@@ -1,12 +1,13 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "usage: %s <file>\n", argv[0]);
|
||||
return 1;
|
||||
|
||||
63
chp4/readv.c
63
chp4/readv.c
@@ -1,41 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void) {
|
||||
char foo[48], bar[51], baz[49];
|
||||
int fd = open("buccaneer.txt", O_RDONLY);
|
||||
if (fd == -1) {
|
||||
perror("open");
|
||||
return 1;
|
||||
}
|
||||
int main(void)
|
||||
{
|
||||
char foo[48], bar[51], baz[49];
|
||||
int fd = open("buccaneer.txt", O_RDONLY);
|
||||
if (fd == -1) {
|
||||
perror("open");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// setup our iovec structures
|
||||
struct iovec iov[3];
|
||||
iov[0].iov_base = foo;
|
||||
iov[0].iov_len = sizeof(foo);
|
||||
iov[1].iov_base = bar;
|
||||
iov[1].iov_len = sizeof(bar);
|
||||
iov[2].iov_base = baz;
|
||||
iov[2].iov_len = sizeof(baz);
|
||||
// setup our iovec structures
|
||||
struct iovec iov[3];
|
||||
iov[0].iov_base = foo;
|
||||
iov[0].iov_len = sizeof(foo);
|
||||
iov[1].iov_base = bar;
|
||||
iov[1].iov_len = sizeof(bar);
|
||||
iov[2].iov_base = baz;
|
||||
iov[2].iov_len = sizeof(baz);
|
||||
|
||||
// read into the structures with a single call
|
||||
int nr = readv(fd, iov, 3);
|
||||
if (nr == -1) {
|
||||
perror("readv");
|
||||
return 1;
|
||||
}
|
||||
// read into the structures with a single call
|
||||
int nr = readv(fd, iov, 3);
|
||||
if (nr == -1) {
|
||||
perror("readv");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
printf("%d: %s", i, (char *)iov[i].iov_base);
|
||||
for (int i = 0; i < 3; i++)
|
||||
printf("%d: %s", i, (char *)iov[i].iov_base);
|
||||
|
||||
if (close(fd)) {
|
||||
perror("close");
|
||||
return 1;
|
||||
}
|
||||
if (close(fd)) {
|
||||
perror("close");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,45 +1,44 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void) {
|
||||
struct iovec iov[3];
|
||||
int main(void)
|
||||
{
|
||||
struct iovec iov[3];
|
||||
|
||||
char *buf[] = {
|
||||
"The term buccaneer comes from the word boucan.\n",
|
||||
"A boucan is a wooden frame used for cooking meet.\n",
|
||||
"Buccaneer is the West Indies name for a pirate.\n"
|
||||
};
|
||||
char *buf[] = {"The term buccaneer comes from the word boucan.\n",
|
||||
"A boucan is a wooden frame used for cooking meet.\n",
|
||||
"Buccaneer is the West Indies name for a pirate.\n"};
|
||||
|
||||
int fd = open("buccaneer.txt", O_WRONLY | O_CREAT | O_TRUNC);
|
||||
if (fd == -1) {
|
||||
perror("open");
|
||||
return 1;
|
||||
}
|
||||
int fd = open("buccaneer.txt", O_WRONLY | O_CREAT | O_TRUNC);
|
||||
if (fd == -1) {
|
||||
perror("open");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// fill out three iovec structures
|
||||
for (int i = 0; i < 3; i++) {
|
||||
iov[i].iov_base = buf[i];
|
||||
iov[i].iov_len = strlen(buf[i]) + 1;
|
||||
}
|
||||
// fill out three iovec structures
|
||||
for (int i = 0; i < 3; i++) {
|
||||
iov[i].iov_base = buf[i];
|
||||
iov[i].iov_len = strlen(buf[i]) + 1;
|
||||
}
|
||||
|
||||
// with a single call, write them all out
|
||||
ssize_t nr = writev(fd, iov, 3);
|
||||
if (nr == -1) {
|
||||
perror("writev");
|
||||
return 1;
|
||||
}
|
||||
// with a single call, write them all out
|
||||
ssize_t nr = writev(fd, iov, 3);
|
||||
if (nr == -1) {
|
||||
perror("writev");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("wrote %lu bytes\n", nr);
|
||||
printf("wrote %lu bytes\n", nr);
|
||||
|
||||
if (close(fd)) {
|
||||
perror("close");
|
||||
return 1;
|
||||
}
|
||||
if (close(fd)) {
|
||||
perror("close");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user