It is currently Sat May 25, 2013 5:23 am

All times are UTC




Post new topic Reply to topic  [ 22 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: startup help
PostPosted: Thu Dec 20, 2007 2:49 pm 
Zenwalk Packager
Zenwalk Packager

Joined: Fri Mar 10, 2006 9:09 am
Posts: 2797
Location: Amsterdam
DodoFXP wrote:
But the "/" gets me a lot of fubar...
Is there another way?


The two-liner is a copy & paste from an application that compiles 0-errors, 0-warnings .

Are you *sure* the "/" is the cause of your problems? My reservations where w.r.t. the '*the_file' part of your original question... That's why i included the var-definition in the reply...

_________________
In case of panic: scream and run in circles!


 Profile Send private message  
 
 Post subject: Re: startup help
PostPosted: Thu Dec 20, 2007 6:32 pm 
Master of the known universe
Master of the known universe
User avatar

Joined: Mon Aug 28, 2006 11:57 am
Posts: 1040
Location: Karlsruhe, Germany
Hi again

I want to parse mtab to get the mountpoint of a device. I already have its block device(/dev/sd-something).
Before I start parsing anything, I just want to print the whole content of mtab onto the console.
So I thought it was done by something like this:

FILE *mtab;
mtab=fopen("/etc/mtab","r");
do
{
printf("%c",*mtab):
mtab++;
}
while(!strcmp(*mtab,"\0"));

But I get a segmentation fault. I am starting to think that FILE *mtab does not work as a pointer...as I thought it was. How would I go about reading the content of a file? Are there any tricks?

_________________
...and just when you thought, the shadows were safe...


 Profile Send private message  
 
 Post subject: Re: startup help
PostPosted: Thu Dec 20, 2007 7:50 pm 
Regular Zenwalker
Regular Zenwalker

Joined: Fri Feb 02, 2007 8:22 am
Posts: 54
Code:
FILE *mtab;
mtab=fopen("/etc/mtab","r");
if (mtab == NULL) {
      printf("Cant read /etc/mtab\n");
      exit(1);
}
char line[200];

while( fgets(line, sizeof(line), mtab) != NULL ) {
      printf("%s",line):
}
fclose(mtab);


fopen doesn't return the content of a file as a string. It just returns of pointer to a file handle (--> man stdio).
To read from or to write to a text file you need other functions (--> man fgets, man fputs)
At the end you have to close the file again.

If you want to split a string (in your task to get the mountpoint) you can use the function strtok (--> man strtok, there is an example at the end)


 Profile Send private message  
 
 Post subject: Re: startup help
PostPosted: Fri Dec 21, 2007 3:57 pm 
Master of the known universe
Master of the known universe
User avatar

Joined: Mon Aug 28, 2006 11:57 am
Posts: 1040
Location: Karlsruhe, Germany
Hi

I have managed to read the whole file with fread/fseek/ftell and some calloc'ing.
I will have look into strtok...
Cheers

_________________
...and just when you thought, the shadows were safe...


 Profile Send private message  
 
 Post subject: Re: startup help
PostPosted: Tue Dec 25, 2007 8:46 pm 
Master of the known universe
Master of the known universe
User avatar

Joined: Mon Aug 28, 2006 11:57 am
Posts: 1040
Location: Karlsruhe, Germany
Hi everyone! I hope you enjoyed a nice Xmas, but now back to work!  :)
I have been working on the code. I am still on the read-the-f%$#ing-mtab part.
My struggle is the following:I want to read the file line-by-line so that I can further "chop" it down with strtok.
The first step was to read how many lines there are in the file. I do this by looking for "\n"s.
Then, thought I was being smart by line_array=malloc(line_count*sizeof(int)) to then store into pointers to the arrays in line_array[n]. There reason for this was, that I see no other way of addressing the strings containing the lines. But for some reason the line_array[1...4]=calloc(some_length,sizeof(char)) does not work.
It says that it a pointer is being assigned to an int without a cast. Sure. But I though the addresses where numbers, thus could be stored in an int...
I thought this was a creative, cool idea, but there is still some error......

