Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

embedding C code in a shell script


It just occurred to me that with very little work, zsh could add this feature using similar techniques to the what you can read about here:

So, for example, one could embed a C function to call realpath(3).

You could compile the code as a separate command, or you could do more work and dynamically link it in and call it.


compiledFunction realpath() {
compiler    = gcc

#include <stdio.h>
#include <sys/param.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  int ind;
  int trouble = 0;
  char resolvedPath[MAXPATHLEN];
  // printf("Invoked as %s\n", argv[0]);
  if (argc <= 1) {
fprintf(stderr, "\nUsage: realpath path ...\n\nExit 1 if any paths fail.\n\n");
  for (ind = 1; ind < argc; ++ind) {
    if (realpath(argv[ind], resolvedPath) == NULL) {
      fprintf(stderr, "%s: No such path: %s\n", argv[1], argv[ind]);
      trouble = 1;
    } else {
      printf("%s\n", resolvedPath);
  exit(trouble ? 1 : 0);


Messages sorted by: Reverse Date, Date, Thread, Author