-
Notifications
You must be signed in to change notification settings - Fork 3
/
join.c
72 lines (58 loc) · 1.77 KB
/
join.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "split.h"
#include <fcntl.h> // for open
#include <unistd.h> // for write, read and close
#include <string.h> // for strlen
static void getFileNameWithoutExtension(char *filename, char *filetitle,char
*ext);
static void substring(char *str, int start, int length, char *out);
int join(char *firstfile, char *destfile)
{
char filename_without_number[FILENAME_LEN_MAX];
char startnumberstr[3];
int startnumber;
char currentfile[FILENAME_LEN_MAX];
getFileNameWithoutExtension(firstfile,
filename_without_number,
startnumberstr);
startnumber = atoi(startnumberstr);
int sourcefd, destinationfd;
if ((destinationfd = open(destfile,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR)) == -1)
PANIC("open",2);
if (snprintf(currentfile,FILENAME_LEN_MAX,"%s",firstfile) >=
FILENAME_LEN_MAX)
return ERR_FILE_NAME_TOO_LONG;
while ((sourcefd = open(currentfile,O_RDONLY)) != -1)
{
int readlength;
do
{
if ((readlength = read(sourcefd, buffer,BUFFER_SIZE)) == -1)
PANIC("read",2);
if (write(destinationfd,buffer,readlength) == -1)
PANIC("write",2);
}while(readlength > 0);
close(sourcefd);
if (FL(currentfile,filename_without_number,++startnumber) >=
FILENAME_LEN_MAX)
return ERR_FILE_NAME_TOO_LONG;
}
return --startnumber;
}
static void getFileNameWithoutExtension(char *filename, char *filetitle,char *ext)
{
int l;
if ((l = strlen(filename)) == 0)
{
filetitle[0] = '\0';
ext[0] = '\0';
}
int indexOfLastDot = l;
while(indexOfLastDot >= 0 && filename[--indexOfLastDot] != '.');
substring(filename, 0,indexOfLastDot,filetitle);
substring(filename,indexOfLastDot+1,(l - indexOfLastDot + 1),ext);
}
static void substring(char *str, int start, int length, char *out)
{
while(length-- > 0 && (*out++ = *(str++ + start)) != '\0');
*out = '\0';
}