-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
72 lines (55 loc) · 1.84 KB
/
utils.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
/*
* Copyright (c) 2017 Jose M. Dana
*
* This file is part of gadget_snapshots.
* http://github.com/jmdana/gadget_snapshots
*
* gadget_snapshots is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation (version 3 of the License only).
*
* gadget_snapshots is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gadget_snapshots. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <termios.h>
int same_file(char *src, char *dst) {
struct stat s1;
struct stat s2;
int f1;
int f2;
f1 = open(src, O_RDONLY);
f2 = open(dst, O_RDONLY);
fstat(f1, &s1);
fstat(f2, &s2);
close(f1);
close(f2);
return s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev;
}
int getonechar() {
int c;
static struct termios oldt, newt;
/*tcgetattr gets the parameters of the current terminal
STDIN_FILENO will tell tcgetattr that it should write the settings
of stdin to oldt*/
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
/*ICANON normally takes care that one line at a time will be processed
that means it will return if it sees a "\n" or an EOF or an EOL*/
newt.c_lflag &= ~(ICANON);
/*Those new settings will be set to STDIN
TCSANOW tells tcsetattr to change attributes immediately. */
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
c = getchar();
/*restore the old settings*/
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return c;
}