cpdir.c (97d92980a96a50750844f420cc225ddf918f0699) cpdir.c (1994f5c7aadb7dc3c2b9f2c06c387cb0f838d2b3)
1/*-
2 * Copyright (C) 1996
3 * David L. Nugent. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 58 unchanged lines hidden (view full) ---

67 DIR *d = opendir(skel);
68
69 if (d != NULL) {
70 struct dirent *e;
71
72 while ((e = readdir(d)) != NULL) {
73 char *p = e->d_name;
74
1/*-
2 * Copyright (C) 1996
3 * David L. Nugent. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 58 unchanged lines hidden (view full) ---

67 DIR *d = opendir(skel);
68
69 if (d != NULL) {
70 struct dirent *e;
71
72 while ((e = readdir(d)) != NULL) {
73 char *p = e->d_name;
74
75 sprintf(src, "%s/%s", skel, p);
76 if (stat(src, &st) == 0) {
75 if (snprintf(src, sizeof(src), "%s/%s", skel, p) >= (int)sizeof(src))
76 warn("warning: pathname too long '%s/%s' (skel not copied)", skel, p);
77 else if (stat(src, &st) == 0) {
77 if (strncmp(p, "dot.", 4) == 0) /* Conversion */
78 p += 3;
78 if (strncmp(p, "dot.", 4) == 0) /* Conversion */
79 p += 3;
79 sprintf(dst, "%s/%s", dir, p);
80 if (S_ISDIR(st.st_mode)) { /* Recurse for this */
80 if (snprintf(dst, sizeof(dst), "%s/%s", dir, p) >= (int)sizeof(dst))
81 warn("warning: path too long '%s/%s' (skel file skipped)", dir, p);
82 else {
83 if (S_ISDIR(st.st_mode)) { /* Recurse for this */
81 if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0)
82 copymkdir(dst, src, (st.st_mode & 0777), uid, gid);
84 if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0)
85 copymkdir(dst, src, (st.st_mode & 0777), uid, gid);
86 chflags(dst, st.st_flags); /* propogate flags */
83 /*
87 /*
84 * Note: don't propogate 'special' attributes
88 * Note: don't propogate special attributes
89 * but do propogate file flags
85 */
90 */
86 } else if (S_ISREG(st.st_mode) && (outfd = open(dst, O_RDWR | O_CREAT | O_EXCL, st.st_mode)) != -1) {
91 } else if (S_ISREG(st.st_mode) && (outfd = open(dst, O_RDWR | O_CREAT | O_EXCL, st.st_mode)) != -1) {
87 if ((infd = open(src, O_RDONLY)) == -1) {
88 close(outfd);
89 remove(dst);
90 } else {
91 int b;
92
93 /*
94 * Allocate our copy buffer if we need to
95 */
96 if (copybuf == NULL)
97 copybuf = malloc(4096);
98 while ((b = read(infd, copybuf, 4096)) > 0)
99 write(outfd, copybuf, b);
100 close(infd);
92 if ((infd = open(src, O_RDONLY)) == -1) {
93 close(outfd);
94 remove(dst);
95 } else {
96 int b;
97
98 /*
99 * Allocate our copy buffer if we need to
100 */
101 if (copybuf == NULL)
102 copybuf = malloc(4096);
103 while ((b = read(infd, copybuf, 4096)) > 0)
104 write(outfd, copybuf, b);
105 close(infd);
106 /*
107 * Propogate special filesystem flags
108 */
109 fchown(outfd, uid, gid);
110 fchflags(outfd, st.st_flags);
101 close(outfd);
102 chown(dst, uid, gid);
103 }
111 close(outfd);
112 chown(dst, uid, gid);
113 }
114 }
104 }
105 }
106 }
107 closedir(d);
108 }
109 }
110 if (--counter == 0 && copybuf != NULL) {
111 free(copybuf);
112 copybuf = NULL;
113 }
114 }
115}
116
115 }
116 }
117 }
118 closedir(d);
119 }
120 }
121 if (--counter == 0 && copybuf != NULL) {
122 free(copybuf);
123 copybuf = NULL;
124 }
125 }
126}
127