Thread: unix fold utility

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    8

    unix fold utility

    Hello again! lol. I have to create a fold utility program as our final project (we had 3 choices). I am having trouble getting started on it. I will post a link to the description of what the fold command is supposed to do.

    http://www.conestogac.on.ca/~set/cou..._fold_2008.htm

    This is due on Tuesday btw.

    UPDATE: So far I have this...

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	FILE *inputFile; //pointer to a file//
    
    	if (argv[1] == '-s')
    	{
    	// Fold line after the last blank character within the first width column positions. 
    	// If there is no such character, fold anyway. 
    	}
    
    	if (argv[2] == '-w' && argv[3] == /* width variable here*/)
    	{
    	// Specify a line width to use instead of the default 80 columns.
    	}
    
    	inputFile = fopen(argv[4], "r"); //open a file for reading//
    
    	if (inputFile = fopen(argv[4], "r") == NULL)
    	{
    		printf("Cannot open file for reading.\n"); //display error if file is not found//
    		return -1;
    	}
    	
    	if (inputFile = fopen(argv[4], "r") != NULL)
    	{
    		return -2;
    	}
    
    	//other code here//
    
    	return 0;
    }
    I guess that separate functions would be a good idea to use for each command line argument?
    Last edited by Sgemin_001; 12-06-2008 at 03:12 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Sketch out a rough program and someone from the msg board is sure to help.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    thanks, this board indeed need to keep track of your last assignments
    I may have missed the blog-related section in the FAQ...

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Can I ask if this is your major and you started in September?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't think that either of your command line switches deserve their own function; after all, for -s you should probably just do word_wrap = true and for -w you would just do columns = <whatever>. You do need to not assume that they will come in that order, or that both of them will be given. In other words, you need to loop through each command line argument in turn, and process each. Note also that you cannot assume a space between -w and 75 (or whatever number), so assuming that they are in separate argv's won't work. You also shouldn't try to open your input file three times. And what's with the return -2? (Oh, and for that matter, what's with the return -1? Look at your spec sheet again.)

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And technically, the error message when you can not open the file is incorrect.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    This looks more to be a switch/case assignment with some strlen() usage thrown in. I would review how getopt() works.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I believe this to be a foolproof and completely optimized way of parsing your particular options:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void wrong () {
    	puts("Usage: fold [-s] [-w width] file ...");
    	exit (-2);
    }
    
    int parse_w (char *arg) {
    	int i, retv;
    	if (sscanf(arg,"&#37;d",&retv)!=1) wrong();
    	return retv;	
    }
    
    int main (int argc, char *argv[]) {
    	int i, len, sflag=0, width=80;
    	FILE *fstRO;
    	for (i=1;i<argc;i++) {
    		if (strcmp(argv[i],"-s")==0) {sflag=1;continue;}
    		if (strncmp(argv[i],"-w",2)==0) {
    			if (strlen(argv[i])>2) {
    				if (sscanf(argv[i], "-w%d",&width)!=1) wrong();
    				continue;}
    			else if (i==argc-1) wrong();
    			width=parse_w(argv[i+1]);
    			i++; continue;
    		}
    		break;
    	}
    	if (!(fstRO=fopen(argv[i],"r"))) {
    		puts("File not openable");
    		return -1;}
    	printf("%s opened read-only\n",argv[i]);
    	if (sflag==1) puts("-s set");
    	printf("width set to %d\n",width);
    	fclose(fstRO);
    	return 0;
    }
    edit: except it doesn't parse the filename...oh well
    edit2: okay now it does
    Last edited by MK27; 12-06-2008 at 05:34 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That looks like it should be right for the switches, if not the filenames, of course. I think the usual idiom is a switch on the second letter of the argument; for two possible arguments I don't know that there's a performance hit either way, but the switch may make it a little easier to tell "illegal option" from "first filename".

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Cool

    Quote Originally Posted by tabstop View Post
    That looks like it should be right for the switches, if not the filenames, of course. I think the usual idiom is a switch on the second letter of the argument; for two possible arguments I don't know that there's a performance hit either way, but the switch may make it a little easier to tell "illegal option" from "first filename".
    Our paths crossed, I edited it to get the filename. Re: what you are saying about optimization, so

    Code:
     
    if (argv[i][0]=='-') switch (argv[i][1]) {
                case ('s'):
       //...etc
    I guess this could save some strcmp
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Cripes, why reinvent the wheel?? Use getopt!

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rags_to_riches View Post
    Cripes, why reinvent the wheel?? Use getopt!
    You're making a pretty big assumption, there, that I'm not sure is warranted.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You know what, you're right...I did jump to a big ol' conclusion, didn't I?

    Sorry.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    8
    Quote Originally Posted by MK27 View Post
    Can I ask if this is your major and you started in September?
    I did start in September. I am having some trouble focusing though.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Before handing over the assignment the teacher must have covered how to do option processing. Hard to believe she or he would just pull it out of the hat.
    Last edited by itCbitC; 12-06-2008 at 11:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. which utility in UNIX
    By Ron in forum C Programming
    Replies: 15
    Last Post: 06-19-2008, 08:23 AM
  2. How to program in unix
    By Cpro in forum Linux Programming
    Replies: 21
    Last Post: 02-12-2008, 10:54 AM
  3. Setting up a Unix box
    By @nthony in forum Tech Board
    Replies: 6
    Last Post: 07-22-2007, 10:22 PM
  4. UNIX script
    By boshke in forum Linux Programming
    Replies: 2
    Last Post: 12-10-2001, 12:55 PM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 07:37 AM