LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-09-2011, 07:36 PM   #1
wch705
LQ Newbie
 
Registered: Jul 2010
Location: Australia
Distribution: Debian
Posts: 23

Rep: Reputation: 0
writing a mkdir -p C program


i am learning how to write a mkdir with -p option.
I am struggling for creating subfolders.
the chdir() is not working and thus subdirectories can't be created.
could someone please have a look for me,thanks?
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>

void tokenizer(char *path);
main(int ac,char *av[])
{
	
	int result;
	if (strcmp(av[1],"-p")!=0)
	{
		if (mkdir(av[2],0644)==-1)
		{
			perror("cant make it");
			exit(1);
		}
	
	}
	else if (strcmp(av[1],"-p")==0)
	tokenizer(av[2]);
	
	return EXIT_SUCCESS;
}
void tokenizer(char *path)
{
	char *dir;
	if (mkdir(path, 0644)==-1)
	{
		dir=strtok(path,"/");
		while (dir!=NULL)
		{
			printf("%s\n",dir);
			mkdir(dir, 0644);
			if (chdir(dir)==-1)
			printf("not right\n");
			dir=strtok(NULL,"/");

		}
	
	}
}
 
Old 04-09-2011, 10:20 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I would suggest that you avoid the usage of chdir(); that sort of violates what I believe the command-line mkdir is doing. Consider the following -- if you want to make the series of directories tmp/foo/morefoo, then you would first create tmp, then tmp/foo, and finally tmp/foo/morefoo. Check for errors along the way; abort if any are produced. You may also want to consider employing the use of stat() or lstat() to see if the directory exists prior to doing anything.

Btw, you need to set the execute-bit on the directories you create. 0644 is not appropriate; try 0755, or something more or less restrictive. In fact, allow the user to specify the mode, using a -m option, similar to the real mkdir.

Good luck with your homework!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help in writing a program with C in Linux anuzephyr Programming 5 11-15-2010 06:35 AM
how to give options like '-v' for mkdir using system call mkdir() ? nehapawar Linux - Newbie 2 02-05-2010 02:13 AM
tablature writing program? bruno buys Linux - Software 1 04-01-2006 04:03 PM
Writing a batch program mrobertson Programming 12 02-19-2006 12:55 PM
CD Writing program? Which is best? nro Linux - Software 14 09-01-2004 10:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration