-
Notifications
You must be signed in to change notification settings - Fork 0
/
mypwd.c
124 lines (113 loc) · 3.32 KB
/
mypwd.c
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
/*
CPE 357 Lab05
Author: Dylan Baxter (dybaxter)
File: mypwd.c
Description: This file contains main functionality for a program that will
print the curren working directory of the the user up to 2048 characters.
and print "path too long" otherwise
*/
#include<unistd.h>
#include<limits.h>
#include"dir.h"
int main(int argc, char* argv[]){
/*Initialze Variables*/
char* path = (char*)malloc(sizeof(char)*PATH_MAX);
path[0] = '\0';
/*Dirlist of 1 char dirs has max of PATH_MAX/2 */
char* dirList[PATH_MAX/2];
int i;
/*Initialze dirlist*/
for(i = 0; i< (PATH_MAX/2); i++){
dirList[i] = NULL;
}
int dirCount = 0;
struct stat sb;
struct stat sb_old;
DIR* currentDir;
struct dirent* e;
/*While parent of current dir is not itself*/
while(!(isBaseDir())){
/*Stat Current Directory*/
if(-1 == stat(".",&sb_old)){
perror("Home stat fail");
exit(EXIT_FAILURE);
}
/*Go up a directory*/
if(-1==chdir("..")){
perror("Change Dir");
exit(EXIT_FAILURE);
}
/*Open parent dir for reading*/
if(NULL==(currentDir = opendir("."))){
perror("Open dir");
exit(EXIT_FAILURE);
}
if(NULL==(e = readdir(currentDir))){
perror("read dir");
exit(EXIT_FAILURE);
}
/*While there are files in parent dir*/
while((e = readdir(currentDir))){
/*Look for a file with the same device
and inode as previous*/
if(-1 == stat(e->d_name,&sb)){
perror("Home stat fail");
exit(EXIT_FAILURE);
}
if(statEquals(sb, sb_old)){
/*Store name of file in path string*/
if(dirCount > PATH_MAX/2){
printf("Filepath too long\n");
exit(EXIT_FAILURE);
}
dirList[dirCount] = mystrdup(e->d_name);
dirCount++;
break;
}
}
/*Close Current Dir*/
if(-1==closedir(currentDir)){
perror("Close dir");
exit(EXIT_FAILURE);
}
}
/*Initialize path Process*/
int pathLength = 0;
char delim[2];
delim[0] = '/';
delim[1] = '\0';
limitConcat(path, delim, PATH_MAX);
pathLength++;
/*Construct Path*/
for(i=dirCount-1; i >= 0;i--){
/*For each directory in reverse order*/
if(dirList[i] != NULL){
/*Increase pathlength*/
pathLength = pathLength+strlen(dirList[i]);
/*Check to make sure path does not exceed limit*/
if(pathLength < PATH_MAX-1){
/*Add dir name and foreward slash*/
limitConcat(path, delim, PATH_MAX);
pathLength++;
limitConcat(path, dirList[i], PATH_MAX);
}
else{
printf("Filepath too long!\n");
exit(EXIT_FAILURE);
}
}
}
/*Null terminate*/
path[pathLength+1] = '\0';
/*Return to original dir and print path*/
chdir(path);
printf("%s\n",path);
/*Free Memory*/
free(path);
for(i = 0; i< (PATH_MAX/2); i++){
if(dirList[i] != NULL){
free(dirList[i]);
}
}
return 0;
}