Reformat code
This commit is contained in:
@@ -1,69 +1,71 @@
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define BUFF_LEN 4096
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// Check number of arguments
|
||||
if (argc < 3) {
|
||||
fprintf(stderr, "Usage: %s SOURCE DEST\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// Check number of arguments
|
||||
if (argc < 3) {
|
||||
fprintf(stderr, "Usage: %s SOURCE DEST\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
// Get arguments
|
||||
const char *src = argv[1];
|
||||
const char *dst = argv[2];
|
||||
|
||||
// Get arguments
|
||||
const char *src = argv[1];
|
||||
const char *dst = argv[2];
|
||||
// Open source file in read only mode
|
||||
int srcfd = open(src, O_RDONLY);
|
||||
if (srcfd == -1) {
|
||||
perror("Failed open source file");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Open source file in read only mode
|
||||
int srcfd = open(src, O_RDONLY);
|
||||
if (srcfd == -1) {
|
||||
perror("Failed open source file");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Open destination file in write mode, if exists trunc file otherwise create a new file
|
||||
int dstfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (dstfd == -1) {
|
||||
perror("Failed open destination file");
|
||||
if (close(srcfd) == -1) {
|
||||
perror("Failed close source file");
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
// Open destination file in write mode, if exists trunc file otherwise
|
||||
// create a new file
|
||||
int dstfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (dstfd == -1) {
|
||||
perror("Failed open destination file");
|
||||
if (close(srcfd) == -1) {
|
||||
perror("Failed close source file");
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int exit_code = EXIT_SUCCESS;
|
||||
char buff[BUFF_LEN]; // reading data buffer
|
||||
ssize_t read_bytes;
|
||||
while ((read_bytes = read(srcfd, buff, BUFF_LEN)) != 0) {
|
||||
if (read_bytes == -1) {
|
||||
// There is an error for reading source file
|
||||
if (errno == EINTR) continue;
|
||||
perror("Failed to read source file");
|
||||
exit_code = EXIT_FAILURE;
|
||||
break;
|
||||
}
|
||||
int exit_code = EXIT_SUCCESS;
|
||||
char buff[BUFF_LEN]; // reading data buffer
|
||||
ssize_t read_bytes;
|
||||
while ((read_bytes = read(srcfd, buff, BUFF_LEN)) != 0) {
|
||||
if (read_bytes == -1) {
|
||||
// There is an error for reading source file
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
perror("Failed to read source file");
|
||||
exit_code = EXIT_FAILURE;
|
||||
break;
|
||||
}
|
||||
|
||||
// Write buffer at destination file
|
||||
if (write(dstfd, buff, read_bytes) == -1) {
|
||||
perror("Error to write to destination file");
|
||||
exit_code = EXIT_FAILURE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Write buffer at destination file
|
||||
if (write(dstfd, buff, read_bytes) == -1) {
|
||||
perror("Error to write to destination file");
|
||||
exit_code = EXIT_FAILURE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Try to close files
|
||||
if (close(srcfd) == -1) {
|
||||
perror("Failed to close source file");
|
||||
exit_code = EXIT_FAILURE;
|
||||
}
|
||||
if (close(dstfd) == -1) {
|
||||
perror("Failed to close destination file");
|
||||
exit_code = EXIT_FAILURE;
|
||||
}
|
||||
// Try to close files
|
||||
if (close(srcfd) == -1) {
|
||||
perror("Failed to close source file");
|
||||
exit_code = EXIT_FAILURE;
|
||||
}
|
||||
if (close(dstfd) == -1) {
|
||||
perror("Failed to close destination file");
|
||||
exit_code = EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return exit_code;
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
#include "tree.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "tree.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
const char *path = (argc > 1) ? argv[1] : ".";
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
const char *path = (argc > 1) ? argv[1] : ".";
|
||||
|
||||
if (print_tree(path, 0) == -1) {
|
||||
perror("Error traversing directory");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (print_tree(path, 0) == -1) {
|
||||
perror("Error traversing directory");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,35 +1,39 @@
|
||||
#include "tree.h"
|
||||
#include <dirent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include "tree.h"
|
||||
|
||||
int print_tree(const char *path, int depth) {
|
||||
DIR *dir = opendir(path);
|
||||
if (!dir) return -1;
|
||||
int print_tree(const char *path, int depth)
|
||||
{
|
||||
DIR *dir = opendir(path);
|
||||
if (!dir)
|
||||
return -1;
|
||||
|
||||
struct dirent *entry;
|
||||
while ((entry = readdir(dir)) != NULL) {
|
||||
// Skip . and ..
|
||||
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
||||
continue;
|
||||
struct dirent *entry;
|
||||
while ((entry = readdir(dir)) != NULL) {
|
||||
// Skip . and ..
|
||||
if (strcmp(entry->d_name, ".") == 0 ||
|
||||
strcmp(entry->d_name, "..") == 0)
|
||||
continue;
|
||||
|
||||
for (int i = 0; i < depth; i++) printf(" ");
|
||||
printf("|-- %s\n", entry->d_name);
|
||||
for (int i = 0; i < depth; i++)
|
||||
printf(" ");
|
||||
printf("|-- %s\n", entry->d_name);
|
||||
|
||||
// Construct full path
|
||||
char full_path[4096];
|
||||
snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name);
|
||||
// Construct full path
|
||||
char full_path[4096];
|
||||
snprintf(full_path, sizeof(full_path), "%s/%s", path,
|
||||
entry->d_name);
|
||||
|
||||
struct stat st;
|
||||
if (stat(full_path, &st) == 0 && S_ISDIR(st.st_mode)) {
|
||||
print_tree(full_path, depth + 1);
|
||||
}
|
||||
}
|
||||
struct stat st;
|
||||
if (stat(full_path, &st) == 0 && S_ISDIR(st.st_mode)) {
|
||||
print_tree(full_path, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
return 0;
|
||||
closedir(dir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,4 +4,3 @@
|
||||
int print_tree(const char *path, int depth);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user