/usr/share/ada/adainclude/texttools/system.c is in libtexttools3-dev 2.0.7-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | #include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <unistd.h> /* unlink */
#include <errno.h> /* red hat */
#include <wait.h> /* for runit */
#include <dirent.h>
#include <error.h>
/*----------------------------*/
/* */
/* System C commands from Ada */
/* */
/*----------------------------*/
char Interface_String[255]; /* for passing data back/forth */
char SessionLogName[80]; /* for the session log - set from ada */
char s[81]; /* temporary string */
const char LongDateFormat[] = "%A, %B %d, %Y";
const char DateFormat[] = "%m/%d/%Y";
const char TimeFormat[] = "%T";
/* C_errno -- binding to errno variable. Under Gnat 5.x, */
/* errno is not visible to Ada for some reason. */
int C_errno() {
return errno;
}
void C_reset_errno() {
errno = 0;
}
/* CFileExists -- determine if a file exists */
char CFileExists( char * path ) {
struct stat statinfo;
int result;
if ( stat( path, &statinfo ) ) {
result = 0;
} else {
result = 1;
}
return result;
}
/* CLongDate -- get long date and return result in Interface_String */
void CLongDate() {
time_t t;
struct tm *tm;
time( &t );
tm = localtime( &t );
strftime( Interface_String, 255, LongDateFormat, tm );
}
/* CDate -- get date and return result in Interface_String */
void CDate() {
time_t t;
struct tm *tm;
time( &t );
tm = localtime( &t );
strftime( Interface_String, 255, DateFormat, tm );
}
/* CTime -- get long date and return result in Interface_String */
void CTime() {
time_t t;
struct tm *tm;
time( &t );
tm = localtime( &t );
strftime( Interface_String, 255, TimeFormat, tm );
}
/* CUNIX -- Execute UNIX Shell Command */
int CUNIX()
{
return system( Interface_String );
}
/* GETTEMPNAME -- Get Name for a Temporary File */
/*void GetTempName()
{
strcpy( Interface_String, "/tmp/pegasoftXXXXXX" );
mktemp( Interface_String );
}*/
/* CGetPID -- get my process ID */
long CGetPID() {
long longid;
longid = 0;
longid = getpid();
return longid;
}
/* CGetTimeStamp -- return a time stamp (number of seconds since 1970 */
/* no longer used
long CGetTimeStamp() {
long longtime;
time_t temptime;
longtime = 0;
time( &temptime );
longtime = temptime;
return longtime;
}
*/
/* CGetUNIX -- Execute UNIX Shell Command, return result */
int CGetUNIX (char * temppath) {
int length = 0;
int result; /* result of system call */
FILE *outfile; /* file to read results from */
char *tmpptr = temppath; /* don't manipulate param ptr directly */
/* Redirect command output to temppath file */
do {
if (length >= sizeof(Interface_String)) {
printf ("CGETUNIX: command too long.\n");
Interface_String[0] = 0;
return -1; } }
while (Interface_String[length++] != 0);
Interface_String[length-1] = '>';
do {
if (length >= sizeof(Interface_String)) {
printf ("CGETUNIX: temppath too long.\n");
Interface_String[0] = 0;
return -1; } }
while ((Interface_String[length++] = *tmpptr++) != 0);
/* Execute system call */
result = system (Interface_String);
if (result != 0) {
Interface_String[0] = 0;
return result; }
/* Return command output in interface string */
if ((outfile = fopen (temppath, "r")) == NULL) {
error (0, errno, "CGetUNIX: fopen");
Interface_String[0] = 0;
return -1; }
if (fgets (Interface_String, sizeof(Interface_String), outfile)) {
error (EXIT_FAILURE, 0, "CGetUNIX: fgets failed.\n"); }
if (fclose (outfile) != 0) {
error (EXIT_FAILURE, errno, "CGetUNIX: fclose"); }
if (unlink (temppath) != 0) {
error (EXIT_FAILURE, errno, "CGetUNIX: unlink"); }
return 0; }
/* CNICE -- Change Process Priority */
void CNice (int change) {
if (nice (change) == -1) {
error (EXIT_FAILURE, errno, "CNice: nice"); } }
/* CNotEmpty -- check for an empty file (>0 if not empty) */
char CNotEmpty()
{
int f;
/* int b; */
/* struct stat buf; */
/* char buf1; */
char buf2[255];
strcpy( buf2, "test -s " );
if ( strcat( buf2, Interface_String ) <= 0 ) {
printf("CNotEmpty: bad string: '%s'\n",buf2);
return 1;
} else {
f = system( buf2 );
if (f == 0)
return 0;
else
return 1;
}
/*
f = open( &Interface_String[0], O_RDONLY );
if (f == -1) return( 0 );
b = read( f, &buf1, sizeof(buf) );
close( f );
return( b );
*/
}
/* LOCKIT -- Lock a file, return 0 if success */
int LockIt()
{
const int f = creat (Interface_String, O_RDWR);
if (f == -1)
return (-1);
return (close(f));
}
/* UNLOCKIT -- Unlock a file */
void UnlockIt()
{
unlink( Interface_String );
}
/* CSETPATH */
int CSetPath()
{
return chdir( Interface_String );
}
/* CSYNC */
void CSync() {
sync();
sync();
sync();
}
long Crealtotalmem() {
unsigned long amount;
FILE* fp;
if ((fp=fopen ("/proc/meminfo", "r")) == NULL) {
error (0, errno, "Crealtotalmem: fopen");
return 0; }
/* skip headings */
if (fgets (s, 80, fp) == NULL) {
error (EXIT_FAILURE, 0, "Crealtotalmem: fgets failed.\n"); }
if (fscanf (fp, "%80s %lu", s, &amount ) == EOF) {
error (EXIT_FAILURE, 0, "Crealtotalmem: fscanf failed.\n"); }
if (fclose (fp) != 0) {
error (EXIT_FAILURE, errno, "Crealtotalmem: fclose"); }
return (long)amount; }
long Crealfreemem() {
unsigned long amount;
FILE* fp;
if ((fp=fopen ("/proc/meminfo", "r")) == NULL) {
error (0, errno, "Crealfreemem: fopen");
return 0; }
/* skip headings */
if (fgets (s, 80, fp) == NULL) {
error (EXIT_FAILURE, 0, "Crealfreemem: fgets failed.\n"); }
if (fscanf (fp, "%80s %lu %lu %lu", s, &amount, &amount, &amount) == EOF) {
error (EXIT_FAILURE, 0, "Crealfreemem: fscanf failed.\n"); }
if (fclose (fp) != 0) {
error (EXIT_FAILURE, errno, "Crealfreemem: fclose"); }
return (long)amount; }
long Ctotalmem() {
unsigned long amount;
unsigned long swapamount;
unsigned long t; /* discard stuff we don't want*/
FILE* fp;
if ((fp=fopen ("/proc/meminfo", "r")) == NULL) {
error (0, errno, "Crealfreemem: fopen");
return 0; }
/* skip headings */
if (fgets (s, 80, fp) == NULL) {
error (EXIT_FAILURE, 0, "Crealfreemem: fgets failed.\n"); }
if (fscanf (fp, "%80s %lu. %lu %lu %lu %lu %lu", s, &amount, &t, &t, &t, &t, &t) == EOF
|| fscanf (fp, "%80s %lu", s, &swapamount) == EOF) {
error (EXIT_FAILURE, 0, "Ctotalmem: fscanf failed.\n"); }
if (fclose (fp) != 0) {
error (EXIT_FAILURE, errno, "Crealfreemem: fclose"); }
return (long)(amount+swapamount); }
long Cfreemem() {
unsigned long amount = 0;
unsigned long swapamount;
unsigned long t; /* discard stuff we don't want*/
FILE* fp;
if ((fp=fopen ("/proc/meminfo", "r")) == NULL) {
error (0, errno, "Crealfreemem: fopen");
return 0; }
/* skip headings */
if (fgets (s, 80, fp) == NULL) {
error (EXIT_FAILURE, 0, "Crealfreemem: fgets failed.\n"); }
if (fscanf( fp, "%80s %lu %lu %lu %lu %lu %lu", s, &t, &t, &amount, &t, &t, &t) == EOF
|| fscanf (fp, "%80s %lu %lu %lu", s, &t, &t, &swapamount) == EOF) {
error (EXIT_FAILURE, 0, "Ctotalmem: fscanf failed.\n"); }
if (fclose (fp) != 0) {
error (EXIT_FAILURE, errno, "Crealfreemem: fclose"); }
return (long)(amount+swapamount); }
int Crnd( int limit ) {
/* uniformly distributed random numbers */
return ( rand() % limit )+1;
}
int Cnormalrnd( int limit ) {
/* normally distributed random numbers */
long sum; /* the sum of the random numbers */
int i; /* for for loop */
for (sum=0, i=1; i<=8; i++)
sum += Crnd( limit );
return ( sum / 8 );
}
int COdds( int percent ) {
return ( Crnd(100) >= percent );
}
void Csetrndseed( int seed ) {
srand( seed );
}
/* Generic Appending to a Text File */
void CAppend( char * Filename ) {
/* add a line to a file */
FILE *fp;
if (( fp=fopen( Filename, "a") ) != NULL) {
fprintf(fp, "%s\n", Interface_String );
fclose( fp );
}
}
/* Session Logging */
void CClearSessionLog() {
/* clean session log */
FILE *fp;
if ((fp=fopen(SessionLogName,"w"))!=NULL) fclose(fp);
}
void CAddSessionLog() {
/* add a message to the session log */
CAppend( SessionLogName );
}
int CUnlink( char * Filename ) {
/* delete a file */
if ( unlink( Filename ) != 0 ) {
return errno;
} else {
return 0;
}
}
void CUSleep( unsigned long usec ) {
/* delay for usec microseconds */
usleep( usec );
}
int CIsDirectory( char * path ) {
struct stat statinfo;
int result;
if ( stat( path, &statinfo ) ) {
result = 0;
} else {
if (statinfo.st_mode & S_IFDIR) {
result = 1;
} else {
result = 0;
}
}
return result;
}
int CRunIt( char * path, char * outfile,
char * param1, char * param2, char * param3 ) {
pid_t child, result;
int fd0, fd1, fd2;
int status;
int i;
if ( !(child = fork()) ) {
/* Redirect stdin, out, err */
for (i=0; i< FOPEN_MAX; ++i )
close( i );
fd0 = open( "/dev/null", O_RDONLY );
if (fd0 < 0 ) exit( 110 );
fd1 = open( outfile, O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH );
if (fd1 < 0 ) exit( 111 );
fd2 = dup( 1 );
if (param1[0]=='\0') {
execlp( path, path, NULL );
} else if (param2[0]=='\0') {
execlp( path, path, param1, NULL );
} else if (param3[0]=='\0') {
execlp( path, path, param1, param2, NULL );
} else {
execlp( path, path, param1, param2, param3, NULL );
}
/* if we got here, file probably wasn't found */
exit( errno );
}
result = waitpid( child, &status, 0 );
/* wait( &status ); */
/* if ( WIFEXITED( status ) != 0 ) */
/* status = WEXITSTATUS( status ); */
status = 112;
if ( result >= 0 ) {
status = WIFEXITED( status );
}
return status;
}
int CRunItForStdErr( char * path, char * outfile,
char * param1, char * param2, char * param3 ) {
/* dicard standard out, standard error to outfile */
/* written for uuchk */
pid_t child, result;
int fd0, fd1, fd2;
int status;
int i;
if ( !(child = fork()) ) {
/* Redirect stdin, out, err */
for (i=0; i< FOPEN_MAX; ++i ) close( i );
fd0 = open( "/dev/null", O_RDONLY );
if (fd0 < 0 ) exit( 110 );
fd1 = open( "/dev/null", O_WRONLY );
if (fd1 < 0 ) exit( 111 );
fd2 = open( outfile, O_WRONLY | O_CREAT | O_TRUNC ,
S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH );
if (fd2 < 0 ) exit( 111 );
if (param1[0]=='\0') {
execlp( path, path, NULL );
} else if (param2[0]=='\0') {
execlp( path, path, param1, NULL );
} else if (param3[0]=='\0') {
execlp( path, path, param1, param2, NULL );
} else {
execlp( path, path, param1, param2, param3, NULL );
}
/* if we got here, file probably wasn't found */
exit( errno );
}
status = 112;
result = waitpid( child, &status, 0 );
if ( result >= 0 )
status = WIFEXITED( status );
return status;
}
char * C_readdir( DIR* dp ) {
struct dirent *de;
de = readdir( dp );
if ( de == NULL )
return NULL;
else
return de->d_name;
}
|