File: fileUtils.c

package info (click to toggle)
nedit 5.02-4
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 2,456 kB
  • ctags: 2,911
  • sloc: ansic: 39,134; yacc: 335; makefile: 65; sh: 8
file content (212 lines) | stat: -rw-r--r-- 5,981 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*******************************************************************************
*									       *
* fileUtils.c -- File utilities for Nirvana applications		       *
*									       *
* Copyright (c) 1991 Universities Research Association, Inc.		       *
* All rights reserved.							       *
* 									       *
* This material resulted from work developed under a Government Contract and   *
* is subject to the following license:  The Government retains a paid-up,      *
* nonexclusive, irrevocable worldwide license to reproduce, prepare derivative *
* works, perform publicly and display publicly by or for the Government,       *
* including the right to distribute to other Government contractors.  Neither  *
* the United States nor the United States Department of Energy, nor any of     *
* their employees, makes any warrenty, express or implied, or assumes any      *
* legal liability or responsibility for the accuracy, completeness, or         *
* usefulness of any information, apparatus, product, or process disclosed, or  *
* represents that its use would not infringe privately owned rights.           *
*                                        				       *
* Fermilab Nirvana GUI Library						       *
* July 28, 1992								       *
*									       *
* Written by Mark Edel							       *
*									       *
* Modified by:	DMR - Ported to VMS (1st stage for Histo-Scope)		       *
*									       *
*******************************************************************************/
static char SCCSID[] = "@(#)fileUtils.c	1.5     1/10/96";
#include <string.h>
#ifdef VAXC
#define NULL (void *) 0
#endif /*VAXC*/
#ifdef VMS
#include "vmsparam.h"
#else
#include <sys/param.h>
#include <unistd.h>
#include <pwd.h>
#endif /*VMS*/
#include "fileUtils.h"

#define TRUE 1
#define FALSE 0

static int normalizePathname(char *pathname);
static int compressPathname(char *pathname);
static char *nextSlash(char *ptr);
static char *prevSlash(char *ptr);
static int compareThruSlash(char *string1, char *string2);
static void copyThruSlash(char **toString, char **fromString);

/*
** Decompose a Unix file name into a file name and a path
*/
int ParseFilename(char *fullname, char *filename, char *pathname)
{
    int fullLen = strlen(fullname);
    int i, pathLen, fileLen;
	    
#ifdef VMS
    /* find the last ] or : */
    for (i=fullLen-1; i>=0; i--) {
    	if (fullname[i] == ']' || fullname[i] == ':')
	    break;
    }
#else  /* UNIX */
    /* find the last slash */
    for (i=fullLen-1; i>=0; i--) {
    	if (fullname[i] == '/')
	    break;
    }
#endif

    /* move chars before / (or ] or :) into pathname,& after into filename */
    pathLen = i + 1;
    fileLen = fullLen - pathLen;
    strncpy(pathname, fullname, pathLen);
    pathname[pathLen] = 0;
    strncpy(filename, &fullname[pathLen], fileLen);
    filename[fileLen] = 0;
#ifdef VMS
    return TRUE;
#else     /* UNIX specific... Modify at a later date for VMS */
    return normalizePathname(pathname);
#endif
}

#ifndef VMS

/*
** Expand tilde characters which begin file names as done by the shell
*/
int ExpandTilde(char *pathname)
{
    struct passwd *passwdEntry;
    char username[MAXPATHLEN], temp[MAXPATHLEN], *nameEnd;
    
    if (pathname[0] != '~')
	return TRUE;
    nameEnd = strchr(&pathname[1], '/');
    if (nameEnd == NULL)
	nameEnd = pathname + strlen(pathname);
    strncpy(username, &pathname[1], nameEnd - &pathname[1]);
    username[nameEnd - &pathname[1]] = '\0';
    if (username[0] == '\0')
    	passwdEntry = getpwuid(getuid());
    else
    	passwdEntry = getpwnam(username);
    if (passwdEntry == NULL)
	return FALSE;
    sprintf(temp, "%s/%s", passwdEntry->pw_dir, nameEnd);
    printf("expanded path name to \"%s\"\n", temp);
    strcpy(pathname, temp);
    return TRUE;
}
    
static int normalizePathname(char *pathname)
{
    char oldPathname[MAXPATHLEN], wd[MAXPATHLEN];

    /* if this is a relative pathname, prepend current directory */
    if (pathname[0] != '/') {
        /* make a copy of pathname to work from */
	strcpy(oldPathname, pathname);
	/* get the working directory */
	getcwd(wd, MAXPATHLEN);
	/* prepend it to the path */
	strcpy(pathname, wd);
	strcat(pathname, "/");
	strcat(pathname, oldPathname);
    }
    /* compress out .. and . */
    return compressPathname(pathname);
}


static int compressPathname(char *pathname)
{
    char *inPtr, *outPtr;

    /* compress out . and .. */
    inPtr = &pathname[1];		/* start after initial / */
    outPtr = &pathname[1];
    while (TRUE) {
	/* if the next component is "../", remove previous component */
	if (compareThruSlash(inPtr, "../")) {
	    /* error if already at beginning of string */
	    if (outPtr == &pathname[1])
	        return FALSE;
	    /* back up outPtr to remove last path name component */
	    outPtr = prevSlash(outPtr);
	    inPtr = nextSlash(inPtr);
	} else if (compareThruSlash(inPtr, "./")) {
	    /* don't copy the component if it's the redundant "./" */
	    inPtr = nextSlash(inPtr);
	} else {
	    /* copy the component to outPtr */
	    copyThruSlash(&outPtr, &inPtr);
	}
	if (inPtr == NULL) {
	    return TRUE;
	}
    }
}

static char *nextSlash(char *ptr)
{
    for(; *ptr!='/'; ptr++) {
    	if (*ptr == '\0')
	    return NULL;
    }
    return ptr + 1;
}

static char *prevSlash(char *ptr)
{
    for(ptr -= 2; *ptr!='/'; ptr--);
    return ptr + 1;
}

static int compareThruSlash(char *string1, char *string2)
{
    while (TRUE) {
    	if (*string1 != *string2)
	    return FALSE;
	if (*string1 =='\0' || *string1=='/')
	    return TRUE;
	string1++;
	string2++;
    }
}

static void copyThruSlash(char **toString, char **fromString)
{
    char *to = *toString;
    char *from = *fromString;
    
    while (TRUE) {
        *to = *from;
        if (*from =='\0') {
            *fromString = NULL;
            return;
        }
	if (*from=='/') {
	    *toString = to + 1;
	    *fromString = from + 1;
	    return;
	}
	from++;
	to++;
    }
}
#endif /* UNIX */