Find the specified string value in the specified file

Source: Internet
Author: User

# Include <stdio. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <stdlib. h>
# Include <string. h>
# Include <Linux/kernel. h>

Static unsigned long get_file_size (const char * path)
{
Struct stat statbuff;
If (STAT (path, & statbuff)> = 0 ){
Return statbuff. st_size;
}
Return-1;
}

Static unsigned int print_network_name (void)
{
Int FD =-1;
Int size = 0;
Int I = 0;
Char Buf [100];
Char strbuf [100];
Char * P;
Unsigned long filesize = get_file_size ("./sets. conf ");
Char * buffer = (char *) malloc (filesize + 1 );
Char * tmpbuf = (char *) malloc (filesize + 1 );
If (null = buffer | null = tmpbuf)
{
Printf ("% s malloc error \ n", _ FUNC __);
Return-1;
}
FD = open ("./sets. conf", o_rdonly );
If (-1 = FD)
{
Printf ("% s open error \ n", _ FUNC __);
Return-1;
}
Size = read (FD, buffer, filesize );
If (size! = Filesize)
{
Printf ("% s Read File Len error \ n", _ FUNC __);
Close (FD );
Return-1;
}
Close (FD );

P = strstr (buffer, "networkdevice ");
Memcpy (tmpbuf, P, (unsigned INT) (P-buffer)/sizeof (char )));
P = strstr (tmpbuf, "devicename ");
P + = sizeof ("devicename ");
While (* P! = '\ N ')
{
Buf [I] = * P;
I ++;
P ++;
}
Buf [I] = '\ 0 ';

Free (buffer );
Free (tmpbuf );

Memset (strbuf, 0xff, 100 );
Sprintf (strbuf, "networkname: % s", Buf );
Printf ("% s \ n", strbuf );

Return 0;
}

Void main (void)

{

Print_network_name (();

}

Find the value of devicename in the [networkdevice] field of sets. conf.

[Networkdevice]
Connectednetwork = DHCP
Devicename = eth0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.