Thread: Lost in C

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    Lost in C

    Hello, I have a C programming problem I need to do for a class. Unfortunately this class is not a programming class and we are left to learn the basics a C programming on our own. The only programming experience I have is one semester of Java, which doesn't seem to be helping me much for this class. I would appreciate any help even if it is a link to a site that may provide helpful information. The assignment deals with system calls in linux using C programming. I am not even sure how to start this so any information will be a help. Here is the question:

    In this assignment, you are going to write a C program, say bush.c.Your program (bush.c) should behave as follows:
    • At startup, your program should create the backup directory backup under the user’s home directory if it does not already exist. It should then display the prompt “bush: ” (without the quotes) on the screen and wait for user input (command).
    • If the user enters exit, your program should exit.
    • If the user enters an empty line, bush.c should simply display the prompt again (on a new line) and wait for user input.
    • If the user enters one of the file backup commands (bu, rs, dbu), bush.c should do the specified operation by calling the appropriate functions described below, and then should display the prompt “bush: ” (again, without the quotes) on a new line and wait for user input.
    • If the user enters any other command line, bush.c should execute the command within a new process by calling the cmd function described below. After the command has been executed, bush.c should display its prompt on a new line and wait for user input.

    Organize your program using the following functions (you will have to implement these functions using the Linux file system calls as much as necessary). Note that the file name, file, is a full path name and each of the following messages should be printed after the string file is replaced with the actual full path name.

    void bu (char *file)
    If the file to be backed up, file, does not exist, print a message like
    The file to be backed up, file, does not exist.
    and display the prompt for next command. Otherwise, if there is a file named file within the backup directory, print a message like
    The backup already contains a file named file.
    and display the prompt for next command. Otherwise, copy the file to be backedup, file, into the backup directory, backup (under the appropriate subdirectory).

    void rs (char *file)
    If the file to be restored, file, does not exist in the backup directory, print a message like
    The backup does not contain the file, file, to be restored.
    and display the prompt for next command. Otherwise, copy the backup file into its proper location (even if it already exists).

    void dbu (char *file)
    If the backup file to be deleted, file, does not exist within the backup directory, print a message like
    The backup does not contain the file, file, to be deleted.
    and wait for the next command. Otherwise, delete the specified file from the backup directory using the unlink systemcall.

    void cmd (char *cmdln)
    Create a new process and execute the command in cmdln within this process.

    void init
    Initialize the backup by creating the backup directory (backup) under the user’s home directory, if it does not already exist.
    Write your main program appropriately using the above functions.

  2. #2
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    Ok, that's your assignment...but what's your question? What are you having trouble with?

    If you don't know where to start, this might be a good place to begin reading:
    http://www.opengroup.org/onlinepubs/.../unistd.h.html

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    Well first problem is how do i make the prompt say bush? Also thank you for the reply.

  4. #4
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    You mean like this:
    Code:
    string command;
    while(strcmp(command,"exit")!=0)
    {
    printf("bush: ");
    scanf("%d",&command);
    if(strcmp(command,"bs")==0)
    {
    //execute bs code here
    }
    //other commands,etc here
    }
    For that you need to include cstring. That's essentially making your own limited command prompt.
    Last edited by jmd15; 10-31-2005 at 09:42 PM.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, that's wrong. You can't compare strings in C with ==. You have to use something like strcmp to compare strings. Also, you've posted C++ on the C forum.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    Oh thanks for the update I don't need to pull my hair out anymore. I was even more confused then before.

    I think I have that part figured out now after reading some c programming links. Now I am stumped on how to tell if the user enters a blank line:

    If the user enters exit, your program should exit.
    • If the user enters an empty line, bush.c should simply display the prompt again (on a new line) and wait for user input.


    Thanks for all your help.

  7. #7
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Thanks Quzah, I'm sorry. I forget when I switch from the C++ board to the C board. Code now C revised.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    Does the following code make any sense for this part?

    void bu (char *file)
    If the file to be backed up, file, does not exist, print a message like The file to be backed up, file, does not exist.
    and display the prompt for next command. Otherwise, if there is a file named file within the backup directory, print a message like The backup already contains a file named file.
    and display the prompt for next command. Otherwise, copy the file to be backedup, file, into the backup directory, backup (under the appropriate subdirectory).


    Code:
    void bu(char *file)
    {
    char buffer[1024];
    int fd, newfd;
    char *fullpath;
    
    printf("BU called\n");
    printf("Value of file is %s\n",file);
    fd = open(file, O_RDONLY);
    if(fd == -1)
    {
    printf("error, file does not exist\n");
    return; 
    }
    else
    {
    //backup file
    
    //createpath() builds the path for the new file under the backup directory
    fullpath = createpath(file);
    newfd = open(fullpath, O_CREAT);
    }
    
    int inputlength;
    while( (inputlength = read(fd, buffer, 1024)) > 0)
    {
    write(newfd,buffer,inputlength);
    }
    close(fd);
    close(newfd);
    return;
    }

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    For the next part of the problem I basically changed a few things in the bu part but I think it takes more then this.

    void rs (char *file)
    If the file to be restored, file, does not exist in the backup directory, print a message like
    The backup does not contain the file, file, to be restored.
    and display the prompt for next command. Otherwise, copy the backup file into its proper location (even if it already exists).

    Code:
    void rs(char *file)
    {
    char buffer[1024];
    int fd, newfd;
    char *fullpath;
    
    newfd = open(file, O_RDONLY);
    if(newfd == -1)
    {
    printf("error, file does not exist\n");
    return;
    }
    else
    {
    //restore file
    
    
    fullpath = createpath(file);
    fd = open(fullpath, O_CREAT|O_WRONLY);
    }
    
    int inputlength;
    while( (inputlength = read(newfd, buffer, 1024)) > 0)
    {
    write(fd,buffer,inputlength);
    }
    close(fd);
    close(newfd);
    return;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The getting lost and walking in circles
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 05-07-2008, 09:53 AM
  2. I lost my laptop, DVD and money
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-01-2004, 07:13 PM
  3. lost disk
    By Benzakhar in forum Linux Programming
    Replies: 7
    Last Post: 01-11-2004, 06:18 PM
  4. Lost ID number
    By ripper079 in forum C++ Programming
    Replies: 13
    Last Post: 10-04-2002, 12:51 PM
  5. API, LOST... help
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 03-13-2002, 03:19 PM