futil.c revision 67429
1327Sjkh#ifndef lint
230221Scharnierstatic const char rcsid[] =
350479Speter  "$FreeBSD: head/usr.sbin/pkg_install/add/futil.c 67429 2000-10-22 09:53:27Z jkh $";
4327Sjkh#endif
5327Sjkh
6327Sjkh/*
7327Sjkh * FreeBSD install - a package for the installation and maintainance
8327Sjkh * of non-core utilities.
9327Sjkh *
10327Sjkh * Redistribution and use in source and binary forms, with or without
11327Sjkh * modification, are permitted provided that the following conditions
12327Sjkh * are met:
13327Sjkh * 1. Redistributions of source code must retain the above copyright
14327Sjkh *    notice, this list of conditions and the following disclaimer.
15327Sjkh * 2. Redistributions in binary form must reproduce the above copyright
16327Sjkh *    notice, this list of conditions and the following disclaimer in the
17327Sjkh *    documentation and/or other materials provided with the distribution.
18327Sjkh *
19327Sjkh * Jordan K. Hubbard
20327Sjkh * 18 July 1993
21327Sjkh *
22327Sjkh * Miscellaneous file access utilities.
23327Sjkh *
24327Sjkh */
25327Sjkh
2630221Scharnier#include <err.h>
27327Sjkh#include "lib.h"
28327Sjkh#include "add.h"
29327Sjkh
30327Sjkh/*
31327Sjkh * Assuming dir is a desired directory name, make it and all intervening
32327Sjkh * directories necessary.
33327Sjkh */
34327Sjkh
35327Sjkhint
36327Sjkhmake_hierarchy(char *dir)
37327Sjkh{
38327Sjkh    char *cp1, *cp2;
398857Srgrimes
40327Sjkh    if (dir[0] == '/')
41327Sjkh	cp1 = cp2 = dir + 1;
42327Sjkh    else
43327Sjkh	cp1 = cp2 = dir;
44327Sjkh    while (cp2) {
4567429Sjkh	if ((cp2 = strchr(cp1, '/')) !=NULL )
46327Sjkh	    *cp2 = '\0';
47327Sjkh	if (fexists(dir)) {
4857330Sjkh	    if (!isdir(dir)) {
4957330Sjkh		if (cp2)
5057330Sjkh		    *cp2 = '/';
51327Sjkh		return FAIL;
5257330Sjkh	    }
53327Sjkh	}
54327Sjkh	else {
5557330Sjkh	    if (vsystem("mkdir %s", dir)) {
5657330Sjkh		if (cp2)
5757330Sjkh		    *cp2 = '/';
58327Sjkh		return FAIL;
5957330Sjkh	    }
60327Sjkh	    apply_perms(NULL, dir);
61327Sjkh	}
62327Sjkh	/* Put it back */
63327Sjkh	if (cp2) {
64327Sjkh	    *cp2 = '/';
65327Sjkh	    cp1 = cp2 + 1;
66327Sjkh	}
67327Sjkh    }
68327Sjkh    return SUCCESS;
69327Sjkh}
70327Sjkh
71327Sjkh/* Using permission defaults, apply them as necessary */
72327Sjkhvoid
734996Sjkhapply_perms(char *dir, char *arg)
74327Sjkh{
754996Sjkh    char *cd_to;
76327Sjkh
774996Sjkh    if (!dir || *arg == '/')	/* absolute path? */
784996Sjkh	cd_to = "/";
79327Sjkh    else
804996Sjkh	cd_to = dir;
814996Sjkh
82327Sjkh    if (Mode)
834996Sjkh	if (vsystem("cd %s && chmod -R %s %s", cd_to, Mode, arg))
8430221Scharnier	    warnx("couldn't change modes of '%s' to '%s'", arg, Mode);
854996Sjkh    if (Owner && Group) {
864996Sjkh	if (vsystem("cd %s && chown -R %s.%s %s", cd_to, Owner, Group, arg))
8730221Scharnier	    warnx("couldn't change owner/group of '%s' to '%s.%s'",
884996Sjkh		   arg, Owner, Group);
894996Sjkh	return;
904996Sjkh    }
914996Sjkh    if (Owner) {
924996Sjkh	if (vsystem("cd %s && chown -R %s %s", cd_to, Owner, arg))
9330221Scharnier	    warnx("couldn't change owner of '%s' to '%s'", arg, Owner);
944996Sjkh	return;
954996Sjkh    } else if (Group)
964996Sjkh	if (vsystem("cd %s && chgrp -R %s %s", cd_to, Group, arg))
9730221Scharnier	    warnx("couldn't change group of '%s' to '%s'", arg, Group);
98327Sjkh}
99327Sjkh
100