File: chdir.cpp

package info (click to toggle)
perm 0.4.0-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 976 kB
  • sloc: cpp: 13,499; makefile: 98; sh: 12
file content (240 lines) | stat: -rw-r--r-- 4,727 bytes parent folder | download | duplicates (5)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "chdir.h"
#ifdef _WIN32
#include <direct.h>
#else
#ifdef _WIN64
#include <direct.h>
#else
#define _chdir chdir
#define _getcwd getcwd
#include <unistd.h>
#endif
#endif

int goto_working_directory(const char* path)
{
    int returnV;
    if (path[0] == '\0') {
        returnV = -1;
    } else {
        returnV = tosomedir(path);
    }
    /** currently no warning message
    if(returnV == -1) {
    	cout << "Stay in the current directory" << endl;
    	print_working_directory();
    } */
    return(returnV);
}

char* get_working_directory(char* path)
{
    if (_getcwd(path, FILENAME_MAX) == NULL) {
        perror("_getcwd error");
        path[0] = '\0';
    }
    return(path);
}

string get_working_directory(void)
{
    char path[FILENAME_MAX];
    if (get_working_directory(path)) {
        return(string(path));
    }
    return(string(""));
}

bool is_accessible_directory(const char * path)
{
    bool isAccessibleDir = false;
    string cwd = get_working_directory();
    if (goto_working_directory(path) == 0) {
        isAccessibleDir = true;
    }
    // go back to original dir
    goto_working_directory(cwd.c_str());
    return(isAccessibleDir);
}


int print_working_directory(void)
{
    char path[FILENAME_MAX];

    if (get_working_directory(path)) {
        cout << "\nThe working directory is " << path << endl;
    }
    return(0);
}

int toparentdir(void)
{
    int i, l;
    char path[FILENAME_MAX];

    if (_getcwd(path, FILENAME_MAX) == NULL)
        perror("_getcwd error");

    l = (int)strlen(path);
    for (i = l - 1; i >= 0; i--) {
#ifdef WIN32
        if (path[i] == '\\')
#else
        if (path[i] == '/')
#endif
            break;
        else if (path[i] == ':') {
            printf("Already in the root");
            return(-1);
        }
    }
    path[i] = '\0';

    if (_chdir(path)) {
        printf("Unable to locate the directory: %s\n", path);
        return(-1);
    } else if (path[i-1] == ':') {
        printf("Disk root");
        return(0);// in the root
    } else {
        return(1);
    }
}

int tochilddir(const char* dirname)
{
    int l;
    char path[FILENAME_MAX];

    if (_getcwd(path, FILENAME_MAX) == NULL)
        perror("_getcwd error");

    l = (int)strlen(path);
#ifdef WIN32
    path[l] = '\\';
#elif defined _WIN64
    path[l] = '\\';
#else
    path[l] = '/';
#endif
    strcpy(&path[l+1], dirname);

    if (_chdir(path)) {
        printf("Unable to locate the directory: %s\n", path);
        return(-1);
    } else {
        return(system("dir *.wri"));
    }
}

int tosomedir(const char* path)
{
    if (_chdir(path)) {
        // printf("Unable to locate the directory: %s\n", path);
        return(-1);
    } else {
        return(0);
    }
}

int tosiblingdir(const char* dirname)
{
    toparentdir();
    tochilddir(dirname);
    return(0);
}

int str2Int(char* str)
{
    int value;
    int i, j; //j is a value of a digit;

    for (value = 0, i = 0; (str[i] != '\0'); i++) {
        j = (int)(str[i] - '0');
        if (j >= 0 && j <= 9) {
            value = value * 10 + j;
        } else {
            return(-1);
        }
    }
    return(value);
}

string pathLize(const char* path) //For windows
{
    char dirPath[FILENAME_MAX];
#ifdef WIN32
    strcpy(dirPath, path);
#elif defined _WIN64
    strcpy(dirPath, path);
#else
    if (path[0] == '/')
        strcpy(dirPath, path);
    else
        sprintf(dirPath, "./%s", path);
#endif
    return(string(dirPath));
}

int createdir(const char* path)
{
    struct stat st;
#ifdef WIN32
    if ( stat(path, &st) == 0 || \
            _mkdir(path) == 0) {
#else
#ifdef _WIN64
    if ( stat(path, &st) == 0 || \
            _mkdir(path) == 0) {
#else
#define MODE_MASK 0777
    string dirPath = pathLize(path);
    if ( stat(dirPath.c_str(), &st) == 0 || \
            mkdir(dirPath.c_str(), MODE_MASK ) == 0) {
#endif
#endif
        return(0);
    } else {
        printf("Fail to create the dir %s", path);
        return(1);
    }
}

int deletedir(const char* path)
{
    char systemcomand[FILENAME_MAX];
    sprintf(systemcomand, "rmdir %s", path);
    return(system(systemcomand));
}
int deletefile(const char* path)
{
    char systemcomand[FILENAME_MAX];
    sprintf(systemcomand, "del %s", path);
    return(system(systemcomand));
}
char* getnamefrompath(char* Path, char* filename)
{
    int i;

    for (i = (int)strlen(Path); i >= 0; i--) {

#ifdef WIN32
        if (Path[i] == '\\')
#else
        if (Path[i] == '/')
#endif
        {
            break;
        }
    }
    strcpy(filename, &(Path[i+1]));
    for (i = (int)strlen(filename); i > 0; i--) {
        if (filename[i] == '.') {
            filename[i] = '\0';
        }
    }
    return(filename);
}