Transferred from: http://www.cnblogs.com/alan-forever/p/3721908.html
The current working directory can be obtained by GETCWD ().
1 #include <unistd.h>2 3 char *getcwd (char *cwdbuf, size_t size);
A successful call returns a pointer to Cwdbuf, and the failure returns NULL.
GETCWD () is the implementation of "Linux/unix system Programming Manual" exercise 18.5, the title is as follows:
Implement a function that is equivalent to GETCWD (). Tip: To get the name of the current working directory, you can call Opendir () and Readdir () to traverse its parent directory (.. ) to find an entry that has the same I-node number and device number as the current working directory. And so on, along the level of the Directory tree layer (ChDir (..) ) and scan, you can build a complete directory path. When the current directory is the same as the current working directory, it ends the traversal. Whether the function is invoked successfully or not, the caller should be dispatched back to its starting directory (using open () and Fchdir () to easily implement this function)
1. Get the file information through stat, find the correct directory according to the I-node number and device number in the file information
2, using Opendir (), Readdir () to obtain the directory information, the directory can not be read () to obtain information.
Ps:tlpi_hdr.h header file for "Linux/unix system Programming Manual" header file, you can go to the author's website to download, where the Errexit () is the error handling function ....
1/* 2 * ===================================================================================== 3 * 4 * Filen AME:18.5.C 5 * 6 * description:7 * 8 * version:1.0 9 * created:2014 May 11 14:04 35 sec 1 0 * Revision:none * COMPILER:GCC * Author:alan (), [email protected] 14 * ORGANIZATION:15 * 16 * ===================================================================================== 17 */ #include <sys/stat.h> #include <fcntl.h> #include <dirent.h> #include <sys/types.h> ; #include "tlpi_hdr.h" #define BUF_MAX 4096, extern int errno; *GETCWD Char (char *cwdbuf, size_t size) {of char Path[buf_max], Cwd[buf_max]; DIR *dirp; Irent *DP; The struct stat sb, sb_d, sb_1; dev_t Dev; ino_t ino; PNS while (1) {38//Get the file information for the current directory if (stat (".", &sb) = =-1) erreXIT ("stat"); * Dev = sb.st_dev; Ino = Sb.st_ino; 43 44//Gets the parent directory's corresponding directory stream and the parent directory's file information (Dirp = Opendir (".")) = = NULL) errexit ("Opendir"); if (Stat (":", &sb_1) = =-1) errexit ("stat"); 49 50//Determine if the current directory is the same as the parent directory if (Sb_1.st_dev = = Dev && Sb_1.st_ino = = Ino) errno = 0; 55 56//directory stream read entry in parent directory (DP = Readdir (DIRP)) = NULL) {snprintf (path, buf_ MAX, "... /%s ", dp->d_name); if (stat (path, &sb_d) = =-1) errexit ("stat"); 62 63//Get the entry for the current directory and refine the catalog to the full if (dev = sb_d.st_dev && ino = = Sb_d.st_ino) {65 memset (CWD, 0, sizeof (CWD)); if (strcat (CWD, "/") = = NULL) errexit ("strcat"); if (strcat (CWD, dp->d_name) = = NULL) errexit ("strcat"); 70 if (strcat (CWD, cwdbuf) = = NULL) errexit ("strcat"); if (strncpy (Cwdbuf, CWD, buf_max) = = NULL) errexit ("strncpy"); a break; (+) (DP = NULL && errno! = 0) Bayi errexit (" Readdir "); Closedir (DIRP); ChDir (".."); Change the current directory, cwdbuf return; The ARGC int main (int, char *argv[]) {Buf[buf_max char]; T_buf[buf_max]; char Char *p; 94 int FD; (FD = Open (".", o_rdonly) = =-1) errexit ("open"); 98 if (argc! = 1) usageerr ("%s", Argv[0]); 101 102 p = GETCWD (buf, Buf_max); 103 if (p = = NULL) 104 Errexit ("My Getcwd"); ("My GETCWD:%s\n", p); 106 Fchdir (FD); Return the original directory 107 108 p = GETCWD (T_buf, Buf_max), 109 if (p = = NULL) errexit ("GETCWD"), 111 printf ("GETCWD:%s\n ", p); 113 exit (exit_success); 114}
Test results:
[Email protected]:~/code/tlpi$ pwd/home/lancelot/code/tlpi[email protected]:~/code/tlpi$./18.5 My GETCWD:/home/ LANCELOT/CODE/TLPIGETCWD:/home/lancelot/code/tlpi
Implementation of Linux GETCWD () "Go"