2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright (c) 2012 by Delphix. All rights reserved.
17 * Make a directory busy. If the argument is an existing file or directory,
18 * simply open it directly and pause. If not, verify that the parent directory
19 * exists, and create a new file in that directory.
23 #include <sys/types.h>
34 static __attribute__((noreturn
)) void
35 usage(const char *progname
)
37 (void) fprintf(stderr
, "Usage: %s <dirname|filename>\n", progname
);
41 static __attribute__((noreturn
)) void
53 if ((pid
= fork()) < 0) {
55 } else if (pid
!= 0) {
56 (void) fprintf(stdout
, "%ld\n", (long)pid
);
68 get_basename(const char *path
)
70 const char *bn
= strrchr(path
, '/');
71 return (bn
? bn
+ 1 : path
);
75 get_dirnamelen(const char *path
)
77 const char *end
= strrchr(path
, '/');
78 return (end
? end
- path
: -1);
82 main(int argc
, char *argv
[])
85 boolean_t isdir
= B_FALSE
;
90 while ((c
= getopt(argc
, argv
, "")) != -1) {
103 if (stat(argv
[0], &sbuf
) != 0) {
105 const char *dname
, *fname
;
110 * The argument supplied doesn't exist. Copy the path, and
111 * remove the trailing slash if present.
113 if ((arg
= strdup(argv
[0])) == NULL
)
115 arglen
= strlen(arg
);
116 if (arg
[arglen
- 1] == '/')
117 arg
[arglen
- 1] = '\0';
119 /* Get the directory and file names. */
120 fname
= get_basename(arg
);
122 if ((dnamelen
= get_dirnamelen(arg
)) != -1)
123 arg
[dnamelen
] = '\0';
127 /* The directory portion of the path must exist */
128 if (stat(dname
, &sbuf
) != 0 || !(sbuf
.st_mode
& S_IFDIR
))
131 if (asprintf(&fpath
, "%s/%s", dname
, fname
) == -1)
136 switch (sbuf
.st_mode
& S_IFMT
) {
143 if ((fpath
= strdup(argv
[0])) == NULL
)
151 if (open(fpath
, O_CREAT
| O_RDWR
, 0600) < 0)
154 if (opendir(fpath
) == NULL
)