a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 74|回复: 0

[综合辅导] Linux认证备考辅导之unix环境高级编程

[复制链接]
发表于 2012-8-4 12:07:07 | 显示全部楼层 |阅读模式
Linux认证备考辅导之unix环境高级编程
. J7 ^: E* G/ d- U+ ]# q每个进程都有一个当前的工作目录,此目录是搜索所有相对路径的起点。
/ T" W  w+ D; X' L! d% a进程通过调用chdir或fcdir函数可以更改当前工作目录。- S7 e2 k$ q% C& J1 c- Q4 B1 [& D
进程通过调用chdir或者fchdir函数可以更改当前工作目录。& `- P7 t0 t3 [8 z9 Z5 n$ w2 S
view plain #include《unistd.h》 int chdir(const char* pathname);int fchdir(int filedes);
: i/ [/ Z. t& Q- A& c" P; t9 O这两个函数分别用文件名和文件描述符来制定新的工作目录。
0 k6 F) k# h" S& Q+ y0 x. ]; g先查看GNU C手册。
+ w! ?, @5 ]( K: qint 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.# i' R. R0 B1 [' o7 \4 C
实例;$ w5 y) N. n& @+ j3 f, J
view plain #include“apue.h”* {# i, ?+ E2 i
int main(void)
: o  e6 P% K: K& W9 x{ if(chdir(“/devis/wangchenglin”)《0)6 ]) W# }. z$ X7 J' F( K+ d
err_sys(“chdir failed”);printf(“chdir to /devis/wangchenglin succeeded\n”);exit(0);
& Z8 {2 J& I* q6 m( f9 B( S}
0 \, [9 @, _% _2 k" _运行结果:
9 M! \7 J4 S- B) P0 ^7 h" i看到并没有改变工作目录,这是因为shell创建了一个子进程,又该自己子进程执行这个命令。可知,为了改变shell进程自己的工作目录,shell应该直接调用chdir函数,为此cd命令的执行程序直接包含在shell程序中。& Q+ R; o' ^. ~* R4 |
函数getcwd这样的功能,它从当前工作目录(。目录项开始),用……目录项找到其上一级的目录,然后赌气目录项,知道该目录项中的i节点编号与工作目录i节点编号相同,这就就找到了其对应文件名。按照这种方法,组成上一,直到遇到跟。
7 c% s( {# X: D: T6 V/ B/ dview plain #include《unistd.h》
3 q% z, P: w2 |, C& Fchar *getcwd(char* buf,size_t size);
# H& i/ @* T' ~向此函数传递两个参数,一个是缓冲地址buf,领个是缓冲长度size.缓冲必须有足够长度以容纳绝对路径名再加上一个null终止符,否则返回出错。
3 \1 U8 A" ^' Q7 B+ N6 F, y7 @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:
- h" O7 V6 a6 ]* Dview plain char * gnu_getcwd ()" K3 ]) p6 l( M/ X1 g
{ size_t size = 100;
! o; c( Y* Z" ~6 Ywhile (1)
. V( P) L! A% ?( T6 Y{ char *buffer = (char *) xmalloc (size);if (getcwd (buffer, size) == buffer)5 O  |3 D/ j# ^% v& S7 @% r- o
return buffer;free (buffer);if (errno != ERANGE)0 r. F+ t3 e: A! I* f# M+ j0 O4 \
return 0;size *= 2;}
5 _( J3 B9 `$ l6 R% c实例
) {# s7 ~# t( Jview 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)
7 z1 Y( P  R2 a{ char * ptr;int size;9 i) Y+ s( I6 U2 V( ]/ o
if(chdir(“/devis/wangchenglin”)《0)
. u2 u; @3 {' w( {0 k+ o) r. Uerr_sys(“chdir failed”);$ j$ c3 }" w# y6 m* N7 O" G6 V: B
ptr=path_alloc(&size);if(getcwd(ptr,size)==NULL)
1 \5 N3 r4 I8 q* ?7 E; d: @err_sys(“getcwd failed”);, w( ^" I  Z& }, Y& d0 j
printf(“cwd=%s\n”,ptr);exit(0);1 ^# J) a2 R9 i7 v" B' S: \
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-18 02:06 , Processed in 0.176875 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表