Example #1
0
int	find(char **tab, int pos, char **res)
{
  char	*str;
  int	len;
  int	i;
  int	j;

  str = tab[pos];
  len = my_strlen(str);
  i = 0;
  while (len - i > 0)
    {
      j = 0;
      while (j <= i)
	{
	  if (find_part(tab, pos, str + j, len - i))
	    {
	      if (*res == NULL || len > my_strlen(*res))
		*res = my_strduplen(str + j, len - i);
	      return (1);
	    }
	  j++;
	}
      i++;
    }
  return (0);
}
/**********************************************************
 * insert: Prompts the user for information about a new   *
 *         part and then inserts the part into the        *
 *         database. Prints an error message and returns  *
 *         prematurely if the part already exists or the  *
 *         database is full.                              *
 **********************************************************/
void insert(struct part *inventory, int *num_parts)
{
  int part_number;
  float price;

  if (*num_parts == MAX_PARTS) {
    printf("Database is full; can't add more parts.\n");
    return;
  }

  printf("Enter part number: ");
  scanf("%d", &part_number);
  if (find_part(inventory, *num_parts, part_number) >= 0) {
    printf("Part already exists.\n");
    return;
  }

  inventory[*num_parts].number = part_number;
  printf("Enter part name: ");
  read_line(inventory[*num_parts].name, NAME_LEN);
  printf("Enter quantity on hand: ");
  scanf("%d", &inventory[*num_parts].on_hand);
  printf("Enter price of part: ");
  scanf("%f", &inventory[*num_parts].price);
  (*num_parts)++;
}
/* Add a participant with this part_name to the participant list for the poll
   with this poll_name in the list at head_pt. Duplicate participant names
   are not allowed. Set the availability of this participant to avail.
   Return: 0 on success 
      1 for poll does not exist with this name
      2 for participant by this name already in this poll
      3 for availibility string is wrong length. Particpant not added.
*/
int add_participant(char *part_name, char *poll_name, Poll *head_ptr, 
                    char *avail) {
    Poll *poll;
    if ((poll = find_poll(poll_name, head_ptr)) == NULL) {
        return 1;
    }
    Participant *part;
    if ((part = find_part(part_name, poll)) != NULL) {
        return 2;
    }
    if (strlen(avail) != poll->num_slots) {
        return 3;
    }


    // create the participant to add
    Participant *new_part = Malloc(sizeof(struct participant));
    // copy name in and ensure it is a string
    strncpy(new_part->name, part_name, 31);
    new_part->name[31] = '\0';

    // set comment to NULL so we we can know if we need to free 
    // allocated memory when we delete the participant
    new_part->comment = NULL;

    // set availability for this new participant
    new_part->availability = Malloc(poll->num_slots + 1);
    // strcpy is safe because we checked the lengths
    strcpy(new_part->availability, avail);

    // insert this participant at the head of the participant list for this poll
    new_part->next = poll->participants;
    poll->participants = new_part;
    return 0;
}
Example #4
0
/*!
    \qmlmethod point QtLocation::Map::from_coordinate(coordinate coordinate, bool clipToViewPort)

    Returns the position relative to the map item which corresponds to the \a coordinate.

    If \a cliptoViewPort is \c true, or not supplied then returns an invalid QPointF if
    \a coordinate is not within the current viewport.
*/
QcVectorDouble
QcViewport::coordinate_to_screen(const QcVectorDouble & projected_coordinate, bool clip_to_viewport) const
{
  // qInfo() << coordinate << clip_to_viewport
  //         << (int)projected_coordinate.x() << (int)projected_coordinate.y();

  const QcViewportPart * part = find_part(projected_coordinate);
  if (!part) {
    qWarning() << "out of domain";
    return QcVectorDouble(qQNaN(), qQNaN());
  }

  QcVectorDouble screen_position = to_px(part->map_vector(projected_coordinate));

  // Fixme: purpose
  if (clip_to_viewport) {
    int w = width();
    int h = height();
    double x = screen_position.x();
    double y = screen_position.y();
    if ((x < 0.0) || (x > w) || (y < 0) || (y > h) || qIsNaN(x) || qIsNaN(y))
      return QcVectorDouble(qQNaN(), qQNaN());
  }

  // qInfo() << screen_position;

  return screen_position;
}
/**********************************************************
 * insert: Prompts the user for information about a new   *
 *         part and then inserts the part into the        *
 *         database. Prints an error message and returns  *
 *         prematurely if the part already exists or the  *
 *         database is full.                              *
 **********************************************************/
void insert(void)
{
    struct part *temp;
    int part_number;

    if (num_parts == max_parts) {
        max_parts *= 2;
        temp = realloc(inventory, max_parts * sizeof(struct part));
        if (temp == NULL) {
            printf("Insufficient memory: can't add more parts.\n");
            return;
        }
        inventory = temp;
    }

    printf("Enter part number: ");
    scanf("%d", &part_number);

    if (find_part(part_number) >= 0) {
        printf("Part already exists.\n");
        return;
    }

    inventory[num_parts].number = part_number;
    printf("Enter part name: ");
    read_line(inventory[num_parts].name, NAME_LEN);
    printf("Enter quantity on hand: ");
    scanf("%d", &inventory[num_parts].on_hand);
    num_parts++;
}
Example #6
0
/**********************************************************
 * search: Prompts the user to enter a part number, then  *
 *         looks up the part in the database. If the part *
 *         exists, prints the name and quantity on hand;  *
 *         if not, prints an error message.               *
 **********************************************************/
void search(struct part inventory[])
{
  int i, number;

  printf("Enter part number: ");
  scanf("%d", &number);
  i = find_part(inventory, number);
  if (i >= 0) {
    printf("Part name: %s\n", inventory[i].name);
    printf("Quantity on hand: %d\n", inventory[i].on_hand);
  } else
    printf("Part not found.\n");
}
Example #7
0
/*
 * menu handlers
 */
static int mount_internal_mmc(void)
{
	struct part_info *autoboot_ptn;

	/* Mount partition for kexec */
	autoboot_ptn = find_part(disk_info, g_2ndstageboot_part);
	if (mount_partition(autoboot_ptn)) {
		pr_error("Can't mount second-stage boot partition (%s)\n",
				g_2ndstageboot_part);
		return -1;
	}

	return 0;
}
/**********************************************************
 * update: Prompts the user to enter a part number.       *
 *         Prints an error message if the part doesn't    *
 *         exits; other wise, prompts the user to enter   *
 *         change in quantity on hand and updates the     *
 *         database.                                      */
void update(void) {
	int i, number, change;

	printf("Enter part number: ");
	scanf("%d", &number);
	i = find_part(number);
	if (i >= 0) {
		printf("Enter change in quatity on hand:");
		scanf("%d", &change);
		inventory[i].on_hand += change;
	} else {
		printf("Part not found.\n");
	}
}
/**********************************************************
 * update: Prompts the user to enter a part number.       *
 *         Prints an error message if the part doesn't    *
 *         exist; otherwise, prompts the user to enter    *
 *         change in quantity on hand and updates the     *
 *         database.                                      *
 **********************************************************/
void update(struct part *inventory, int num_parts)
{
  int i, number, change;

  printf("Enter part number: ");
  scanf("%d", &number);
  i = find_part(inventory, num_parts, number);
  if (i >= 0) {
    printf("Enter change in quantity on hand: ");
    scanf("%d", &change);
    inventory[i].on_hand += change;
  } else
    printf("Part not found.\n");
}
/**********************************************************
 * search: Prompts the user to enter a part number, then  *
 *         looks up the part in the database. If the part *
 *         exists, prints the name and quantity on hand;  *
 *         if not, prints an error message.               *
 **********************************************************/
void search(struct part *inventory)
{
  int number;
  struct part *p;

  printf("Enter part number: ");
  scanf("%d", &number);
  p = find_part(inventory, number);
  if (p != NULL) {
    printf("Part name: %s\n", p->name);
    printf("Quantity on hand: %d\n", p->on_hand);
  } else
    printf("Part not found.\n");
}
Example #11
0
void search(part* parts, int num_parts)
{
    int number;
    printf("Enter part number: ");
    scanf("%d", &number);
    int index = find_part(parts, num_parts, number);
    if (index < num_parts)
    {
        printf("Part name: %s\n", parts[index].name);
        printf("Quantity on hand: %d\n", parts[index].on_hand);
        return;
    }
    printf("Part not found\n");
    return;
}
void modify_price(struct part *inventory, int num_parts)
{
  int i, number;
  float change;

  printf("Enter part number: ");
  scanf("%d", &number);
  i = find_part(inventory, num_parts, number);
  if (i >= 0) {
    printf("Enter new price: ");
    scanf("%f", &change);
    inventory[i].price = change;
  } else
    printf("Part not found.\n");
}
/**********************************************************
 * update: Prompts the user to enter a part number.       *
 *         Prints an error message if the part doesn't    *
 *         exist; otherwise, prompts the user to enter    *
 *         change in quantity on hand and updates the     *
 *         database.                                      *
 **********************************************************/
void update(struct part *inventory)
{
  int number, change;
  struct part *p;

  printf("Enter part number: ");
  scanf("%d", &number);
  p = find_part(inventory, number);
  if (p != NULL) {
    printf("Enter change in quantity on hand: ");
    scanf("%d", &change);
    p->on_hand += change;
  } else
    printf("Part not found.\n");
}
Example #14
0
int read_part_jobs_file (char* filename)
{ /* reads jobs sequence file and inits parts structures */
   char str[1024];
   char name[NAME_SZ];
   char* pstr, *tok;
   int read, ct;

   int pid, jid, jidx;

   FILE *fptr = fopen (filename, "r\0");

   if (fptr == NULL) return 1;

   ct = 0;
   while (!feof(fptr)) {
      memset(name, 0, sizeof(name));
      memset(str, 0, sizeof(str));

      if (!(read = fscanf (fptr, "%s %[^\n]s\n", name, str))) break;
      if (read == 1) return 4;
      if (name[0] == '\0') break;
      if (name[0] != 'P' && name[0] != 'p') return 2;

		pid = find_part(name);
		if (pid == -1) return 1;
		jidx = 0;

      pstr = (char *) str; tok = (char *)strtok (pstr, " ");
      while (tok != NULL) {
         if (tok[0] != 'J' && tok[0] != 'j') return 3;
			jid = find_job(tok);
			if (jid == -1) return 2;

			parts[pid].jobs[jidx] = jid;
			jidx++;

         tok = (char *)strtok ((char*)NULL, " ");
      }
		parts[pid].jobs[jidx] == -1;
		parts[pid].totjobs = jidx;
      ct++;
   }
  
   if (ct != total_parts) return 4;

   fclose (fptr);
   return 0;
}
Example #15
0
void print(part* parts, int num_parts)
{
    printf("Part Number    Part Name                Quantity on Hand\n");
    int number_array[MAX_PARTS] = {0};
    for (int i = 0; i < num_parts; i++)
    {
        number_array[i] = parts[i].number;
    }
    my_qsort (number_array, number_array + num_parts - 1);
    for (int i = 0; i < num_parts; i++)
    {
        int index = find_part(parts, num_parts, number_array[i]);
        printf("%7d        %-25s%11d\n", parts[index].number, parts[index].name, parts[index].on_hand);
    }
    return;
}
Example #16
0
static void fixup_fnd( struct FASL_UnswizzledFnDescr *xd )
{
  struct FASL_PartHdr *xp = xd->container;
  struct FASL_ModuleHdr *xm;
  struct part_descr *p;
  unsigned i;

  if (xd->real_fn_d) {
    return;
  }

  xm = xp->container;

  if (!xm->module)
    {
      struct module_descr *m;

      /* printf( "** mapping in %s module\n", xm->name );*/
      m = find_module( xm->name );
      if (!m) {
        char *p = strchr( xm->name, '|' );
        if (p) {
          scheme_error( "dynamic module ~s not loaded: ~a", 2, 
                        make_string( p+1 ),
                        make_string( dynamic_link_errors() ) );
        } else {
          scheme_error( "static module ~s not loaded", 1, 
                        make_string( xm->name ) );
        }
      }
      xm->module = m;
    }
  /* printf( "** mapping in %s[%d]\n", xm->name, xp->part_tag );*/
  p = find_part( xm->module, xp->part_tag );
  if (!p)
    {
      scheme_error( "module ~s is missing part ~d",
                    2,
                    make_string( xm->name ),
                    int2fx( xp->part_tag ) );
    }
  for (i=0; p->functions[i]; i++)
    {
      xp->fnds[i].real_fn_d = p->functions[i];
      xp->fnds[i].real_code_ptr = p->functions[i]->monotones[0];
    }
}
Example #17
0
void update(part* parts, int num_parts)
{
    int number;
    printf("Enter part number: ");
    scanf("%d", &number);
    int index = find_part(parts, num_parts, number);
    if (index < num_parts)
    {
        int change_on_hand;
        printf("Enter change in quantity on hand: ");
        scanf("%d", &change_on_hand);
        parts[index].on_hand += change_on_hand;
        return;
    }
    printf("Part not found\n");
    return;
}
/* Add availabilty for the participant with this part_name to the poll with
 * this poll_name. Return values:
 *    0 success
 *    1 no poll by this name
 *    2 no participant by this name for this poll
 *    3 avail string is incorrect size for this poll 
 */
