Thursday, February 2, 2012

Program to automate pre program writing process

When writing a program, usually we need to create a folder and create a .C file inside the folder. In the .C file, header files need to be added. This program automates this and opens the .C file in Emacs for writing the program.
 /*usage: ./prog <directory name> <file name w/o extension.c>  
 location: mybin  
 need: to create a directory and program file to write programs. it creates the directory in the default progs folder at a single stroke. this program also writes a default code within the file  
 creats a dir <directory name> and inside it creates a file <filename> no need to add extension .c as it automatically appends it  
  */  
 #include<unistd.h>  
 #include<sys/types.h>  
 #include<sys/stat.h>  
 #include<error.h>  
 #include<errno.h>  
 #include<dirent.h>  
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<wait.h>  
 int main(int argc, char *argv[])  
 {  
  int flag;  
  DIR *dp;  
  FILE *fp;  
  char s[256];  
  pid_t pid;  
  int ret;  
  if((flag=chdir("/home/abishek/progs"))==-1)//default dir for progs in system  
   {  
    error(1,errno,"cannot open the progs folder bub");  
   }  
  if((flag=mkdir(argv[1],S_IRUSR|S_IWUSR|S_IXUSR))==-1)//creates dir with gn name  
   {  
    error(1,errno,"Directory not created bub");  
   }  
  dp=opendir(argv[1]);  
  if((flag=chdir(argv[1]))==-1)  
   {  
    error(1,errno,"Error in changing Dir bub");  
   }  
  sprintf(s,"%s.c",argv[2]);//appends.c to the file name given  
  char *arg[]={"emacs","-nw",s,NULL};  
  fp=fopen(s,"w");  
  fprintf(fp,"/*:) %s :) have fun */\n",argv[2]);//default code written into the program  
  fprintf(fp,"// header files\n");  
  fprintf(fp,"#include<unistd.h>\n#include<stdio.h>\n");  
  fprintf(fp,"//main func\nint main(int argc char, *argv[])\n{\nreturn 0; \n}");  
  if(fclose(fp)==-1)  
   {  
    error(1,errno,"cannot close file bub");  
   }  
  ret=execv("/usr/bin/emacs",arg);  
  if(ret==-1)  
   {  
    error(1,errno,"error opening emacs");  
   }  
 }  

No comments:

Post a Comment