#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <dirent.h>

int main(int argc, char * argv[]) {
    long size; int retVal; DIR * dir;
    char * buf, * ptr, arg [20]="ls -l | grep ";
    if (argc!=2) {
        printf ("Usage: dirFun dirname\n");
        return (-1); }
    dir = opendir(argv[1]);
    if (dir) {
        printf ("Directory exists. Aborting...\n");
    closedir(dir);
    return (-2);  }
    size = pathconf(".", _PC_PATH_MAX);
    if ((buf = (char *)malloc((size_t)size)) != NULL)
        ptr = getcwd(buf, (size_t)size);  
    printf ("Current Working Directory is %s\n", buf); 
    printf ("Creating directory %s inside directory %s\n", 
            argv[1], buf);
    free(buf); 
    retVal = mkdir (argv[1],0755);
    if (retVal==-1) {
        printf("Error Number : %d\n", errno); 
        perror("Error Description");     
        return (-2); }
    strcat (arg, argv[1]);
    system (arg);
    printf ("Removing directory %s\n", argv[1]);
    rmdir (argv[1]);
    return (0); }
    
    
