Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid some C functions that are not in C99 #760

Merged
merged 1 commit into from
Nov 21, 2023
Merged

avoid some C functions that are not in C99 #760

merged 1 commit into from
Nov 21, 2023

Conversation

mflatt
Copy link
Contributor

@mflatt mflatt commented Nov 19, 2023

Commit ff97b90 introduces "self-exe.c" using C99-and-later forms, which were not previously used in Chez Scheme. But it also uses strdup and strtok_r, which are widely available but not C99. Those functions turn out not to be available on some old environments that are still relevant to Racket, for example.

It always seems dumb to write yet another alternative to strdup and strtok, but the trade-off here seems worthwhile to me.

@ur4t
Copy link
Contributor

ur4t commented Nov 19, 2023

How about using implementations from musl libc?

And it might reduce a bit of size to define those helpers only if they are not available.

@mflatt
Copy link
Contributor Author

mflatt commented Nov 20, 2023

Ok, switched to the musl implementations. I did not try to figure out detecting when strcpy and/or strtok_r are available.

@jltaylor-us
Copy link
Contributor

Why would we want to borrow code with yet another license and have to figure out what our obligations are under that license when you already had perfectly good code that does exactly what you want?

@ur4t
Copy link
Contributor

ur4t commented Nov 20, 2023

Why would we want to borrow code with yet another license and have to figure out what our obligations are under that license when you already had perfectly good code that does exactly what you want?

These are our original copy_string with minor improvements and new get_self_path_generic without strtok_r (strspn and strcspn are in c99).

static char *copy_string(const char *s) {
  if (s == NULL) {
    return NULL;
  }
  size_t l = strlen(s) + 1;
  char *r = (char *)malloc(l);
  if (r == NULL) {
    return NULL;
  }
  return (char *)memcpy(r, s, l);
}

static char *get_self_path_generic(const char *exec_file) {
  if (strchr(exec_file, '/')) {
    return copy_string(exec_file);
  }
  char *pv = getenv("PATH");
  if (pv == NULL) {
    return NULL;
  }
  char *s = copy_string(pv);
  if (s == NULL) {
    return NULL;
  }
  for (char *p = s + strspn(s, ":"); *p != '\0'; p += strspn(p, ":")) {
    char *t = p;
    p += strcspn(p, ":");
    if (*p != '\0') {
      *p++ = '\0';
    }
    char *r = path_append(t, exec_file);
    if (access(r, X_OK) == 0) {
      free(s);
      return r;
    }
    free(r);
  }
  free(s);
  return NULL;
}

@mflatt
Copy link
Contributor Author

mflatt commented Nov 21, 2023

The most recent push has the old-but-improved code.

I also looked for calls to malloc that need a check for a NULL result and added checks. I'm not sure that's really useful when allocating something the size that _NSGetExecutablePath reports, say, since it seems like a catastrophic situation. But I might be wrong, and uniformity seems like the best policy.

@mflatt mflatt merged commit 2c700cf into cisco:main Nov 21, 2023
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants