Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OBITools3
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
23
Issues
23
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
Operations
Operations
Incidents
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
OBITools
OBITools3
Commits
927c684f
Commit
927c684f
authored
Aug 03, 2017
by
Celine Mercier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Utils: new function to copy the content of a file into another file
parent
344566d9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
0 deletions
+74
-0
src/utils.c
src/utils.c
+60
-0
src/utils.h
src/utils.h
+14
-0
No files found.
src/utils.c
View file @
927c684f
...
...
@@ -36,6 +36,66 @@
**********************************************************************/
int
copy_file
(
const
char
*
src_file_path
,
const
char
*
dest_file_path
)
{
int
src_fd
,
dst_fd
,
n
,
err
;
unsigned
char
buffer
[
4096
];
src_fd
=
open
(
src_file_path
,
O_RDONLY
);
if
(
src_fd
==
-
1
)
{
obi_set_errno
(
OBI_UTILS_ERROR
);
obidebug
(
1
,
"
\n
Error opening a file to copy"
);
return
-
1
;
}
dst_fd
=
open
(
dest_file_path
,
O_CREAT
|
O_WRONLY
,
0777
);
// overwrite if already exists
if
(
dst_fd
==
-
1
)
{
obi_set_errno
(
OBI_UTILS_ERROR
);
obidebug
(
1
,
"
\n
Error opening a file to write a copy: %s"
,
dest_file_path
);
return
-
1
;
}
while
(
1
)
{
err
=
read
(
src_fd
,
buffer
,
4096
);
if
(
err
==
-
1
)
{
obi_set_errno
(
OBI_UTILS_ERROR
);
obidebug
(
1
,
"
\n
Problem reading a file to copy"
);
return
-
1
;
}
n
=
err
;
if
(
n
==
0
)
break
;
err
=
write
(
dst_fd
,
buffer
,
n
);
if
(
err
==
-
1
)
{
obi_set_errno
(
OBI_UTILS_ERROR
);
obidebug
(
1
,
"
\n
Problem writing to a file while copying"
);
return
-
1
;
}
}
if
(
close
(
src_fd
)
<
0
)
{
obi_set_errno
(
OBI_UTILS_ERROR
);
obidebug
(
1
,
"
\n
Error closing a file after copying it"
);
return
-
1
;
}
if
(
close
(
dst_fd
)
<
0
)
{
obi_set_errno
(
OBI_UTILS_ERROR
);
obidebug
(
1
,
"
\n
Error closing a file after copying to it"
);
return
-
1
;
}
return
0
;
}
int
digit_count
(
index_t
i
)
{
int
n_digits
;
...
...
src/utils.h
View file @
927c684f
...
...
@@ -25,6 +25,20 @@
#define ONE_IF_ZERO(x) (((x)==0)?1:(x))
/**< If x is equal to 0, x takes the value 1.
*/
/**
* @brief Copy the content of a file into another file.
*
* @param src_file_path The path to the source file to copy.
* @param dest_file_path The path to the destination file.
*
* @retval 0 if the operation was successfully completed.
* @retval -1 if an error occurred.
*
* @since July 2017
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
int
copy_file
(
const
char
*
src_file_path
,
const
char
*
dest_file_path
);
/**
* @brief Counts the number of digits of a number.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment