/*[XSI]*/ /* Test whether a basic mknod invocation works. */ #include #include #include #include "../basic.h" static char* create_tmpdir(void) { const char* tmpdir = getenv("TMPDIR"); if ( !tmpdir ) tmpdir = "/tmp"; size_t template_len = strlen(tmpdir) + strlen("/os-test.XXXXXX"); char* template = malloc(template_len + 1); if ( !template ) err(1, "malloc"); // mkdtemp is unfortunately less portable than link, so emulate it. while ( 1 ) { strcpy(template, tmpdir); strcat(template, "/os-test.XXXXXX"); int fd = mkstemp(template); if ( fd < 0 ) err(1, "mkstemp"); close(fd); if ( unlink(template) < 0 ) err(1, "unlink"); if ( mkdir(template, 0700) < 0 ) { if ( errno == EEXIST ) continue; err(1, "mkdir"); } break; } return template; } int main(void) { char* tmpdir = create_tmpdir(); char* path = malloc(strlen(tmpdir) + sizeof("/foo")); if ( !path ) { warn("malloc"); rmdir(tmpdir); exit(1); } strcpy(path, tmpdir); strcat(path, "/foo"); if ( mknod(path, S_IFIFO | 0600, 0) < 0 ) { warn("mkfifo"); rmdir(tmpdir); exit(1); } unlink(path); rmdir(tmpdir); return 0; }