Reformat code

This commit is contained in:
Fabio Scotto di Santolo
2025-07-04 10:39:07 +02:00
parent 2bea38d7fc
commit dd53e83af6
28 changed files with 632 additions and 524 deletions

View File

@@ -1,7 +1,7 @@
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
@@ -20,7 +20,7 @@ int main(void)
if (fd == -1)
perror("Third open");
close(fd);
fd = open("test.txt", O_CREAT);
if (fd == -1)
perror("Fourth open");

View File

@@ -1,12 +1,13 @@
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int fd;
fd = open("test.txt", O_CREAT | O_EXCL, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
fd = open("test.txt", O_CREAT | O_EXCL,
S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
if (fd == -1)
perror("open");
close(fd);

View File

@@ -6,51 +6,56 @@
#define _XOPEN_SOURCE 500
#include <unistd.h>
int main(void) {
printf("Try to open file read.txt\n");
int fd = open("read.txt", O_RDONLY);
if (fd == -1) {
perror("Failed to open read.txt");
return -1;
}
printf("Open file success\n");
int main(void)
{
printf("Try to open file read.txt\n");
int fd = open("read.txt", O_RDONLY);
if (fd == -1) {
perror("Failed to open read.txt");
return -1;
}
printf("Open file success\n");
int len;
// move file's cursor at the end of file and return bytes
if ((len = lseek(fd, 0, SEEK_END)) == -1) {
perror("Failed to move cursor at end of read.txt file");
return -1;
}
int len;
// move file's cursor at the end of file and return bytes
if ((len = lseek(fd, 0, SEEK_END)) == -1) {
perror("Failed to move cursor at end of read.txt file");
return -1;
}
// cannot rewind cursor at the start file because pread use "bytes_read_total" for position
// cannot rewind cursor at the start file because pread use
// "bytes_read_total" for position
printf("File size of %d bytes\n", len);
printf("File size of %d bytes\n", len);
printf("Reading file read.txt\n");
char buf[len + 1];
memset(buf, 0, sizeof(buf));
printf("Reading file read.txt\n");
char buf[len + 1];
memset(buf, 0, sizeof(buf));
size_t bytes_read_total = 0;
ssize_t ret;
while (bytes_read_total < len && (ret = pread(fd, buf, len - bytes_read_total, bytes_read_total)) != -1) {
if (ret == -1) {
if (errno == EINTR) continue;
perror("Failed to read read.txt");
break;
}
bytes_read_total += ret;
}
size_t bytes_read_total = 0;
ssize_t ret;
while (bytes_read_total < len &&
(ret = pread(fd, buf, len - bytes_read_total,
bytes_read_total)) != -1) {
if (ret == -1) {
if (errno == EINTR)
continue;
perror("Failed to read read.txt");
break;
}
bytes_read_total += ret;
}
buf[bytes_read_total] = '\0';
buf[bytes_read_total] = '\0';
printf("File text is:\n\"%s\"\n", buf);
printf("File text is:\n\"%s\"\n", buf);
printf("Close file read.txt\n");
if (close(fd) == -1) {
perror("Failed to close read.txt");
return -1;
}
printf("Close file read.txt\n");
return 0;
if (close(fd) == -1) {
perror("Failed to close read.txt");
return -1;
}
return 0;
}

View File

@@ -5,51 +5,55 @@
#include <string.h>
#include <unistd.h>
int main(void) {
printf("Try to open file read.txt\n");
int fd = open("read.txt", O_RDONLY);
if (fd == -1) {
perror("Failed to open read.txt");
return -1;
}
printf("Open file success\n");
int main(void)
{
printf("Try to open file read.txt\n");
int fd = open("read.txt", O_RDONLY);
if (fd == -1) {
perror("Failed to open read.txt");
return -1;
}
printf("Open file success\n");
int len;
if ((len = lseek(fd, 0, SEEK_END)) == -1) {
perror("Failed to move cursor at end of read.txt file");
return -1;
}
printf("File size of %d bytes\n", len);
if (lseek(fd, 0, SEEK_SET) == -1) {
perror("Failed to move cursor at start of read.txt file");
return -1;
}
int len;
if ((len = lseek(fd, 0, SEEK_END)) == -1) {
perror("Failed to move cursor at end of read.txt file");
return -1;
}
printf("File size of %d bytes\n", len);
if (lseek(fd, 0, SEEK_SET) == -1) {
perror("Failed to move cursor at start of read.txt file");
return -1;
}
printf("Reading file read.txt\n");
char buf[len + 1];
memset(buf, 0, sizeof(buf));
printf("Reading file read.txt\n");
char buf[len + 1];
memset(buf, 0, sizeof(buf));
size_t bytes_read_total = 0;
ssize_t ret;
while (bytes_read_total < len && (ret = read(fd, buf + bytes_read_total, len - bytes_read_total)) != 0) {
if (ret == -1) {
if (errno == EINTR) continue;
perror("Failed to read read.txt");
break;
}
bytes_read_total += ret;
}
size_t bytes_read_total = 0;
ssize_t ret;
while (bytes_read_total < len &&
(ret = read(fd, buf + bytes_read_total,
len - bytes_read_total)) != 0) {
if (ret == -1) {
if (errno == EINTR)
continue;
perror("Failed to read read.txt");
break;
}
bytes_read_total += ret;
}
buf[bytes_read_total] = '\0';
buf[bytes_read_total] = '\0';
printf("File text is:\n\"%s\"\n", buf);
printf("File text is:\n\"%s\"\n", buf);
printf("Close file read.txt\n");
if (close(fd) == -1) {
perror("Failed to close read.txt");
return -1;
}
printf("Close file read.txt\n");
return 0;
if (close(fd) == -1) {
perror("Failed to close read.txt");
return -1;
}
return 0;
}

View File

@@ -1,7 +1,8 @@
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
int main(void) {
int main(void)
{
int ret;
ret = truncate("./pirate.txt", 45);

View File

@@ -1,10 +1,11 @@
#include <poll.h>
#include <stdio.h>
#include <unistd.h>
#include <poll.h>
#define TIMEOUT 5 /* poll timeout, in seconds */
#define TIMEOUT 5 /* poll timeout, in seconds */
int main(void) {
int main(void)
{
struct pollfd fds[2];
int ret;
@@ -30,7 +31,7 @@ int main(void) {
if (fds[0].revents & POLLIN)
printf("stdin is readable\n");
if (fds[1].revents & POLLOUT)
printf("stdout is writable\n");

View File

@@ -6,7 +6,8 @@
#define TIMEOUT 5
#define BUF_LEN 1024
int main(void) {
int main(void)
{
struct timeval tv;
fd_set readfds;
int ret;
@@ -14,19 +15,13 @@ int main(void) {
/* Wait on stdin for intput. */
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
/* Wait up to file seconds */
tv.tv_sec = TIMEOUT;
tv.tv_usec = 0;
/* All right, now block! */
ret = select(
STDIN_FILENO + 1,
&readfds,
NULL,
NULL,
&tv
);
ret = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv);
if (ret == -1) {
perror("select");
return 1;
@@ -42,7 +37,7 @@ int main(void) {
* nonzero, but we will humor ourselves.)
*/
if (FD_ISSET(STDIN_FILENO, &readfds)) {
char buf[BUF_LEN+1];
char buf[BUF_LEN + 1];
int len;
/* guaranteed to not block */
@@ -56,7 +51,7 @@ int main(void) {
buf[len] = '\0';
printf("read: %s\n", buf);
}
return 0;
}

View File

@@ -1,11 +1,11 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#define FILE_NUMBER 3
#define FILENAME_MAX_LEN 10
@@ -15,7 +15,8 @@
int create_tmp_file(char *filename);
void cleanup(char *filenames[]);
int main(void) {
int main(void)
{
int exit_code = 0;
char *filename_template = "test%d.txt";
char *filenames[FILE_NUMBER] = {NULL};
@@ -23,48 +24,47 @@ int main(void) {
// Create a bunch of number ephemeral files
for (int i = 0; i < FILE_NUMBER; i++) {
char *filename = (char *)malloc(FILENAME_MAX_LEN*sizeof(char));
char *filename =
(char *)malloc(FILENAME_MAX_LEN * sizeof(char));
if (!filename) {
perror("malloc failed");
exit_code = -1;
goto exit;
}
snprintf(filename, FILENAME_MAX_LEN, filename_template, i+1);
snprintf(filename, FILENAME_MAX_LEN, filename_template, i + 1);
filenames[i] = filename;
printf("Opening file %s...\n", filename);
fds[i] = create_tmp_file(filename);
if (fds[i] == -1) {
fprintf(stderr, "Error to open file %s: %s\n", filename, strerror(errno));
fprintf(stderr, "Error to open file %s: %s\n", filename,
strerror(errno));
exit_code = -1;
goto exit;
}
printf("Open file %s with file descriptor %d\n", filename, fds[i]);
printf("Open file %s with file descriptor %d\n", filename,
fds[i]);
// write something in the files
dprintf(fds[i], "Hello I am %s\n", filename);
}
fd_set readfds;
FD_ZERO(&readfds);
for (int i = 0; i < FILE_NUMBER; i++) {
FD_SET(fds[i], &readfds);
}
struct timeval *timeout = (struct timeval *)malloc(sizeof(struct timeval));
struct timeval *timeout =
(struct timeval *)malloc(sizeof(struct timeval));
timeout->tv_sec = TIMEOUT;
timeout->tv_usec = 0;
/* This function do not work with regular files because for the operating system,
* a regular file is ready always.
* This example is wrong the best way to use select() is with socket, pipe or other channel types.
/* This function do not work with regular files because for the
* operating system, a regular file is ready always. This example is
* wrong the best way to use select() is with socket, pipe or other
* channel types.
*/
int ret = select(
FILE_NUMBER + 1,
&readfds,
NULL,
NULL,
timeout
);
int ret = select(FILE_NUMBER + 1, &readfds, NULL, NULL, timeout);
if (ret == -1) {
fprintf(stderr, "Error to select: %s\n", strerror(errno));
@@ -79,10 +79,11 @@ int main(void) {
for (int i = 0; i < FILE_NUMBER; i++) {
int fd = fds[i];
if (FD_ISSET(fd, &readfds)) {
char buf[BUF_LEN+1];
char buf[BUF_LEN + 1];
int len = read(fd, buf, BUF_LEN);
if (len == -1) {
fprintf(stderr, "Error reading file %s: %s\n", filenames[i], strerror(errno));
fprintf(stderr, "Error reading file %s: %s\n",
filenames[i], strerror(errno));
exit_code = 1;
goto exit;
}
@@ -98,8 +99,9 @@ exit:
for (int i = 0; i < FILE_NUMBER; i++) {
int fd = fds[i];
printf("Close file with file descriptor %d\n", fd);
if (close(fd) == -1) {
fprintf(stderr, "Error to close file %d: %s\n", fd, strerror(errno));
if (close(fd) == -1) {
fprintf(stderr, "Error to close file %d: %s\n", fd,
strerror(errno));
}
}
@@ -108,11 +110,12 @@ exit:
printf("Cleanup files\n");
cleanup(filenames);
return exit_code;
}
int create_tmp_file(char *filename) {
int create_tmp_file(char *filename)
{
int fd = open(filename, O_CREAT | O_EXCL | O_RDWR, 0644);
if (fd == -1) {
return -1;
@@ -120,11 +123,13 @@ int create_tmp_file(char *filename) {
return fd;
}
void cleanup(char *filenames[]) {
void cleanup(char *filenames[])
{
for (int j = 0; j < FILE_NUMBER; j++) {
if (filenames[j] != NULL) {
if (remove(filenames[j]) == -1)
fprintf(stderr, "Failed to remove %s: %s\n", filenames[j], strerror(errno));
if (remove(filenames[j]) == -1)
fprintf(stderr, "Failed to remove %s: %s\n",
filenames[j], strerror(errno));
free(filenames[j]);
filenames[j] = NULL;
}