You can use getcwd () to obtain the current working directory.
1 #include <unistd.h>2 3 char *getcwd(char *cwdbuf, size_t size);
If the call succeeds, a pointer to cwdbuf is returned. If the call fails, NULL is returned.
The implementation of getcwd () is exercise 18.5 in the Linux/Unix programming manual. The questions are as follows:
Implement a function equivalent to getcwd. Tip: to get the name of the current working directory, you can use opendir () and readdir () to traverse its parent directory (..) in the left-side navigation pane. In this way, you can build a complete directory path by checking and scanning the directory tree (chdir. When the current directory is the same as the current working directory, the traversal ends. Whether the function is successfully called or not, the caller should be sent back to the starting directory (using open () and fchdir () to conveniently implement this function)
1. Use stat to obtain the file information and find the correct directory based on the I-node number and device number in the file information.
2. Use opendir () and readdir () to obtain the directory information. The directory cannot be obtained through read.
PS: The tlpi_hdr.h header file is the header file of the Linux/Unix System Programming Manual. You can download it from the author's website. errExit () is the error handler function ....
1/* 2 * ========================================== ========================================================== =========== 3*4 * Filename: 18.5.c 5*6 * Description: 7*8 * Version: 1.0 9 * Created: May 11, 2014 minutes 35 seconds 10 * Revision: none 11 * Compiler: gcc 12*13 * Author: alan (), alan19920626@gmail.com 14 * Organization: 15*16 * ============================================ ========================================================== ========== 17 */18 19 # include <sys/stat. h> 20 # include <fcntl. h> 21 # include <dirent. h> 22 # include <sys/types. h> 23 # include "tlpi_hdr.h" 24 25 # define BUF_MAX 4096 26 27 extern int errno; 28 29 char * Getcwd (char * cwdbuf, size_t size) {30 char path [BUF_MAX], cwd [BUF_MAX]; 31 DIR * dirp; 32 struct dirent * dp; 33 struct stat sb, sb_d, sb_1; 34 dev_t dev; 35 ino_t ino; 36 37 while (1) {38 // get the file information of the current directory 39 if (s Tat (". ", & sb) =-1) 40 errExit (" stat "); 41 dev = sb. st_dev; 42 ino = sb. st_ino; 43 44 // get the file information of the corresponding directory stream and parent directory of the parent directory 45 if (dirp = opendir (".. ") = NULL) 46 errExit (" opendir "); 47 if (stat (".. ", & sb_1) =-1) 48 errExit (" stat "); 49 50 // determine whether the current directory is the same as the parent directory 51 if (sb_1.st_dev = dev & sb_1.st_ino = ino) 52 break; 53 54 errno = 0; 55 56 // read The Entry 57 while (dp = readdir (dirp) in the directory stream corresponding to the parent directory ))! = NULL) {58 snprintf (path, BUF_MAX ,".. /% s ", dp-> d_name); 59 60 if (stat (path, & sb_d) =-1) 61 errExit (" stat "); 62 63 // get the entries corresponding to the current directory and gradually improve the directory 64 if (dev = sb_d.st_dev & ino = sb_d.st_ino) {65 memset (cwd, 0, sizeof (cwd); 66 if (strcat (cwd, "/") = NULL) 67 errExit ("strcat"); 68 if (strcat (cwd, dp-> d_name) = NULL) 69 errExit ("strcat"); 70 if (strcat (cwd, cwdbuf) = NULL) 71 errExit ("strcat "); 72 73 if (Strncpy (cwdbuf, cwd, BUF_MAX) = NULL) 74 errExit ("strncpy"); 75 break; 76} 77 78} 79 80 if (dp = NULL & errno! = 0) 81 errExit ("readdir"); 82 83 closedir (dirp); 84 chdir (".. "); // change the current directory 85} 86 87 return cwdbuf; 88} 89 90 int main (int argc, char * argv []) {91 char buf [BUF_MAX]; 92 char t_buf [BUF_MAX]; 93 char * p; 94 int fd; 95 96 if (fd = open (". ", O_RDONLY) =-1) 97 errExit (" open "); 98 99 if (argc! = 1) 100 usageErr ("% s", argv [0]); 101 102 p = Getcwd (buf, BUF_MAX); 103 if (p = NULL) 104 errExit ("My getcwd"); 105 printf ("My getcwd: % s \ n", p); 106 fchdir (fd ); // send back the original directory 107 108 p = getcwd (t_buf, BUF_MAX); 109 if (p = NULL) 110 errExit ("getcwd"); 111 printf ("getcwd: % s \ n ", p); 112 113 exit (EXIT_SUCCESS); 114}
Test results:
lancelot@debian:~/Code/tlpi$ pwd/home/lancelot/Code/tlpilancelot@debian:~/Code/tlpi$ ./18.5 My getcwd: /home/lancelot/Code/tlpigetcwd: /home/lancelot/Code/tlpi
Tucao & harvest: I was planning to write learning records slowly, but I felt that I learned a lot only by writing some key points and some exercises, so I plan to put them aside and write them again when I have time. The key points must be summarized slowly. First, I did some exercises. By the way, I learned how to use system call and library functions to do some practical things first ...... Do this one afternoon... Really weak food, but it's really fun to make it ..... There's a long way to go !!! Continue to work !!!