source: trunk/athena/bin/install/install.c @ 14021

Revision 14021, 9.4 KB checked in by danw, 25 years ago (diff)
warns
RevLine 
[7823]1/*
2 * Copyright (c) 1987 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley.  The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
[9437]18/* Copyright 1996 by the Massachusetts Institute of Technology.
19 *
20 * Permission to use, copy, modify, and distribute this
21 * software and its documentation for any purpose and without
22 * fee is hereby granted, provided that the above copyright
23 * notice appear in all copies and that both that copyright
24 * notice and this permission notice appear in supporting
25 * documentation, and that the name of M.I.T. not be used in
26 * advertising or publicity pertaining to distribution of the
27 * software without specific, written prior permission.
28 * M.I.T. makes no representations about the suitability of
29 * this software for any purpose.  It is provided "as is"
30 * without express or implied warranty.
31 */
[7823]32
[14021]33static const char rcsid[] = "$Id: install.c,v 1.6 1999-11-22 15:59:33 danw Exp $";
[7823]34
35#include <sys/types.h>
36#include <sys/param.h>
37#include <sys/stat.h>
[9437]38#include <sys/time.h>
[14021]39#include <sys/wait.h>
[9437]40#include <errno.h>
[7823]41#include <fcntl.h>
42#include <grp.h>
43#include <pwd.h>
44#include <stdio.h>
[9437]45#include <stdlib.h>
[8859]46#include <string.h>
[7823]47#include <ctype.h>
48#include <limits.h>
[14021]49#include <unistd.h>
[7823]50
[14021]51#ifdef NEED_UTIMES_PROTO
52int utimes(const char *path, const struct timeval *times);
53#endif
54
[7823]55#define MAXARGS 1024
56
57#ifndef MAXBSIZE
58#define MAXBSIZE 10240
59#endif
60
61#ifndef MIN
[9437]62#define MIN(a,b) (((a) < (b)) ? (a) : (b))
[7823]63#endif
64
65static uid_t uid;
66static gid_t gid;
67
[9437]68static int docopy = 0;
69static int dostrip = 0;
70static int domove = 0;
71static int dotime = 0;
72static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
[7823]73
[9437]74static char *group;
75static char *owner;
76static char pathbuf[MAXPATHLEN];
[7823]77
[9437]78static void install(const char *from_name, const char *to_name, int isdir);
79static void copy(int from_fd, const char *from_name, int to_fd,
80                 const char *to_name);
81static void strip(const char *name);
82static int isnumber(const char *s);
83static int atoo(const char *str);
84static void bad(const char *head, const char *str);
85static void usage(void);
[7823]86
[9437]87int main(int cmdline_argc, char **cmdline_argv)
[7823]88{
[9437]89  extern char *optarg;
90  extern int optind;
91  struct stat from_sb, to_sb;
92  int ch, no_target;
93  char *to_name;
94  struct passwd *pp;
95  struct group *gp;
96  int argc = 1;
97  char *args[MAXARGS], **argv = args;
98  char *inst_env;
[7823]99
[9437]100  /* Copy any command-line arguments from INSTOPT into argv. */
101  inst_env = getenv("INSTOPT");
102  if (inst_env)
103    {
[14021]104      while (isspace((unsigned char)*inst_env))
[9437]105        inst_env++;
106      while (*inst_env)
107        {
108          argv[argc++] = inst_env;
[14021]109          while (*inst_env && !isspace((unsigned char)*inst_env))
[9437]110            inst_env++;
111          if (*inst_env)
112            *inst_env++ = 0;
[14021]113          while (isspace((unsigned char)*inst_env))
[9437]114            inst_env++;
115        }
116    }
[7823]117
[9437]118  if (argc + cmdline_argc > MAXARGS)
119    {
120      fprintf(stderr, "install: too many command-line arguments.\n");
121      return 1;
122    }
[7823]123
[9437]124  /* Copy the original arguments into argv. */
125  argv[0] = *cmdline_argv++;
126  while (--cmdline_argc)
127    argv[argc++] = *cmdline_argv++;
[7823]128
[9437]129  while ((ch = getopt(argc, argv, "cdstg:m:o:")) != EOF)
130    {
131      switch(ch)
132        {
133        case 'c':
134          docopy = 1;
135          break;
136        case 'd':
137          domove = 1;
138          break;
139        case 'g':
140          group = optarg;
141          break;
142        case 'm':
143          mode = atoo(optarg);
144          break;
145        case 'o':
146          owner = optarg;
147          break;
148        case 's':
149          dostrip = 1;
150          break;
151        case 't':
152          dotime = 1;
153          break;
154        case '?':
155        default:
156          usage();
[7823]157        }
[9437]158    }
159  argc -= optind;
160  argv += optind;
161  if (argc < 2)
162    usage();
[7823]163
[9437]164  /* Check for multiple specifications of copy and move. */
165  if (domove && docopy)
166    {
167      fprintf(stderr, "install: cannot specify both -c and -d\n");
168      return 1;
169    }
[7823]170
[9437]171  /* If neither copy nor move specified, do copy. */
172  if (!domove)
173    docopy = 1;
[7823]174
[9437]175  /* Get group and owner ids. */
176  if (owner)
177    {
178      to_name = strchr(owner, '.');
179      if (to_name)
180        {
181          *to_name++ = '\0';
182          if (!group)
183            group = to_name;
184          else
185            {
186              fputs("install: multiple specification of the group\n", stderr);
187              return 1;
[7823]188            }
[9437]189        }
190      if (!isnumber(owner))
191        {
192          pp = getpwnam(owner);
193          if (!pp)
194            {
195              fprintf(stderr, "install: unknown user %s.\n", owner);
196              return 1;
[7823]197            }
[9437]198          else
199            uid = pp->pw_uid;
200        }
201      else
202        uid = atoi(owner);
203    }
204  else
205    uid = -1;
206
207  if (group)
208    {
209      if (!isnumber(group))
210        {
211          gp = getgrnam(group);
212          if (!gp)
213            {
214              fprintf(stderr, "install: unknown group %s.\n", group);
215              return 1;
[7823]216            }
[9437]217          else
218            gid = gp->gr_gid;
[7823]219        }
[9437]220      else
221        gid = atoi(group);
222    }
223  else
224    gid = -1;
[7823]225
[9437]226  to_name = argv[argc - 1];
227  no_target = stat(to_name, &to_sb);
228  if (!no_target && S_ISDIR(to_sb.st_mode))
229    {
230      for (; *argv != to_name; argv++)
231        install(*argv, to_name, 1);
232      return 0;
233    }
[7823]234
[9437]235  /* can't do file1 file2 directory/file */
236  if (argc != 2)
237    usage();
238
239  if (!no_target)
240    {
241      if (stat(*argv, &from_sb))
242        {
243          fprintf(stderr, "install: can't find %s.\n", *argv);
244          return 1;
[7823]245        }
[9437]246      if (!S_ISREG(to_sb.st_mode))
247        {
248          fprintf(stderr, "install: %s isn't a regular file.\n", to_name);
249          exit(1);
250        }
251      if (to_sb.st_dev == from_sb.st_dev && to_sb.st_ino == from_sb.st_ino)
252        {
253          fprintf(stderr, "install: %s and %s are the same file.\n", *argv,
254                  to_name);
255          return 1;
256        }
257
258      /* Unlink now, avoid ETXTBSY errors later. */
259      unlink(to_name);
260    }
261  install(*argv, to_name, 0);
262  return 0;
[7823]263}
264
[9437]265/* install -- build a path name and install the file */
266static void install(const char *from_name, const char *to_name, int isdir)
[7823]267{
[9437]268  struct stat from_sb;
269  struct timeval timep[2];
270  int devnull, from_fd, to_fd;
271  const char *c;
[7823]272
[9437]273  /* If trying to install "/dev/null" to a directory, fail. */
274  if (isdir || strcmp(from_name, "/dev/null"))
275    {
276      if (stat(from_name, &from_sb))
277        {
278          fprintf(stderr, "install: can't find %s.\n", from_name);
279          exit(1);
[7823]280        }
[9437]281      if (!S_ISREG(from_sb.st_mode))
282        {
283          fprintf(stderr, "install: %s isn't a regular file.\n", from_name);
284          exit(1);
285        }
[7823]286
[9437]287      /* Build the target path. */
288      if (isdir)
289        {
290          c = strrchr(from_name, '/');
291          c = (c) ? c + 1 : from_name;
292          sprintf(pathbuf, "%s/%s", to_name, c);
[7823]293        }
[9437]294      else
295        strcpy(pathbuf, to_name);
296      devnull = 0;
297    }
298  else
299    devnull = 1;
[7823]300
[9437]301  /* Unlink now, avoid ETXTBSY errors later. */
302  unlink(pathbuf);
303
304  /* Create target. */
305  to_fd = open(pathbuf, O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
306  if (to_fd < 0)
307    {
308      fprintf(stderr, "install: %s: %s\n", pathbuf, strerror(errno));
309      exit(1);
310    }
311  if (!devnull)
312    {
313      from_fd = open(from_name, O_RDONLY, 0);
314      if (from_fd < 0)
315        {
316          unlink(pathbuf);
317          bad("install: open: ", from_name);
318          exit(1);
[7823]319        }
[9437]320      copy(from_fd, from_name, to_fd, pathbuf);
321      close(from_fd);
322      close(to_fd);
323      if (dostrip)
324        strip(pathbuf);
325      if (!docopy)
326        unlink(from_name);
327    }
328  if (dotime)
329    {
330      timep[0].tv_sec = from_sb.st_atime;
331      timep[1].tv_sec = from_sb.st_mtime;
332      timep[0].tv_usec = timep[1].tv_usec = 0;
333      if (utimes(pathbuf, timep))
334        bad("install: utimes", pathbuf);
335    }
336  /* set owner, group, mode. and time for target */
337  if (chmod(pathbuf, mode))
338    bad("install: fchmod", pathbuf);
339  if ((uid != -1 || gid != -1))
340    {
341      uid = (uid != -1) ? uid : from_sb.st_uid;
342      gid = (gid != -1) ? gid : from_sb.st_gid;
343      if (chown(pathbuf, uid, gid) < 0)
344        bad("install: chown: ", to_name);
345    }
[7823]346}
347
[9437]348/* copy -- copy from one file to another */
349static void copy(int from_fd, const char *from_name, int to_fd,
350                 const char *to_name)
[7823]351{
[9437]352  int n;
353  char buf[MAXBSIZE];
[7823]354
[9437]355  while ((n = read(from_fd, buf, sizeof(buf))) > 0)
356    {
357      if (write(to_fd, buf, n) != n)
358        bad("install: write: ", to_name);
359    }
360  if (n == -1)
361      bad("install: read: ", from_name);
[7823]362}
363
[9437]364static void strip(const char *name)
[7823]365{
[9437]366  pid_t pid;
[7823]367
[9437]368  fflush(NULL);
369  pid = fork();
370  if (pid == 0)
371    {
372      execlp(STRIP, STRIP, name, NULL);
373      fprintf(stderr, "Cannot execute %s: %s\n", STRIP, strerror(errno));
374      exit(1);
375    }
376  else if (pid > 0)
377    {
378      while (waitpid(pid, NULL, 0) < 0 && errno == EINTR)
379        ;
380    }
[7823]381}
382
[9437]383/* isnumber -- determine whether string is a number */
384static int isnumber(const char *s)
[7823]385{
[9437]386  while(*s)
387    {
[14021]388      if (!isdigit((unsigned char)*s))
[9437]389        return 0;
390      else
391        s++;
392    }
393  return 1;
394}
[7823]395
[9437]396/* atoo -- octal string to int */
397static int atoo(const char *str)
398{
399  int val;
400
[14021]401  for (val = 0; isdigit((unsigned char)*str); ++str)
[9437]402    val = val * 8 + *str - '0';
403  return val;
[7823]404}
405
[9437]406/* bad -- remove created target and die */
407static void bad(const char *head, const char *str)
[7823]408{
[9437]409  fprintf(stderr, "%s%s: %s\n", head, str, strerror(errno));
410  unlink(pathbuf);
411  exit(1);
[7823]412}
413
[9437]414/* usage -- print a usage message and die */
[14021]415static void usage(void)
[7823]416{
[9437]417  fputs("usage: install [-cds] [-g group] [-m mode] [-o owner]"
418        " file1 file2;\n\tor file1 ... fileN directory\n", stderr);
419  exit(1);
[7823]420}
Note: See TracBrowser for help on using the repository browser.