(root)/
acl-2.3.1/
tools/
sequence.c
       1  /*
       2    File: sequence.c
       3    (Linux Access Control List Management)
       4  
       5    Copyright (C) 1999, 2000
       6    Andreas Gruenbacher, <andreas.gruenbacher@gmail.com>
       7   	
       8    This program is free software; you can redistribute it and/or
       9    modify it under the terms of the GNU Lesser General Public
      10    License as published by the Free Software Foundation; either
      11    version 2.1 of the License, or (at your option) any later version.
      12  
      13    This program is distributed in the hope that it will be useful,
      14    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16    Lesser General Public License for more details.
      17  
      18    You should have received a copy of the GNU Lesser General Public
      19    License along with this library; if not, write to the Free Software
      20    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
      21  */
      22  
      23  
      24  #include "config.h"
      25  #include <stdlib.h>
      26  #include "sequence.h"
      27  
      28  
      29  cmd_t
      30  cmd_init(
      31  	void)
      32  {
      33  	cmd_t cmd;
      34  
      35  	cmd = malloc(sizeof(struct cmd_obj));
      36  	if (cmd) {
      37  		cmd->c_tag = ACL_UNDEFINED_TAG;
      38  		cmd->c_perm = 0;
      39  	}
      40  	return cmd;
      41  }
      42  
      43  
      44  void
      45  cmd_free(
      46  	cmd_t cmd)
      47  {
      48  	free(cmd);
      49  }
      50  
      51  
      52  seq_t
      53  seq_init(
      54  	void)
      55  {
      56  	seq_t seq = (seq_t)malloc(sizeof(struct seq_obj));
      57  	if (seq == NULL)
      58  		return NULL;
      59  	seq->s_first = seq->s_last = NULL;
      60  	return seq;
      61  }
      62  
      63  
      64  int
      65  seq_free(
      66  	seq_t seq)
      67  {
      68  	cmd_t cmd = seq->s_first;
      69  	while (cmd) {
      70  		seq->s_first = seq->s_first->c_next;
      71  		cmd_free(cmd);
      72  		cmd = seq->s_first;
      73  	}
      74  	free(seq);
      75  	return 0;
      76  }
      77  
      78  
      79  int
      80  seq_empty(
      81  	seq_t seq)
      82  {
      83  	return (seq->s_first == NULL);
      84  }
      85  
      86  
      87  int
      88  seq_append(
      89  	seq_t seq,
      90  	cmd_t cmd)
      91  {
      92  	cmd->c_next = NULL;
      93  	if (seq->s_first == NULL) {
      94  		seq->s_first = seq->s_last = cmd;
      95  	} else {
      96  		seq->s_last->c_next = cmd;
      97  		seq->s_last = cmd;
      98  	}
      99  	return 0;
     100  }
     101  
     102  
     103  int
     104  seq_append_cmd(
     105  	seq_t seq,
     106  	cmd_tag_t cmd,
     107  	acl_type_t type)
     108  {
     109  	cmd_t cmd_d = cmd_init();
     110  	if (cmd_d == NULL)
     111  		return -1;
     112  	cmd_d->c_cmd = cmd;
     113  	cmd_d->c_type = type;
     114  	if (seq_append(seq, cmd_d) != 0) {
     115  		cmd_free(cmd_d);
     116  		return -1;
     117  	}
     118  	return 0;
     119  }
     120  
     121  
     122  int
     123  seq_get_cmd(
     124  	seq_t seq,
     125  	int which,
     126  	cmd_t *cmd)
     127  {
     128  	if (which == SEQ_FIRST_CMD) {
     129  		if (seq->s_first == NULL)
     130  			return 0;
     131  		if (cmd)
     132  			*cmd = seq->s_first;
     133  		return 1;
     134  	} else if (which == SEQ_NEXT_CMD) {
     135  		if (cmd == NULL)
     136  			return -1;
     137  		if (*cmd) {
     138  			*cmd = (*cmd)->c_next;
     139  			return (*cmd == NULL) ? 0 : 1;
     140  		}
     141  		return 0;
     142  	} else {
     143  		return -1;
     144  	}
     145  }
     146  
     147  
     148  int
     149  seq_delete_cmd(
     150  	seq_t seq,
     151  	cmd_t cmd)
     152  {
     153  	cmd_t prev = seq->s_first;
     154  
     155  	if (cmd == seq->s_first) {
     156  		seq->s_first = seq->s_first->c_next;
     157  		cmd_free(cmd);
     158  		return 0;
     159  	}
     160  	while (prev != NULL && prev->c_next != cmd)
     161  		prev = prev->c_next;
     162  	if (prev == NULL)
     163  		return -1;
     164  	if (cmd == seq->s_last)
     165  		seq->s_last = prev;
     166  	prev->c_next = cmd->c_next;
     167  	cmd_free(cmd);
     168  	return 0;
     169  }
     170