Linux C Create directory functions mkdir related "Go"

Source: Internet
Author: User
Tags create directory strcmp

Transferred from: http://blog.csdn.net/fallenink/article/details/8480483

Original address: http://sharp2wing.iteye.com/blog/1280802

————————————————————————————————————————————————

I.linux C Creating a directory function mkdir mode setting problem

Function Prototypes:

#include <sys/stat.h>

int mkdir (const char *path, mode_t mode);

Parameters:

Path is the directory name

Mode is the directory permission

return value:

A return of 0 indicates success, 1 indicates an error, and the errno value is set.

Mode bit:

Mode indicates the permissions for the new directory and can take the following values:

S_irusr
S_iread

S_iwusr
S_iwrite
S_ixusr
S_iexec
S_irwxu
This was equivalent to (S_IRUSR | S_IWUSR | S_IXUSR).
S_irgrp
Read permission bit for the group owner of the file. Usually 040.
S_iwgrp
Write permission bit for the group owner of the file. Usually 020.
S_ixgrp
Execute or search permission bit for the group owner of the file. Usually 010.
S_irwxg
This was equivalent to (S_IRGRP | S_iwgrp | S_IXGRP).
S_iroth
Read permission bit for other users. usually 04.
S_iwoth
Write permission bit for other users. usually 02.
S_ixoth
Execute or search permission bit for other users. usually 01.
S_irwxo
This was equivalent to (S_iroth | S_iwoth | S_ixoth).
S_isuid
The Set-user-id on execute bit, usually 04000. See how to change Persona.
S_isgid
The Set-group-id on execute bit, usually 02000. See how to change Persona.
S_isvtx
This is the sticky bit, usually 01000.

"Man 2 mkdir can see the next"


Example:

#include <sys/types.h> #include <sys/stat.h>
int status;

Status = MkDir ("/home/newdir", S_irwxu | S_irwxg | S_iroth | S_ixoth);

This creates a Newdir directory where permissions are viewed by Ls-al as

Drwxr-xr-x

Consistent with the directory permission bits created with the Linux command mkdir.



Ii. creating a multilevel directory in C language under Linux

int Createdir (const char *spathname)
{
Char dirname[256];
strcpy (DirName, spathname);
int i,len = strlen (DirName);
if (dirname[len-1]!= '/')
strcat (DirName, "/");

Len = strlen (DirName);

for (I=1;   i<len; i++)
{
if (dirname[i]== '/')
{
Dirname[i] = 0;
if (Access (DirName, NULL)!=0)
{
if (mkdir (DirName, 0755) ==-1)
{
Perror ("mkdir error");
return-1;
}
}
Dirname[i] = '/';
}
}

return 0;
}

Iii.linux C Programming: Create a thread, monitor a directory, and once a new file appears in the directory, transfer the files to the specified directory.
/*
Header file
*/
#define Srcpath "srcpath/"
#define Dstpath "dstpath/"

int MoveFile ()
{
DIR *dir;
struct Dirent *dt;
FILE *FP1,*FP2;
Char filename1[256],filename2[256];
Char buf[1024];
int readsize,writesize;

if (dir = Opendir (srcpath)) = = NULL)
{
printf ("Opendir%s error\n", Srcpath);
return-1;
}
memset (filename1,0,sizeof (filename1));
strcpy (Filename1,srcpath);
memset (filename2,0,sizeof (filename2));
strcpy (Filename2,dstpath);
while (1)
{
while ((dt = Readdir (dir)) = NULL)
{
if (strcmp (Dt->d_name, ".") ==0| | strcmp (Dt->d_name, "..") ==0)
{
Continue
}
If there is a directory in this directory, you can judge the
This assumes that the initial empty directory
strcat (Filename1,dt->d_name);
strcat (Filename2,dt->d_name);
If you have fewer process resources, you can use Linux system commands directly

FP1 = fopen (filename1, "RB");
if (fp1==null)
{
printf ("Open%s failed/n", filename1);
return-1;
}

FP2 = fopen (filename2, "WB");
if (fp2==null)
{
printf ("Open%s failed/n", filename2);
Fclose (FP1);
return-1;
}

while ((ReadSize = Fread (buf,sizeof (BUF), 1,FP1)) >0)
{
Total + = ReadSize;
memset (buf,0,sizeof (BUF));
Writesize = fwrite (buf,sizeof (BUF), 1,FP2);
if (writesize!==readsize)
{
printf ("Write error");
Return-2;
Fclose (FP1);
Fclose (FP2);
}
}
Fclose (FP1);
Fclose (FP2);
RmDir (filename2);
}
}
}

int main (int argc,char **argv)
{
pthread_t id1;
int ret;
ret = pthread_create (&AMP;ID1, NULL, (void*) movefile, NULL);
return ret;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////

1. Create a Directory

#include <sys/stat.h>
#include <sys/types.h>

int mkdir (const char *pathname, mode_t mode);

Where mode is used in the form of 0777,0755.

2. Determine if a directory exists

Opendir can be used to judge, this is a relatively simple way.

#include <sys/types.h>
#include <dirent.h>

DIR *opendir (const char *name);

The Opendir () function opens a directory stream corresponding to the directory name, and returns a pointer to the D  Irectory Stream. The stream is positioned at the first entry in the directory.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////

Access (): Determine if you have permission to access files

related functions
Stat,open,chmod,chown,setuid,setgid
Table header File
#include <unistd.h>
defining functions
int access (const char * pathname, int mode);
function Description
Access () checks whether a file that already exists can be read/written. The parameter mode has several case combinations, R_OK,W_OK,X_OK and F_OK. R_OK,W_OK and X_OK are used to check whether a file has read, write, and execute permissions. The F_OK is used to determine whether the file exists. Because Access () only checks for permissions, ignores file patterns or file contents, if a directory is represented as "writable", it means that operations such as creating new files in the directory can be made, rather than implying that the directory can be treated as a file. For example, you will find that DOS files have "executable" permissions, but will fail when executed with EXECVE ().
return value
If all the permissions you want to check are checked, a value of 0 is returned, which indicates success and returns 1 if one of the permissions is disabled.
error code
The file specified by the eaccess parameter pathname does not meet the permissions required for the test.
Erofs files to test write permissions exist in the read-only file system.
The Efault parameter pathname pointer is out of the accessible memory space.
The EINVAL parameter mode is incorrect.
Enametoolong parameter pathname too long.
The Enotdir parameter pathname is a directory.
Enomem Core memory is low
The Eloop parameter pathname has too many symbolic connection problems.
EIO I/O access error.
Additional Instructions
Use Access () for user authentication to be particularly cautious, such as an empty file after access () to open () may cause problems with the system security.

Also refer to:the Linux access () function and the Readdir () function

Linux C Create directory functions mkdir related "Go"

Related Article

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.