syscyg.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /* syscyg.c Copyright (C) 1989-2002 Codemist Ltd */
  2. /*
  3. * System-specific code for use with the "cygwin" compilers and
  4. * libraries that provide a sort of Unix work-alike environment while
  5. * hosted on Windows (95, 98 or NT, 2000).
  6. */
  7. /*
  8. * This code may be used and modified, and redistributed in binary
  9. * or source form, subject to the "CCL Public License", which should
  10. * accompany it. This license is a variant on the BSD license, and thus
  11. * permits use of code derived from this in either open and commercial
  12. * projects: but it does require that updates to this code be made
  13. * available back to the originators of the package.
  14. * Before merging other code in with this or linking this code
  15. * with other packages or libraries please check that the license terms
  16. * of the other material are compatible with those of this.
  17. */
  18. /* Signature: 61611e22 21-Apr-2002 */
  19. #include "machine.h"
  20. #include <sys/stat.h>
  21. #ifndef NO_UNISTD_AVAILABLE
  22. /*
  23. * Posix mandates a header <unistd.h>, which is why I feel entitled to
  24. * include it here. But for systems that do not I can assert
  25. * NO_UNISTD_AVAILABLE in machine.h and worry about other ways to
  26. * reference the relevant facilities...
  27. */
  28. #include <unistd.h>
  29. #endif
  30. #include <stdarg.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include <sys/types.h>
  35. #include <dirent.h>
  36. #include <errno.h>
  37. #include "tags.h"
  38. #include "externs.h"
  39. #include "sys.h"
  40. #ifdef TIMEOUT
  41. #include "timeout.h"
  42. #endif
  43. #include "filename.c"
  44. int change_directory(char *filename, char *old, size_t n)
  45. {
  46. process_file_name(filename, old, n);
  47. if (*filename == 0) return 1;
  48. if (chdir(filename))
  49. { char err_buf[LONGEST_LEGAL_FILENAME+100];
  50. char *msg;
  51. switch (errno)
  52. {
  53. case ENOTDIR:
  54. msg = "A component of %s is not a directory.";
  55. break;
  56. case ENOENT:
  57. msg = "The directory %s does not exist.";
  58. break;
  59. case EACCES:
  60. msg = "Insufficient permission for %s.";
  61. break;
  62. /*
  63. * This symbol seems not to be available under HP versions of Unix.
  64. * Since I am just producing pretty error messages here the loss of
  65. * functionality missing it out is pretty minor...
  66. */
  67. case ELOOP:
  68. msg = "Pathname %s has too many symbolic links.";
  69. break;
  70. case ENAMETOOLONG:
  71. msg = "The pathname %s is too long.";
  72. break;
  73. default:
  74. msg = "Cannot change directory to %s.";
  75. break;
  76. }
  77. sprintf(err_buf, msg, filename);
  78. aerror0(err_buf);
  79. return 1;
  80. }
  81. else return 0;
  82. }
  83. int create_directory(char *filename, char *old, size_t n)
  84. {
  85. process_file_name(filename, old, n);
  86. if (*filename == 0) return 1;
  87. return mkdir(filename, 0770);
  88. }
  89. static void remove_files(char *name, int dirp, long int size)
  90. /* Remove a file, or a directory and all its contents */
  91. {
  92. switch (dirp)
  93. {
  94. case 0: /* SCAN_FILE */
  95. remove(name);
  96. return;
  97. case 2: /* SCAN_ENDDIR */
  98. rmdir(name);
  99. return;
  100. default: /* 1 == SCAN_STARTDIR */
  101. return;
  102. }
  103. }
  104. int delete_file(char *filename, char *old, size_t n)
  105. {
  106. process_file_name(filename, old, n);
  107. if (*filename == 0) return 0;
  108. /*
  109. * We cannot simply use remove here, since this will not
  110. * work with directories and their contents. Hence the
  111. * use of scan_directory.
  112. */
  113. scan_directory(filename, remove_files);
  114. return 0;
  115. }
  116. /* extern char *getcwd(char *s, size_t n); in case unistd not used */
  117. int get_current_directory(char *s, int n)
  118. {
  119. #ifdef NO_GETCWD
  120. aerror0("cannot get current directory name.");
  121. *s = 0;
  122. return 0;
  123. #else
  124. if (getcwd(s, n) == 0)
  125. { switch(errno)
  126. {
  127. case ERANGE:
  128. aerror0("the pathname of the current directory is too long.");
  129. break;
  130. case EACCES:
  131. aerror0("insufficient permission to get pathname.");
  132. break;
  133. default:
  134. aerror0("cannot get current directory name.");
  135. break;
  136. }
  137. *s = 0;
  138. return 0;
  139. }
  140. else return strlen(s);
  141. #endif
  142. }
  143. #ifndef S_IFMT
  144. #ifdef __S_IFMT
  145. #define S_IFMT __S_IFMT
  146. #endif
  147. #endif
  148. #ifndef S_IFDIR
  149. #ifdef __S_IFDIR
  150. #define S_IFDIR __S_IFDIR
  151. #endif
  152. #endif
  153. int directoryp(char *filename, char *old, size_t n)
  154. {
  155. struct stat buf;
  156. process_file_name(filename, old, n);
  157. if (*filename == 0) return 0;
  158. if (stat(filename,&buf) == -1) return 0;
  159. return ((buf.st_mode & S_IFMT) == S_IFDIR);
  160. }
  161. char *get_truename(char *filename, char *old, size_t n)
  162. {
  163. struct stat buf;
  164. char *temp, *fn, *dir, *pwd;
  165. process_file_name(filename, old, n);
  166. if (*filename == 0) aerror("truename");
  167. /* Find out whether we have a file or a directory */
  168. if (stat(filename,&buf) == -1) aerror0("truename: cannot stat file");
  169. /* Store current directory */
  170. /* /*
  171. * The next line is UNSATISFACTORY because Posix explicitly says (at least in
  172. * in the copy of 1003.1 that I have) that getcwd has undefined behaviour
  173. * if its first argument is NULL.
  174. */
  175. if ((pwd = (char *)getcwd((char *)NULL, LONGEST_LEGAL_FILENAME)) == NULL)
  176. aerror0("truename: cannot get current working directory");
  177. if ((buf.st_mode & S_IFMT) == S_IFDIR)
  178. { /* We have a directory */
  179. char *dir = (char*)(*malloc_hook)(LONGEST_LEGAL_FILENAME);
  180. if (chdir(filename) != 0)
  181. aerror0("truename: cannot change directory");
  182. if (getcwd(dir,LONGEST_LEGAL_FILENAME) == NULL)
  183. aerror0("truename: cannot get current working directory");
  184. if (chdir(pwd) != 0)
  185. aerror0("truename: cannot change directory");
  186. (*free_hook)(pwd);
  187. /*
  188. * Axiom-specific hack: truename preserves '/' at the end of
  189. * a path
  190. */
  191. if (old[n-1] == '/' && dir[strlen(dir)-1] != '/')
  192. { n = strlen(dir);
  193. dir[n] = '/';
  194. dir[n+1] = '\0';
  195. }
  196. return dir;
  197. }
  198. else
  199. { /* Assume we have some kind of file */
  200. temp = strrchr(filename,'/');
  201. if (temp)
  202. { /* Found a directory component */
  203. fn = (char *)(*malloc_hook)(1+strlen(temp));
  204. strcpy(fn, temp); /* strdup(temp); */
  205. *temp = '\0';
  206. /* fn is now "/file" and filename is the directory */
  207. if (chdir(filename) != 0)
  208. aerror0("truename: cannot change directory");
  209. /* /* getcwd(NULL,...) invalid */
  210. if ((temp = (char *)getcwd((char *)NULL,LONGEST_LEGAL_FILENAME)) == NULL)
  211. aerror0("truename: cannot get current working directory");
  212. if (chdir(pwd) != 0)
  213. aerror0("truename: cannot change directory");
  214. dir = (char *)(*malloc_hook)((strlen(temp) + strlen(fn) + 1)*sizeof(char));
  215. /* /*
  216. * No check for malloc failure...
  217. */
  218. strcpy(dir, temp);
  219. (*free_hook)(temp);
  220. (*free_hook)(pwd);
  221. strcat(dir, fn);
  222. (*free_hook)(fn);
  223. return dir;
  224. }
  225. else
  226. { dir = (char *)(*malloc_hook)((strlen(pwd) + strlen(filename) + 2)*sizeof(char));
  227. /* /* No check for malloc failure */
  228. strcpy(dir,pwd);
  229. strcat(dir, "/");
  230. strcat(dir, filename);
  231. (*free_hook)(pwd);
  232. return dir;
  233. }
  234. }
  235. }
  236. #ifndef DO_NOT_USE_STAT
  237. int file_readable(char *filename, char *old, size_t n)
  238. {
  239. struct stat buf;
  240. process_file_name(filename, old, n);
  241. if (*filename == 0) return 0;
  242. if (stat(filename,&buf) == -1)
  243. return 0; /* File probably does not exist */
  244. else if (geteuid() == buf.st_uid)
  245. return (buf.st_mode & S_IRUSR);
  246. else if (getegid() == buf.st_gid)
  247. return (buf.st_mode & S_IRGRP);
  248. else
  249. return (buf.st_mode & S_IROTH);
  250. }
  251. int file_writeable(char *filename, char *old, size_t n)
  252. {
  253. struct stat buf;
  254. process_file_name(filename, old, n);
  255. if (*filename == 0) return 0;
  256. if (stat(filename,&buf) == -1)
  257. return 0; /* Should we check to see if the directory is writeable? */
  258. else if (geteuid() == buf.st_uid)
  259. return (buf.st_mode & S_IWUSR);
  260. else if (getegid() == buf.st_gid)
  261. return (buf.st_mode & S_IWGRP);
  262. else
  263. return (buf.st_mode & S_IWOTH);
  264. }
  265. #else
  266. int file_readable(char *filename, char *old, size_t n)
  267. {
  268. FILE *fp;
  269. process_file_name(filename, old, n);
  270. if (*filename == 0) return 0;
  271. /* The "correct" way to do this is via stat, but this is much simpler! */
  272. fp = fopen(filename,"r");
  273. if (fp == NULL) return 0;
  274. else
  275. { fclose(fp);
  276. return 1;
  277. }
  278. }
  279. int file_writeable(char *filename, char *old, size_t n)
  280. {
  281. FILE *fp;
  282. process_file_name(filename, old, n);
  283. if (*filename == 0) return 0;
  284. fp = fopen(filename,"a");
  285. if (fp == NULL) return 0;
  286. else
  287. { fclose(fp);
  288. return 1;
  289. }
  290. }
  291. #endif
  292. int rename_file(char *from_name, char *from_old, size_t from_size,
  293. char *to_name, char *to_old, size_t to_size)
  294. {
  295. process_file_name(from_name, from_old, from_size);
  296. process_file_name(to_name, to_old, to_size);
  297. if (*from_name == 0 || *to_name == 0) return 0;
  298. return rename(from_name,to_name);
  299. }
  300. #ifdef NAG_VERSION
  301. int list_directory_members(char *filename, char *old, char **filelist[],
  302. size_t n)
  303. { struct dirent **namelist;
  304. int number_of_entries, i;
  305. char **files;
  306. process_file_name(filename, old, n);
  307. /* scandir expects "." for the current directory */
  308. if (*filename == 0) number_of_entries = scandir(".",&namelist,NULL,NULL);
  309. else number_of_entries = scandir(filename,&namelist,NULL,NULL);
  310. /*
  311. * If the scandir failed then return now, since we make an assumption later
  312. * that we found at least two entries: "." and "..".
  313. */
  314. if (number_of_entries == -1) return -1;
  315. files=(char **)(*malloc_hook)(number_of_entries*sizeof(char *));
  316. for (i=0;i<number_of_entries;++i)
  317. { files[i] = strdup(namelist[i]->d_name);
  318. (*free_hook)(namelist[i]);
  319. }
  320. (*free_hook)(namelist);
  321. *filelist = files;
  322. /*
  323. * When we return we will prepend the directory name to the files, so we
  324. * must make sure it is suitable for that. This is done here since it is
  325. * platform dependent (i.e. in DOS we would need to ensure the last
  326. * character was "\").
  327. */
  328. /*
  329. i=strlen(filename);
  330. if (i > 0 && filename[i-1] != '/')
  331. { filename[i]='/';
  332. filename[i+1]='\0';
  333. }
  334. */
  335. return number_of_entries;
  336. }
  337. #else
  338. void list_directory_members(char *filename, char *old,
  339. size_t n, directory_callback *fn)
  340. {
  341. process_file_name(filename, old, n);
  342. scan_files(filename, fn);
  343. }
  344. #endif
  345. CSLbool file_exists(char *filename, char *old, size_t n, char *tt)
  346. /*
  347. * This returns YES if the file exists, and as a side-effect copies a
  348. * textual form of the last-changed-time of the file into the buffer tt.
  349. */
  350. {
  351. struct stat statbuff;
  352. process_file_name(filename, old, n);
  353. if (*filename == 0) return NO;
  354. if (stat(filename, &statbuff) != 0) return NO;
  355. strcpy(tt, ctime(&(statbuff.st_mtime)));
  356. return YES;
  357. }
  358. /*
  359. * getenv() is a mild pain in two respects - firstly Ultrix uses
  360. * a non-ANSI definition (using 2 args not 1), and the MSDOS seems
  361. * to have a strong preference for upper case names. To allow for
  362. * all this I do not call getenv() directly but go via the following
  363. * code that can patch things up.
  364. *
  365. * It APPEARS that the cygwin environment wants parameter names to be passed
  366. * in upper case regardless of anything. This is similar to the DOS/Windows
  367. * situation.
  368. */
  369. #ifdef TWO_ARG_GETENV
  370. char *my_getenv(char *s)
  371. {
  372. char uppercasename[LONGEST_LEGAL_FILENAME];
  373. char *p = uppercasename;
  374. int c;
  375. while ((c = *s++) != 0) *p++ = toupper(c);
  376. *p = 0;
  377. static char value[LONGEST_LEGAL_FILENAME];
  378. getenv(uppercasename, value);
  379. return value;
  380. }
  381. #else
  382. char *my_getenv(char *s)
  383. {
  384. char uppercasename[LONGEST_LEGAL_FILENAME];
  385. char *p = uppercasename;
  386. int c;
  387. while ((c = *s++) != 0) *p++ = toupper(c);
  388. *p = 0;
  389. return getenv(uppercasename);
  390. }
  391. #endif
  392. int my_system(char *s)
  393. {
  394. return system(s);
  395. }
  396. FILE *my_popen(char *a, char *b)
  397. {
  398. #ifdef NCC_LIB
  399. return NULL;
  400. #else
  401. return (FILE *)popen(a, b);
  402. #endif
  403. }
  404. void my_pclose(FILE *a)
  405. {
  406. #ifndef NCC_LIB
  407. pclose(a);
  408. #endif
  409. }
  410. #ifndef DO_NOT_USE_GETUID
  411. /*
  412. * "machine.h" should set DO_NOT_USE_GETUID if that function is not
  413. * properly available. Not having it will make the treatment of
  414. * (eg) "~xxx/..." in filenames less satisfactory.
  415. */
  416. #include <pwd.h>
  417. int get_home_directory(char *b, int len)
  418. {
  419. int i;
  420. struct passwd *pw = getpwuid(getuid());
  421. strcpy(b, pw->pw_dir);
  422. i = strlen(b);
  423. /* Here the directory handed back has "/" forced in as its final character */
  424. if ( b[i-1] != '/')
  425. { b[i++] = '/';
  426. b[i] = 0;
  427. }
  428. return i;
  429. }
  430. int get_users_home_directory(char *b, int len)
  431. {
  432. struct passwd *pw = getpwnam(b);
  433. if (pw != NULL) strcpy(b, pw->pw_dir);
  434. else strcpy(b, "."); /* use current directory if getpwnam() fails */
  435. return strlen(b);
  436. }
  437. #else /* USE_GETUID */
  438. int get_home_directory(char *b, int len)
  439. {
  440. int i;
  441. strcpy(b, getenv("HOME")); /* Probably works with most shells */
  442. i = strlen(b);
  443. if ( b[i-1] != '/')
  444. { b[i++] = '/';
  445. b[i] = 0;
  446. }
  447. return i;
  448. }
  449. int get_users_home_directory(char *b, int len)
  450. {
  451. strcpy(b, "."); /* use current directory if getpwnam() no available */
  452. return 1;
  453. }
  454. #endif /* USE_GETUID */
  455. #ifdef UNIX_TIMES
  456. /*
  457. * This is a BSD-style clock facility, possibly giving a resolution of
  458. * only 1/100 second. I believe that Portable Standard Lisp typically
  459. * reports user time, which is why I do this. A further nasty here
  460. * is that I am probably compiling this file in ANSI mode, and on
  461. * at least some computers this makes #include <sys/times.h> fairly
  462. * ineffective (ugh), so I declare all the structures and functions I
  463. * want directly (ugh ugh) and hope they are as needed. Consider this
  464. * when you port to a new machine.
  465. */
  466. clock_t read_clock(void)
  467. {
  468. struct my_tms {
  469. clock_t tms_utime;
  470. clock_t tms_stime;
  471. clock_t tms_cutime;
  472. clock_t tms_cstime;
  473. } tmsbuf;
  474. clock_t w1, w2, w3;
  475. extern void times(/*struct my_tms * */);
  476. times(&tmsbuf);
  477. w1 = tmsbuf.tms_utime; /* User time in UNIX_TIMES ticks */
  478. w2 = CLOCKS_PER_SEC;
  479. w3 = UNIX_TIMES;
  480. return (clock_t)((double)w1 * ((double)w2/(double)w3));
  481. }
  482. #endif
  483. void accept_tick()
  484. {
  485. }
  486. #ifdef __kcm
  487. extern int _ttyhandle;
  488. int batchp()
  489. {
  490. return (_ttyhandle != 0);
  491. }
  492. #else
  493. #ifdef NCC_LIB
  494. int batchp()
  495. {
  496. extern int _fisatty(FILE*);
  497. return !_fisatty(stdin);
  498. }
  499. #else
  500. #if BSD_LIB
  501. int batchp()
  502. {
  503. return !isatty(fileno(stdin));
  504. }
  505. #else
  506. #error "Unknown Library type"
  507. #endif /* BSD_LIB */
  508. #endif /* NCC_LIB */
  509. #endif /* __kcm */
  510. /*
  511. * The next procedure is responsible for establishing information about
  512. * where the main checkpoint image should be recovered from, and where
  513. * and fasl files should come from.
  514. */
  515. char *find_image_directory(int argc, char *argv[])
  516. {
  517. char image[LONGEST_LEGAL_FILENAME];
  518. char pgmname[LONGEST_LEGAL_FILENAME];
  519. char *w;
  520. /*
  521. * Here I assume Unix, or something sufficiently like it, and
  522. * if the current program is called xxx, then I want an environment
  523. * variable called xxx.img to tell me where to find the image file
  524. * and the fasl directory.
  525. */
  526. #ifdef PUBLIC
  527. strcpy(pgmname, "/usr/local/lib/reduce"); /* fixed name */
  528. w = my_getenv("reduceimg");
  529. if (w != NULL) strcpy(image, w);
  530. else strcpy(image, pgmname);
  531. #else
  532. if (argc > 0 && argv[0] != NULL)
  533. { int i, j, k;
  534. w = argv[0];
  535. i = j = k = strlen(w);
  536. while (i > 0 && w[i-1] != '/') i--;
  537. /*
  538. * There is some question (in the cygnus world) whether the file name of
  539. * an executable does or doe snot have ".exe" on the end of it. Just to be on
  540. * the safe side here I will strip off any suffix that might be present!
  541. */
  542. while (j > i && w[j] != '.') j--;
  543. if (j <= i) j = k;
  544. sprintf(pgmname, "%.*s.img", j-i, &w[i]); /* final component of argv[0] */
  545. sprintf(program_name, "%.*s", j-i, &w[i]);
  546. }
  547. else strcpy(pgmname, "csl.img"); /* even argv[0] is not available! */
  548. w = my_getenv(pgmname);
  549. #endif
  550. if (w != NULL) strcpy(image, w);
  551. else strcpy(image, pgmname);
  552. /*
  553. * I copy from local vectors into malloc'd space to hand my
  554. * answer back.
  555. */
  556. w = (char *)(*malloc_hook)(1+strlen(image));
  557. /*
  558. * The error exit here seem unsatisfactory...
  559. */
  560. if (w == NULL)
  561. { fprintf(stderr, "\n+++ Panic - run out of space\n");
  562. exit(EXIT_FAILURE);
  563. }
  564. strcpy(w, image);
  565. return w;
  566. }
  567. /*
  568. * The following function controls memory allocation policy
  569. */
  570. int32 ok_to_grab_memory(int32 current)
  571. {
  572. #ifdef COMMON
  573. return current;
  574. #else
  575. return 3*current + 2;
  576. #endif
  577. }
  578. #include "fileops.c"
  579. #include "scandir.c"
  580. /* end of syscyg.c */