Wierd Segmentation Faults on Global Variable

This is a discussion on Wierd Segmentation Faults on Global Variable within the C Programming forums, part of the General Programming Boards category; Hello I'm creating a little x-windows e-mail util. I have my widget defined globally (ie above all code) When I ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    6

    Wierd Segmentation Faults on Global Variable

    Hello

    I'm creating a little x-windows e-mail util.
    I have my widget defined globally (ie above all code)

    When I login to the servers from home on my machine my program works fine, but when I try it in computer at the lab, I get segmentation faults as soon as a try and use the global variable(s). I'll post some code for example purposes

    ************************************************** ***********************
    Widget toplevel = NULL; (or Widget toplevel

    main(argc, argv)
    char *argv[];
    {
    toplevel = XtVaAppInitialize(&app, "Demos", NULL, 0, &argc, argv, NULL,

    I get a seg fault as soon as that last line runs. Theres a lot more code but that's the meaty part that screws up.

    Any ideas . . I think it might have to do with defining them in a header file. HELP!?!

    Carmen

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    1. Use code tags.
    2. Post code that will compile.
    3. Use something like ANSI-C, that style of C is about 20 years old.

    State operating systems and compilers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    6

    Code style

    No ........, this is the bull........ you have to put with at Ryerson Computer Science. I'm just trying to work with the code i was given. I"ll check out the tags and throw up my code in one sec . . .

    Carmen

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    6

    my code . . .

    Code:
    #include <X11/Intrinsic.h>
    #include <X11/StringDefs.h>
    #include <stdio.h>
    #include <Xm/List.h>
    #include <Xm/LabelG.h>
    #include <Xm/Label.h>
    #include <Xm/RowColumn.h>
    #include <Xm/PanedW.h>
    #include <Xm/TextF.h>
    #include <Xm/PushB.h>
    #include <Xm/BulletinB.h>
    #include <X11/Shell.h>
    #include <string.h>
    #include <stddef.h>
    #include <sys/types.h>
    #include <dirent.h>
    
    #define TRUE 1
    #define FALSE 0
    
    //Widget toplevel;
    XmStringCharSet charset = XmSTRING_DEFAULT_CHARSET;
    
    XmString lastItemSelected;
    XmString      label;
    Pixel pixel1, pixel2, pixel3;
    XColor alloc_color;
    Colormap cmap;
    Display *display;    
    Widget toplevel;
    Widget  rowcol;
    Widget rowcol2;
    Widget message;
    Widget to;
    Widget cc;
    Widget subject;
    Widget send;
    Widget receive;
    Widget help;
    Widget quit;
    Widget sendMessage;
    Widget compose_shell;
    Widget compose;
    Widget receive_shell;
    Widget folder_shell;
    Widget receiveList;
    Widget folder;
    Widget folderList;
    Widget deleteMessageButton;
    Widget replyMessageButton;
    Widget folders;
    Arg args[10];
    unsigned short red, green, blue;
    int position;
    char *messageVal;
    char *toVal;
    char *ccVal;
    char *subjectVal;
    char *lastMessage;
    char *getlogin();
    char *mailLocation;
    char *username;
    char location[999999];
    
    
    void add_item();
    void search_item();
    void drawComposeWin();
    void drawReceiveWin(); 
    void drawFoldersWin(); 
    void drawHelpWin(); 
    void sendMsg(); 
    void messageSelected();
    void deleteMessage(); 
    void button_help();
    
    
    
    
    
    void replyMessage(w, client_data, call_data)
    Widget w;
    caddr_t client_data;
    XmAnyCallbackStruct *call_data;
    {
        char *replyTo;
        char *tempItem;
        
        XtPopdown(receive_shell);
        receive_shell = NULL;
        drawComposeWin();
        
        XmStringGetLtoR(lastItemSelected, XmSTRING_DEFAULT_CHARSET, &tempItem);
        XmTextFieldSetString(to, tempItem);
    
    
    
        
    }
    void deleteMessage(w, client_data, call_data)
    Widget w;
    caddr_t client_data;
    XmAnyCallbackStruct *call_data;
    {
        
        
        int foundMessage = FALSE;
        int atEndofMessage = FALSE;
        int onDeleteMessage = FALSE;
        FILE *fp;
        char *temp;
        char line[1000000];
        char newMailFile[1000000];
        int line_length = 9999;
        XmString messages[999];
        int numOfMessages = 0;
        XmString tempXmString[999];
        int i;
        int num=0;
    
        strcpy(line, "");
        strcpy(temp, "");
        
        
        fp = fopen(location, "r");
        while(fgets(temp, 99999999, fp) != NULL ){
            if(temp[0] == 'F' && temp[1] == 'r' && temp[2] == 'o' && temp[3] == 'm' && temp[4] != ':' && temp[5] != 'M' && temp[6] != 'A' && temp[7] != 'I' && temp[8] != 'L' && temp[9] != 'E' && temp[10] != 'R'){
                numOfMessages++;    
                        
            }
            if(numOfMessages != position){
                strcat(newMailFile, temp);
            }    
        }
        
        fclose(fp);
        
        
        fp = fopen(location, "w");
        
        XmListDeleteAllItems(receiveList);
        fclose(fp);
        fp = fopen(location, "r");
        while( fgets(temp, 99999999, fp) != NULL ){
            if(temp[0] == 'F' && temp[1] == 'r' && temp[2] == 'o' && temp[3] == 'm' && temp[4] != ':' && temp[5] != 'M' && temp[6] != 'A' && temp[7] != 'I' && temp[8] != 'L' && temp[9] != 'E' && temp[10] != 'R'){
                    if(atEndofMessage == FALSE && foundMessage == FALSE){
                        num++;
                        numOfMessages++;
                        foundMessage = TRUE;    
                        atEndofMessage = FALSE;    
                        for(i=5; temp[i] != ' '; i++){
                            temp[-5+i] = temp[i];
                            
                        }
                        temp[i-5] = '\0';
                        
                        
                        tempXmString[0] = XmStringCreateSimple(temp);
                        XmListAddItems(receiveList, tempXmString, 1, numOfMessages - 1);
                        
                    }
                    else if(foundMessage == TRUE){
                        atEndofMessage = TRUE;
                        for(i=5; temp[i] != ' '; i++){
                            temp[-5+i] = temp[i];
                            
                        }
                        temp[i-5] = '\0';
                        
                        tempXmString[0] = XmStringCreateSimple(temp);
                        XmListAddItems(receiveList, tempXmString, 1, numOfMessages - 1);
                    }
            }
            
            if(foundMessage == TRUE && atEndofMessage == FALSE){
                strcat(line, temp);
                
            }
            if(atEndofMessage == TRUE){
                strcat(line, "\n------------------------------------------------------------------\n");
                strcpy(line, "");
                atEndofMessage = FALSE;
                strcat(line, temp);
            }
            
        }
        XmTextSetString(message, "");
        fclose(fp);
        
    }
    
    void messageSelected(w, client_data, call_data)
    Widget w;
    caddr_t client_data;
    XmListCallbackStruct *call_data;
    {
        int foundMessage = FALSE;
        int atEndofMessage = FALSE;
        FILE *fp;
        char temp[100];
        char line[1000000];
        int line_length = 9999;
        XmString messages[999];
        int numOfMessages = 0;
        int num = 0;
        XmString tempXmString[999];
        position = call_data->item_position;
        lastItemSelected = call_data->item;
        
        
        
        strcpy(line, "");
        strcpy(temp, "");
        
    
        fp = fopen(location, "r");
        while(fgets(temp, 99999999, fp) != NULL ){
            if(temp[0] == 'F' && temp[1] == 'r' && temp[2] == 'o' && temp[3] == 'm' && temp[4] != ':' && temp[5] != 'M' && temp[6] != 'A' && temp[7] != 'I' && temp[8] != 'L' && temp[9] != 'E' && temp[10] != 'R'){
                                    
                atEndofMessage = TRUE;
                foundMessage = FALSE;
    
            }
            if(temp[0] == 'X' && temp[1] == '-' && temp[2] == 'U' && temp[3] == 'I'){
                fgets(temp, 99999, fp);
                if(temp[0] == '\n'){
    
                    num++;
                    numOfMessages++;                                    
                    foundMessage = TRUE;
                    atEndofMessage = FALSE;
                    strcpy(line, "");                    
                }
        
            }
            if(temp[0] == 'X' && temp[1] == '-' && temp[2] == 'O' && temp[3] == 'r' && temp[4] == 'c'){
                fgets(temp, 99999, fp);
                if(temp[0] == '\n'){    
                    num++;
                    numOfMessages++;
                                        
                    foundMessage = TRUE;
                    atEndofMessage = FALSE;
                    strcpy(line, "");                    
                }
            }        
            if(atEndofMessage == FALSE && foundMessage == TRUE){
                
                if((position) == numOfMessages){
    
                    XmTextSetString(message, "");
                    XmTextSetString(message, line);
                    atEndofMessage = FALSE;
    
                }
                
            }
            if(foundMessage == TRUE && atEndofMessage == FALSE){
                strcat(line, temp);
    
                
            }
            
        }
        lastMessage = XmTextGetString(message);
        
        fclose(fp);
    
        
    ;
    }
    void folderSelected(w, client_data, call_data)
    Widget w;
    caddr_t client_data;
    XmListCallbackStruct *call_data;
    {
        int foundMessage = FALSE;
        int atEndofMessage = FALSE;
        FILE *fp;
        char temp[100];
        char line[1000000];
        char *tempDirectory;
        char *directory;
        char *home;
        int line_length = 9999;
        XmString messages[999];
        int numOfMessages = 0;
        XmString tempXmString[999];
        
        
        home = getenv("HOME");
        XmStringGetLtoR(call_data->item, XmSTRING_DEFAULT_CHARSET, &tempDirectory);
    
    
            XtFree(directory);
        XtFree(tempDirectory);
    
        
    ;
    }
    void button_folders(w, client_data, call_data)
    Widget w;
    caddr_t client_data;
    XmAnyCallbackStruct *call_data;
    {
        drawFoldersWin();
    }
     void button_receive(w, client_data, call_data)
    Widget w;
    caddr_t client_data;
    XmAnyCallbackStruct *call_data;
    {
        drawReceiveWin();
    }
    void button_send(w, client_data, call_data)
    Widget w;
    caddr_t client_data;
    XmAnyCallbackStruct *call_data;
    {
        drawComposeWin();
    }
    void button_help(w, client_data, call_data)
    Widget w;
    caddr_t client_data;
    XmAnyCallbackStruct *call_data;
    {
        drawHelpWin();
    } 
    void button_quit(w, client_data, call_data)
    Widget w;
    caddr_t client_data;
    XmAnyCallbackStruct *call_data;
    {
        exit(1);
    }
    void sendMsg(w, client_data, call_data)
    Widget w;
    caddr_t client_data;
    XmAnyCallbackStruct *call_data;
    {
        FILE *fp;
        int pid, pipefds[2];
        char *username, *getlogin();
        
        XtVaGetValues(message, XmNvalue, &messageVal, NULL); 
        XtVaGetValues(to, XmNvalue, &toVal, NULL);
        XtVaGetValues(cc, XmNvalue, &ccVal, NULL);
        XtVaGetValues(subject, XmNvalue, &subjectVal, NULL);
        
        
        if (pipe(pipefds) < 0) {
        perror("pipe");
        exit(1);
        }
        
        if ((pid = fork()) < 0) {
        perror("fork");
        exit(1);
        }
        
        if (pid == 0) {
        close(0);
        dup(pipefds[0]);
        close(pipefds[0]);
        close(pipefds[1]);
        execl("/bin/mail", "mail", "-s", subjectVal, toVal, 0);
        perror("exec");
        
        }
        
        close(pipefds[0]);
        
        
        fp = fdopen(pipefds[1], "w");
        
        fprintf(fp, "%s", messageVal);
        fclose(fp);
        
           while (wait((int *) 0) != pid);
        
        XtVaSetValues(message, XmNvalue, "", NULL); 
        XtVaSetValues(to, XmNvalue, "", NULL);
        XtVaSetValues(cc, XmNvalue, "", NULL);
        XtVaSetValues(subject, XmNvalue, "", NULL);
    
    }
    void drawHelpWin(){
        int foundMessage = FALSE;
        int atEndofMessage = FALSE;
        FILE *fp;
        char temp[100];
        char helpFile[1000000];
        int line_length = 9999;
        XmString messages[999];
        int numOfMessages;
        XmString tempXmString[999];
        int i;
        char home[99999];
             
        XtVaGetValues(toplevel, XtNcolormap, &cmap, NULL); 
        compose_shell = XtCreatePopupShell("Compose Message Window", transientShellWidgetClass, toplevel, NULL, 0);
        compose = XtCreateManagedWidget("compose", xmRowColumnWidgetClass, compose_shell, NULL, 0);
        XtVaSetValues(compose, XmNbackground, pixel1, NULL);
    
        rowcol = XtVaCreateWidget("rowcol", xmBulletinBoardWidgetClass, compose,  XmNbackground, pixel1, NULL);
    
    
         
       
        label = XmStringCreateSimple("Help:");
        XtVaCreateManagedWidget("message_label", xmLabelWidgetClass, rowcol,
            XmNx, 0,
            XmNy, 10,        
            XmNlabelString,  label,
            NULL);
        
        XmStringFree(label);
        XtSetArg(args[0], XmNrows,      50);
        XtSetArg(args[1], XmNcolumns,  80);
        XtSetArg(args[2], XmNeditable,  True);
        XtSetArg(args[3], XmNeditMode,  XmMULTI_LINE_EDIT);
        XtSetArg(args[4], XmNx, 0);
        XtSetArg(args[5], XmNy, 30);
        
        message = XmCreateScrolledText(rowcol, "message", args, (Cardinal *)6); 
        XtManageChild(message);
        
        XtVaSetValues(compose_shell, 
            XmNx, 100, 
            XmNy, 100,
            XmNwidth, 600, 
            XmNheight, 700,
            XmNbackground, pixel1,
            NULL);
            
        fp = fopen("./help", "r");
        while( fgets(temp, 99999999, fp) != NULL ){
            strcat(helpFile, temp);
            
        }
        XmTextSetString(message, helpFile);
        XtPopup(compose_shell, XtGrabExclusive);
            XtManageChild(rowcol);
            XtRealizeWidget(compose_shell);
    }
    void drawComposeWin(){
          
        XtVaGetValues(toplevel, XtNcolormap, &cmap, NULL); 
        compose_shell = XtCreatePopupShell("Compose Message Window", transientShellWidgetClass, toplevel, NULL, 0);
        compose = XtCreateManagedWidget("compose", xmRowColumnWidgetClass, compose_shell, NULL, 0);
        XtVaSetValues(compose, XmNbackground, pixel1, NULL);
    
        rowcol = XtVaCreateWidget("rowcol", xmBulletinBoardWidgetClass, compose,  XmNbackground, pixel1, NULL);
    
        label = XmStringCreateSimple("To:");
         XtVaCreateManagedWidget(
            "to", 
            xmLabelWidgetClass, rowcol,
            XmNlabelString,  label,
            XmNx, 0,
            XmNy, 0,
            NULL);
            
        XmStringFree(label);
            
        to = XtVaCreateManagedWidget("to",
            xmTextFieldWidgetClass, rowcol,
            XmNx, 0,
            XmNy, 30,
            NULL);
        
    
        label = XmStringCreateSimple("CC:");
        XtVaCreateManagedWidget("search_label", xmLabelWidgetClass, rowcol,
            XmNlabelString,  label,
            XmNx, 0,
            XmNy, 60,
            NULL);
        XmStringFree(label);
        
        cc = XtVaCreateManagedWidget("cc",
            xmTextFieldWidgetClass, rowcol,
            XmNcolumns,     25,
          XmNx, 0,
        XmNy, 80,
            NULL);
     
       
        label = XmStringCreateSimple("Subject:");
        XtVaCreateManagedWidget("search_label", xmLabelWidgetClass, rowcol,
            XmNlabelString,  label,
          XmNx, 0,
        XmNy, 110,            
            NULL);
        XmStringFree(label);
        subject = XtVaCreateManagedWidget("subject",
            xmTextFieldWidgetClass, rowcol,
          XmNx, 0,
        XmNy, 130,        
        XmNcolumns,     25,
            NULL);
      
         
       
        label = XmStringCreateSimple("Message:");
        XtVaCreateManagedWidget("message_label", xmLabelWidgetClass, rowcol,
            XmNx, 0,
            XmNy, 160,        
            XmNlabelString,  label,
            NULL);
        
        XmStringFree(label);
        XtSetArg(args[0], XmNrows,      30);
        XtSetArg(args[1], XmNcolumns,   80);
        XtSetArg(args[2], XmNeditable,  True);
        XtSetArg(args[3], XmNeditMode,  XmMULTI_LINE_EDIT);
        XtSetArg(args[4], XmNx, 0);
        XtSetArg(args[5], XmNy, 180);
        
        message = XmCreateScrolledText(rowcol, "message", args, (Cardinal *)6); 
        XtManageChild(message);
        
        
        XtSetArg(args[0], XmNwidth,      50);
        XtSetArg(args[1], XmNheight,      30);
        XtSetArg(args[2], XmNx,      200);
        XtSetArg(args[3], XmNy,      30);
           
        sendMessage = XtCreateManagedWidget("Send", xmPushButtonWidgetClass, rowcol, args, (Cardinal *)4);
        XtAddCallback(sendMessage, XmNactivateCallback, sendMsg, NULL);
        
        
        XtVaSetValues(compose_shell, 
            XmNx, 100, 
            XmNy, 100,
            XmNwidth, 600, 
            XmNheight, 700,
            XmNbackground, pixel1,
            NULL);
            
        XtPopup(compose_shell, XtGrabExclusive);
            XtManageChild(rowcol);
            XtRealizeWidget(compose_shell);
    }
    void drawReceiveWin(){
        
        int foundMessage = FALSE;
        int atEndofMessage = FALSE;
        FILE *fp;
        char temp[10000];
        char line[1000000];
        int line_length = 9999;
        XmString messages[999];
        int numOfMessages;
        XmString tempXmString[999];
        int i;
        int num=0;
        
        XtVaGetValues(toplevel, XtNcolormap, &cmap, NULL); 
         
            receive_shell = XtCreatePopupShell("Receive Mail Window", transientShellWidgetClass, toplevel, NULL, 0);
    
            receive = XtCreateManagedWidget("compose"
            , xmRowColumnWidgetClass, receive_shell, 
            NULL, 0);
            
            XtVaSetValues(receive, XmNbackground, pixel2, NULL);
        
            rowcol = XtVaCreateWidget("rowcol",
                xmBulletinBoardWidgetClass, receive, 
                XmNbackground, pixel2,
                NULL);
           
           label = XmStringCreateSimple("Messages: ");
              XtVaCreateManagedWidget("messagesLabel", xmLabelWidgetClass, rowcol,
            XmNlabelString,  label,
            XmNx, 0,
            XmNy, 0,
            NULL);
         XmStringFree(label);
             label = XmStringCreateSimple("Message Text: ");
            XtVaCreateManagedWidget("message_label", xmLabelWidgetClass, rowcol,
            XmNx, 220,
            XmNy, 0,        
            XmNlabelString,  label,
            NULL);
        XmStringFree(label);
        
        XtSetArg(args[0], XmNrows,      30);
        XtSetArg(args[1], XmNcolumns,   80);
        XtSetArg(args[2], XmNeditable,  True);
        XtSetArg(args[3], XmNeditMode,  XmMULTI_LINE_EDIT);
        XtSetArg(args[4], XmNx, 220);
        XtSetArg(args[5], XmNy, 50);
        XtSetArg(args[6], XmNwidth, 300);
    
    
        message = XmCreateScrolledText(rowcol, "message", args, 7); 
        XtManageChild(message);
        
          receiveList = XtVaCreateManagedWidget("receiveList",
            xmListWidgetClass, rowcol,
        XmNx, 0,
        XmNy, 50,
        XmNwidth, 200, 
             XmNheight, 500,
        XmNlistSizePolicy, XmCONSTANT,
        XmNselectionPolicy, XmEXTENDED_SELECT,
    
        NULL);
        XtAddCallback(receiveList, XmNextendedSelectionCallback, messageSelected, NULL);
        XtAddCallback(receiveList, XmNdefaultActionCallback, messageSelected, NULL);
        
     
        
        fp = fopen(location, "r");
    
        while( fgets(temp, 99999999, fp) != NULL ){
            
            if(temp[0] == 'F' && temp[1] == 'r' && temp[2] == 'o' && temp[3] == 'm' && temp[4] != ':' && temp[5] != 'M' && temp[6] != 'A' && temp[7] != 'I' && temp[8] != 'L' && temp[9] != 'E' && temp[10] != 'R'){
                                         
                    if(atEndofMessage == FALSE && foundMessage == FALSE){
                        num++;
                        numOfMessages++;
                        foundMessage = TRUE;    
                        atEndofMessage = FALSE;    
                        for(i=5; temp[i] != ' '; i++){
                            temp[-5+i] = temp[i];
                            
                        }
                        temp[i-5] = '\0';
                        tempXmString[0] = XmStringCreateSimple(temp);
                        XmListAddItems(receiveList, tempXmString, 1, numOfMessages - 1);
    
                    }
                    else if(foundMessage == TRUE){
                        atEndofMessage = TRUE;
                        for(i=5; temp[i] != ' '; i++){
                            temp[-5+i] = temp[i];
                            
                        }
                        temp[i-5] = '\0';
                        
                        tempXmString[0] = XmStringCreateSimple(temp);
                        XmListAddItems(receiveList, tempXmString, 1, numOfMessages - 1);
                                    
                    }
            }
            
            if(foundMessage == TRUE && atEndofMessage == FALSE){
                strcat(line, temp);
                
            }
            if(atEndofMessage == TRUE){
                
                strcat(line, "\n------------------------------------------------------------------\n");
    
                strcpy(line, "");
                atEndofMessage = FALSE;
                strcat(line, temp);
                                
            }
    
            
        }
      
        fclose(fp);
    
        XtSetArg(args[0], XmNwidth,      50);
        XtSetArg(args[1], XmNheight,      30);
        XtSetArg(args[2], XmNx,      525);
        XtSetArg(args[3], XmNy,      50);
                
        deleteMessageButton = XtCreateManagedWidget("Delete", xmPushButtonWidgetClass, rowcol, args, (Cardinal *)4);
        XtAddCallback(deleteMessageButton, XmNactivateCallback, deleteMessage, NULL);
          
        XtSetArg(args[0], XmNwidth,      50);
        XtSetArg(args[1], XmNheight,      30);
        XtSetArg(args[2], XmNx,      525);
        XtSetArg(args[3], XmNy,      90);
    
        replyMessageButton = XtCreateManagedWidget("Reply", xmPushButtonWidgetClass, rowcol, args, (Cardinal *)4);
        XtAddCallback(replyMessageButton, XmNactivateCallback, replyMessage, NULL);
        XtVaSetValues(receive_shell, 
            XmNx, 100, 
            XmNy, 100,
            XmNwidth, 600, 
            XmNheight, 700,
            NULL);
        XtPopup(receive_shell, XtGrabExclusive);
        XtManageChild(rowcol);
        XtRealizeWidget(receive_shell);
    }
    void drawFoldersWin(){
        
        int foundMessage = FALSE;
        int atEndofMessage = FALSE;
        char temp[100];
        char line[10000];
        int line_length = 9999;
        XmString messages[999];
        int numOfMessages;
        XmString tempXmString[999];
        int i;
        char home[99999];    DIR *dp;
        struct dirent *ep;
         
        strcpy(home, getenv("HOME"));
        
    
    
            strcat(home, "/mail");
        dp = opendir (home);
        
      
        XtVaGetValues(toplevel, XtNcolormap, &cmap, NULL); 
    
            folder_shell = XtCreatePopupShell("Receive Mail Window", transientShellWidgetClass, toplevel, NULL, 0);
        XtVaSetValues(folder_shell, 
            XmNx, 100, 
            XmNy, 100,
            XmNwidth, 750, 
            XmNheight, 700,
            XmNbackground, pixel3,
            NULL);
            
        folder = XtCreateManagedWidget("folder"
            , xmRowColumnWidgetClass, folder_shell, 
            NULL, 0);
                
            XtVaSetValues(folder, XmNbackground, pixel3, NULL);
        
            rowcol = XtVaCreateWidget("rowcol",
                xmBulletinBoardWidgetClass, folder, 
                XmNbackground, pixel3,
                NULL);
           
           label = XmStringCreateSimple("Folders: ");
              XtVaCreateManagedWidget("foldersLabel", xmLabelWidgetClass, rowcol,
            XmNlabelString,  label,
            XmNx, 0,
            XmNy, 0,    
            NULL);
         XmStringFree(label);
         
        label = XmStringCreateSimple("Message Text: ");
            XtVaCreateManagedWidget("message_label", xmLabelWidgetClass, rowcol,
            XmNx, 440,
            XmNy, 0,        
            XmNlabelString,  label,
            NULL);
        XmStringFree(label);
         
        XtSetArg(args[0], XmNrows,      30);
        XtSetArg(args[1], XmNcolumns,   40);
        XtSetArg(args[2], XmNeditable,  True);
        XtSetArg(args[3], XmNeditMode,  XmMULTI_LINE_EDIT);
        XtSetArg(args[4], XmNx, 440);
        XtSetArg(args[5], XmNy, 50);
        
        message = XmCreateScrolledText(rowcol, "message", args, (Cardinal *)6); 
        XtManageChild(message);
    
          folderList = XtVaCreateManagedWidget("folderList",
            xmListWidgetClass, rowcol,
        XmNx, 0,
        XmNy, 50,
        XmNwidth, 200, 
             XmNheight, 500,
        XmNlistSizePolicy, XmCONSTANT,
        XmNselectionPolicy, XmEXTENDED_SELECT,
    
        NULL);
        XtAddCallback(folderList, XmNextendedSelectionCallback, folderSelected, NULL);
        XtAddCallback(folderList, XmNdefaultActionCallback, folderSelected, NULL);
        
        if (dp != NULL)
        {
            while (ep = readdir (dp)){
                tempXmString[0] = XmStringCreateSimple(ep->d_name);
                XmListAddItems(folderList, tempXmString, 1, numOfMessages - 1);
            }
            (void) closedir (dp);
        }
        else{
            perror ("Couldn't open the directory");
        }
        
        
           receiveList = XtVaCreateManagedWidget("receiveList",
            xmListWidgetClass, rowcol,
        XmNx, 225,
        XmNy, 50,
        XmNwidth, 200, 
             XmNheight, 500,
        XmNlistSizePolicy, XmCONSTANT,
        XmNselectionPolicy, XmEXTENDED_SELECT,
    
        NULL);
        XtAddCallback(receiveList, XmNextendedSelectionCallback, messageSelected, NULL);
        XtAddCallback(receiveList, XmNdefaultActionCallback, messageSelected, NULL);
        
    
            
        
        XtPopup(folder_shell, XtGrabExclusive);
        XtManageChild(rowcol);
        XtRealizeWidget(folder_shell);
        
     
    }
    main(argc, argv)
    char *argv[];
    {
        XtAppContext  app;
        Widget toplevel;
        
    
       if ((username = getlogin()) == NULL) {  
            fprintf(stderr, "Who are you?\n");
            exit(1);
        }
    
        red = 0;
        blue = 65535;
        green = 0;
    
        toplevel = XtVaAppInitialize(&app, "Demos", NULL, 0, &argc, argv, NULL, NULL);
    printf("........");
    exit(1);
        rowcol2 = XtVaCreateManagedWidget("rowcol2", xmBulletinBoardWidgetClass, toplevel, XmNbackground, pixel1,NULL);
        
       
        XtSetArg(args[0], XmNwidth,      50);
        XtSetArg(args[1], XmNheight,      30);
        XtSetArg(args[2], XmNx,      20);
        XtSetArg(args[3], XmNy,      30);
        send = XtCreateManagedWidget("Compose", xmPushButtonWidgetClass, rowcol2, args, 4);
        XtAddCallback(send, XmNactivateCallback, button_send, NULL);
        
        XtSetArg(args[0], XmNwidth,      50);
        XtSetArg(args[1], XmNheight,      30);
        XtSetArg(args[2], XmNx,      80);
        XtSetArg(args[3], XmNy,      30);
        receive = XtCreateManagedWidget("Receive", xmPushButtonWidgetClass, rowcol2, args, 4);
        XtAddCallback(receive, XmNactivateCallback, button_receive, NULL);
    
        XtSetArg(args[0], XmNwidth,      50);
        XtSetArg(args[1], XmNheight,      30);
        XtSetArg(args[2], XmNx,      140);
        XtSetArg(args[3], XmNy,      30);
        folders = XtCreateManagedWidget("Folders", xmPushButtonWidgetClass, rowcol2, args, 4);
        XtAddCallback(folders, XmNactivateCallback, button_folders, NULL);    
    
        XtSetArg(args[0], XmNwidth,      50);
        XtSetArg(args[1], XmNheight,      30);
        XtSetArg(args[2], XmNx,      200);
        XtSetArg(args[3], XmNy,      30);
        help = XtCreateManagedWidget("Help", xmPushButtonWidgetClass, rowcol2, args, 4);
        XtAddCallback(help, XmNactivateCallback, button_help, NULL); 
        
        XtSetArg(args[0], XmNwidth,      50);
        XtSetArg(args[1], XmNheight,      30);
        XtSetArg(args[2], XmNx,      260);
        XtSetArg(args[3], XmNy,      30);
        quit = XtCreateManagedWidget("Quit", xmPushButtonWidgetClass, rowcol2, args, 4);
        XtAddCallback(quit, XmNactivateCallback, button_quit, NULL); 
        
        display = XtDisplay(toplevel);
        XtVaGetValues(toplevel, XtNcolormap, &cmap, NULL);
        
        alloc_color.red = red;
        alloc_color.blue = blue;
        alloc_color.green = green;
        alloc_color.flags = DoRed|DoGreen|DoBlue;
        
        XAllocColor(display, cmap, &alloc_color);
        
        pixel1 = alloc_color.pixel;
        
        red = 65535;
        blue = 0;
        green = 0;
        
        alloc_color.red = red;
        alloc_color.blue = blue;
        alloc_color.green = green;
        alloc_color.flags = DoRed|DoGreen|DoBlue;
        
        XAllocColor(display, cmap, &alloc_color);
        pixel2 = alloc_color.pixel;
        
        red = 0;
        blue = 0;
        green = 65535;
        
        alloc_color.red = red;
        alloc_color.blue = blue;
        alloc_color.green = green;
        alloc_color.flags = DoRed|DoGreen|DoBlue;
        
        XAllocColor(display, cmap, &alloc_color);
        pixel3 = alloc_color.pixel;
        
        /*
         * Get the user's name.
         */
        if ((username = getlogin()) == NULL) {  
            fprintf(stderr, "Who are you?\n");
            exit(1);
        }
    
        
        
        
        strcpy(location, "/usr/mail/");
        strcat(location, username);
        
        
        XtRealizeWidget(toplevel);
        XtAppMainLoop(app);
        
    }

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    6
    I'm using a Linux box at school labs, i'm not positive on the details of the compilation. Problem is this code works perfect when I log in from home, but not when I'm in the labs . . . .?
    Carmen

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    6

    Omfg

    I have not clue why, but I changed the Widget send . . .to Widget send_widget and now everything works fine.

    Maybe I my C skills suck but it sure makes me angry sometimes

    Carmen

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    char line[1000000];
    char newMailFile[1000000];

    Try not using such stupidly large arrays for reading lines from a text file.
    I mean, you might try changing the default stack size a process can have (check the ulimit command), but that's only the start of your problems.

    char line [BUFSIZ];
    ought to be good enough for reading lines from any mail file you're likely to come across.

    > Problem is this code works perfect when I log in from home
    Yeah, nothing to do with your coding, purely down to luck.

    Here's some more horror.
    > strcpy(temp, "");
    temp isn't pointing anywhere, you just blasted a hole in someone elses memory.

    > while(fgets(temp, 99999999, fp) != NULL ){
    temp still isn't pointing anywhere, and you've blasted away even more memory.

    > if(temp[0] == 'F' && temp[1] == 'r' && te
    Try using strncmp(), it will make this line much shorter.

    > strcat(newMailFile, temp);
    This isn't initialised either, so god-only knows where you're appending that to. Not the place you want that's for sure.
    Besides, a big buffer like that is a poor way of storing all the lines of a text file.
    Consider something like a linked list.

    I'm sure there's plenty of other things wrong, but I've got other stuff to do than diagnose all the bugs in that code.

    Personally, I think you need to learn C from someone who has a clue, because it's clear from that code that whovever you're learning from hasn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scope of global variable vs. static
    By chiefmonkey in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2009, 01:23 PM
  2. deleting and re-assigning a global variable pointer
    By Canadian0469 in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2008, 03:47 PM
  3. wierd segmentation fault with fopen
    By jaredw in forum C Programming
    Replies: 5
    Last Post: 08-27-2008, 08:43 PM
  4. global variable at dynamic library
    By Mortissus in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2007, 03:44 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21