Thanks

_________________
...and just when you thought, the shadows were safe...


 Profile Send private message  
 
 Post subject: Re: startup help
PostPosted: Tue Dec 25, 2007 9:01 pm 
Zenwalk Packager
Zenwalk Packager

Joined: Fri Mar 10, 2006 9:09 am
Posts: 2797
Location: Amsterdam
My guess would be that you need a:

char **lines = NULL;                                              // just defining the lines[] var...
lines = (char **) malloc(nLines * sizeof (char *));  // you are allocating room for an array of pointer-to-char, right..?
lines[0] = ptr_to_first_line;                                    // that [1] was bugging me... C-arrays always start at [0]

_________________
In case of panic: scream and run in circles!


 Profile Send private message  
 
 Post subject: Re: startup help
PostPosted: Fri Dec 28, 2007 11:37 pm 
Master of the known universe
Master of the known universe
User avatar

Joined: Mon Aug 28, 2006 11:57 am
Posts: 1040
Location: Karlsruhe, Germany
Hi coders!

I have implemented prfasses correction on the array filled with pointers.
Here is the code. It is still not doing anything useful. The output of mtab is still not right.
I think I have something wrong with either the allocated size for a line or the copying of the string.
HEre it is:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib-2.0/glib.h>
#include <dbus-1.0/dbus/dbus.h>
#include <hal/libhal.h>

#define CR 13
#define LF 10

struct mountpoint
{
   char *blockdevice;
   char *mountplace;
};

