17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c4dcc55Scasper * Common Development and Distribution License (the "License"). 6*7c4dcc55Scasper * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*7c4dcc55Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate * 257c478bd9Sstevel@tonic-gate * Create unique plain files or directories. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #include <stdio.h> 317c478bd9Sstevel@tonic-gate #include <stdlib.h> 327c478bd9Sstevel@tonic-gate #include <unistd.h> 337c478bd9Sstevel@tonic-gate #include <string.h> 347c478bd9Sstevel@tonic-gate #include <locale.h> 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate static void 377c478bd9Sstevel@tonic-gate usage(void) 387c478bd9Sstevel@tonic-gate { 397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 407c478bd9Sstevel@tonic-gate gettext("Usage: mktemp [-dqtu] [-p prefix_dir] [template]\n")); 417c478bd9Sstevel@tonic-gate exit(1); 427c478bd9Sstevel@tonic-gate /* NOTREACHED */ 437c478bd9Sstevel@tonic-gate } 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate int 467c478bd9Sstevel@tonic-gate main(int argc, char **argv) 477c478bd9Sstevel@tonic-gate { 487c478bd9Sstevel@tonic-gate int opt; 497c478bd9Sstevel@tonic-gate char *prefix = NULL; 507c478bd9Sstevel@tonic-gate boolean_t dounlink = B_FALSE; 517c478bd9Sstevel@tonic-gate boolean_t domkdir = B_FALSE; 527c478bd9Sstevel@tonic-gate boolean_t quiet = B_FALSE; 537c478bd9Sstevel@tonic-gate boolean_t usetmpdir = B_FALSE; 547c478bd9Sstevel@tonic-gate char template[] = "tmp.XXXXXX"; 557c478bd9Sstevel@tonic-gate char *tmpl; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN 607c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 617c478bd9Sstevel@tonic-gate #endif 627c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate opterr = 0; 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate while ((opt = getopt(argc, argv, "dqtup:")) != EOF) { 677c478bd9Sstevel@tonic-gate switch (opt) { 687c478bd9Sstevel@tonic-gate case 'd': 697c478bd9Sstevel@tonic-gate domkdir = B_TRUE; 707c478bd9Sstevel@tonic-gate break; 717c478bd9Sstevel@tonic-gate case 'q': 727c478bd9Sstevel@tonic-gate quiet = B_TRUE; 737c478bd9Sstevel@tonic-gate break; 747c478bd9Sstevel@tonic-gate case 'p': 757c478bd9Sstevel@tonic-gate prefix = optarg; 767c478bd9Sstevel@tonic-gate /* FALLTHROUGH - -p implies -t */ 777c478bd9Sstevel@tonic-gate case 't': 787c478bd9Sstevel@tonic-gate usetmpdir = B_TRUE; 797c478bd9Sstevel@tonic-gate break; 807c478bd9Sstevel@tonic-gate case 'u': 817c478bd9Sstevel@tonic-gate dounlink = B_TRUE; 827c478bd9Sstevel@tonic-gate break; 837c478bd9Sstevel@tonic-gate default: 847c478bd9Sstevel@tonic-gate usage(); 857c478bd9Sstevel@tonic-gate } 867c478bd9Sstevel@tonic-gate } 877c478bd9Sstevel@tonic-gate argc -= optind; 887c478bd9Sstevel@tonic-gate argv += optind; 897c478bd9Sstevel@tonic-gate switch (argc) { 907c478bd9Sstevel@tonic-gate case 0: 917c478bd9Sstevel@tonic-gate tmpl = template; 927c478bd9Sstevel@tonic-gate usetmpdir = B_TRUE; 937c478bd9Sstevel@tonic-gate break; 947c478bd9Sstevel@tonic-gate case 1: 957c478bd9Sstevel@tonic-gate tmpl = argv[0]; 967c478bd9Sstevel@tonic-gate break; 977c478bd9Sstevel@tonic-gate default: 987c478bd9Sstevel@tonic-gate usage(); 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate if (usetmpdir) { 1027c478bd9Sstevel@tonic-gate char *tmp = getenv("TMPDIR"); 1037c478bd9Sstevel@tonic-gate size_t len; 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate if (strchr(tmpl, '/') != NULL) { 1067c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1077c478bd9Sstevel@tonic-gate gettext("mktemp: template argument specified " 1087c478bd9Sstevel@tonic-gate "with -t/-p option must not contain '/'" 1097c478bd9Sstevel@tonic-gate "\n")); 1107c478bd9Sstevel@tonic-gate return (1); 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate /* TMPDIR overrides -p so that scripts will honor $TMPDIR */ 1137c478bd9Sstevel@tonic-gate if (tmp != NULL) 1147c478bd9Sstevel@tonic-gate prefix = tmp; 1157c478bd9Sstevel@tonic-gate else if (prefix == NULL) 1167c478bd9Sstevel@tonic-gate prefix = "/tmp"; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate len = snprintf(NULL, 0, "%s/%s", prefix, tmpl) + 1; 1197c478bd9Sstevel@tonic-gate tmp = malloc(len); 1207c478bd9Sstevel@tonic-gate if (tmp == NULL) { 1217c478bd9Sstevel@tonic-gate perror("malloc"); 1227c478bd9Sstevel@tonic-gate return (1); 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate (void) snprintf(tmp, len, "%s/%s", prefix, tmpl); 1257c478bd9Sstevel@tonic-gate tmpl = tmp; 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate if (domkdir) { 129*7c4dcc55Scasper if (mkdtemp(tmpl) == NULL) { 1307c478bd9Sstevel@tonic-gate if (!quiet) { 1317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1327c478bd9Sstevel@tonic-gate gettext("mktemp: failed to create " 1337c478bd9Sstevel@tonic-gate "directory: %s\n"), tmpl); 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate return (1); 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate if (dounlink) 1387c478bd9Sstevel@tonic-gate (void) rmdir(tmpl); 1397c478bd9Sstevel@tonic-gate } else { 1407c478bd9Sstevel@tonic-gate if (mkstemp(tmpl) < 0) { 1417c478bd9Sstevel@tonic-gate if (!quiet) { 1427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1437c478bd9Sstevel@tonic-gate gettext("mktemp: failed to create file: " 1447c478bd9Sstevel@tonic-gate "%s\n"), tmpl); 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate return (1); 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate if (dounlink) 1497c478bd9Sstevel@tonic-gate (void) unlink(tmpl); 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate (void) puts(tmpl); 1527c478bd9Sstevel@tonic-gate return (0); 1537c478bd9Sstevel@tonic-gate } 154