unix環(huán)境高級(jí)編程
進(jìn)程通過調(diào)用chdir或fcdir函數(shù)可以更改當(dāng)前工作目錄。
進(jìn)程通過調(diào)用chdir或者fchdir函數(shù)可以更改當(dāng)前工作目錄。
view plain #include
這兩個(gè)函數(shù)分別用文件名和文件描述符來制定新的工作目錄。
先查看GNU C手冊(cè)。
int chdir (const char *filename) [Function] This function is used to set the process‘s working directory to filename. The normal, successful return value from chdir is 0. A value of -1 is returned to indicate an error. The errno error conditions defined for this function are the usual file name syntax errors (see Section 11.2.3 [File Name Errors], page 224), plus ENOTDIR if the file filename is not a directory. int fchdir (int filedes) [Function] This function is used to set the process’s working directory to directory associated with the file descriptor filedes. The normal, successful return value from fchdir is 0. A value of -1 is returned to indicate an error. The following errno error conditions are defined for this function:EACCES Read permission is denied for the directory named by dirname. EBADF The filedes argument is not a valid file descriptor. ENOTDIR The file descriptor filedes is not associated with a directory. EINTR The function call was interrupt by a signal.
實(shí)例;
view plain #include"apue.h"
int main(void)
{ if(chdir("/devis/wangchenglin")<0)
err_sys("chdir failed");printf("chdir to /devis/wangchenglin succeededn");exit(0);
}
運(yùn)行結(jié)果:
看到并沒有改變工作目錄,這是因?yàn)閟hell創(chuàng)建了一個(gè)子進(jìn)程,又該自己子進(jìn)程執(zhí)行這個(gè)命令?芍,為了改變shell進(jìn)程自己的工作目錄,shell應(yīng)該直接調(diào)用chdir函數(shù),為此cd命令的執(zhí)行程序直接包含在shell程序中。
函數(shù)getcwd這樣的功能,它從當(dāng)前工作目錄(。目錄項(xiàng)開始),用……目錄項(xiàng)找到其上一級(jí)的目錄,然后賭氣目錄項(xiàng),知道該目錄項(xiàng)中的i節(jié)點(diǎn)編號(hào)與工作目錄i節(jié)點(diǎn)編號(hào)相同,這就就找到了其對(duì)應(yīng)文件名。按照這種方法,組成上一,直到遇到跟。
view plain #include
char *getcwd(char* buf,size_t size);
向此函數(shù)傳遞兩個(gè)參數(shù),一個(gè)是緩沖地址buf,領(lǐng)個(gè)是緩沖長(zhǎng)度size.緩沖必須有足夠長(zhǎng)度以容納絕對(duì)路徑名再加上一個(gè)null終止符,否則返回出錯(cuò)。
char * getcwd (char *buffer, size t size) [Function] The getcwd function returns an absolute file name representing the current working directory, storing it in the character array buffer that you provide. The size argument is how you tell the system the allocation size of buffer. The GNU library version of this function also permits you to specify a null pointer for the buffer argument. Then getcwd allocates a buffer automatically, as with malloc(see Section 3.2.2 [Unconstrained Allocation], page 33)。 If the size is greater than zero, then the buffer is that large; otherwise, the buffer is as large as necessary to hold the result. The return value is buffer on success and a null pointer on failure. The following errno error conditions are defined for this function:EINVAL The size argument is zero and buffer is not a null pointer. ERANGE The size argument is less than the length of the working directory name. You need to allocate a bigger array and try again. EACCES Permission to read or search a component of the file name was denied. You could implement the behavior of GNU‘s getcwd (NULL, 0) using only the standard behavior of getcwd:
view plain char * gnu_getcwd ()
{ size_t size = 100;
while (1)
{ char *buffer = (char *) xmalloc (size);if (getcwd (buffer, size) == buffer)
return buffer;free (buffer);if (errno != ERANGE)
return 0;size *= 2;}
實(shí)例
view plain #include "apue.h" char*path_alloc(int* size) { char *p = NULL; if(!size) return NULL; p = malloc(256); if(p) *size = 256; else *size = 0; return p; } int main(void)
{ char * ptr;int size;
if(chdir("/devis/wangchenglin")<0)
err_sys("chdir failed");
ptr=path_alloc(&size);if(getcwd(ptr,size)==NULL)
err_sys("getcwd failed");
printf("cwd=%sn",ptr);exit(0);
}