1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 1996 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*0Sstevel@tonic-gate /* All Rights Reserved */ 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate /* 31*0Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 32*0Sstevel@tonic-gate * The Regents of the University of California 33*0Sstevel@tonic-gate * All Rights Reserved 34*0Sstevel@tonic-gate * 35*0Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 36*0Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 37*0Sstevel@tonic-gate * contributors. 38*0Sstevel@tonic-gate */ 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate #include <sys/param.h> 43*0Sstevel@tonic-gate #include <sys/types.h> 44*0Sstevel@tonic-gate #include <sys/stat.h> 45*0Sstevel@tonic-gate #include <sys/file.h> 46*0Sstevel@tonic-gate #include <fcntl.h> 47*0Sstevel@tonic-gate #include <grp.h> 48*0Sstevel@tonic-gate #include <pwd.h> 49*0Sstevel@tonic-gate #include <stdio.h> 50*0Sstevel@tonic-gate #include <ctype.h> 51*0Sstevel@tonic-gate #include <errno.h> 52*0Sstevel@tonic-gate #include <locale.h> 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate #define DEF_GROUP "staff" /* default group */ 55*0Sstevel@tonic-gate #define DEF_OWNER "root" /* default owner */ 56*0Sstevel@tonic-gate #define DEF_MODE 0755 /* default mode */ 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate char *group = DEF_GROUP; 59*0Sstevel@tonic-gate char *owner = DEF_OWNER; 60*0Sstevel@tonic-gate int mode = DEF_MODE; 61*0Sstevel@tonic-gate int sflag = 0; 62*0Sstevel@tonic-gate struct passwd *pp; 63*0Sstevel@tonic-gate struct group *gp; 64*0Sstevel@tonic-gate extern int errno; 65*0Sstevel@tonic-gate int copy(); 66*0Sstevel@tonic-gate void usage(); 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate main(argc, argv) 69*0Sstevel@tonic-gate int argc; 70*0Sstevel@tonic-gate char **argv; 71*0Sstevel@tonic-gate { 72*0Sstevel@tonic-gate extern char *optarg; 73*0Sstevel@tonic-gate extern int optind; 74*0Sstevel@tonic-gate struct stat stb; 75*0Sstevel@tonic-gate char *dirname; 76*0Sstevel@tonic-gate int ch; 77*0Sstevel@tonic-gate int i; 78*0Sstevel@tonic-gate int rc; 79*0Sstevel@tonic-gate int dflag = 0; 80*0Sstevel@tonic-gate int gflag = 0; 81*0Sstevel@tonic-gate int oflag = 0; 82*0Sstevel@tonic-gate int mflag = 0; 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 87*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 88*0Sstevel@tonic-gate #endif 89*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate while ((ch = getopt(argc, argv, "dcg:o:m:s")) != EOF) 92*0Sstevel@tonic-gate switch((char)ch) { 93*0Sstevel@tonic-gate case 'c': 94*0Sstevel@tonic-gate break; /* always do "copy" */ 95*0Sstevel@tonic-gate case 'd': 96*0Sstevel@tonic-gate dflag++; 97*0Sstevel@tonic-gate break; 98*0Sstevel@tonic-gate case 'g': 99*0Sstevel@tonic-gate gflag++; 100*0Sstevel@tonic-gate group = optarg; 101*0Sstevel@tonic-gate break; 102*0Sstevel@tonic-gate case 'm': 103*0Sstevel@tonic-gate mflag++; 104*0Sstevel@tonic-gate mode = atoo(optarg); 105*0Sstevel@tonic-gate break; 106*0Sstevel@tonic-gate case 'o': 107*0Sstevel@tonic-gate oflag++; 108*0Sstevel@tonic-gate owner = optarg; 109*0Sstevel@tonic-gate break; 110*0Sstevel@tonic-gate case 's': 111*0Sstevel@tonic-gate sflag++; 112*0Sstevel@tonic-gate break; 113*0Sstevel@tonic-gate case '?': 114*0Sstevel@tonic-gate default: 115*0Sstevel@tonic-gate usage(); 116*0Sstevel@tonic-gate } 117*0Sstevel@tonic-gate argc -= optind; 118*0Sstevel@tonic-gate argv += optind; 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate /* get group and owner id's */ 121*0Sstevel@tonic-gate if (!(gp = getgrnam(group))) { 122*0Sstevel@tonic-gate fprintf(stderr, gettext("install: unknown group %s.\n"), group); 123*0Sstevel@tonic-gate exit(1); 124*0Sstevel@tonic-gate } 125*0Sstevel@tonic-gate if (!(pp = getpwnam(owner))) { 126*0Sstevel@tonic-gate fprintf(stderr, gettext("install: unknown user %s.\n"), owner); 127*0Sstevel@tonic-gate exit(1); 128*0Sstevel@tonic-gate } 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate if (dflag) { /* install a directory */ 131*0Sstevel@tonic-gate int exists = 0; 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate if (argc != 1) 134*0Sstevel@tonic-gate usage(); 135*0Sstevel@tonic-gate dirname = argv[0]; 136*0Sstevel@tonic-gate if (mkdirp(dirname, 0777) < 0) { 137*0Sstevel@tonic-gate exists = errno == EEXIST; 138*0Sstevel@tonic-gate if (!exists) { 139*0Sstevel@tonic-gate fprintf(stderr, gettext("install: mkdir: %s: %s\n"), dirname, strerror(errno)); 140*0Sstevel@tonic-gate exit(1); 141*0Sstevel@tonic-gate } 142*0Sstevel@tonic-gate } 143*0Sstevel@tonic-gate if (stat(dirname, &stb) < 0) { 144*0Sstevel@tonic-gate fprintf(stderr, gettext("install: stat: %s: %s\n"), dirname, strerror(errno)); 145*0Sstevel@tonic-gate exit(1); 146*0Sstevel@tonic-gate } 147*0Sstevel@tonic-gate if ((stb.st_mode&S_IFMT) != S_IFDIR) { 148*0Sstevel@tonic-gate fprintf(stderr, gettext("install: %s is not a directory\n"), dirname); 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate /* make sure directory setgid bit is inherited */ 151*0Sstevel@tonic-gate mode = (mode & ~S_ISGID) | (stb.st_mode & S_ISGID); 152*0Sstevel@tonic-gate if (mflag && chmod(dirname, mode)) { 153*0Sstevel@tonic-gate fprintf(stderr, gettext("install: chmod: %s: %s\n"), dirname, strerror(errno)); 154*0Sstevel@tonic-gate if (!exists) 155*0Sstevel@tonic-gate (void) unlink(dirname); 156*0Sstevel@tonic-gate exit(1) ; 157*0Sstevel@tonic-gate } 158*0Sstevel@tonic-gate if (oflag && chown(dirname, pp->pw_uid, -1) && errno != EPERM) { 159*0Sstevel@tonic-gate fprintf(stderr, gettext("install: chown: %s: %s\n"), dirname, strerror(errno)); 160*0Sstevel@tonic-gate if (!exists) 161*0Sstevel@tonic-gate (void) unlink(dirname); 162*0Sstevel@tonic-gate exit(1) ; 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate if (gflag && chown(dirname, -1, gp->gr_gid) && errno != EPERM) { 165*0Sstevel@tonic-gate fprintf(stderr, gettext("install: chgrp: %s: %s\n"), dirname, strerror(errno)); 166*0Sstevel@tonic-gate if (!exists) 167*0Sstevel@tonic-gate (void) unlink(dirname); 168*0Sstevel@tonic-gate exit(1) ; 169*0Sstevel@tonic-gate } 170*0Sstevel@tonic-gate exit(0); 171*0Sstevel@tonic-gate } 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate if (argc < 2) 174*0Sstevel@tonic-gate usage(); 175*0Sstevel@tonic-gate 176*0Sstevel@tonic-gate if (argc > 2) { /* last arg must be a directory */ 177*0Sstevel@tonic-gate if (stat(argv[argc-1], &stb) < 0) 178*0Sstevel@tonic-gate usage(); 179*0Sstevel@tonic-gate if ((stb.st_mode&S_IFMT) != S_IFDIR) 180*0Sstevel@tonic-gate usage(); 181*0Sstevel@tonic-gate } 182*0Sstevel@tonic-gate rc = 0; 183*0Sstevel@tonic-gate for (i = 0; i < argc-1; i++) 184*0Sstevel@tonic-gate rc |= install(argv[i], argv[argc-1]); 185*0Sstevel@tonic-gate exit(rc); 186*0Sstevel@tonic-gate /* NOTREACHED */ 187*0Sstevel@tonic-gate } 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate int 190*0Sstevel@tonic-gate install(from, to) 191*0Sstevel@tonic-gate char *from, *to; 192*0Sstevel@tonic-gate { 193*0Sstevel@tonic-gate int to_fd; 194*0Sstevel@tonic-gate int devnull; 195*0Sstevel@tonic-gate int status = 0; 196*0Sstevel@tonic-gate char *path; 197*0Sstevel@tonic-gate struct stat from_sb, to_sb; 198*0Sstevel@tonic-gate static char pbuf[MAXPATHLEN]; 199*0Sstevel@tonic-gate char buf[MAXPATHLEN + 10]; 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate /* check source */ 202*0Sstevel@tonic-gate if (stat(from, &from_sb)) { 203*0Sstevel@tonic-gate fprintf(stderr, gettext("install: %s: %s\n"), from, strerror(errno)); 204*0Sstevel@tonic-gate return (1); 205*0Sstevel@tonic-gate } 206*0Sstevel@tonic-gate /* special case for removing files */ 207*0Sstevel@tonic-gate devnull = !strcmp(from, "/dev/null"); 208*0Sstevel@tonic-gate if (!devnull && !((from_sb.st_mode&S_IFMT) == S_IFREG)) { 209*0Sstevel@tonic-gate fprintf(stderr, gettext("install: %s isn't a regular file.\n"), from); 210*0Sstevel@tonic-gate return (1); 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate /* build target path, find out if target is same as source */ 214*0Sstevel@tonic-gate if (!stat(path = to, &to_sb)) { 215*0Sstevel@tonic-gate if ((to_sb.st_mode&S_IFMT) == S_IFDIR) { 216*0Sstevel@tonic-gate char *C, *strrchr(); 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate (void) sprintf(path = pbuf, "%s/%s", to, (C = strrchr(from, '/')) ? ++C : from); 219*0Sstevel@tonic-gate if (stat(path, &to_sb)) 220*0Sstevel@tonic-gate goto nocompare; 221*0Sstevel@tonic-gate } 222*0Sstevel@tonic-gate if ((to_sb.st_mode&S_IFMT) != S_IFREG) { 223*0Sstevel@tonic-gate fprintf(stderr, gettext("install: %s isn't a regular file.\n"), path); 224*0Sstevel@tonic-gate return (1); 225*0Sstevel@tonic-gate } 226*0Sstevel@tonic-gate if (to_sb.st_dev == from_sb.st_dev && to_sb.st_ino == from_sb.st_ino) { 227*0Sstevel@tonic-gate fprintf(stderr, gettext("install: %s and %s are the same file.\n"), from, path); 228*0Sstevel@tonic-gate return (1); 229*0Sstevel@tonic-gate } 230*0Sstevel@tonic-gate /* unlink now... avoid ETXTBSY errors later */ 231*0Sstevel@tonic-gate (void) unlink(path); 232*0Sstevel@tonic-gate } 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate nocompare: 235*0Sstevel@tonic-gate /* open target, set mode, owner, group */ 236*0Sstevel@tonic-gate if ((to_fd = open(path, O_CREAT|O_WRONLY|O_TRUNC, 0)) < 0) { 237*0Sstevel@tonic-gate fprintf(stderr, gettext("install: %s: %s\n"), path, strerror(errno)); 238*0Sstevel@tonic-gate return (1); 239*0Sstevel@tonic-gate } 240*0Sstevel@tonic-gate if (fchmod(to_fd, mode)) { 241*0Sstevel@tonic-gate fprintf(stderr, gettext("install: chmod: %s: %s\n"), path, strerror(errno)); 242*0Sstevel@tonic-gate status = 1; 243*0Sstevel@tonic-gate close(to_fd); 244*0Sstevel@tonic-gate goto inst_done; 245*0Sstevel@tonic-gate } 246*0Sstevel@tonic-gate if (!devnull) { 247*0Sstevel@tonic-gate status = copy(from, to_fd, path); /* copy */ 248*0Sstevel@tonic-gate close(to_fd); 249*0Sstevel@tonic-gate } 250*0Sstevel@tonic-gate if (sflag) { 251*0Sstevel@tonic-gate sprintf(buf, "strip %s", path); 252*0Sstevel@tonic-gate system(buf); 253*0Sstevel@tonic-gate } 254*0Sstevel@tonic-gate if (chown(path, pp->pw_uid, gp->gr_gid) && errno != EPERM) { 255*0Sstevel@tonic-gate fprintf(stderr, gettext("install: chown: %s: %s\n"), path, strerror(errno)); 256*0Sstevel@tonic-gate status = 1; 257*0Sstevel@tonic-gate } 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gate inst_done: 260*0Sstevel@tonic-gate if (status) 261*0Sstevel@tonic-gate (void) unlink(path); 262*0Sstevel@tonic-gate return (status); 263*0Sstevel@tonic-gate } 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate /* 266*0Sstevel@tonic-gate * copy -- 267*0Sstevel@tonic-gate * copy from one file to another 268*0Sstevel@tonic-gate */ 269*0Sstevel@tonic-gate int 270*0Sstevel@tonic-gate copy(from_name, to_fd, to_name) 271*0Sstevel@tonic-gate int to_fd; 272*0Sstevel@tonic-gate char *from_name, *to_name; 273*0Sstevel@tonic-gate { 274*0Sstevel@tonic-gate int n, from_fd; 275*0Sstevel@tonic-gate int status = 0; 276*0Sstevel@tonic-gate char buf[MAXBSIZE]; 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) { 279*0Sstevel@tonic-gate fprintf(stderr, gettext("install: open: %s: %s\n"), from_name, strerror(errno)); 280*0Sstevel@tonic-gate return (1); 281*0Sstevel@tonic-gate } 282*0Sstevel@tonic-gate while ((n = read(from_fd, buf, sizeof(buf))) > 0) 283*0Sstevel@tonic-gate if (write(to_fd, buf, n) != n) { 284*0Sstevel@tonic-gate fprintf(stderr, gettext("install: write: %s: %s\n"), to_name, strerror(errno)); 285*0Sstevel@tonic-gate status = 1; 286*0Sstevel@tonic-gate goto copy_done; 287*0Sstevel@tonic-gate } 288*0Sstevel@tonic-gate if (n == -1) { 289*0Sstevel@tonic-gate fprintf(stderr, gettext("install: read: %s: %s\n"), from_name, strerror(errno)); 290*0Sstevel@tonic-gate status = 1; 291*0Sstevel@tonic-gate goto copy_done; 292*0Sstevel@tonic-gate } 293*0Sstevel@tonic-gate 294*0Sstevel@tonic-gate copy_done: 295*0Sstevel@tonic-gate (void) close(from_fd); 296*0Sstevel@tonic-gate return (status); 297*0Sstevel@tonic-gate } 298*0Sstevel@tonic-gate 299*0Sstevel@tonic-gate /* 300*0Sstevel@tonic-gate * atoo -- 301*0Sstevel@tonic-gate * octal string to int 302*0Sstevel@tonic-gate */ 303*0Sstevel@tonic-gate int 304*0Sstevel@tonic-gate atoo(str) 305*0Sstevel@tonic-gate register char *str; 306*0Sstevel@tonic-gate { 307*0Sstevel@tonic-gate register int val; 308*0Sstevel@tonic-gate 309*0Sstevel@tonic-gate for (val = 0; isdigit(*str); ++str) 310*0Sstevel@tonic-gate val = val * 8 + *str - '0'; 311*0Sstevel@tonic-gate return(val); 312*0Sstevel@tonic-gate } 313*0Sstevel@tonic-gate 314*0Sstevel@tonic-gate 315*0Sstevel@tonic-gate /* 316*0Sstevel@tonic-gate * usage -- 317*0Sstevel@tonic-gate * print a usage message and die 318*0Sstevel@tonic-gate */ 319*0Sstevel@tonic-gate void 320*0Sstevel@tonic-gate usage() 321*0Sstevel@tonic-gate { 322*0Sstevel@tonic-gate fputs(gettext("usage: install [-cs] [-g group] [-m mode] [-o owner] file ... destination\n"), stderr); 323*0Sstevel@tonic-gate fputs(gettext(" install -d [-g group] [-m mode] [-o owner] dir\n"), stderr); 324*0Sstevel@tonic-gate exit(1); 325*0Sstevel@tonic-gate } 326*0Sstevel@tonic-gate 327*0Sstevel@tonic-gate /* 328*0Sstevel@tonic-gate * mkdirp -- 329*0Sstevel@tonic-gate * make a directory and parents if needed 330*0Sstevel@tonic-gate */ 331*0Sstevel@tonic-gate int 332*0Sstevel@tonic-gate mkdirp(dir, mode) 333*0Sstevel@tonic-gate char *dir; 334*0Sstevel@tonic-gate int mode; 335*0Sstevel@tonic-gate { 336*0Sstevel@tonic-gate int err; 337*0Sstevel@tonic-gate char *slash; 338*0Sstevel@tonic-gate char *strrchr(); 339*0Sstevel@tonic-gate extern int errno; 340*0Sstevel@tonic-gate 341*0Sstevel@tonic-gate if (mkdir(dir, mode) == 0) 342*0Sstevel@tonic-gate return (0); 343*0Sstevel@tonic-gate if (errno != ENOENT) 344*0Sstevel@tonic-gate return (-1); 345*0Sstevel@tonic-gate slash = strrchr(dir, '/'); 346*0Sstevel@tonic-gate if (slash == NULL) 347*0Sstevel@tonic-gate return (-1); 348*0Sstevel@tonic-gate *slash = '\0'; 349*0Sstevel@tonic-gate err = mkdirp(dir, 0777); 350*0Sstevel@tonic-gate *slash = '/'; 351*0Sstevel@tonic-gate if (err) 352*0Sstevel@tonic-gate return (err); 353*0Sstevel@tonic-gate return mkdir(dir, mode); 354*0Sstevel@tonic-gate } 355