views:

47

answers:

1

Hi following code sample seems to have some problem or either it has to do something with OS internals.

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

    static void usage();
    #define MONC_AM_CONF "/tmp/monc.conf"

int main(int argc, char **argv)
{
        int ch;
        char list[200];
        int len;
        int fd;

        memset(list,0,200);
        if (argc < 11) {
                usage();
                exit(1);
        }

        while ((ch = getopt(argc, argv, "N:S:P:H:R:")) != -1) {
                switch (ch) {
                        case 'N':
                                len = strlen(optarg) + 2;
                                sprintf(list,"%s::",optarg);
                                break;
                        case 'S':
                                sprintf(list+len,"%s::",optarg);
                                len = len + strlen(optarg) + 2;
                                break;
                        case 'P':
                                sprintf(list+len,"%s::",optarg);
                                len = len + strlen(optarg) + 2;
                                break;
                        case 'H':
                                sprintf(list+len,"%s::",optarg);
                                len = len + strlen(optarg) + 2;
                                break;
                        case 'R':
                                sprintf(list+len,"%s ",optarg);
                                len = len + strlen(optarg);
                                break;
                        default:
                                printf ("You specified a parameter I don't "
                                                "know about.\n");
                                break;
                }
        }
        argc -= optind;
        argv += optind;

        printf("Total length of string is %d\n",len);
        printf("The string is %s\n",list);

        fd = open(MONC_AM_CONF, O_WRONLY|O_CREAT|O_APPEND, 0644);
        lseek(fd, 0,SEEK_END);
        write(fd,list,len);
        close(fd);

        return 0;
}

static void usage()
{
printf("Please provide the command in correct format\n");
printf("monc_am_config -N <Comp_Name> -S <Start Script Path> -P <Stop Script Path> -H <HealthCheck Script Path> -R <Recovery Policy>\n");

return ;
}

When I am issuing commands I am getting the output file each time different. I am expecting execution of this program each time should write the information in the new line but it is writing into the same line in the file.

Plz help.

+2  A: 

You should append "\n" to list, i.e

list[len++] = '\n';
Calvin1602
and hope that 199 characters is all that will ever be needed
msw
Thanks for quick reply !! It works for me.
Arpit