以为informix没有产生sequence的功能,所以自己造了一个车轮

#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

int lock_reg(int fd)
{
 struct flock lock;
 
 lock.l_type = F_WRLCK;
 lock.l_start = 0;
 lock.l_whence = SEEK_SET;
 lock.l_len = 0; 
 
 return ( fcntl(fd, F_SETLKW, &lock) );
}

int GetSequence(char *Seq_path,int init_flag)
{
 int fd;
 char buffer[10];
 int seq;
 int ret;
 
 fd = open(Seq_path, O_RDWR|O_CREAT, 0644);
 if (fd<0)
 {
  perror("Open SeqPath Error:");
  return -1;
 }
 ret = lock_reg(fd);
 if (ret<0)
 {
  perror("Lock SeqPath Error:");
  close(fd);
  return -1;
 }
 if (init_flag) seq = 1;
 else
 {
  ret=read(fd, buffer, sizeof(buffer));
  if(ret>0) {
   buffer[ret]=0;
   seq=atoi(buffer)+1;
  }
  else seq = 1;
 }
 printf("%s",seq);
 ret=sprintf(buffer, "%09d/n", seq);
 lseek(fd, 0, SEEK_SET);
 write(fd, buffer, ret);
 close(fd);
 return seq;
}

int InitSequence(char *Seq_path)
{
 return ( GetSequence(Seq_path, 1) ); 
}

int AddSequence(char *Seq_path)
{
 return ( GetSequence(Seq_path, 0) ); 
}

int main()
{
 /*printf("Init Sequence %d/n", InitSequence("biz") );*/
 printf("Add Sequence %d/n", AddSequence("biz") );
}

你可能感兴趣的:(struct,buffer,Informix,Path,include)