xref: /openbsd-src/regress/lib/libc/telldir/utils.c (revision 36cd4f112ba9185542870bf6e070ac18e11a471b)
1*36cd4f11Sbluhm /*	$OpenBSD: utils.c,v 1.2 2017/07/27 15:08:37 bluhm Exp $	*/
2aeea1f27Sschwarze 
3aeea1f27Sschwarze /*	Written by Otto Moerbeek, 2006,  Public domain.	*/
4aeea1f27Sschwarze 
5aeea1f27Sschwarze #include <sys/types.h>
6aeea1f27Sschwarze #include <sys/stat.h>
7aeea1f27Sschwarze #include <dirent.h>
8aeea1f27Sschwarze #include <err.h>
9aeea1f27Sschwarze #include <fcntl.h>
10aeea1f27Sschwarze #include <limits.h>
11aeea1f27Sschwarze #include <stdio.h>
12aeea1f27Sschwarze #include <string.h>
13aeea1f27Sschwarze #include <unistd.h>
14aeea1f27Sschwarze 
15aeea1f27Sschwarze #include "utils.h"
16aeea1f27Sschwarze 
17aeea1f27Sschwarze void
createfiles(int nfiles)18aeea1f27Sschwarze createfiles(int nfiles)
19aeea1f27Sschwarze {
20aeea1f27Sschwarze 	int i, fd;
21aeea1f27Sschwarze 	char file[PATH_MAX];
22aeea1f27Sschwarze 
23aeea1f27Sschwarze 	mkdir("d", 0755);
24aeea1f27Sschwarze 	for (i = 0; i < nfiles; i++) {
25aeea1f27Sschwarze 		snprintf(file, sizeof file, "d/%d", i);
26aeea1f27Sschwarze 		if ((fd = open(file, O_CREAT | O_WRONLY, 0600)) == -1)
27aeea1f27Sschwarze 			err(1, "open %s", file);
28aeea1f27Sschwarze 		close(fd);
29aeea1f27Sschwarze 	}
30aeea1f27Sschwarze }
31aeea1f27Sschwarze 
32aeea1f27Sschwarze void
delfiles(void)33aeea1f27Sschwarze delfiles(void)
34aeea1f27Sschwarze {
35aeea1f27Sschwarze 	DIR *dp;
36aeea1f27Sschwarze 	struct dirent *f;
37aeea1f27Sschwarze 	char file[PATH_MAX];
38aeea1f27Sschwarze 
39aeea1f27Sschwarze 	dp = opendir("d");
40aeea1f27Sschwarze 	if (dp == NULL)
41aeea1f27Sschwarze 		err(1, "opendir");
42*36cd4f11Sbluhm 	while ((f = readdir(dp))) {
43aeea1f27Sschwarze 		if (strcmp(f->d_name, ".") == 0 ||
44aeea1f27Sschwarze 		    strcmp(f->d_name, "..") == 0)
45aeea1f27Sschwarze 			continue;
46aeea1f27Sschwarze 		snprintf(file, sizeof file, "d/%s", f->d_name);
47aeea1f27Sschwarze 		if (unlink(file) == -1)
48aeea1f27Sschwarze 			err(1, "unlink %s", f->d_name);
49aeea1f27Sschwarze 	}
50aeea1f27Sschwarze 	closedir(dp);
51aeea1f27Sschwarze 	if (rmdir("d") == -1)
52aeea1f27Sschwarze 		err(1, "rmdir");
53aeea1f27Sschwarze }
54