Linux C implements MKDIR functionality

Source: Internet
Author: User
Tags strcmp


One: Background

The mkdir command in Linux is used to create a directory, corresponding to the use of Linux system call function mkdir to implement the function of directory creation. Just to create a directory, a system call is sufficient, this article is to use the MkDir function to implement the MKDIR-P this option to create a parent directory implementation that does not exist.


Second: Ideas

for a a/b/c such a multi-level directory, to implement the parent directory to create a lot of methods and ideas, you can do string processing out of a first-level directory, but this implementation is very cumbersome, so that I think of recursive implementation.

Ideas are as follows:

1. First determine whether A/B/C exists, does not exist to obtain its parent directory judgment. If there is a direct exit

2. Determine whether A/b exists, does not exist to obtain its parent directory, if there is an exit

3. Determine whether a A/exists, not exist to obtain its parent directory, if there is an exit

4. Exit if its parent directory is. or/

The idea is broadly as follows. To achieve this, you need a function that can get the parent directory of a directory. Decisive man.

The final positioning of the dirname function is very much in line with my requirements. The function declaration is as follows:

#include <libgen.h>char *dirname (char *path);

Three: Realize

#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include  <sys/stat.h># include <sys/types.h> #include  <libgen.h> #include  <string.h> #define   Maxsize 100void createdir (char *); Int main (int argc,char *argv[]) {#判断参数          if (argc <= 1) {                 printf ("mkdir:usage:dirstr\n");                 exit (1);         } #遍历参数, operate on each parameter         while (--ARGC) {                 argv++;                 createdir (*ARGV);         }}void createdir (Char *path) {        char data[ maxsize];          #判断是否是当前目录或/Catalogue          if (strcmp (Path, ".")  == 0)  | |   (strcmp (Path, "/") ==0)                  return;          #判断目录是否存在          if (Access (PATH,F_OK)  == 0)                  return;         else{         #保存目录                  strcpy (Data,path);          #获取目录的父目录                  dirname (path);         #递归执行                  createdir (PATH);         }         #创建目录          if (mkdir (data,777)  == -1) {                 perror ("Mkdir error");                 exit (1);          }        return;}

Four: summary

in writing mkdir-p This function, the idea is very clear, the code is basically written well, but the debugging took a long time. The reason is that dirname this function, see its declaration is obviously to a directory of the path string pointer, return a pointer to its directory of string pointers, but it is not. DirName not only returns a string pointer to its parent directory, but it may also modify the value of the passed parameter path to the parent directory string. The man document describes the following:

The dirname () function may modify the string pointed-to-path, and may return a pointer to static storage Then being overwritten by subsequent calls to DirName (). Eventually it was debugged via printf, without the help of GDB, which was mostly uncomfortable with the guy.

This article is from the "Focus on Linux" blog, so be sure to keep this source http://forlinux.blog.51cto.com/8001278/1530287

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.