dthelp: Change to ANSI function definitions
[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 libraries 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 #include <sys/wait.h>
51
52 #include <unistd.h>
53 #include <limits.h>
54 #ifdef __hpux
55 #include <sys/getaccess.h>
56 #endif
57
58 #include <Xm/Xm.h>
59
60 #include <Dt/EnvControlP.h>
61 #include <Dt/DtNlUtils.h>
62 #include "Encaps.h"
63
64 #include "FileMgr.h"
65 #include "Desktop.h"
66 #include "Main.h"
67
68 /************************************************************************
69  *
70  *  RunFileCommand
71  *    This function is called to fork a process and run a command in the
72  *    event that PAM does not have the appropriate capabilities (i.e. PAM
73  *    isn't superuser and superuser privilege is required).  PAM waits 
74  *    for completion of the forked command process.
75  *
76  *  PARAMETERS:
77  *
78  *    command_path -- this is a pointer to the full pathname of the
79  *    command to be run.
80  *
81  *    argument1 -- this is a pointer to the first command argument.
82  *
83  *    argument2 -- this is a pointer to the second command argument.
84  *
85  *    argument3 -- this is a pointer to the third command argument.
86  *
87  *  RETURN VALUE:
88  *
89  *    If the command could not be run or if it exited with a non-zero
90  *    value then a non-zero value is returned.
91  *
92  ************************************************************************/
93
94 int
95 RunFileCommand(
96         register char *command_path,
97         register char *argument1,
98         register char *argument2,
99         register char *argument3)
100 {
101    static char *pname = "RunFileCommand";
102    register int child;           /* process id of command process */
103    register int wait_return;     /* return value from wait */
104             int exit_value;      /* command exit value */
105    register char *command_name;  /* pointer to the command name */
106    register int i;
107    void (*oldSig)();
108   
109    /* prepare to catch the command termination */
110
111    oldSig = signal (SIGCHLD, SIG_DFL);
112
113    /* fork a process to run command */
114
115    if ((child = fork ()) < 0)   /* fork failed */
116    {
117       (void) signal (SIGCHLD, oldSig);
118       return (-1);
119    }
120
121    if (child != 0)              /* parend (PAM) process */
122    {
123       DBGFORK(("%s:  forked child<%d>\n", pname, child));
124
125       do                        /* wait for completion of command */
126       {
127          wait_return = wait (&exit_value);
128       } while (wait_return != child);
129
130       (void) signal (SIGCHLD, oldSig); /* child stopped or terminated */
131
132       return (exit_value);      /* if exit_value == 0 then success */
133    }
134     
135
136    DBGFORK(("%s:  child forked\n", pname));
137
138    /*  child (command) process  */
139
140    /*  redirect stdin, stdout, stderr to /dev/null  */
141
142    for (i = 0; i < 3; i++)
143    {
144       (void) close (i);
145       (void) open ("/dev/null", O_RDWR);
146    }
147
148
149    /*  set pointer to simple command name  */
150
151    if ((command_name = (char *)strrchr (command_path, '/')) == 0)
152       command_name = command_path;
153    else
154       command_name++;
155
156    _DtEnvControl(DT_ENV_RESTORE_PRE_DT);
157    (void) execl (command_path, command_name, argument1, argument2, argument3,0);
158     
159    DBGFORK(("%s:  child exiting\n", pname));
160
161    exit (-1);                     /* error exit */
162 }