int update_availability(char *part_name, char *poll_name, char *avail, 
          Poll *head_ptr) {
    Poll *poll;
    if ((poll = find_poll(poll_name, head_ptr)) == NULL) {
        return 1;
    }
    Participant *part;
    if ((part = find_part(part_name, poll)) == NULL) {
        return 2;
    }
    if (strlen(avail) != poll->num_slots) {
        return 3;
    }
    /* strcpy is safe because we allocated the right amount earlier */
    strcpy(part->availability, avail);
    return 0;
}
Example #19
0
void search(void)
{
	int part_number;
	int i;
	
	printf("Enter part number: ");
	scanf("%d", &part_number);

	i = find_part(part_number);
	if(i >= 0) {
		printf("Part name: %s\n", inventory[i].name);
		printf("Quantity on hand: %d", inventory[i].on_hand);
		return;
	} else {
		printf("Part not found");
		return;
	}
}
Example #20
0
void update(void)
{
	int i, part_number, on_hand;

	printf("Enter part number: ");
	scanf("%d", &part_number);

	i = find_part(part_number);
	if (i >= 0) {
		printf("Enter quantity on hand: ");
		scanf("%d", &on_hand);
		inventory[i].on_hand = on_hand;
		return;
	} else {
		printf("Part not found");
		return;
	}
}
/* Add a comment from the participant with this part_name to the poll with
   this poll_name. Return values:
      0 success
      1 no poll by this name
      2 no participant by this name for this poll
 */
int add_comment(char *part_name, char *poll_name, char *comment, 
                Poll *head_ptr) {
    Poll *poll;
    if ((poll = find_poll(poll_name, head_ptr)) == NULL) {
        return 1;
    }
    Participant *part;
    if ((part = find_part(part_name, poll)) == NULL) {
        return 2;
    }
    // set or replace comment
    // if comment was previously set, then space needs to be freed
    if (part->comment != NULL) {
        free(part->comment);
    }
    part->comment = Malloc(strlen(comment) + 1);
    strcpy(part->comment, comment);
    return 0;
}
Example #22
0
int
block::new_part(char *part_name)
{
	part *part_ptr;
	int part_no;

	// If a part already exists with the desired part name, this is an error.

	if (find_part(part_name))
		return(0);

	// Determine the part number of the new part.

	if (part_list)
		part_no = last_part_ptr->part_no + 1;
	else
		part_no = 1;

	// Create the part and set it's number and name.

	if ((part_ptr = new part(part_no)) == NULL ||
		!part_ptr->set_part_name(part_name))
		return(0);

	// Add the part to the end of the part list.

	if (part_list)
		last_part_ptr->next_part_ptr = part_ptr;
	else
		part_list = part_ptr;

	// Remember it as the last part added.

	last_part_ptr = part_ptr;

	// Return the part number.

	return(part_no);
}
Example #23
0
/**********************************************************
 * insert: Prompts the user for information about a new   *
 *         part and then inserts the part into the        *
 *         database. Prints an error message and returns  *
 *         prematurely if the part already exists or the  *
 *         database is full.                              *
 **********************************************************/
void insert(struct part inventory[])
{
  int part_number;

  if (num_parts == MAX_PARTS) {
    printf("Database is full; can't add more parts.\n");
    return;
  }

  printf("Enter part number: ");
  scanf("%d", &part_number);
  if (find_part(inventory, part_number) >= 0) {
    printf("Part already exists.\n");
    return;
  }

  inventory[num_parts].number = part_number;
  printf("Enter part name: ");
  read_line(inventory[num_parts].name, NAME_LEN);
  printf("Enter quantity on hand: ");
  scanf("%d", &inventory[num_parts].on_hand);
  num_parts++;
}
Example #24
0
/* Set up a specific partition in preparation for auto-update The source_device
 * is the partition that the update is stored on; if it's the same
 * as the partition that we're performing this routine on, verify its
 * integrity and resize it instead of formatting.
 *
 * Note that erase_partition does a 'quick' format; the disk is not zeroed
 * out. */
