Created
November 27, 2012 16:22
-
-
Save mikeplate/4155216 to your computer and use it in GitHub Desktop.
Execute script in sub folder with same name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Launch Command | |
| * | |
| * This program will search for an executable with the same name, or with extra file extension | |
| * ".sh", in all sub folders and execute it if such a file exists. | |
| * | |
| * I've written this program since I didn't like that you can't double click Bash script files in | |
| * Nautilus file manager on Ubuntu and have them executed immediately without changing settings. | |
| * With this program, I can have Bash script files in sub folder(s) and have them executed | |
| * immediately when the user double clicks this program. | |
| */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <sys/types.h> | |
| #include <sys/stat.h> | |
| #include <dirent.h> | |
| #include <linux/limits.h> | |
| #include <unistd.h> | |
| void cut_end_string(const char* buf, int c, char* dst, size_t dstSize) { | |
| char* end = strrchr(buf, c); | |
| if (end!=NULL) { | |
| strncpy(dst, end+1, dstSize); | |
| } | |
| else if (dstSize>0) { | |
| dst[0] = 0; | |
| } | |
| } | |
| void chop_end_string(char* buf, int c) { | |
| char* end = strrchr(buf, c); | |
| if (end!=NULL) | |
| *end = 0; | |
| } | |
| int is_string_any_of(const char* source, const char** check) { | |
| while (*check) { | |
| if (strcmp(source, *check)==0) | |
| return 1; | |
| check++; | |
| } | |
| return 0; | |
| } | |
| int main(int argc, char** argv) { | |
| char* path = malloc(PATH_MAX+1); | |
| realpath(argv[0], path); | |
| char* fileName = malloc(PATH_MAX+1); | |
| cut_end_string(path, '/', fileName, PATH_MAX); | |
| chop_end_string(path, '/'); | |
| const char* DIRNAMES[] = { ".", "..", NULL }; | |
| struct dirent* entry; | |
| DIR* dir = opendir(path); | |
| char* entryPath = malloc(PATH_MAX+1); | |
| char* checkPath = malloc(PATH_MAX+1); | |
| if (dir!=NULL) { | |
| int found = 0; | |
| while ((entry = readdir(dir)) && !found) { | |
| struct stat info; | |
| strncpy(entryPath, path, PATH_MAX); | |
| strncat(entryPath, "/", PATH_MAX); | |
| strncat(entryPath, entry->d_name, PATH_MAX); | |
| stat(entryPath, &info); | |
| if (S_ISDIR(info.st_mode) && !is_string_any_of(entry->d_name, DIRNAMES)) { | |
| strncpy(checkPath, path, PATH_MAX); | |
| strncat(checkPath, "/", PATH_MAX); | |
| strncat(checkPath, entry->d_name, PATH_MAX); | |
| strncat(checkPath, "/", PATH_MAX); | |
| strncat(checkPath, fileName, PATH_MAX); | |
| if (access(checkPath, X_OK)==0) { | |
| found = 1; | |
| chdir(path); | |
| system(checkPath); | |
| } | |
| else { | |
| strncat(checkPath, ".sh", PATH_MAX); | |
| if (access(checkPath, X_OK)==0) { | |
| found = 1; | |
| chdir(path); | |
| system(checkPath); | |
| } | |
| } | |
| } | |
| } | |
| closedir(dir); | |
| } | |
| free(entryPath); | |
| free(checkPath); | |
| free(path); | |
| free(fileName); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment