/* From AUUGN Feb 1998 tidypath.c - tidy up a long $PATH Copyright 1998 David Purdue David.Purdue@computer.org */ #ifdef __hpux #define _INCLUDE_POSIX_SOURCE #define _INCLUDE_HPUX_SOURCE #define _INCLUDE_XOPEN_SOURCE #endif #include #include #include #include /*#include */ #include #include void main(int argc, char *argv[]) { char **final_path, *path_ptr, *end_dir; int num_colons, i, j, num_dirs, dir_length, found; /* struct stat sbuf; */ if (argc != 2) { /* no options supported at present ! */ fprintf(stderr,"Usage:\n" "csh> setenv PATH `%s $PATH`\n" "sh$ export PATH=`%s $PATH`\n", argv[0], argv[0]); exit(1); } /* count the number of colons in the path */ num_colons = 0; path_ptr = index(argv[1], ':'); while (path_ptr != NULL) { num_colons++; path_ptr++; path_ptr = index(path_ptr, ':'); } /* there will never be more directories out than in. */ final_path = (char **) calloc(num_colons + 1, sizeof(char *)); path_ptr = argv[1]; num_dirs = 0; /* load up the path */ while (path_ptr != NULL) { /*printf("Starting[%d]=>%s\n", num_dirs, path_ptr);*/ end_dir = index(path_ptr, ':'); if (end_dir == NULL) { dir_length = strlen(path_ptr); } else { dir_length = end_dir - path_ptr; } if (dir_length <= 0 ) { fprintf(stderr, "empty directory encountered on unique dir %d\n", num_dirs); path_ptr++; continue; } found = 0; for (j = 0; j < num_dirs; j++) { /* if (strcmp (path_ptr, final_path[j]) == 0) {*/ if (strncmp (path_ptr, final_path[j], dir_length) == 0) { if (strlen(final_path[j]) != dir_length) { fprintf(stderr,"%s != %s\n", final_path[j], path_ptr); continue; } found = 1; break; } } if (found == 0) { final_path[num_dirs] = malloc(dir_length + 1); if (NULL == final_path[num_dirs]) { perror("malloc"); exit (errno); } strncpy(final_path[num_dirs], path_ptr, dir_length); final_path[num_dirs][dir_length] = '\0'; /*printf("++%s++\n", final_path[num_dirs]);*/ if (0 == access(final_path[num_dirs], X_OK)) { num_dirs ++; } else { perror(final_path[num_dirs]); free(final_path[num_dirs]); } } if (end_dir == NULL) { path_ptr = NULL; } else { end_dir++; path_ptr = end_dir; } } /* Write out the path */ for(i = 0; i < (num_dirs - 1); i++) { printf("%s:", final_path[i]); } printf("%s\n", final_path[num_dirs-1]); exit(0); }