misc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* Copyright (c) 1993-2007 by Richard Kelsey and Jonathan Rees.
  2. See file COPYING. */
  3. #include <stdio.h>
  4. #include <stdlib.h> /* for getenv(), etc. (POSIX?/ANSI) */
  5. #include <string.h> /* for strncpy(), etc. (POSIX/ANSI) */
  6. #include <pwd.h> /* for getpwnam() (POSIX.1) */
  7. #include <unistd.h> /* for sysconf(), etc. (POSIX.1/.2)*/
  8. #include <errno.h>
  9. #include <sys/stat.h>
  10. #include <locale.h> /* ISO C99 */
  11. #include "sysdep.h"
  12. #include "c-mods.h"
  13. /*
  14. Expanding Unix filenames
  15. Unix Sucks
  16. Richard Kelsey Wed Jan 17 21:40:26 EST 1990
  17. Later modified by others who wish to remain anonymous
  18. Expands initial ~ and ~/ in string `name', leaving the result in `buffer'.
  19. `buffer_len' is the length of `buffer'.
  20. Note: strncpy(x, y, n) copies from y to x.
  21. */
  22. char *s48_expand_file_name (char *name, char *buffer, int buffer_len)
  23. {
  24. #define USER_NAME_SIZE 256
  25. char *dir, *p, user_name[USER_NAME_SIZE];
  26. struct passwd *user_data;
  27. int dir_len, i;
  28. int name_len = strlen(name);
  29. dir = 0;
  30. if (name[0] == '~') {
  31. name++; name_len--;
  32. if (name[0] == '/' || name[0] == 0) {
  33. dir = getenv("HOME"); }
  34. else {
  35. for (i = 0, p = name; i < name_len && *p != '/'; i++, p++)
  36. if (i > (USER_NAME_SIZE - 2)) {
  37. fprintf(stderr,
  38. "\ns48_expand_file_name: user name longer than %d characters\n",
  39. USER_NAME_SIZE - 3);
  40. return(NULL); };
  41. strncpy(user_name, name, i);
  42. user_name[i] = 0;
  43. user_data = getpwnam(user_name);
  44. if (!user_data) {
  45. fprintf(stderr, "\ns48_expand_file_name: unknown user \"%s\"\n",
  46. user_name);
  47. return(NULL); };
  48. name_len -= i;
  49. name = p;
  50. dir = user_data->pw_dir; } }
  51. else if (name[0] == '$') {
  52. name++; name_len--;
  53. for (i = 0, p = name; i < name_len && *p != '/'; i++, p++)
  54. if (i > (USER_NAME_SIZE - 2)) {
  55. fprintf(stderr,
  56. "\ns48_expand_file_name: environment variable longer than %d characters\n",
  57. USER_NAME_SIZE - 3);
  58. return(NULL); };
  59. strncpy(user_name, name, i);
  60. user_name[i] = 0;
  61. name_len -= i;
  62. name = p;
  63. dir = getenv(user_name); }
  64. if (dir) {
  65. dir_len = strlen(dir);
  66. if ((name_len + dir_len + 1) > buffer_len) {
  67. fprintf(stderr, "\ns48_expand_file_name: supplied buffer is too small\n");
  68. return(NULL); };
  69. strncpy(buffer, dir, dir_len);
  70. strncpy(buffer + dir_len, name, name_len);
  71. buffer[name_len + dir_len] = 0; }
  72. else {
  73. if ((name_len + 1) > buffer_len) {
  74. fprintf(stderr, "\ns48_expand_file_name: supplied buffer is too small\n");
  75. return(NULL); };
  76. strncpy(buffer, name, name_len);
  77. buffer[name_len] = 0; }
  78. return(buffer);
  79. }
  80. /* test routine
  81. main(argc, argv)
  82. int argc;
  83. char *argv[];
  84. {
  85. char buffer[32];
  86. s48_expand_file_name(argv[1], buffer, 32);
  87. printf("%s\n", buffer);
  88. return(0);
  89. }
  90. */
  91. /* Driver loop for tail-recursive calls */
  92. long s48_return_value;
  93. long
  94. s48_run_machine(long (*proc) (void))
  95. {
  96. while (proc != 0)
  97. proc = (long (*) (void)) (*proc)();
  98. return s48_return_value;
  99. }
  100. unsigned char *
  101. ps_error_string(long the_errno)
  102. {
  103. return((unsigned char *)strerror(the_errno));
  104. }
  105. /* Getting the length of a file. */
  106. long
  107. s48_get_file_size(unsigned char *name)
  108. {
  109. struct stat file_data;
  110. int status;
  111. if (-1 == stat((char*)name, &file_data) ||
  112. ! S_ISREG(file_data.st_mode))
  113. return -1;
  114. else
  115. return file_data.st_size;
  116. }
  117. /* encoding of argv */
  118. char*
  119. s48_get_os_string_encoding(void)
  120. {
  121. static char setlocale_called = PSFALSE;
  122. char *codeset;
  123. static char* encoding = NULL;
  124. /* Mike has no clue what the rationale for needing this is. */
  125. if (!setlocale_called)
  126. {
  127. setlocale(LC_CTYPE, "");
  128. setlocale_called = PSTRUE;
  129. }
  130. if (encoding == NULL)
  131. {
  132. codeset = nl_langinfo(CODESET); /* this ain't reentrant */
  133. encoding = malloc(strlen(codeset) + 1);
  134. if (encoding == NULL)
  135. return NULL;
  136. strcpy(encoding, codeset);
  137. }
  138. return encoding;
  139. }