当前位置: 技术问答>linux和unix
急 段错误问题,哪位高手帮看看!
来源: 互联网 发布时间:2016-11-02
本文导语: 写了一段代码,是向文件中写数据,但不知道为什么可以执行 写文件也正常,就是总打印奇怪信息,哪位帮看看啊,急等!非常感谢。 代码: #include #include #include "fcntl.h" #include "stdlib.h" #include "stdio.h"...
写了一段代码,是向文件中写数据,但不知道为什么可以执行 写文件也正常,就是总打印奇怪信息,哪位帮看看啊,急等!非常感谢。
代码:
#include
#include
#include "fcntl.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "ctype.h"
#define MAX 500
//This function being used to open the xfs's projects file.
static FILE* openprofile(char *file)
{
FILE *profile;
if((profile = fopen(file,"r+b")) == NULL){
printf("Open the projects file failure,please check the file in the etc direct.n");
exit(1);
}
//printf("successfuln");//test info
return profile;
}
//this function being used to close the xfs projects file int the /etc
static void closeprofile(FILE *fileid)
{
fclose(fileid);
//printf("close successfuln"); //test info
}
//This function being used to write info to projects files
static int writepro(FILE *projects,char *s)
{
if(projects == NULL||s == NULL){
printf("File is not exit or the String is NULl,please check itu.n");
}
if(fseek(projects,0L,SEEK_END) == -1){
printf("SEEK Error");
return 0;
exit(1);
}
if(fputs(s,projects) == EOF){
printf("Write Errorn");
return 0;
exit(1);
}
rewind(projects);
return 1;
}
//This function being used to discuss a file is or is't null?
static int isnull(FILE *file)
{
long int len=-1;
if(fseek(file,0L,SEEK_END) == -1){
printf("SEEK Error");
return 0;
exit(1);
}
len=ftell(file);
rewind(file);
if(len == 0){
return 1;
}
if(len!=0){
return 0;
}
}
static char* linkstring(char *pro,char *s)
{
char buffer[MAX];
int i;
if(pro==NULL||s==NULL){
exit(1);
}
strcat(pro,":");
strcat(pro,s);
return pro;
}
static char* getentry(char src[],char result[])
{
char *p;
int i;
p=src;
p = strtok(src,"/");
while(1){
if(p == NULL){break;}
for(i=0;p[i]!='';i++){
result[i]=*(p+i);
}
result[i]='';
p = strtok(NULL,"/");
}
return result;
}
static int getid(FILE *file)
{
char **q;
int id;
char buffer[MAX];
while(!feof(file)){
fgets(buffer,1000,file);
// printf(buffer); //test use
}
id=strtoul(buffer,q,10);
//printf("%dn",i); //test use
return (id+1);
}
static void quota(char *prodir,char *jid,char *dir,char *bsoft,char *bhard)
{
char quota_pro[100]="0xfs_quota -x -c ";
char quota_set[100]="xfs_quota -x -c ";
char *pro="'project -s ";
char *limit="'limit -p ";
char *bs="bsoft=";
char *bh=" bhard=";
char d[MAX];
char dirbuf[MAX];
FILE *file;
int id;
if(dir==NULL||bsoft==NULL||bhard==NULL){
printf("Bad Error or Bad Setting Value.n");
exit(1);
}
strcpy(dirbuf,dir);
getentry(dirbuf,d);
strcat(quota_pro,pro);
strcat(quota_pro,d);
strcat(quota_pro,"'");
strcat(quota_set,pro);
strcat(quota_set,limit);
strcat(quota_set,bs);
strcat(quota_set,bsoft);
strcat(quota_set,bh);
strcat(quota_set,bhard);
strcat(quota_set," ");
strcat(quota_set,d);
strcat(quota_set,"'");
printf("%sn",quota_set); //test use
char linkstr[MAX];
file=openprofile(prodir);
if(isnull(file)){
sprintf(linkstr,"%d",1);
linkstring(linkstr,dir);
printf("%sn",linkstr); //test use
writepro(file,linkstr);
}
else{
id=getid(file);
printf("%dn",id);
sprintf(linkstr,"%d",id);
linkstring(linkstr,dir);
printf("%sn",linkstr); //test use
writepro(file,linkstr);
}
//printf("test use"); //test use
//closeprofile(file);
}
int main()
{
//FILE *file;
//char *dir = "51:/mnt/andir/an5n";
//int fileisnull;
//file=openprofile("/mycode/projects");
//writepro(file,dir);
//fileisnull = isnull(file);
//printf("%dn",fileisnull);
//closeprofile(file);
//printf("test infon");
//char dir[MAX]="/mnt/andir/an1";
//linkforpro(525,'');
//printf("%sn",dir);
//char s1[MAX]="/mnt/andir/an1";
//char s2[MAX]="project an1";
//linkforpro(s2,s1);
//printf(s2);
char dirpro[]="/mnt/andir/an9n";
char s2[100]="100m";
char s3[100]="100m";
char s4[100]="/etc/projects";
char s5[100]="/etc/projid";
quota(s4,s5,dirpro,s2,s3);
printf("*****************n");
return 0;
/*char s[100]="1";
char p[100]="/mnt/andir/an1";
linkstring(s,p);
printf("%sn",s);*/
}
**************************************************
gdb单步执行的调试信息
Starting program: /mycode/xfstest
xfs_quota -x -c 'project -s 'limit -p bsoft=100m bhard=100m an9
'
Breakpoint 1, quota (prodir=0x7ffff1af09c0 "/etc/projects", jid=0x7ffff1af0950 "/etc/projid",
dir=0x7ffff1af0b10 "/mnt/andir/an9n", bsoft=0x7ffff1af0aa0 "100m", bhard=0x7ffff1af0a30 "100m") at xfs_quota_set.c:147
147 printf("%dn",id);
(gdb) n
lookup 0x0000000000400000 0x0000000000000420 -> 0x00002b2ab90d4000 0x000000000004a060 /1 printf
54
148 sprintf(linkstr,"%d",id);
(gdb) n
lookup 0x0000000000400000 0x0000000000000480 -> 0x00002b2ab90d4000 0x000000000004a1a0 /1 sprintf
149 linkstring(linkstr,dir);
(gdb) n
150 printf("%sn",linkstr); //test use
(gdb) n
54:/mnt/andir/an9
151 writepro(file,linkstr);
(gdb) n
lookup 0x0000000000400000 0x0000000000000450 -> 0x00002b2ab90d4000 0x000000000005d5b0 /1 fputs
156 }
(gdb) n
main () at xfs_quota_set.c:183
183 printf("*****************n");
(gdb) n
*****************
184 return 0;
(gdb) n
190 }
(gdb) n
0x00002b2ab90f1154 in __libc_start_main () from /lib64/libc.so.6
(gdb) n
Single stepping until exit from function __libc_start_main,
which has no line number information.
7359:
7359: calling fini: /mycode/xfstest [0]
7359:
7359:
7359: calling fini: /lib64/libc.so.6 [0]
7359:
7359:
7359: runtime linker statistics:
7359: final number of relocations: 98
7359: final number of relocations from cache: 3
Program exited normally.
非常感谢!每次都出现
0x00002b2ab90f1154 in __libc_start_main () from /lib64/libc.so.6
(gdb) n
Single stepping until exit from function __libc_start_main,
which has no line number information.
7359:
7359: calling fini: /mycode/xfstest [0]
7359:
7359:
7359: calling fini: /lib64/libc.so.6 [0]
7359:
7359:
7359: runtime linker statistics:
7359: final number of relocations: 98
7359: final number of relocations from cache: 3
Program exited normally.
代码:
#include
#include
#include "fcntl.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "ctype.h"
#define MAX 500
//This function being used to open the xfs's projects file.
static FILE* openprofile(char *file)
{
FILE *profile;
if((profile = fopen(file,"r+b")) == NULL){
printf("Open the projects file failure,please check the file in the etc direct.n");
exit(1);
}
//printf("successfuln");//test info
return profile;
}
//this function being used to close the xfs projects file int the /etc
static void closeprofile(FILE *fileid)
{
fclose(fileid);
//printf("close successfuln"); //test info
}
//This function being used to write info to projects files
static int writepro(FILE *projects,char *s)
{
if(projects == NULL||s == NULL){
printf("File is not exit or the String is NULl,please check itu.n");
}
if(fseek(projects,0L,SEEK_END) == -1){
printf("SEEK Error");
return 0;
exit(1);
}
if(fputs(s,projects) == EOF){
printf("Write Errorn");
return 0;
exit(1);
}
rewind(projects);
return 1;
}
//This function being used to discuss a file is or is't null?
static int isnull(FILE *file)
{
long int len=-1;
if(fseek(file,0L,SEEK_END) == -1){
printf("SEEK Error");
return 0;
exit(1);
}
len=ftell(file);
rewind(file);
if(len == 0){
return 1;
}
if(len!=0){
return 0;
}
}
static char* linkstring(char *pro,char *s)
{
char buffer[MAX];
int i;
if(pro==NULL||s==NULL){
exit(1);
}
strcat(pro,":");
strcat(pro,s);
return pro;
}
static char* getentry(char src[],char result[])
{
char *p;
int i;
p=src;
p = strtok(src,"/");
while(1){
if(p == NULL){break;}
for(i=0;p[i]!='';i++){
result[i]=*(p+i);
}
result[i]='';
p = strtok(NULL,"/");
}
return result;
}
static int getid(FILE *file)
{
char **q;
int id;
char buffer[MAX];
while(!feof(file)){
fgets(buffer,1000,file);
// printf(buffer); //test use
}
id=strtoul(buffer,q,10);
//printf("%dn",i); //test use
return (id+1);
}
static void quota(char *prodir,char *jid,char *dir,char *bsoft,char *bhard)
{
char quota_pro[100]="0xfs_quota -x -c ";
char quota_set[100]="xfs_quota -x -c ";
char *pro="'project -s ";
char *limit="'limit -p ";
char *bs="bsoft=";
char *bh=" bhard=";
char d[MAX];
char dirbuf[MAX];
FILE *file;
int id;
if(dir==NULL||bsoft==NULL||bhard==NULL){
printf("Bad Error or Bad Setting Value.n");
exit(1);
}
strcpy(dirbuf,dir);
getentry(dirbuf,d);
strcat(quota_pro,pro);
strcat(quota_pro,d);
strcat(quota_pro,"'");
strcat(quota_set,pro);
strcat(quota_set,limit);
strcat(quota_set,bs);
strcat(quota_set,bsoft);
strcat(quota_set,bh);
strcat(quota_set,bhard);
strcat(quota_set," ");
strcat(quota_set,d);
strcat(quota_set,"'");
printf("%sn",quota_set); //test use
char linkstr[MAX];
file=openprofile(prodir);
if(isnull(file)){
sprintf(linkstr,"%d",1);
linkstring(linkstr,dir);
printf("%sn",linkstr); //test use
writepro(file,linkstr);
}
else{
id=getid(file);
printf("%dn",id);
sprintf(linkstr,"%d",id);
linkstring(linkstr,dir);
printf("%sn",linkstr); //test use
writepro(file,linkstr);
}
//printf("test use"); //test use
//closeprofile(file);
}
int main()
{
//FILE *file;
//char *dir = "51:/mnt/andir/an5n";
//int fileisnull;
//file=openprofile("/mycode/projects");
//writepro(file,dir);
//fileisnull = isnull(file);
//printf("%dn",fileisnull);
//closeprofile(file);
//printf("test infon");
//char dir[MAX]="/mnt/andir/an1";
//linkforpro(525,'');
//printf("%sn",dir);
//char s1[MAX]="/mnt/andir/an1";
//char s2[MAX]="project an1";
//linkforpro(s2,s1);
//printf(s2);
char dirpro[]="/mnt/andir/an9n";
char s2[100]="100m";
char s3[100]="100m";
char s4[100]="/etc/projects";
char s5[100]="/etc/projid";
quota(s4,s5,dirpro,s2,s3);
printf("*****************n");
return 0;
/*char s[100]="1";
char p[100]="/mnt/andir/an1";
linkstring(s,p);
printf("%sn",s);*/
}
**************************************************
gdb单步执行的调试信息
Starting program: /mycode/xfstest
xfs_quota -x -c 'project -s 'limit -p bsoft=100m bhard=100m an9
'
Breakpoint 1, quota (prodir=0x7ffff1af09c0 "/etc/projects", jid=0x7ffff1af0950 "/etc/projid",
dir=0x7ffff1af0b10 "/mnt/andir/an9n", bsoft=0x7ffff1af0aa0 "100m", bhard=0x7ffff1af0a30 "100m") at xfs_quota_set.c:147
147 printf("%dn",id);
(gdb) n
lookup 0x0000000000400000 0x0000000000000420 -> 0x00002b2ab90d4000 0x000000000004a060 /1 printf
54
148 sprintf(linkstr,"%d",id);
(gdb) n
lookup 0x0000000000400000 0x0000000000000480 -> 0x00002b2ab90d4000 0x000000000004a1a0 /1 sprintf
149 linkstring(linkstr,dir);
(gdb) n
150 printf("%sn",linkstr); //test use
(gdb) n
54:/mnt/andir/an9
151 writepro(file,linkstr);
(gdb) n
lookup 0x0000000000400000 0x0000000000000450 -> 0x00002b2ab90d4000 0x000000000005d5b0 /1 fputs
156 }
(gdb) n
main () at xfs_quota_set.c:183
183 printf("*****************n");
(gdb) n
*****************
184 return 0;
(gdb) n
190 }
(gdb) n
0x00002b2ab90f1154 in __libc_start_main () from /lib64/libc.so.6
(gdb) n
Single stepping until exit from function __libc_start_main,
which has no line number information.
7359:
7359: calling fini: /mycode/xfstest [0]
7359:
7359:
7359: calling fini: /lib64/libc.so.6 [0]
7359:
7359:
7359: runtime linker statistics:
7359: final number of relocations: 98
7359: final number of relocations from cache: 3
Program exited normally.
非常感谢!每次都出现
0x00002b2ab90f1154 in __libc_start_main () from /lib64/libc.so.6
(gdb) n
Single stepping until exit from function __libc_start_main,
which has no line number information.
7359:
7359: calling fini: /mycode/xfstest [0]
7359:
7359:
7359: calling fini: /lib64/libc.so.6 [0]
7359:
7359:
7359: runtime linker statistics:
7359: final number of relocations: 98
7359: final number of relocations from cache: 3
Program exited normally.
|
看不清楚,我觉得应该重点排查指针引用问题,避免空对指针操作
|
感觉像是数组越界了,破坏了返回地址。