static int provision_partition(const char *name, Volume *source_volume)
{
	struct part_info *ptn;
	char *device = NULL;
	int ret = -1;

	/* Set up cache partition */
	ptn = find_part(disk_info, name);
	if (!ptn) {
		pr_error("Couldn't find %s partition. Is your "
				"disk_layout.conf valid?\n", name);
		goto out;
	}
	device = find_part_device(disk_info, ptn->name);
	if (!device) {
		pr_error("Can't get %s partition device node!\n", name);
		goto out;
	}
	/* Not checking device2; if people are declaring multiple devices
	 * for cache and data, they're nuts */
	if (!strcmp(source_volume->device, device)) {
		if (ext4_filesystem_checks(device)) {
			pr_error("%s filesystem corrupted.\n", name);
			goto out;
		}
	} else {
		if (erase_partition(ptn)) {
			pr_error("Couldn't format %s partition.\n", name);
			goto out;
		}
	}

	ret = 0;
out:
	free(device);
	return ret;
}
Example #25
0
int insert(part* parts, int num_parts)
{
    if (num_parts == MAX_PARTS)
    {
        printf("Inventory space is full: ");
        return num_parts;
    }
    int number;
    printf("Enter part number: ");
    scanf("%d", &number);
    int index = find_part(parts, num_parts, number);
    if (index < num_parts)
    {
        printf("Part number already exists\n");
        return num_parts;
    }
    printf("Enter part name: ");
    scanf("%s", parts[num_parts].name);
    printf("Enter quantity on hand: ");
    scanf("%d", &parts[num_parts].on_hand);
    parts[num_parts].number = number;
    ++num_parts;
    return num_parts;
}
Example #26
0
/**********************************************************
 * insert: Prompts the user for information about a new   *
 *         part and then inserts the part into the        *
 *         database. Prints an error message and returns  *
 *         prematurely if the part already exists or the  *
 *         database is full                               *
 *********************************************************/
void insert(void)
{
    int part_number;

    printf("Enter part number: ");
    scanf("%d", &part_number);

    if (find_part(part_number) >= 0) {
        printf("Part already exists.\n");
        return;
    }

    if (num_parts == max_parts) {
        max_parts *= 2;
        inventory = realloc(inventory, sizeof(struct part) * max_parts);
    }

    inventory[num_parts].number = part_number;
    printf("Enter part name: ");
    read_line(inventory[num_parts].name, NAME_LEN);
    printf("Enter quantity on hand: ");
    scanf("%d", &inventory[num_parts].on_hand);
    num_parts++;
}
static int
parse_args(int argc, char *argv[], struct disk_info **dinfo, int *test,
           int *verbose)
{
    char *layout_conf = NULL;
    char *img_file = NULL;
    struct stat64 filestat;
    int x;

    while ((x = getopt (argc, argv, "vthl:i:")) != EOF) {
        switch (x) {
            case 'h':
                return usage();
            case 'l':
                layout_conf = optarg;
                break;
            case 't':
                *test = 1;
                break;
            case 'i':
                img_file = optarg;
                break;
            case 'v':
                *verbose = 1;
                break;
            default:
                fprintf(stderr, "Unknown argument: %c\n", (char)optopt);
                return usage();
        }
    }

    if (!img_file || !layout_conf) {
        fprintf(stderr, "Image filename and configuration file are required\n");
        return usage();
    }

    /* we'll need to parse the command line later for partition-file
     * mappings, so make sure there's at least something there */
    if (optind >= argc) {
        fprintf(stderr, "Must provide partition -> file mappings\n");
        return usage();
    }

    if (stat64(img_file, &filestat)) {
        perror("Cannot stat image file");
        return 1;
    }

    /* make sure we don't screw up and write to a block device on the host
     * and wedge things. I just don't trust myself. */
    if (!S_ISREG(filestat.st_mode)) {
        fprintf(stderr, "This program should only be used on regular files.");
        return 1;
    }

    /* load the disk layout file */
    if (!(*dinfo = load_diskconfig(layout_conf, img_file))) {
        fprintf(stderr, "Errors encountered while loading disk conf file %s",
                layout_conf);
        return 1;
    }

    /* parse the filename->partition mappings from the command line and patch
     * up a loaded config file's partition table entries to have
     * length == filesize */
    x = 0;
    while (optind < argc) {
        char *pair = argv[optind++];
        char *part_name;
        struct part_info *pinfo;
        struct stat64 tmp_stat;

        if (x >= MAX_NUM_PARTS) {
            fprintf(stderr, "Error: Too many partitions specified (%d)!\n", x);
            return 1;
        }

        if (!(part_name = strsep(&pair, "=")) || !pair || !(*pair)) {
            fprintf(stderr, "Error parsing partition mappings\n");
            return usage();
        }

        if (!(pinfo = find_part(*dinfo, part_name))) {
            fprintf(stderr, "Partition '%s' not found.\n", part_name);
            return 1;
        }

        /* here pair points to the filename (after the '=') */
        part_file_map[x].pinfo = pinfo;
        part_file_map[x++].filename = pair;

        if (stat64(pair, &tmp_stat) < 0) {
            fprintf(stderr, "Could not stat file: %s\n", pair);
            return 1;
        }

        pinfo->len_kb = (tmp_stat.st_size + 1023) >> 10;
    }

    return 0;
}