/* This function extracts a single FSRef from a NavReplyRecord. */
static OSErr ExtractSingleItem(const NavReplyRecord *reply, FSRef *item)
{
    FSSpec fss;
    SInt32 itemCount;
    DescType junkType;
    AEKeyword junkKeyword;
    Size junkSize;
    OSErr osErr;

    osErr = AECountItems(&reply->selection, &itemCount);
    if( itemCount != 1 )	/* we only work with one object at a time */
        osErr = paramErr;
    if( osErr == noErr )
        osErr = AEGetNthPtr(&reply->selection, 1, typeFSS, &junkKeyword, &junkType, &fss, sizeof(fss), &junkSize);
    if( osErr == noErr )
    {
        mycheck(junkType == typeFSS);
        mycheck(junkSize == sizeof(FSSpec));

        /* We call FSMakeFSSpec because sometimes Nav is braindead		*/
        /* and gives us an invalid FSSpec (where the name is empty).	*/
        /* While FSpMakeFSRef seems to handle that (and the file system	*/
        /* engineers assure me that that will keep working (at least	*/
        /* on traditional Mac OS) because of the potential for breaking	*/
        /* existing applications), I'm still wary of doing this so		*/
        /* I regularise the FSSpec by feeding it through FSMakeFSSpec.	*/

        if( fss.name[0] == 0 )
            osErr = FSMakeFSSpec(fss.vRefNum, fss.parID, fss.name, &fss);
        if( osErr == noErr )
            osErr = FSpMakeFSRef(&fss, item);
    }

    return osErr;
}
Exemple #2
0
int mywrite(const char *user, const char *pass,
            const char *file, const char *mode){

  if (mycheck(user,pass)!=0) exit(-1);

  int i=0;
  int res=-2;
  const char *dir;

  while ((dir=allowed_dirs[i++]) != NULL){
    if ((strlen(file)>strlen(dir)) &&
        (strncmp(dir, file, strlen(dir)) == 0) &&
        (strchr(file+strlen(dir), '/') == NULL)){ // check for / in filename
      res=0;
      break;
    }
  }
  if (res!=0) return res;

  FILE *F;
  char buf[1024];
  F=fopen(file, mode);
  if (F==NULL) return -3;

  int sum=0;
  while (!feof(stdin)){
    int count=fread(buf, 1, sizeof(buf), stdin);
    if (ferror(stdin)) return -4;
    fwrite(buf, 1, count, F);
    if (ferror(F)) return -4;
    sum+=count;
  }
  fclose(F);
  return sum;
}
Exemple #3
0
int main(void)
{
        //test for partition_cmp-----------------------------
/*   for (unsigned p = 0; p <= 10; p += 5)
  {
                int tab[11] = { 2, 8, 42, 9, 13, 5, 3, 23, 40, -3, 55 };
                printf("p = %u  =>  m = %u\n", p, partition_cmp(tab, 0, 11, p, increasing));
                print_int_array(stdout, tab, 11);
        }

        //test for partition_cmp-------------------------------------*/

/*test for pivot_first----------------------------------------

int tab[10] = { 30, 8, 42, 9, 13, -3, 3, 23, 40, 5 };
printf("p = %u\n", pivot_median3(tab, 0, 10, increasing));*/

//test for pivot_first----------------------------------------


//test quick_sort------------------------------------------------
  srand(0);
  const unsigned tsize = 500000;
  int* tab1 = malloc(tsize * sizeof(int));
  int* tab2 = malloc(tsize * sizeof(int));
  for (unsigned i = 0; i < tsize; ++i)
    tab2[i] = tab1[i] = rand(); // = i;

  mycheck(quick_sort_cmp, tab1);
  mycheck(quick_sort_cmp_loop, tab2);
  return 0;

//test quick_sort------------------------------------------------

return 0; 
}
Exemple #4
0
int main(int argc, char *argv[]){
  if (argc<4) usage();

  const char *action=argv[1];
  const char *user=argv[2];
  const char *pass=argv[3];

  // create time string for logging
  char tstr[20] = "";
  time_t t=time(NULL);
  struct tm *tmp = localtime(&t);
  if (tmp != NULL) {
    if (strftime(tstr, sizeof(tstr), "%F %T", tmp) == 0)
      strncpy(tstr,"",strlen(tstr));
  }

  // check password
  if (!strcmp(action, "check")){
    int res=mycheck(user,pass);
    mylog("%19s %6s %s = %i\n", tstr, action, user, res);
    exit(res);
  }

  // write/append file
  if (!strcmp(action, "write") ||
      !strcmp(action, "append")){
    if (argc<5) usage();
    const char *file=argv[4];
    char mode[3]; mode[0]=action[0]; mode[1]='\0';
    int res=mywrite(user, pass, file, mode);
    mylog("%19s %6s %s by %s = %i\n", tstr, action, file, user, res);
    exit(res<0 ? res:0);
  }

  usage();
}