Changes in / [2367f1c:a16d7e5]
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
runtests.sh
r37ac344 r5aa33fd 5 5 export BARNOWL_BIN_DIR="$SRCDIR/" 6 6 7 HARNESS_PERL=./tester exec prove --failurest/7 HARNESS_PERL=./tester exec prove t/ -
tester.c
re073081 rb4a678a 4 4 #undef WINDOW 5 5 6 #include <errno.h>7 6 #include <unistd.h> 8 #include <pwd.h>9 #include <stdio.h>10 7 #include <stdlib.h> 11 #include <string.h>12 #include <sys/types.h>13 8 14 9 #undef instr … … 129 124 #define FAIL_UNLESS(desc,pred) do { int __pred = (pred); \ 130 125 numtests++; \ 131 printf("%s % d %s", (__pred) ? "ok" : (numfailed++, "not ok"), numtests, desc);\126 printf("%s %s", (__pred)?"ok":(numfailed++,"not ok"), desc); \ 132 127 if(!(__pred)) printf("\t(%s:%d)", __FILE__, __LINE__); printf("%c", '\n'); } while(0) 133 128 … … 136 131 { 137 132 int numfailed=0; 138 const char *home;139 char *s, *path;140 struct passwd *pw;141 133 142 134 printf("# BEGIN testing owl_util\n"); … … 233 225 g_string_free(g, true); 234 226 235 236 s = owl_util_baseclass("barnowl");237 FAIL_UNLESS("baseclass barnowl", !strcmp("barnowl", s));238 g_free(s);239 s = owl_util_baseclass("unbarnowl");240 FAIL_UNLESS("baseclass unbarnowl", !strcmp("barnowl", s));241 g_free(s);242 s = owl_util_baseclass("unununbarnowl.d.d");243 FAIL_UNLESS("baseclass unununbarnowl.d.d", !strcmp("barnowl", s));244 g_free(s);245 s = owl_util_baseclass("ununun.d.d");246 FAIL_UNLESS("baseclass ununun.d.d", !strcmp("", s));247 g_free(s);248 s = owl_util_baseclass("d.d.d.d");249 FAIL_UNLESS("baseclass d.d.d.d", !strcmp("d", s));250 g_free(s);251 s = owl_util_baseclass("n.d.d.d");252 FAIL_UNLESS("baseclass n.d.d.d", !strcmp("n", s));253 g_free(s);254 s = owl_util_baseclass("ununun.");255 FAIL_UNLESS("baseclass ununun.", !strcmp(".", s));256 g_free(s);257 s = owl_util_baseclass("unununu");258 FAIL_UNLESS("baseclass unununu", !strcmp("u", s));259 g_free(s);260 261 262 s = owl_util_makepath("foo/bar");263 FAIL_UNLESS("makepath foo/bar", !strcmp("foo/bar", s));264 g_free(s);265 s = owl_util_makepath("//foo///bar");266 FAIL_UNLESS("makepath //foo///bar", !strcmp("/foo/bar", s));267 g_free(s);268 s = owl_util_makepath("foo/~//bar/");269 FAIL_UNLESS("makepath foo/~//bar/", !strcmp("foo/~/bar/", s));270 g_free(s);271 s = owl_util_makepath("~thisuserhadreallybetternotexist/foobar/");272 FAIL_UNLESS("makepath ~thisuserhadreallybetternotexist/foobar/",273 !strcmp("~thisuserhadreallybetternotexist/foobar/", s));274 g_free(s);275 276 errno = 0;277 pw = getpwuid(getuid());278 if (pw) {279 home = pw->pw_dir;280 } else {281 /* Just make some noise so we notice. */282 home = "<WHAT>";283 fprintf(stderr, "getpwuid: %s", errno ? strerror(errno) : "No such user");284 }285 s = owl_util_makepath("~");286 FAIL_UNLESS("makepath ~", !strcmp(home, s));287 g_free(s);288 289 path = g_strconcat(home, "/foo/bar/baz", NULL);290 s = owl_util_makepath("~///foo/bar//baz");291 FAIL_UNLESS("makepath ~///foo/bar//baz", !strcmp(path, s));292 g_free(s);293 g_free(path);294 295 errno = 0;296 pw = getpwnam("root");297 if (pw) {298 home = pw->pw_dir;299 } else {300 /* Just make some noise so we notice. */301 home = "<WHAT>";302 fprintf(stderr, "getpwnam: %s", errno ? strerror(errno) : "No such user");303 }304 305 s = owl_util_makepath("~root");306 FAIL_UNLESS("makepath ~root", !strcmp(home, s));307 g_free(s);308 309 path = g_strconcat(home, "/foo/bar/baz", NULL);310 s = owl_util_makepath("~root///foo/bar//baz");311 FAIL_UNLESS("makepath ~root///foo/bar//baz", !strcmp(path, s));312 g_free(s);313 g_free(path);314 315 227 /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */ 316 228 printf("# END testing owl_util (%d failures)\n", numfailed); -
util.c
rffe1135 rb8a3e00 37 37 CALLER_OWN char *owl_util_makepath(const char *in) 38 38 { 39 char *out; 40 int i, j; 41 if (in[0] == '~') { 42 /* Attempt tilde-expansion of the first component. Get the 43 tilde-prefix, which goes up to the next slash. */ 44 struct passwd *pw; 45 const char *end = strchr(in + 1, '/'); 46 if (end == NULL) 47 end = in + strlen(in); 48 49 if (end == in + 1) { 50 /* My home directory. */ 51 pw = getpwuid(getuid()); 39 int i, j, x; 40 char *out, user[MAXPATHLEN]; 41 struct passwd *pw; 42 43 out=g_new(char, MAXPATHLEN+1); 44 out[0]='\0'; 45 j=strlen(in); 46 x=0; 47 for (i=0; i<j; i++) { 48 if (in[i]=='~') { 49 if ( (i==(j-1)) || /* last character */ 50 (in[i+1]=='/') ) { /* ~/ */ 51 /* use my homedir */ 52 pw=getpwuid(getuid()); 53 if (!pw) { 54 out[x]=in[i]; 55 } else { 56 out[x]='\0'; 57 strcat(out, pw->pw_dir); 58 x+=strlen(pw->pw_dir); 59 } 60 } else { 61 /* another user homedir */ 62 int a, b; 63 b=0; 64 for (a=i+1; i<j; a++) { 65 if (in[a]==' ' || in[a]=='/') { 66 break; 67 } else { 68 user[b]=in[a]; 69 i++; 70 b++; 71 } 72 } 73 user[b]='\0'; 74 pw=getpwnam(user); 75 if (!pw) { 76 out[x]=in[i]; 77 } else { 78 out[x]='\0'; 79 strcat(out, pw->pw_dir); 80 x+=strlen(pw->pw_dir); 81 } 82 } 83 } else if (in[i]=='/') { 84 /* check for a double / */ 85 if (i<(j-1) && (in[i+1]=='/')) { 86 /* do nothing */ 87 } else { 88 out[x]=in[i]; 89 x++; 90 } 52 91 } else { 53 /* Someone else's home directory. */ 54 char *user = g_strndup(in + 1, end - (in + 1)); 55 pw = getpwnam(user); 56 g_free(user); 57 } 58 59 /* Patch together a new path. Replace the ~ and tilde-prefix with 60 the homedir. */ 61 if (pw) { 62 out = g_strconcat(pw->pw_dir, end, NULL); 63 } else { 64 out = g_strdup(in); 65 } 66 } else { 67 out = g_strdup(in); 68 } 69 70 /* And a quick pass to remove duplicate slashes. */ 71 for (i = j = 0; out[i] != '\0'; i++) { 72 if (out[i] != '/' || i == 0 || out[i-1] != '/') 73 out[j++] = out[i]; 74 } 75 out[j] = '\0'; 76 return out; 92 out[x]=in[i]; 93 x++; 94 } 95 } 96 out[x]='\0'; 97 return(out); 77 98 } 78 99
Note: See TracChangeset
for help on using the changeset viewer.