Skip to content

Commit 06c896c

Browse files
committed
numfiles and scandir are two new performance utilities.
1 parent cfc2861 commit 06c896c

File tree

4 files changed

+155
-1
lines changed

4 files changed

+155
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ dg
44
pr
55
rd
66
id
7+
scandir
8+
numfiles
79
readn
810
writen
911
ictest

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
# =====================================================================================
3232
#
3333

34-
PROGRAMS = dg pr rd id
34+
PROGRAMS = dg pr rd id numfiles scandir
3535
CFLAGS = -Wall -g -DUSE_FLOCK
3636
LDFLAGS = -lm
3737

@@ -57,3 +57,8 @@ rd: rd.c mh.c
5757
id: id.c
5858
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
5959

60+
numfiles: numfiles.c
61+
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
62+
63+
scandir: scandir.c
64+
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

numfiles.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* vim: set ts=2 sw=2:
2+
* =====================================================================================
3+
*
4+
* Filename: numfiles.c
5+
*
6+
* Description: File generator
7+
*
8+
* Version: 1.0
9+
* Created: 2012-01-10 15:47
10+
* Revision: none
11+
* Compiler: gcc
12+
*
13+
* Author: Peter Meszaros (pme), [email protected]
14+
* Company: Infalk Co.
15+
*
16+
* =====================================================================================
17+
*
18+
* This program is free software: you can redistribute it and/or modify
19+
* it under the terms of the GNU General Public License as published by
20+
* the Free Software Foundation, either version 3 of the License, or
21+
* (at your option) any later version.
22+
*
23+
* This program is distributed in the hope that it will be useful,
24+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
25+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26+
* GNU General Public License for more details.
27+
*
28+
* You should have received a copy of the GNU General Public License
29+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
30+
*
31+
* =====================================================================================
32+
*/
33+
34+
#include <stdio.h>
35+
#include <stdlib.h>
36+
#include <unistd.h>
37+
#include <sys/types.h>
38+
#include <sys/stat.h>
39+
#include <fcntl.h>
40+
#include <err.h>
41+
#include <libgen.h>
42+
43+
int main(int argc, char *argv[])
44+
{
45+
int i;
46+
int n = atoi(argv[1]);
47+
48+
if (argc != 3)
49+
errx(EXIT_FAILURE, "Usage: %s numfiles dirname", basename(argv[0]));
50+
51+
for(i=1; i < n; i++) {
52+
int fd;
53+
char filename[1024];
54+
55+
snprintf(filename, 1024, "%s/%010d", argv[2], i);
56+
57+
if ((fd = open(filename, O_CREAT, 0644)) == -1)
58+
err(EXIT_FAILURE, "open(%s)", filename);
59+
60+
close(fd);
61+
62+
if (!(i % 1000)) printf("%d files created\n", i);
63+
}
64+
65+
return 0;
66+
}

scandir.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* vim: set ts=2 sw=2:
2+
* =====================================================================================
3+
*
4+
* Filename: scandir.c
5+
*
6+
* Description: Directory scanner
7+
*
8+
* Version: 1.0
9+
* Created: 2012-01-10 15:50
10+
* Revision: none
11+
* Compiler: gcc
12+
*
13+
* Author: Peter Meszaros (pme), [email protected]
14+
* Company: Infalk Co.
15+
*
16+
* =====================================================================================
17+
*
18+
* This program is free software: you can redistribute it and/or modify
19+
* it under the terms of the GNU General Public License as published by
20+
* the Free Software Foundation, either version 3 of the License, or
21+
* (at your option) any later version.
22+
*
23+
* This program is distributed in the hope that it will be useful,
24+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
25+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26+
* GNU General Public License for more details.
27+
*
28+
* You should have received a copy of the GNU General Public License
29+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
30+
*
31+
* =====================================================================================
32+
*/
33+
34+
#define _GNU_SOURCE
35+
#include <stdio.h>
36+
#include <stdlib.h>
37+
#include <dirent.h>
38+
#include <sys/time.h>
39+
#include <err.h>
40+
#include <libgen.h>
41+
42+
int min, max;
43+
44+
int filter(const struct dirent *de)
45+
{
46+
int n = atoi(de->d_name);
47+
48+
return (n >= min && n <= max) ? 1 : 0;
49+
}
50+
51+
int main(int argc, char *argv[])
52+
{
53+
int dn;
54+
int i;
55+
struct dirent **de = NULL;
56+
struct timeval fm, to, d;
57+
58+
if (argc != 4)
59+
errx(EXIT_FAILURE, "Usage: %s min max dirpath", basename(argv[0]));
60+
61+
min = atoi(argv[1]);
62+
max = atoi(argv[2]);
63+
64+
gettimeofday(&fm, NULL);
65+
66+
if ((dn = scandir(argv[3], &de, filter, versionsort)) == -1)
67+
err(EXIT_FAILURE, "scandir(%s)", argv[3]);
68+
69+
gettimeofday(&to, NULL);
70+
71+
timersub(&to, &fm, &d);
72+
fprintf(stderr, "time: %ld.%06ld\n", d.tv_sec, d.tv_usec);
73+
74+
for (i=0; i<dn; i++) {
75+
printf("%d - '%s'\n", i, de[i]->d_name);
76+
}
77+
78+
free(de);
79+
80+
return 0;
81+
}

0 commit comments

Comments
 (0)