tilde.c (119610) | tilde.c (157184) |
---|---|
1/* tilde.c -- Tilde expansion code (~/foo := $HOME/foo). */ 2 3/* Copyright (C) 1988,1989 Free Software Foundation, Inc. 4 5 This file is part of GNU Readline, a library for reading lines 6 of text with interactive input and history editing. 7 8 Readline is free software; you can redistribute it and/or modify it --- 29 unchanged lines hidden (view full) --- 38 39#if defined (HAVE_STDLIB_H) 40# include <stdlib.h> 41#else 42# include "ansi_stdlib.h" 43#endif /* HAVE_STDLIB_H */ 44 45#include <sys/types.h> | 1/* tilde.c -- Tilde expansion code (~/foo := $HOME/foo). */ 2 3/* Copyright (C) 1988,1989 Free Software Foundation, Inc. 4 5 This file is part of GNU Readline, a library for reading lines 6 of text with interactive input and history editing. 7 8 Readline is free software; you can redistribute it and/or modify it --- 29 unchanged lines hidden (view full) --- 38 39#if defined (HAVE_STDLIB_H) 40# include <stdlib.h> 41#else 42# include "ansi_stdlib.h" 43#endif /* HAVE_STDLIB_H */ 44 45#include <sys/types.h> |
46#if defined (HAVE_PWD_H) |
|
46#include <pwd.h> | 47#include <pwd.h> |
48#endif |
|
47 48#include "tilde.h" 49 50#if defined (TEST) || defined (STATIC_MALLOC) 51static void *xmalloc (), *xrealloc (); 52#else 53# include "xmalloc.h" 54#endif /* TEST || STATIC_MALLOC */ 55 56#if !defined (HAVE_GETPW_DECLS) | 49 50#include "tilde.h" 51 52#if defined (TEST) || defined (STATIC_MALLOC) 53static void *xmalloc (), *xrealloc (); 54#else 55# include "xmalloc.h" 56#endif /* TEST || STATIC_MALLOC */ 57 58#if !defined (HAVE_GETPW_DECLS) |
59# if defined (HAVE_GETPWUID) |
|
57extern struct passwd *getpwuid PARAMS((uid_t)); | 60extern struct passwd *getpwuid PARAMS((uid_t)); |
61# endif 62# if defined (HAVE_GETPWNAM) |
|
58extern struct passwd *getpwnam PARAMS((const char *)); | 63extern struct passwd *getpwnam PARAMS((const char *)); |
64# endif |
|
59#endif /* !HAVE_GETPW_DECLS */ 60 61#if !defined (savestring) 62#define savestring(x) strcpy ((char *)xmalloc (1 + strlen (x)), (x)) 63#endif /* !savestring */ 64 65#if !defined (NULL) 66# if defined (__STDC__) --- 205 unchanged lines hidden (view full) --- 272#endif 273 ret[i - 1] = fname[i]; 274 ret[i - 1] = '\0'; 275 if (lenp) 276 *lenp = i; 277 return ret; 278} 279 | 65#endif /* !HAVE_GETPW_DECLS */ 66 67#if !defined (savestring) 68#define savestring(x) strcpy ((char *)xmalloc (1 + strlen (x)), (x)) 69#endif /* !savestring */ 70 71#if !defined (NULL) 72# if defined (__STDC__) --- 205 unchanged lines hidden (view full) --- 278#endif 279 ret[i - 1] = fname[i]; 280 ret[i - 1] = '\0'; 281 if (lenp) 282 *lenp = i; 283 return ret; 284} 285 |
286#if 0 287/* Public function to scan a string (FNAME) beginning with a tilde and find 288 the portion of the string that should be passed to the tilde expansion 289 function. Right now, it just calls tilde_find_suffix and allocates new 290 memory, but it can be expanded to do different things later. */ 291char * 292tilde_find_word (fname, flags, lenp) 293 const char *fname; 294 int flags, *lenp; 295{ 296 int x; 297 char *r; 298 299 x = tilde_find_suffix (fname); 300 if (x == 0) 301 { 302 r = savestring (fname); 303 if (lenp) 304 *lenp = 0; 305 } 306 else 307 { 308 r = (char *)xmalloc (1 + x); 309 strncpy (r, fname, x); 310 r[x] = '\0'; 311 if (lenp) 312 *lenp = x; 313 } 314 315 return r; 316} 317#endif 318 |
|
280/* Return a string that is PREFIX concatenated with SUFFIX starting at 281 SUFFIND. */ 282static char * 283glue_prefix_and_suffix (prefix, suffix, suffind) 284 char *prefix; 285 const char *suffix; 286 int suffind; 287{ --- 54 unchanged lines hidden (view full) --- 342 free (expansion); 343 return (dirname); 344 } 345 } 346 347 /* No preexpansion hook, or the preexpansion hook failed. Look in the 348 password database. */ 349 dirname = (char *)NULL; | 319/* Return a string that is PREFIX concatenated with SUFFIX starting at 320 SUFFIND. */ 321static char * 322glue_prefix_and_suffix (prefix, suffix, suffind) 323 char *prefix; 324 const char *suffix; 325 int suffind; 326{ --- 54 unchanged lines hidden (view full) --- 381 free (expansion); 382 return (dirname); 383 } 384 } 385 386 /* No preexpansion hook, or the preexpansion hook failed. Look in the 387 password database. */ 388 dirname = (char *)NULL; |
389#if defined (HAVE_GETPWNAM) |
|
350 user_entry = getpwnam (username); | 390 user_entry = getpwnam (username); |
391#else 392 user_entry = 0; 393#endif |
|
351 if (user_entry == 0) 352 { 353 /* If the calling program has a special syntax for expanding tildes, 354 and we couldn't find a standard expansion, then let them try. */ 355 if (tilde_expansion_failure_hook) 356 { 357 expansion = (*tilde_expansion_failure_hook) (username); 358 if (expansion) --- 8 unchanged lines hidden (view full) --- 367 if (dirname == 0) 368 dirname = savestring (filename); 369 } 370 else 371 { 372 free (username); 373 dirname = glue_prefix_and_suffix (user_entry->pw_dir, filename, user_len); 374 } | 394 if (user_entry == 0) 395 { 396 /* If the calling program has a special syntax for expanding tildes, 397 and we couldn't find a standard expansion, then let them try. */ 398 if (tilde_expansion_failure_hook) 399 { 400 expansion = (*tilde_expansion_failure_hook) (username); 401 if (expansion) --- 8 unchanged lines hidden (view full) --- 410 if (dirname == 0) 411 dirname = savestring (filename); 412 } 413 else 414 { 415 free (username); 416 dirname = glue_prefix_and_suffix (user_entry->pw_dir, filename, user_len); 417 } |
375 | 418#if defined (HAVE_GETPWENT) |
376 endpwent (); | 419 endpwent (); |
420#endif |
|
377 return (dirname); 378} 379 380 381#if defined (TEST) 382#undef NULL 383#include <stdio.h> 384 --- 74 unchanged lines hidden --- | 421 return (dirname); 422} 423 424 425#if defined (TEST) 426#undef NULL 427#include <stdio.h> 428 --- 74 unchanged lines hidden --- |