filename.c
上传用户:blenddy
上传日期:2007-01-07
资源大小:6495k
文件大小:2k
- /*-------------------------------------------------------------------------
- *
- * filename.c
- *
- *
- * Copyright (c) 1994, Regents of the University of California
- *
- *
- * IDENTIFICATION
- * $Header: /usr/local/cvsroot/pgsql/src/backend/utils/adt/filename.c,v 1.17.2.1 1999/08/02 05:24:52 scrappy Exp $
- *
- *-------------------------------------------------------------------------
- */
- #include <pwd.h>
- #include <sys/param.h>
- #include "postgres.h"
- #include "miscadmin.h"
- #include "utils/builtins.h"
- char *
- filename_in(char *file)
- {
- char *str;
- int ind = 0;
- /*
- * XXX - HACK CITY --- REDO should let the shell do expansions
- * (shexpand)
- */
- str = (char *) palloc(MAXPATHLEN * sizeof(*str));
- str[0] = ' ';
- if (file[0] == '~')
- {
- if (file[1] == ' ' || file[1] == '/')
- {
- /* Home directory */
- char *userName;
- struct passwd *pw;
- userName = GetPgUserName();
- if ((pw = getpwnam(userName)) == NULL)
- {
- elog(ERROR, "User %s is not a Unix user on the db server.",
- userName);
- }
- strcpy(str, pw->pw_dir);
- ind = 1;
- }
- else
- {
- /* Someone else's directory */
- char name[17],
- *p;
- struct passwd *pw;
- int len;
- if ((p = (char *) strchr(file, '/')) == NULL)
- {
- strcpy(name, file + 1);
- len = strlen(name);
- }
- else
- {
- len = (p - file) - 1;
- StrNCpy(name, file + 1, len + 1);
- }
- /* printf("name: %sn"); */
- if ((pw = getpwnam(name)) == NULL)
- {
- elog(ERROR, "No such user: %sn", name);
- ind = 0;
- }
- else
- {
- strcpy(str, pw->pw_dir);
- ind = len + 1;
- }
- }
- }
- else if (file[0] == '$')
- { /* $POSTGRESHOME, etc. expand it. */
- char environment[80],
- *envirp,
- *p;
- int len;
- if ((p = (char *) strchr(file, '/')) == NULL)
- {
- strcpy(environment, file + 1);
- len = strlen(environment);
- }
- else
- {
- len = (p - file) - 1;
- StrNCpy(environment, file + 1, len + 1);
- }
- envirp = getenv(environment);
- if (envirp)
- {
- strcpy(str, envirp);
- ind = len + 1;
- }
- else
- elog(ERROR, "Couldn't find %s in your environment", environment);
- }
- else
- ind = 0;
- strcat(str, file + ind);
- return str;
- }
- char *
- filename_out(char *s)
- {
- char *ret;
- if (!s)
- return (char *) NULL;
- ret = (char *) palloc(strlen(s) + 1);
- if (!ret)
- elog(ERROR, "filename_out: palloc failed");
- return strcpy(ret, s);
- }