int main(void)
{
   //some required variables
   static LibHalContext *ctx;   
   DBusError error;
   DBusConnection *bus;
   char **devices;
   char *properties;
   int i_devices, i;
      
   //GMainLoop *main_loop;
   //GMainContext *g_context;
         
   //initialize the system   
   dbus_error_init(&error);
   bus=dbus_bus_get(DBUS_BUS_SYSTEM,&error);
   ctx = libhal_ctx_new();
   
   //Check if the bus and the context have been properly initialized
   if (dbus_error_is_set(&error))
   {
       g_warning("Could not connect to system bus %s\n", error.message);
    }
   
   if (!bus)
   {
      g_warning("Could not connect to system bus: %s\n",error.message);
      dbus_error_free(&error);
      return 1;
   }
      
   if (!ctx)
   {
      g_warning("Could not get context libhal_ctx_new()\n");
      return 1;
   }
   
   libhal_ctx_set_dbus_connection(ctx, bus);
   
   
   //Let's fetch all the devices (UDIs) for devices from the category "storage"
    if (!(devices = libhal_manager_find_device_string_match (ctx,"storage.drive_type","disk", &i_devices, &error))) {
      
       g_warning("Error: %s: %s\n",error.name,error.message);
      
       dbus_error_free(&error);
       libhal_ctx_shutdown (ctx, NULL);
       libhal_ctx_free (ctx);
       return 1;
       }
   
   for (i=0; i < i_devices; i++)
   {
      
      properties=libhal_device_get_property_string(ctx,devices[i],"block.device", &error);
      
      printf("%s:%s\n",devices[i],properties);
      
         
   }
   /*We happily open the mtab file */
   FILE *mtab;
   mtab=fopen("/etc/mtab","r");
   
   /*We get mtabs size to allocate the right ammount of memory*/
   int length;
   fseek(mtab,0,SEEK_END); //bounce to the end
   length = ftell(mtab);   //measure /////and add 1 for the first char (0)..
   fseek(mtab,0,SEEK_SET)   ;//bounce back to the beginning
      
   /*We allocate the right amount of memory*/
   char *storage = calloc(length,sizeof(char));
   
   /*And now we read the file into the storage char*/
   fread(storage,length,1,mtab);
   
   
   /*Output type 1: char-by-char with loop.
    * Good for seachrching for specific characters and counting lines
    * The idea might be to count lines and then see in which line
    * a given block device is in and only use that line.
    * I have to figure out how to create char arrays "on the fly" */
   
   char *position_pointer;
   position_pointer=storage;
   
   int line_count=0;         //USED:This baby will count the lines in a file
   int char_count=0;         //USED:
   int p;
   char *g;               //USED: compare to see if *g is  a \n (linefeed)
   char ** line_array;         //USED: This will hold the pointers to the strings of the lines
      
   //Here we count the amount of lines based on the \n (Linefeed) char
   for(g=strchr(storage, '\n');g!=NULL;g=strchr(g+1,'\n'))
   {
      line_count++;
   }
   
   line_array=(char **)malloc(sizeof(char *)*line_count);
   
   //We have to reset it to 0 since it will be used as the index of the array
   line_count=0;       
   
   char * position_pointer_2;
   
   position_pointer_2=position_pointer;
   
   /*Position_pointer_2 is the "back-track": the distance between position_pointer_2 and
    * position_pointer is the length of the string/line.
    * Position_pointer will slide to the end of a line (LF '\n') and then
    * copy everything between it and position_pointer_2 to a new string.
    * then position_pointer_2 will move to position_pointer+1 to be at the first character
    * _after_ the LF, hence in the new line. then the game will repeat.
    * The pointers to the strings will be stored in an array. The length
    * can be either calculated with strlen or with the pointers etc.
    * The proper amount of memory will be alloceted with malloc/calloc */
   
   //Here we (c)allocate the right amount of memory for the strings.
   
for(p=0;p<length;p++)
   {
      if(*position_pointer == LF)
      {
         if(line_count==0)
         {
            line_array[line_count]=calloc(p+1,sizeof(char));
            strncpy(line_array[line_count],position_pointer_2,p-char_count+1);
            //printf("line_array[%d] contains:%s",line_count,line_array[line_count]);
            printf("line_count:%d,p:%d,char_count:%d,length of string:%d\n",line_count,p,char_count,strlen(line_array[line_count]));
         }
         else
         {
            line_array[line_count]=calloc(p-char_count,sizeof(char));
            strncpy(line_array[line_count],position_pointer+1,p-char_count+1);
            //printf("line_array[%d] contains:%s",line_count,line_array[line_count]);
            printf("line_count:%d,p:%d,char_count:%d,length of string:%d\n",line_count,p,char_count,strlen(line_array[line_count]));
         }
         char_count=p;
         
         line_count++;
         
      }
      position_pointer++;
   }
   
      
   /*Output type 2: the whole string at one
    * Probably good for final output. Slightly uncontrolable.
    * Probably good to print stuff into file in the end with fprintf*/
   //printf("%s",storage);
   //fprintf(stdout,"%s",storage);
   
   
   free(line_array);   
   free(storage);
   fclose(mtab);
   
   
   //main_loop = g_main_loop_new(g_context,FALSE);
   //g_main_loop_run(main_loop);
   
   //Free all the variables
   dbus_error_free(&error);
   libhal_ctx_free(ctx);   
   return 0;
}

And here is the makefile:
#My magic makefile for my little demon

COPTS=-c -DDBUS_API_SUBJECT_TO_CHANGE -I/usr/include/hal -I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include
#-I/usr/include/glib-2.0

LDOPTS= -lhal -ldbus-1 -lglib-2.0


demon:demon.o
gcc $(COPTS) demon-source.c -o demon.o
gcc $(LDOPTS) demon.o -o demon

demon.o:demon-source.c
gcc $(COPTS) demon-source.c -o demon.o

clean:
echo "Removing demon2.o and demon2..."
rm -rf demon.o demon 2> /dev/null



Let's see who can point me to the mistake first! I also hope the coments are enough...

Cheers

_________________
...and just when you thought, the shadows were safe...


 Profile Send private message  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 22 posts ]  Go to page Previous  1, 2

All times are UTC


 Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
 
cron