1 | /* Copyright (C) 1988 Tim Shepard All rights reserved. */ |
---|
2 | |
---|
3 | #include <stdio.h> |
---|
4 | #include <sys/types.h> |
---|
5 | #include <sys/file.h> |
---|
6 | #include <fcntl.h> |
---|
7 | #include <sys/param.h> |
---|
8 | #include <dirent.h> |
---|
9 | #include <sys/stat.h> |
---|
10 | |
---|
11 | #ifndef MAXBSIZE |
---|
12 | #define MAXBSIZE 20480 |
---|
13 | #endif |
---|
14 | |
---|
15 | extern int errno; |
---|
16 | extern int verbosef; |
---|
17 | |
---|
18 | int copyfile(from,to,mode) |
---|
19 | char *from; |
---|
20 | char *to; |
---|
21 | u_short mode; |
---|
22 | { |
---|
23 | int from_fd, to_fd; |
---|
24 | char buffer[MAXBSIZE]; |
---|
25 | int n; |
---|
26 | |
---|
27 | if (verbosef) printf("Copying to %s from %s, mode is %5.5o.\n", to, from, mode); |
---|
28 | |
---|
29 | if ((from_fd = open(from, O_RDONLY, 0)) <= 0) |
---|
30 | return -1; |
---|
31 | |
---|
32 | if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) <= 0) |
---|
33 | { int savederrno = errno; |
---|
34 | (void) close(from_fd); |
---|
35 | errno = savederrno; |
---|
36 | return -1; |
---|
37 | } |
---|
38 | |
---|
39 | while (n = read(from_fd, buffer, MAXBSIZE)) |
---|
40 | { |
---|
41 | if (n < 0) |
---|
42 | { int savederrno = errno; |
---|
43 | (void) close(from_fd); |
---|
44 | (void) close(to_fd); |
---|
45 | (void) unlink(to); |
---|
46 | errno = savederrno; |
---|
47 | return -1; |
---|
48 | } |
---|
49 | if (write(to_fd, buffer, n) != n) |
---|
50 | { int savederrno = errno; |
---|
51 | (void) close(from_fd); |
---|
52 | (void) close(to_fd); |
---|
53 | (void) unlink(to); |
---|
54 | errno = savederrno; |
---|
55 | return -1; |
---|
56 | } |
---|
57 | } |
---|
58 | (void) close(from_fd); |
---|
59 | if (close(to_fd) != 0) |
---|
60 | { int savederrno = errno; |
---|
61 | (void) unlink(to); |
---|
62 | errno = savederrno; |
---|
63 | return -1; |
---|
64 | } |
---|
65 | |
---|
66 | return 0; |
---|
67 | } |
---|
68 | |
---|
69 | int copylink(from,to,mode) |
---|
70 | char *from; |
---|
71 | char *to; |
---|
72 | u_short mode; |
---|
73 | { |
---|
74 | int from_fd, to_fd; |
---|
75 | char buffer[MAXBSIZE]; |
---|
76 | int n; |
---|
77 | |
---|
78 | if (verbosef) |
---|
79 | printf("Copying link to %s from %s, mode is %5.5o.\n", to, from, mode); |
---|
80 | |
---|
81 | if ((n = readlink(from,buffer,MAXBSIZE)) < 0) |
---|
82 | return -1; |
---|
83 | |
---|
84 | buffer[n] = '\0'; |
---|
85 | |
---|
86 | if (symlink(buffer,to) < 0) |
---|
87 | return -1; |
---|
88 | |
---|
89 | return 0; |
---|
90 | } |
---|
91 | |
---|
92 | int recursive_rmdir(dirname) |
---|
93 | char *dirname; |
---|
94 | { |
---|
95 | char path[MAXPATHLEN]; |
---|
96 | DIR *dirp; |
---|
97 | struct dirent *dp; |
---|
98 | struct stat statbuf; |
---|
99 | |
---|
100 | if (verbosef) |
---|
101 | printf("Recursively removing %s.\n", dirname); |
---|
102 | if (!(dirp = opendir(dirname))) |
---|
103 | return -1; |
---|
104 | while ((dp = readdir(dirp)) != NULL) { |
---|
105 | if (!strcmp(dp->d_name, ".") || |
---|
106 | !strcmp(dp->d_name, "..")) |
---|
107 | continue; |
---|
108 | strcpy(path, dirname); |
---|
109 | strcat(path, "/"); |
---|
110 | strcat(path, dp->d_name); |
---|
111 | if (lstat(path, &statbuf)) { |
---|
112 | printf("recursive_rmdir: lstat error %d on file %s.\n", |
---|
113 | errno, path); |
---|
114 | } else { |
---|
115 | if ((statbuf.st_mode & S_IFMT) == S_IFDIR) { |
---|
116 | recursive_rmdir(path); |
---|
117 | rmdir(path); |
---|
118 | } else |
---|
119 | if (unlink(path)) |
---|
120 | printf("recursive_rmdir: unlink error %d on file %s.\n", |
---|
121 | errno, path); |
---|
122 | } |
---|
123 | } |
---|
124 | closedir(dirp); |
---|
125 | return(rmdir(dirname)); |
---|
126 | } |
---|