File Tool
2002-09-29 21:09:01
Category: c:files
Description: Echo, rename, remove, or read a byte from a file in unix.
Author: Luis Araujo
Viewed: 5467
Rating: (5 votes)


/* one.c by Luis Araujo
 *
 * http://www.metalshell.com/
 *
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
 
void eco_func(char *eco_arg);
void new_func(char *name_arg1, char *name_arg2);
void remove_func(char *rem_arg);
void search_func(char *file_arg, char *byte_arg);
void version_func(void);
void help_func(void);     
 
int main(int argc, char *argv[]){
                
    FILE *file;
    char c;
 
    if(argc==4){    
        if(!(strcmp(argv[1], "-n")) || !(strcmp(argv[1], "--name"))){
            new_func(argv[2], argv[3]); exit(0); }
        else if(!(strcmp(argv[1], "-b")) || !(strcmp(argv[1], "--byte"))){
            search_func(argv[2], argv[3]); exit(0);}
        else{ 
            printf("one: invalid option\nTry `one --help`\n"); 
            exit(1);
        }
    }
 
    if(argc==3){
        if(!(strcmp(argv[1], "-e")) || !(strcmp(argv[1], "--echo"))){ 
            eco_func(argv[2]); exit(0); }
        else if(!(strcmp(argv[1], "-r")) || !(strcmp(argv[1], "--remove"))){
            remove_func(argv[2]); exit(0); }
        else{
            printf("one: invalid option\nTry `one --help`\n"); 
            exit(1); 
        }
    }
 
    if(argc==2){
        if(!(strcmp(argv[1], "-v")) || !(strcmp(argv[1], "--version"))){
            version_func(); exit(0); }
        else if(!(strcmp(argv[1], "-h")) || !(strcmp(argv[1], "--help"))){
            help_func(); exit(0); }
        else if(strchr(argv[1], '-')){ 
            printf("one: too few arguments\nTry `one --help`\n"); 
            exit(1);
            }
        }
    else{     
        printf("one: too few arguments\n");
        printf("Try 'one --help'.\n");
        exit(1);
    }
 
    system("/usr/bin/clear");
 
    if((file=fopen(argv[1], "r+"))==NULL)
        if((file=fopen(argv[1], "w+"))==NULL){
            printf("one : it cant create file\n");
            exit (1);
        }
 
        c=getc(file);
        while(!feof(file)){
            putchar(c);
            c=fgetc(file);
        }
            
        c=getchar();
        while(c!='$'){
            fputc(c, file);
            if(ferror(file)){
                perror("one: it cant create file\n");
                exit (1);
            }
            c=getchar();
        }
 
    fclose(file);
 
    return 0;
}
        
 
 
void eco_func(char *eco_arg){
 
    FILE *eco_file;
    char u;
                
    if((eco_file=fopen(eco_arg, "r+"))==NULL){
        printf("one: it cant create file\n");
        exit(1);
    }
                    
    u=getc(eco_file);
 
    while(!feof(eco_file)){
 
        putchar(u);
        u=getc(eco_file);
 
        }
        fclose(eco_file);
 
}
 
 
void new_func(char *name_arg1, char *name_arg2){
 
    if(rename(name_arg1, name_arg2) != 0) 
        printf("one: file cant be renamed\n");
}
 
 
void remove_func(char *rem_arg){
 
    char t;
 
    printf("remove %s (Y/N):", rem_arg);
    t=getchar();
    if(toupper(t)=='Y'){    
        if(remove(rem_arg))
            printf("one: file doesnt exist\n");
        else printf("one: file deleted\n");
    } else printf("one: file not deleted\n");
}    
 
    
void search_func(char *file_arg, char *byte_arg){
 
    FILE *search_file;
 
    if((search_file=fopen(file_arg, "r"))==NULL){
        printf("one: it cant open file\n");
        exit(1);
    }
 
    if(fseek(search_file, atol(byte_arg), SEEK_SET)){
        printf("one: error\n");
        exit(1);
    }
 
    printf("one: byte %ld got %c\n", atol(byte_arg), getc(search_file));
    fclose(search_file);            
 
}
 
 
void help_func(void){
 
    printf("use: one [options] <file>");
    printf("\n\t\tone [options] <file> <new_name>...CHANGE NAME");
    printf("\n\t\tone [options] <file> <num_byte>...GET BYTE");
    printf("\n\n\t-e\t--echo\t\tEcho the file");
    printf("\n\t-n\t--name\t\tRename the file");
    printf("\n\t-r\t--remove\tRemove the file");
    printf("\n\t-b\t--byte\t\tGet info about the byte specified");
    printf("\n\t-v\t--version\tProgram version");
    printf("\n\t-h\t--help\t\tThis help message");
    printf("\n\nOne (on3), pseudo-editor Unix.\n");
    printf("Running the program without options\n");
    printf("will create file.\n"); 
    printf("Use '$' to save and close the file.\n");
    printf("\nBugs `bugs@solk2.cjb.net`\n");
 
}
 
 
void version_func(void){
 
    printf("one version 0.0.1z");
    printf("\nLuis Araujo.\n");
    printf("\nCopyright (C) 2002. Free Software.\n");
 
}