1 /* 2 * Copyright 2002, Sebastian Nozzi <sebnozzi@gmx.net>. 3 * Copyright 2004, Francois Revol. 4 * 5 * Distributed under the terms of the MIT License. 6 */ 7 8 #include <stdio.h> 9 #include <string.h> 10 11 #include <FindDirectory.h> 12 #include <fs_info.h> 13 14 15 #define NO_ERRORS 0 16 #define ARGUMENT_MISSING 1 17 #define WRONG_DIR_TYPE 2 18 19 typedef struct { 20 const char *key; 21 directory_which value; 22 } directoryType; 23 24 #define KEYVALUE_PAIR(key) {#key, key} 25 26 directoryType directoryTypes[] = { 27 // Generic directories 28 KEYVALUE_PAIR(B_DESKTOP_DIRECTORY), 29 KEYVALUE_PAIR(B_TRASH_DIRECTORY), 30 KEYVALUE_PAIR(B_APPS_DIRECTORY), 31 KEYVALUE_PAIR(B_PREFERENCES_DIRECTORY), 32 KEYVALUE_PAIR(B_UTILITIES_DIRECTORY), 33 34 // System directories 35 KEYVALUE_PAIR(B_SYSTEM_DIRECTORY), 36 KEYVALUE_PAIR(B_SYSTEM_ADDONS_DIRECTORY), 37 KEYVALUE_PAIR(B_SYSTEM_BOOT_DIRECTORY), 38 KEYVALUE_PAIR(B_SYSTEM_FONTS_DIRECTORY), 39 KEYVALUE_PAIR(B_SYSTEM_LIB_DIRECTORY), 40 KEYVALUE_PAIR(B_SYSTEM_SERVERS_DIRECTORY), 41 KEYVALUE_PAIR(B_SYSTEM_APPS_DIRECTORY), 42 KEYVALUE_PAIR(B_SYSTEM_BIN_DIRECTORY), 43 KEYVALUE_PAIR(B_SYSTEM_DOCUMENTATION_DIRECTORY), 44 KEYVALUE_PAIR(B_SYSTEM_PREFERENCES_DIRECTORY), 45 KEYVALUE_PAIR(B_SYSTEM_TRANSLATORS_DIRECTORY), 46 KEYVALUE_PAIR(B_SYSTEM_MEDIA_NODES_DIRECTORY), 47 KEYVALUE_PAIR(B_SYSTEM_SOUNDS_DIRECTORY), 48 KEYVALUE_PAIR(B_SYSTEM_DATA_DIRECTORY), 49 50 // Common directories 51 KEYVALUE_PAIR(B_COMMON_DIRECTORY), 52 KEYVALUE_PAIR(B_COMMON_SYSTEM_DIRECTORY), 53 KEYVALUE_PAIR(B_COMMON_ADDONS_DIRECTORY), 54 KEYVALUE_PAIR(B_COMMON_BOOT_DIRECTORY), 55 KEYVALUE_PAIR(B_COMMON_FONTS_DIRECTORY), 56 KEYVALUE_PAIR(B_COMMON_LIB_DIRECTORY), 57 KEYVALUE_PAIR(B_COMMON_SERVERS_DIRECTORY), 58 KEYVALUE_PAIR(B_COMMON_BIN_DIRECTORY), 59 KEYVALUE_PAIR(B_COMMON_ETC_DIRECTORY), 60 KEYVALUE_PAIR(B_COMMON_DOCUMENTATION_DIRECTORY), 61 KEYVALUE_PAIR(B_COMMON_SETTINGS_DIRECTORY), 62 KEYVALUE_PAIR(B_COMMON_DEVELOP_DIRECTORY), 63 KEYVALUE_PAIR(B_COMMON_LOG_DIRECTORY), 64 KEYVALUE_PAIR(B_COMMON_SPOOL_DIRECTORY), 65 KEYVALUE_PAIR(B_COMMON_TEMP_DIRECTORY), 66 KEYVALUE_PAIR(B_COMMON_VAR_DIRECTORY), 67 KEYVALUE_PAIR(B_COMMON_TRANSLATORS_DIRECTORY), 68 KEYVALUE_PAIR(B_COMMON_MEDIA_NODES_DIRECTORY), 69 KEYVALUE_PAIR(B_COMMON_SOUNDS_DIRECTORY), 70 KEYVALUE_PAIR(B_COMMON_DATA_DIRECTORY), 71 KEYVALUE_PAIR(B_COMMON_CACHE_DIRECTORY), 72 73 // User directories 74 KEYVALUE_PAIR(B_USER_DIRECTORY), 75 KEYVALUE_PAIR(B_USER_CONFIG_DIRECTORY), 76 KEYVALUE_PAIR(B_USER_ADDONS_DIRECTORY), 77 KEYVALUE_PAIR(B_USER_BOOT_DIRECTORY), 78 KEYVALUE_PAIR(B_USER_FONTS_DIRECTORY), 79 KEYVALUE_PAIR(B_USER_LIB_DIRECTORY), 80 KEYVALUE_PAIR(B_USER_SETTINGS_DIRECTORY), 81 KEYVALUE_PAIR(B_USER_DESKBAR_DIRECTORY), 82 KEYVALUE_PAIR(B_USER_PRINTERS_DIRECTORY), 83 KEYVALUE_PAIR(B_USER_TRANSLATORS_DIRECTORY), 84 KEYVALUE_PAIR(B_USER_MEDIA_NODES_DIRECTORY), 85 KEYVALUE_PAIR(B_USER_SOUNDS_DIRECTORY), 86 KEYVALUE_PAIR(B_USER_DATA_DIRECTORY), 87 KEYVALUE_PAIR(B_USER_CACHE_DIRECTORY), 88 89 // Legacy system directories 90 KEYVALUE_PAIR(B_BEOS_DIRECTORY), 91 KEYVALUE_PAIR(B_BEOS_SYSTEM_DIRECTORY), 92 KEYVALUE_PAIR(B_BEOS_ADDONS_DIRECTORY), 93 KEYVALUE_PAIR(B_BEOS_BOOT_DIRECTORY), 94 KEYVALUE_PAIR(B_BEOS_FONTS_DIRECTORY), 95 KEYVALUE_PAIR(B_BEOS_LIB_DIRECTORY), 96 KEYVALUE_PAIR(B_BEOS_SERVERS_DIRECTORY), 97 KEYVALUE_PAIR(B_BEOS_APPS_DIRECTORY), 98 KEYVALUE_PAIR(B_BEOS_BIN_DIRECTORY), 99 KEYVALUE_PAIR(B_BEOS_ETC_DIRECTORY), 100 KEYVALUE_PAIR(B_BEOS_DOCUMENTATION_DIRECTORY), 101 KEYVALUE_PAIR(B_BEOS_PREFERENCES_DIRECTORY), 102 KEYVALUE_PAIR(B_BEOS_TRANSLATORS_DIRECTORY), 103 KEYVALUE_PAIR(B_BEOS_MEDIA_NODES_DIRECTORY), 104 KEYVALUE_PAIR(B_BEOS_SOUNDS_DIRECTORY), 105 KEYVALUE_PAIR(B_BEOS_DATA_DIRECTORY), 106 107 {NULL, B_USER_DESKBAR_DIRECTORY} 108 }; 109 110 111 static void 112 listDirectoryWhich(void) 113 { 114 int i; 115 116 for (i = 0; directoryTypes[i].key; i++) { 117 printf("%s\n", directoryTypes[i].key); 118 } 119 } 120 121 122 static bool 123 retrieveDirValue(directoryType *list, const char *key, 124 directory_which *valueOut) 125 { 126 unsigned i = 0; 127 128 while (list[i].key != NULL) { 129 if (strcmp(list[i].key, key) == 0) { 130 *valueOut = list[i].value; 131 return true; 132 } 133 134 i++; 135 } 136 137 return false; 138 } 139 140 141 static void 142 usageMsg() 143 { 144 printf("usage: /bin/finddir -l | [ -v volume ] directory_which\n"); 145 printf("\t-l\t list valid which constants to use\n"); 146 printf("\t-v <file> use the specified volume for directory\n"); 147 printf("\t\t constants that are volume-specific.\n"); 148 printf("\t\t <file> can be any file on that volume.\n"); 149 printf("\t\t defaults to the boot volume.\n"); 150 printf(" For a description of recognized directory_which constants,\n"); 151 printf(" see the find_directory(...) documentation in the Be Book.\n"); 152 } 153 154 155 int 156 main(int argc, char *argv[]) 157 { 158 int directoryArgNr; 159 int status; 160 dev_t volume; 161 directory_which dirType; 162 int returnCode; 163 164 status = NO_ERRORS; 165 directoryArgNr = 1; 166 returnCode = 0; 167 168 dirType = B_BEOS_DIRECTORY; /* so that it compiles */ 169 170 /* By default use boot volume*/ 171 volume = dev_for_path("/boot"); 172 173 if (argc <= 1) { 174 status = ARGUMENT_MISSING; 175 } else { 176 if (strcmp(argv[1], "-l") == 0 ) { 177 listDirectoryWhich(); 178 return 0; 179 } 180 if (strcmp(argv[1], "-v") == 0 ) { 181 if (argc >= 3) { 182 dev_t temp_volume; 183 /* get volume from second arg */ 184 temp_volume = dev_for_path(argv[2]); 185 186 /* Keep default value in case of error */ 187 if (temp_volume >= 0) 188 volume = temp_volume; 189 190 /* two arguments were used for volume */ 191 directoryArgNr+=2; 192 } else { 193 /* set status to argument missing */ 194 status = ARGUMENT_MISSING; 195 } 196 } 197 } 198 199 if (status == NO_ERRORS && argc > directoryArgNr) { 200 /* get directory constant from next argument */ 201 202 if (!retrieveDirValue(directoryTypes, argv[directoryArgNr], &dirType)) 203 status = WRONG_DIR_TYPE; 204 } else { 205 status = ARGUMENT_MISSING; 206 } 207 208 /* Do the actual directoy finding */ 209 210 if (status == NO_ERRORS) { 211 /* Question: would B_PATH_NAME_LENGTH alone have been enough? */ 212 char buffer[B_PATH_NAME_LENGTH+B_FILE_NAME_LENGTH]; 213 status_t result = find_directory (dirType, volume, false, buffer, 214 sizeof(buffer)); 215 if (result == B_OK) { 216 printf("%s\n", buffer); 217 } else { 218 /* else what? */ 219 /* this can not happen! */ 220 fprintf(stderr, "Serious internal error; contact support\n"); 221 } 222 } 223 224 /* Error messages and return code setting */ 225 226 if (status == WRONG_DIR_TYPE) { 227 fprintf(stderr, "%s: unrecognized directory_which constant \'%s\'\n", argv[0], 228 argv[directoryArgNr]); 229 returnCode = 252; 230 } 231 232 if (status == ARGUMENT_MISSING) { 233 usageMsg(); 234 returnCode = 255; 235 } 236 237 return returnCode; 238 } 239 240