Fixes for dtfile on OpenBSD, plus missing prototypes. Use statfs() on BSD to find...
[oweals/cde.git] / cde / programs / dtfile / MkDir.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $TOG: MkDir.c /main/8 1999/12/09 13:06:55 mgreess $ */
24 /************************************<+>*************************************
25  ****************************************************************************
26  *
27  *   FILE:           MkDir.c
28  *
29  *   COMPONENT_NAME: Desktop File Manager (dtfile)
30  *
31  *   Description:    Contains functions to make directories.
32  *
33  *   FUNCTIONS: RunFileCommand
34  *
35  *   (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
36  *   (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
37  *   (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
38  *   (c) Copyright 1993, 1994, 1995 Novell, Inc.
39  *
40  ****************************************************************************
41  ************************************<+>*************************************/
42
43 #include <sys/param.h>
44 #include <sys/types.h>
45
46 #include <fcntl.h>
47 #include <sys/stat.h>
48 #include <signal.h>
49 #include <errno.h>
50
51 #include <unistd.h>
52 #include <limits.h>
53 #ifdef __hpux
54 #include <sys/getaccess.h>
55 #endif
56
57 #include <Xm/Xm.h>
58
59 #include <Dt/EnvControlP.h>
60 #include <Dt/DtNlUtils.h>
61 #include "Encaps.h"
62
63 #include "FileMgr.h"
64 #include "Desktop.h"
65 #include "Main.h"
66
67 /************************************************************************
68  *
69  *  RunFileCommand
70  *    This function is called to fork a process and run a command in the
71  *    event that PAM does not have the appropriate capabilities (i.e. PAM
72  *    isn't superuser and superuser privilege is required).  PAM waits 
73  *    for completion of the forked command process.
74  *
75  *  PARAMETERS:
76  *
77  *    command_path -- this is a pointer to the full pathname of the
78  *    command to be run.
79  *
80  *    argument1 -- this is a pointer to the first command argument.
81  *
82  *    argument2 -- this is a pointer to the second command argument.
83  *
84  *    argument3 -- this is a pointer to the third command argument.
85  *
86  *  RETURN VALUE:
87  *
88  *    If the command could not be run or if it exited with a non-zero
89  *    value then a non-zero value is returned.
90  *
91  ************************************************************************/
92
93 int
94 RunFileCommand(
95         register char *command_path,
96         register char *argument1,
97         register char *argument2,
98         register char *argument3)
99 {
100    static char *pname = "RunFileCommand";
101    register int child;           /* process id of command process */
102    register int wait_return;     /* return value from wait */
103             int exit_value;      /* command exit value */
104    register char *command_name;  /* pointer to the command name */
105    register int i;
106    void (*oldSig)();
107   
108    /* prepare to catch the command termination */
109
110    oldSig = signal (SIGCHLD, SIG_DFL);
111
112    /* fork a process to run command */
113
114    if ((child = fork ()) < 0)   /* fork failed */
115    {
116       (void) signal (SIGCHLD, oldSig);
117       return (-1);
118    }
119
120    if (child != 0)              /* parend (PAM) process */
121    {
122       DBGFORK(("%s:  forked child<%d>\n", pname, child));
123
124       do                        /* wait for completion of command */
125       {
126          wait_return = wait (&exit_value);
127       } while (wait_return != child);
128
129       (void) signal (SIGCHLD, oldSig); /* child stopped or terminated */
130
131       return (exit_value);      /* if exit_value == 0 then success */
132    }
133     
134
135    DBGFORK(("%s:  child forked\n", pname));
136
137    /*  child (command) process  */
138
139    /*  redirect stdin, stdout, stderr to /dev/null  */
140
141    for (i = 0; i < 3; i++)
142    {
143       (void) close (i);
144       (void) open ("/dev/null", O_RDWR);
145    }
146
147
148    /*  set pointer to simple command name  */
149
150    if ((command_name = (char *)strrchr (command_path, '/')) == 0)
151       command_name = command_path;
152    else
153       command_name++;
154
155    _DtEnvControl(DT_ENV_RESTORE_PRE_DT);
156    (void) execl (command_path, command_name, argument1, argument2, argument3,0);
157     
158    DBGFORK(("%s:  child exiting\n", pname));
159
160    exit (-1);                     /* error exit */
161 }