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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
/* util.c
*
* Copyright (C) 2007-2009 Tillmann Werner <tillmann.werner@gmx.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 2 as
* published by the Free Software Foundation. You may not use, modify or
* distribute this program under any other version of the GNU General
* Public License.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* $Id$
*/
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "util.h"
// read a line from a descriptor
int nebula_read_line(int fd, char *line, ssize_t len) {
int chars_read = 0;
int rv = 0;
memset(line, 0, len);
while (1) {
if (chars_read >= len-1) return -1;
rv = read(fd, &line[chars_read], 1);
if (rv == 0) return chars_read;
if (rv < 0) return -1;
if (line[chars_read] == '\n') {
line[chars_read] = '\0';
return chars_read;
}
chars_read++;
}
return 0;
}
int nebula_regular_file(struct dirent *e) {
struct stat sta;
memset(&sta, 0, sizeof(struct stat));
if (stat(e->d_name, &sta) != 0) {
fprintf(stderr, "Error - Unable to get file status for %s: %m.\n", e->d_name);
exit(EXIT_FAILURE);
}
return(S_ISREG(sta.st_mode));
}
int nebula_timesort(const void *a, const void *b) {
struct dirent **A, **B;
struct stat sta, stb;
memset(&sta, 0, sizeof(struct stat));
memset(&stb, 0, sizeof(struct stat));
A = (struct dirent **) a;
B = (struct dirent **) b;
if ((stat((*A)->d_name, &sta) != 0) || (stat((*B)->d_name, &stb) != 0)) {
fprintf(stderr, "Error - Unable to get file status for %s, %s: %m.\n", (*A)->d_name, (*B)->d_name);
exit(EXIT_FAILURE);
}
if (sta.st_ctime < stb.st_ctime) return(-1);
if (sta.st_ctime > stb.st_ctime) return(1);
return(0);
}
|