~ubuntu-branches/debian/stretch/alpine/stretch

« back to all changes in this revision

Viewing changes to pith/osdep/fnexpand.c

  • Committer: Bazaar Package Importer
  • Author(s): Asheesh Laroia
  • Date: 2007-02-17 13:17:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070217131742-99x5c6cpg1pbkdhw
Tags: upstream-0.82+dfsg
ImportĀ upstreamĀ versionĀ 0.82+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#if !defined(lint) && !defined(DOS)
 
2
static char rcsid[] = "$Id: fnexpand.c 200 2006-10-25 19:01:21Z hubert@u.washington.edu $";
 
3
#endif
 
4
 
 
5
/*
 
6
 * ========================================================================
 
7
 * Copyright 2006 University of Washington
 
8
 *
 
9
 * Licensed under the Apache License, Version 2.0 (the "License");
 
10
 * you may not use this file except in compliance with the License.
 
11
 * You may obtain a copy of the License at
 
12
 *
 
13
 *     http://www.apache.org/licenses/LICENSE-2.0
 
14
 *
 
15
 * ========================================================================
 
16
 */
 
17
 
 
18
#include <system.h>
 
19
 
 
20
#include "../charconv/filesys.h"
 
21
 
 
22
#include "fnexpand.h"
 
23
 
 
24
 
 
25
 
 
26
 
 
27
/*----------------------------------------------------------------------
 
28
       Expand the ~ in a file ala the csh (as home directory)
 
29
 
 
30
   Args: buf --  The filename to expand (nothing happens unless begins with ~)
 
31
         len --  The length of the buffer passed in (expansion is in place)
 
32
 
 
33
 Result: Expanded string is returned using same storage as passed in.
 
34
         If expansion fails, NULL is returned
 
35
 ----*/
 
36
char *
 
37
fnexpand(char *buf, int len)
 
38
{
 
39
#ifdef  _WINDOWS
 
40
    /* We used to use ps_global->home_dir, now we have to build it */
 
41
    if(*buf == '~' && *(buf+1) == '\\'){
 
42
        char temp_path[_MAX_PATH], home_buf[_MAX_PATH], *temp_home_str;
 
43
 
 
44
        if(getenv("HOME") != NULL)
 
45
          temp_home_str = getenv("HOME");
 
46
        else{
 
47
            /* should eventually strip this out of get_user_info */
 
48
            char *p, *q;
 
49
 
 
50
            if((p = (char *) getenv("HOMEDRIVE"))
 
51
               && (q = (char *) getenv("HOMEPATH")))
 
52
              snprintf(home_buf, sizeof(home_buf), "%s%s", p, q);
 
53
            else
 
54
              snprintf(home_buf, sizeof(home_buf), "%c:\\", '@' + _getdrive());
 
55
 
 
56
            temp_home_str = home_buf;
 
57
        }
 
58
        snprintf(temp_path, sizeof(temp_path), "%s", buf+1);
 
59
        snprintf(buf, sizeof(buf), "%s%s", temp_path, fname_to_utf8(temp_home_str));
 
60
    }
 
61
    return(buf);
 
62
#else   /* UNIX */
 
63
    struct passwd *pw;
 
64
    register char *x,*y;
 
65
    char name[20], *tbuf;
 
66
    
 
67
    if(*buf == '~') {
 
68
        for(x = buf+1, y = name;
 
69
            *x != '/' && *x != '\0' && y < name + sizeof(name)-1;
 
70
            *y++ = *x++)
 
71
          ;
 
72
 
 
73
        *y = '\0';
 
74
        if(x == buf + 1) 
 
75
          pw = getpwuid(getuid());
 
76
        else
 
77
          pw = getpwnam(name);
 
78
 
 
79
        if(pw == NULL)
 
80
          return((char *)NULL);
 
81
        if(strlen(pw->pw_dir) + strlen(buf) > len) {
 
82
          return((char *)NULL);
 
83
        }
 
84
 
 
85
        if((tbuf = (char *) malloc((len+1)*sizeof(char))) != NULL){
 
86
            snprintf(tbuf, len, "%s%s", pw->pw_dir, x);
 
87
            snprintf(buf, len, "%s", tbuf);
 
88
            free((void *)tbuf);
 
89
        }
 
90
    }
 
91
 
 
92
    return(len ? buf : (char *)NULL);
 
93
#endif  /* UNIX */
 
94
}
 
95
 
 
96