1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
//===============================================================
// vos.cpp - Interface to OS dependent stuff (For Unix)
//
// Copyright (C) 1998 Bruce E. Wampler
//
// This file is part of the V C++ GUI Framework, and is covered
// under the terms of the GNU Library General Public License,
// Version 2. This library has NO WARRANTY. See the source file
// vapp.cxx for more complete information about license terms.
//===============================================================
#include <v/vos.h>
#include <pwd.h> // for getusername
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
const int MaxCompNameLen = 256;
//=========================>>> vOS::vOS <<<==========================
vOS::vOS()
{
// Constructor
// This will be used to determing which OS we are running
// under: e.g., NT vs 95
}
//=========================>>> vOS::~vOS <<<==========================
vOS::~vOS()
{
// Destructor
}
//=========================>>> vOS::vDeleteFile <<<==========================
int vOS::vDeleteFile(const char* filename)
{
return remove(filename) == 0;
}
//=========================>>> vOS::vChDrive <<<==========================
int vOS::vChDrive(int drive)
{
return 1; // no op on unix
}
//=========================>>> vOS::vGetUserName <<<==========================
int vOS::vGetUserName( char* s, int len)
{
// Set s to user name
struct passwd *pw;
uid_t uid;
uid = getuid();
if ((pw = getpwuid(uid)) != 0 &&
pw->pw_name != 0 && *(pw->pw_name) != 0)
{
strncpy(s, pw->pw_name, len);
return 1;
}
strcpy(s,"Unknown");
return 0; /* a number is not a name */
}
//====================>>> vOS::vGetHostName <<<==========================
void vOS::vGetHostName(char* s, int len)
{
// Get host name
gethostname((char *)s, len); // tested on linux only
}
//=========================>>> vOS::vGetPid <<<==========================
long vOS::vGetPid()
{
// return process ID
return (long) getpid();
}
//=========================>>> vOS::vGetCWD <<<==========================
int vOS::vGetCWD(char* buf, int len)
{
// Get name of current directory
if (getcwd((char *)buf, len) == NULL)
{
strcpy(buf, ".");
return 0;
}
return 1;
}
//=========================>>> vOS::vChDir <<<==========================
int vOS::vChDir(char *path)
{
if (path[0] == 0) /* just checking... */
return 0;
if (*path == 0) /* drive name only */
return 0;
return chdir(path) == 0;
}
//=========================>>> vOS::vRunProcess <<<==========================
int vOS::vRunProcess(const char* cmd, const char* StdOut, const char* StdErr,
const int Wait, const int minimize)
{
int ret = 1;
// We will support two modes - a wait mode that lets us redirect
// stdout and stderr, and a don't wait mode that doesn't allow
// redirection. This might get fixed somday if someone wants
// to fix it....
// Execute command via the shell, but redirects stdin/stdout/stderr first.
// stdout/stderr are appended, not overwritten.
if (Wait)
{
if (StdOut && *StdOut) // change stdout
{
if (!freopen(StdOut, "w", stdout))
return 0;
}
if (StdErr && *StdErr) // change stdout
{
if (!freopen(StdErr, "w", stderr))
return 0;
}
ret = system(cmd);
}
else // need to wait, so need to fork...
{
#define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
127, some shell use that already */
char* argv[10]; // up to 10 args
int argcnt;
char args[256];
int inquote;
pid_t pid;
int status = -1;
if (strlen(cmd) >= 255) // don't allow too much!
return EXEC_FAILED;
strcpy(args,cmd); // make a copy to work with
char* p = args;
for (argcnt = 0; *p && argcnt < 10 ; ++argcnt)
{
inquote = 0;
argv[argcnt] = p; // current arg
for ( ; ; )
{
while (*p && (inquote || (*p != ' ' && *p != '\t')))
{
if (*p == '"')
inquote = !inquote;
++p;
}
if (*p == 0)
break;
*p++ = 0; // terminate the arg
while (*p && (*p == ' ' || *p == '\t'))
++p; // skip white space
}
}
argv[argcnt] = 0;
if ((pid = fork()) == -1) /* maybe we should use vfork() */
{
return EXEC_FAILED;
}
else if (pid == 0) // CHILD - do the execvp
{
execvp(argv[0], argv); // should not return!
exit(EXEC_FAILED); // exec failed, return failure code
}
else // PARENT FORK
{
// Wait until child has exited.
#ifdef WAIT_EXECVP
wait(&status);
#endif
ret = 0;
}
}
return ret;
}
|